Skip to main content

apalis_tui/
input.rs

1//! Backend-agnostic input types: this crate's own small mirrors of `ratzilla::event`, so
2//! [`crate::app`] has no web dependency. The `web` backend converts real `ratzilla` events into
3//! these. The `native` backend converts real `crossterm` events into these. Native tests
4//! construct them directly.
5
6/// A key code, mirroring `ratzilla::event::KeyCode`.
7#[derive(Debug, Clone, Copy, Eq, PartialEq)]
8pub enum KeyCode {
9    /// A printable character.
10    Char(char),
11    /// A function key, `F(1)`..`F(12)` typically.
12    F(u8),
13    /// Backspace.
14    Backspace,
15    /// Enter / return.
16    Enter,
17    /// Left arrow.
18    Left,
19    /// Right arrow.
20    Right,
21    /// Up arrow.
22    Up,
23    /// Down arrow.
24    Down,
25    /// Tab.
26    Tab,
27    /// Delete.
28    Delete,
29    /// Home.
30    Home,
31    /// End.
32    End,
33    /// Page up.
34    PageUp,
35    /// Page down.
36    PageDown,
37    /// Escape.
38    Esc,
39    /// A key the backend could not classify.
40    Unidentified,
41}
42
43/// A key event, mirroring `ratzilla::event::KeyEvent`.
44#[derive(Debug, Clone, Copy, Eq, PartialEq)]
45pub struct KeyEvent {
46    /// Which key.
47    pub code: KeyCode,
48    /// Whether Ctrl was held.
49    pub ctrl: bool,
50    /// Whether Alt was held.
51    pub alt: bool,
52    /// Whether Shift was held.
53    pub shift: bool,
54}
55
56impl KeyEvent {
57    /// A plain key with no modifiers held, for tests and simple dispatch.
58    #[must_use]
59    pub const fn plain(code: KeyCode) -> Self {
60        Self {
61            code,
62            ctrl: false,
63            alt: false,
64            shift: false,
65        }
66    }
67}
68
69/// A mouse button, mirroring `ratzilla::event::MouseButton`.
70#[derive(Debug, Clone, Copy, Eq, PartialEq)]
71pub enum MouseButton {
72    /// The primary (left) button.
73    Left,
74    /// The secondary (right) button.
75    Right,
76    /// The middle button.
77    Middle,
78    /// A "back" navigation button.
79    Back,
80    /// A "forward" navigation button.
81    Forward,
82    /// A button the backend could not classify.
83    Unidentified,
84}
85
86/// The type of mouse event that occurred, mirroring `ratzilla::event::MouseEventKind`.
87#[derive(Debug, Clone, Copy, Eq, PartialEq)]
88pub enum MouseEventKind {
89    /// The cursor moved, with no button held.
90    Moved,
91    /// A button was pressed.
92    ButtonDown(MouseButton),
93    /// A button was released.
94    ButtonUp(MouseButton),
95    /// A press-and-release resolved to a single click.
96    SingleClick(MouseButton),
97    /// A press-and-release resolved to a double click.
98    DoubleClick(MouseButton),
99    /// The cursor entered the grid.
100    Entered,
101    /// The cursor left the grid.
102    Exited,
103    /// An event the backend could not classify.
104    Unidentified,
105}
106
107/// A mouse event with terminal grid coordinates, mirroring `ratzilla::event::MouseEvent`.
108#[derive(Debug, Clone, Copy, Eq, PartialEq)]
109pub struct MouseEvent {
110    /// What happened.
111    pub kind: MouseEventKind,
112    /// The grid column, 0-based.
113    pub col: u16,
114    /// The grid row, 0-based.
115    pub row: u16,
116    /// Whether Ctrl was held.
117    pub ctrl: bool,
118    /// Whether Alt was held.
119    pub alt: bool,
120    /// Whether Shift was held.
121    pub shift: bool,
122}
123
124impl MouseEvent {
125    /// A click of `kind` at `(col, row)` with no modifiers held, for tests.
126    #[must_use]
127    pub const fn click(kind: MouseEventKind, col: u16, row: u16) -> Self {
128        Self {
129            kind,
130            col,
131            row,
132            ctrl: false,
133            alt: false,
134            shift: false,
135        }
136    }
137}