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 is_* and as_* methods to the event enums #949

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

joshka
Copy link
Collaborator

@joshka joshka commented Nov 26, 2024

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.:

match event {
    Event::Key(key) if key.kind == KeyEventKind::Press => { ... }
	_ => {}
}

becomes:

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)

@joshka joshka requested a review from TimonPost as a code owner November 26, 2024 02:06
@joshka joshka changed the title Add is_ and as_ methods to the event enums Add is_* and as_* methods to the event enums Nov 26, 2024
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)`
@joshka
Copy link
Collaborator Author

joshka commented Nov 26, 2024

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.

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.

1 participant