Skip to main content

tui_world/
input_event.rs

1use crate::{Keybindings, Pointer, WidgetId, World};
2use crossterm::event::{KeyEvent, MouseEvent, MouseEventKind};
3
4/// An input event that can be handled by the application.
5///
6/// `InputEvent` wraps the different types of input the application can receive:
7/// keyboard events, mouse events, and periodic tick events for animations or updates.
8///
9/// Use [`handle`](InputEvent::handle) to dispatch the event to the appropriate
10/// keybinding or pointer handler.
11///
12/// # Example
13///
14/// ```ignore
15/// match crossterm::event::read()? {
16///     Event::Key(key) => InputEvent::Key(key).handle(&mut world, &active_widgets),
17///     Event::Mouse(mouse) => InputEvent::Mouse(mouse).handle(&mut world, &active_widgets),
18///     _ => {}
19/// }
20/// ```
21#[derive(Debug)]
22pub enum InputEvent {
23    /// A keyboard event.
24    Key(KeyEvent),
25    /// A mouse event (click, drag, scroll, etc.).
26    Mouse(MouseEvent),
27    /// A periodic tick event for animations or timed updates.
28    Tick,
29}
30
31impl InputEvent {
32    /// Handles the event by dispatching to the appropriate handler.
33    ///
34    /// # Panics
35    ///
36    /// Panics if the `Keybindings` resource is not present in the world.
37    pub fn handle(self, world: &mut World, ids: &[WidgetId]) {
38        match self {
39            InputEvent::Key(key) => {
40                let binding = (&key).into();
41                let keybindings = world.remove::<Keybindings>().unwrap();
42                keybindings.handle(&binding, world, ids);
43                world.insert(keybindings);
44            }
45            InputEvent::Mouse(mouse) => {
46                let x = mouse.column;
47                let y = mouse.row;
48
49                match mouse.kind {
50                    MouseEventKind::Down(_) => {
51                        let hit = world.get::<Pointer>().hit_test(x, y);
52                        world.get_mut::<Pointer>().set_active(hit);
53
54                        if let Some(widget_id) = hit {
55                            let pointer = world.get::<Pointer>();
56                            let area = pointer.get(widget_id).copied();
57                            let handler = pointer.get_handler_for(widget_id, mouse.kind);
58                            if let (Some(area), Some(f)) = (area, handler) {
59                                f(world, area, x, y);
60                            }
61                        }
62                    }
63                    MouseEventKind::Drag(_) => {
64                        // Drag events go to the widget that received mouse down
65                        let active = world.get::<Pointer>().active();
66                        if let Some(widget_id) = active {
67                            let pointer = world.get::<Pointer>();
68                            let area = pointer.get(widget_id).copied();
69                            let handler = pointer.get_handler_for(widget_id, mouse.kind);
70                            if let (Some(area), Some(f)) = (area, handler) {
71                                f(world, area, x, y);
72                            }
73                        }
74                    }
75                    MouseEventKind::Up(_) => {
76                        // Up events go to the widget that received mouse down
77                        let active = world.get::<Pointer>().active();
78                        if let Some(widget_id) = active {
79                            let pointer = world.get::<Pointer>();
80                            let area = pointer.get(widget_id).copied();
81                            let handler = pointer.get_handler_for(widget_id, mouse.kind);
82                            if let (Some(area), Some(f)) = (area, handler) {
83                                f(world, area, x, y);
84                            }
85                        }
86                        // Clear active widget on mouse up
87                        world.get_mut::<Pointer>().set_active(None);
88                    }
89                    _ => {}
90                }
91            }
92            InputEvent::Tick => {}
93        }
94    }
95}