Skip to main content

edtui/events/
mod.rs

1mod key;
2#[cfg(feature = "mouse-support")]
3pub(crate) mod mouse;
4pub(crate) mod paste;
5
6#[allow(deprecated)]
7pub use key::deprecated::KeyEvent;
8pub use key::{input::KeyInput, KeyEventHandler, KeyEventRegister, KeyInputSequence};
9
10#[cfg(feature = "mouse-support")]
11pub use mouse::{MouseEvent, MouseEventHandler};
12
13use crate::{events::paste::PasteEventHandler, EditorState};
14use crossterm::event::Event as CTEvent;
15
16/// Handles key and mouse events.
17#[derive(Clone)]
18pub struct EditorEventHandler {
19    pub key_handler: KeyEventHandler,
20}
21
22impl Default for EditorEventHandler {
23    fn default() -> Self {
24        Self::vim_mode()
25    }
26}
27
28impl EditorEventHandler {
29    /// Creates a new `EditorEvent` handler with the given key handler.
30    #[must_use]
31    pub fn new(key_handler: KeyEventHandler) -> Self {
32        Self { key_handler }
33    }
34
35    /// Creates a new `EditorEvent` handler with vim-style keybindings.
36    #[must_use]
37    pub fn vim_mode() -> Self {
38        Self {
39            key_handler: KeyEventHandler::vim_mode(),
40        }
41    }
42
43    /// Creates a new `EditorEvent` handler with emacs-style keybindings.
44    #[must_use]
45    pub fn emacs_mode() -> Self {
46        Self {
47            key_handler: KeyEventHandler::emacs_mode(),
48        }
49    }
50
51    /// Handles key and mouse events.
52    pub fn on_event<T>(&mut self, event: T, state: &mut EditorState)
53    where
54        T: Into<Event>,
55    {
56        match event.into() {
57            Event::Key(event) => self.on_key_event(event, state),
58            #[cfg(feature = "mouse-support")]
59            Event::Mouse(event) => self.on_mouse_event(event, state),
60            Event::Paste(text) => self.on_paste_event(text, state),
61            Event::None => (),
62        }
63    }
64
65    /// Handles key events.
66    pub fn on_key_event<T>(&mut self, event: T, state: &mut EditorState)
67    where
68        T: Into<KeyInput>,
69    {
70        self.key_handler.on_event(event.into(), state);
71    }
72
73    #[cfg(feature = "mouse-support")]
74    /// Handles mouse events.
75    pub fn on_mouse_event<T>(&self, event: T, state: &mut EditorState)
76    where
77        T: Into<MouseEvent>,
78    {
79        MouseEventHandler::on_event(event.into(), state);
80    }
81
82    /// Handles paste events.
83    pub fn on_paste_event(&self, text: String, state: &mut EditorState) {
84        PasteEventHandler::on_event(text, state);
85    }
86}
87
88pub enum Event {
89    Key(KeyInput),
90    #[cfg(feature = "mouse-support")]
91    Mouse(MouseEvent),
92    Paste(String),
93    None,
94}
95
96impl From<CTEvent> for Event {
97    fn from(value: CTEvent) -> Self {
98        match value {
99            CTEvent::Key(event) => Self::Key(event.into()),
100            #[cfg(feature = "mouse-support")]
101            CTEvent::Mouse(event) => Self::Mouse(event.into()),
102            CTEvent::Paste(text) => Self::Paste(text),
103            _ => Self::None,
104        }
105    }
106}