#events #input-event #ratatui #input

rat-event

ratatui event handler trait for widgets

28 releases (9 breaking)

new 0.26.1 Dec 12, 2024
0.26.0 Nov 1, 2024
0.25.2 Oct 20, 2024
0.14.6 Jul 2, 2024

#596 in Command-line interface

Download history 145/week @ 2024-08-21 207/week @ 2024-08-28 20/week @ 2024-09-04 487/week @ 2024-09-11 74/week @ 2024-09-18 59/week @ 2024-09-25 139/week @ 2024-10-02 117/week @ 2024-10-09 362/week @ 2024-10-16 54/week @ 2024-10-23 176/week @ 2024-10-30 19/week @ 2024-11-06 29/week @ 2024-11-13 210/week @ 2024-11-20 162/week @ 2024-11-27 128/week @ 2024-12-04

531 downloads per month
Used in 11 crates (8 directly)

MIT/Apache

53KB
872 lines

crates.io Documentation License License

This crate is a part of rat-salsa.

Rat-Event

Why?

This crate defines the trait HandleEvent to help with composability of event-handling for ratatui widgets.

Objectives are

  • work for all event-types.
  • allow for multiple handlers per widget
    • to override the key-bindings
    • to have different key-bindings for certain scenarios.
  • have a return type to indicate what state change occured.
    pub trait HandleEvent<Event, Qualifier, Return>
where
    Return: ConsumedEvent
{
    fn handle(
        &mut self,
        event: &Event,
        qualifier: Qualifier
    ) -> Return;
}

Event

Can be anything.

Qualifier

There are predefined qualifiers

  • Regular - Do what is considered 'normal' behaviour. Can vary depending on the actual state of the widget (e.g. focus)

  • MouseOnly - Splitting off mouse interaction helps when you only want to redefine the key bindings. And handling mouse events is usually more involved/complicated/specific.

  • DoubleClick - Double clicks are a bit special for widgets, often it requires a distinct return type and it's not as generally needed as other mouse behaviour.

  • Popup, Dialog - Specialized event-handlers, but they tend to popup again and again.

Return

The return type can be anything at all.

To be useful it is required to implement ConsumedEvent to indicate if the event has been handled by the widget and further event-handling can stop.

To set a baseline for the return type this crate defines the enum Outcome which can indicate if a render is necessary or not.

For interop all return types in rat-salsa are convertible to/from Outcome.

Dependencies

~6–16MB
~211K SLoC