Open joshka opened 4 days ago
There is an in-flight PR in derive_more that would be also be useful. It adds an AsVariant
derive. This would make it easy to deal with e.g. if let Some(mouse) = event.as_mouse() { ... }
etc. I'd like to consider waiting for that and adding it to this PR.
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.
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.:
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)