-
Notifications
You must be signed in to change notification settings - Fork 287
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 is_*
and as_*
methods to the event enums
#949
Conversation
a6f2e61
to
18b5317
Compare
is_*
and as_*
methods to the event enums
Often application code only cares about a small subset of possible events. These methods make it simpler to write code which checks whether an event is a particular event type or converts events into the specific type (returning an Option). This can help simplify some nested match blocks. E.g.: ```rust match event { Event::Key(key) if key.kind == KeyEventKind::Press => { ... } } ``` becomes: ```rust if let Some(key) = event.as_key_press() { ... } ``` Similar flexible methods are aded across all the event enums: - `Event::is_focus_gained()` - `Event::is_focus_lost()` - `Event::is_key()` - `Event::is_mouse()` - `Event::is_paste()` - `Event::is_resize()` - `Event::is_key_press()` - `Event::as_key_press() -> Option<&KeyEvent>` - `MouseEventKind::is_*()` - `MouseButton::is_*()` - `KeyEventKind::is_*()` - `KeyEvent::is_press()` - `KeyEvent::is_release()` - `KeyEvent::is_repeat()` - `KeyCode::is_*()` - `KeyCode::is_function_key(n)` - `KeyCode::is_char(c)` - `KeyCode::as_char() -> Option<char>` - `KeyCode::is_media_key(media)` - `KeyCode::is_modifier(modifier)`
18b5317
to
6323263
Compare
There is an in-flight PR in derive_more that would be also be useful. It adds an Obviously a proper match statement is much better when dealing with more than one event type, but there are many use cases that this change would make simpler. |
480b436
to
5dea800
Compare
The derive_more change seems stalled for now, so I added the |
- add is_key_release() and is_key_repeat() checks - add as_key_event() - rename as_key_press() to as_key_press_event() - add as_key_repeat_event() - add as_key_release_event() - add as_mouse_event() - add as_paste_event() - more tests - update event-match and key-display examples
07255ac
to
5ee4931
Compare
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.
Nice! Helper functions always good to have :)
Often application code only cares about a small subset of possible
events. These methods make it simpler to write code which checks whether
an event is a particular event type or converts events into the specific
type (returning an Option).
A simple example of this is waiting for any key, which now becomes:
This can help simplify some nested match blocks. E.g.:
becomes:
Similar flexible methods are aded across all the event enums:
Event::is_focus_gained()
Event::is_focus_lost()
Event::is_key()
Event::is_mouse()
Event::is_paste()
Event::is_resize()
Event::is_key_press()
Event::as_key_press() -> Option<&KeyEvent>
MouseEventKind::is_*()
MouseButton::is_*()
KeyEventKind::is_*()
KeyEvent::is_press()
KeyEvent::is_release()
KeyEvent::is_repeat()
KeyCode::is_*()
KeyCode::is_function_key(n)
KeyCode::is_char(c)
KeyCode::as_char() -> Option<char>
KeyCode::is_media_key(media)
KeyCode::is_modifier(modifier)