Skip to content
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

Merged
merged 3 commits into from
Apr 15, 2020
Merged

Add docs for Painter & Controller #832

merged 3 commits into from
Apr 15, 2020

Conversation

cmyr
Copy link
Member

@cmyr cmyr commented Apr 12, 2020

This is the first part of the major section going over custom
widgets and the general widget model.

This is the first part of the major section going over custom
widgets and the general widget model.
@cmyr cmyr added the S-needs-review waits for review label Apr 12, 2020
Copy link
Collaborator

@luleyleo luleyleo left a 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)

Comment on lines 37 to 42
as a background, we could do (using the [`background`] method on [`WidgetExt`]):

```rust,noplaypen
{{#include ../book_examples/src/custom_widgets_md.rs:background_label}}
```

Copy link
Collaborator

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.

Comment on lines 35 to 59
#[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);
}
}
Copy link
Collaborator

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),
        }
    }
}

Copy link
Member Author

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.

Copy link
Collaborator

@luleyleo luleyleo left a 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

docs/book_examples/src/custom_widgets_md.rs Outdated Show resolved Hide resolved
docs/book_examples/src/custom_widgets_md.rs Outdated Show resolved Hide resolved
Co-Authored-By: Leopold Luley <git@leopoldluley.de>
Copy link
Collaborator

@luleyleo luleyleo left a 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 😅

Copy link
Member

@xStrom xStrom left a 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!

@cmyr cmyr merged commit 185941d into master Apr 15, 2020
@cmyr cmyr deleted the custom-widget-docs branch April 15, 2020 13:49
@xStrom xStrom removed the S-needs-review waits for review label May 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants