-
Notifications
You must be signed in to change notification settings - Fork 567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add docs for Painter & Controller #832
Conversation
This is the first part of the major section going over custom widgets and the general widget model.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a good start!
I think I got a realistic example for the use of Controller
😄
(Plus one nit pick)
docs/src/custom_widgets.md
Outdated
as a background, we could do (using the [`background`] method on [`WidgetExt`]): | ||
|
||
```rust,noplaypen | ||
{{#include ../book_examples/src/custom_widgets_md.rs:background_label}} | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe move the part in braces after the example, makes the reading flow a bit nicer.
#[derive(Default)] | ||
struct AnnoyingController { | ||
suppress_next: bool, | ||
} | ||
|
||
impl Controller<String, TextBox> for AnnoyingController { | ||
fn event( | ||
&mut self, | ||
child: &mut TextBox, | ||
ctx: &mut EventCtx, | ||
event: &Event, | ||
data: &mut String, | ||
env: &Env, | ||
) { | ||
if matches!(event, Event::KeyDown(k) if k.key_code == KeyCode::Backspace) { | ||
self.suppress_next = !self.suppress_next; | ||
if self.suppress_next { | ||
return; | ||
} | ||
} | ||
|
||
// if we want our child to receive this event, we must send it explicitly. | ||
child.event(ctx, event, data, env); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what came to my mind based on #787 .
A TextBox
that fires an action (e.g. for search) when hitting enter or 300 ms after the last key press.
const ACTION: Selector = Selector::new("hello.textbox-action");
struct TextBoxActionController {
timer: Option<TimerToken>,
}
impl TextBoxActionController {
pub fn new() -> Self {
TextBoxActionController { timer: None }
}
// Fire ACTION after 300 ms
fn deadline() -> Instant {
Instant::now() + Duration::from_millis(300)
}
}
impl Controller<String, TextBox> for TextBoxActionController {
fn event(
&mut self,
child: &mut TextBox,
ctx: &mut EventCtx,
event: &Event,
data: &mut String,
env: &Env,
) {
match event {
Event::KeyDown(k) if k.key_code == KeyCode::Return => {
ctx.submit_command(ACTION, None);
}
Event::KeyUp(k) if k.key_code != KeyCode::Return => {
self.timer = Some(ctx.request_timer(Self::deadline()));
child.event(ctx, event, data, env);
}
Event::Timer(token) if Some(*token) == self.timer => {
ctx.submit_command(ACTION, None);
}
_ => child.event(ctx, event, data, env),
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, I've changed the example to this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think going just with the constant in the example is enough
Co-Authored-By: Leopold Luley <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was not sure my suggestions made it as my internet died the very moment I pressed 'submit review' but looks like I was lucky 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always good to have better docs!
This is the first part of the major section going over custom
widgets and the general widget model.