Skip to main content

anathema_widgets/components/events/
mod.rs

1use std::time::Duration;
2
3use anathema_geometry::Size;
4
5pub use self::key::{KeyCode, KeyEvent, KeyState};
6pub use self::mouse::{MouseButton, MouseEvent, MouseState};
7
8mod key;
9mod mouse;
10
11#[derive(Debug, Copy, Clone)]
12pub enum EventType {
13    PreCycle,
14    PostCycle,
15}
16
17impl From<Event> for EventType {
18    fn from(event: Event) -> Self {
19        match event {
20            Event::Resize(_) => EventType::PostCycle,
21            _ => EventType::PreCycle,
22        }
23    }
24}
25
26/// An event
27#[derive(Debug, Copy, Clone, PartialEq)]
28pub enum Event {
29    /// No op
30    Noop,
31    /// Mount, called first time a component is added to the tree
32    Mount,
33    /// Unmount, called when the component is returned to component storage
34    Unmount,
35    /// Stop the runtime
36    Stop,
37    /// Terminal lost focus (not widely supported)
38    Blur,
39    /// Terminal gained focus (not widely supported)
40    Focus,
41    /// Key event
42    Key(KeyEvent),
43    /// Mouse event
44    Mouse(MouseEvent),
45    /// Window was resized
46    Resize(Size),
47    /// Tick
48    Tick(Duration),
49}
50
51impl Event {
52    pub fn is_mouse_event(&self) -> bool {
53        matches!(self, Self::Mouse(_))
54    }
55
56    pub fn get_char(&self) -> Option<char> {
57        match self {
58            Self::Key(event) => event.get_char(),
59            _ => None,
60        }
61    }
62
63    pub fn is_ctrl_c(&self) -> bool {
64        match self {
65            Self::Key(event) => match event.code {
66                KeyCode::Char('c') => event.ctrl,
67                _ => false,
68            },
69            _ => false,
70        }
71    }
72}