35 releases (7 stable)

1.2.3 Feb 2, 2025
1.0.0 Dec 29, 2024
0.26.1 Dec 12, 2024
0.26.0 Nov 1, 2024
0.14.6 Jul 2, 2024

#873 in Command-line interface

Download history 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 150/week @ 2024-12-04 415/week @ 2024-12-11 182/week @ 2024-12-18 187/week @ 2024-12-25 438/week @ 2025-01-01 89/week @ 2025-01-08 260/week @ 2025-01-15 283/week @ 2025-01-22 658/week @ 2025-01-29 226/week @ 2025-02-05 404/week @ 2025-02-12

1,593 downloads per month
Used in 16 crates (9 directly)

MIT/Apache

55KB
914 lines

semver stable 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 - Popup event-handlers are regular event-handlers, but they need processing before regular event-handlers. This is used for widgets that render popups above other widgets, and must make sure that event-handling for the popup doesn't interfere with widgets below the popup. By ensuring the order of event-handling most of the problems can be solved.

  • Dialog - Specialized event-handler for dialog-like popups. They want to be called first to be able to consume all events, thus blocking everything else.

Return

The return type can be anything too.

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.

There is one constraint for the return type: It must implement Consumed to indicate the fundamental property of an event being consumed by a widget. This lib has some control-flow constructs that use this property.

Dependencies

~7–16MB
~218K SLoC