Trait EventHandler

Source
pub trait EventHandler {
    type Context;
    type Event;

    // Required methods
    fn handle_event(event: &Self::Event, ctx: &mut Self::Context);
    fn key_event_mappings(ctx: &Self::Context) -> Vec<(KeyEvent, Self::Event)>;

    // Provided methods
    fn pass_through_key_events(_: &KeyEvent, _: &mut Self::Context) { ... }
    fn pass_through_mouse_events(_: &MouseEvent, _: &mut Self::Context) { ... }
    fn handle_key_event<T: Into<KeyEvent>>(ctx: &mut Self::Context, event: T) { ... }
    fn handle_mouse_event<T: Into<MouseEvent>>(
        ctx: &mut Self::Context,
        event: T,
    ) { ... }
    fn format_event_mappings_as_strings(
        ctx: &Self::Context,
    ) -> Vec<(String, String)>
       where Self::Event: Display { ... }
}
Expand description

A trait for handling events with associated contexts.

Required Associated Types§

Source

type Context

The context in which events are handled.

Source

type Event

The type of events to handle.

Required Methods§

Source

fn handle_event(event: &Self::Event, ctx: &mut Self::Context)

Handles a specific app event with the given context.

§Arguments
  • event - The event to handle.
  • ctx - The context in which the event is handled.
Source

fn key_event_mappings(ctx: &Self::Context) -> Vec<(KeyEvent, Self::Event)>

Retrieves the key event mappings to their corresponding application events.

Returns a map of key events to application events.

Provided Methods§

Source

fn pass_through_key_events(_: &KeyEvent, _: &mut Self::Context)

Passes through key events without any specific handling.

This method is optional and can be used to simply pass through character events without registering any specific handling logic.

Source

fn pass_through_mouse_events(_: &MouseEvent, _: &mut Self::Context)

Passes through key events without any specific handling.

This method is optional and can be used to simply pass through character events without registering any specific handling logic.

Source

fn handle_key_event<T: Into<KeyEvent>>(ctx: &mut Self::Context, event: T)

Handles a key event by dispatching it to the corresponding application event handler.

§Arguments
  • ctx - The context in which the key event is handled.
  • event - The key event to handle.
Source

fn handle_mouse_event<T: Into<MouseEvent>>(ctx: &mut Self::Context, event: T)

Handles a mouse event by dispatching it to the corresponding application event handler.

§Arguments
  • ctx - The context in which the key event is handled.
  • event - The mouse event to handle.
Source

fn format_event_mappings_as_strings( ctx: &Self::Context, ) -> Vec<(String, String)>
where Self::Event: Display,

Converts the key event mappings into a vector of string representations, i.e. (app event as string, key event as string).

This method formats each key event and corresponding app event as strings and returns them as a vector of tuples. If multiple key events map to the same app event, they are merged into a single entry where the keys are concatenated together.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§