crates/tui-core/src/event.rs 3.4 KiB raw
1
//! Typed terminal events.
2
//!
3
//! Events are the input vocabulary consumed by [`App`](crate::App): key
4
//! presses, mouse input, resizes, and caller-defined custom events. Concrete
5
//! backends translate their native event streams into these values.
6
7
use crate::Size;
8
use crate::layout::Point;
9
10
/// Input or application event.
11
#[derive(Debug, Clone, PartialEq, Eq)]
12
pub enum Event<User = ()> {
13
    /// Key input.
14
    Key(Key),
15
    /// Mouse input.
16
    Mouse(Mouse),
17
    /// Terminal resize.
18
    Resize(Size),
19
    /// Bracketed paste or pasted text.
20
    Paste(String),
21
    /// Timer tick.
22
    Tick,
23
    /// User-defined event.
24
    User(User),
25
    /// Unsupported terminal input.
26
    Unsupported,
27
}
28
29
/// Keyboard input.
30
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
31
pub enum Key {
32
    /// Printable character.
33
    Char(char),
34
    /// Control character.
35
    Ctrl(char),
36
    /// Alt-modified character.
37
    Alt(char),
38
    /// Escape.
39
    Esc,
40
    /// Enter.
41
    Enter,
42
    /// Tab.
43
    Tab,
44
    /// Back-tab.
45
    BackTab,
46
    /// Backspace.
47
    Backspace,
48
    /// Delete.
49
    Delete,
50
    /// Insert.
51
    Insert,
52
    /// Left arrow.
53
    Left,
54
    /// Right arrow.
55
    Right,
56
    /// Up arrow.
57
    Up,
58
    /// Down arrow.
59
    Down,
60
    /// Home.
61
    Home,
62
    /// End.
63
    End,
64
    /// Page up.
65
    PageUp,
66
    /// Page down.
67
    PageDown,
68
    /// Function key.
69
    F(u8),
70
    /// Null key.
71
    Null,
72
    /// Unknown key.
73
    Unknown,
74
}
75
76
/// Mouse input.
77
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
78
pub enum Mouse {
79
    /// Button press.
80
    Press {
81
        /// Pressed button.
82
        button: MouseButton,
83
        /// Position.
84
        at: Point,
85
        /// Active modifier keys.
86
        modifiers: Modifiers,
87
    },
88
    /// Button release.
89
    Release {
90
        /// Position.
91
        at: Point,
92
        /// Active modifier keys.
93
        modifiers: Modifiers,
94
    },
95
    /// Mouse drag or hold.
96
    Hold {
97
        /// Position.
98
        at: Point,
99
        /// Active modifier keys.
100
        modifiers: Modifiers,
101
    },
102
}
103
104
/// Mouse button.
105
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
106
pub enum MouseButton {
107
    /// Left button.
108
    Left,
109
    /// Right button.
110
    Right,
111
    /// Middle button.
112
    Middle,
113
    /// Wheel up.
114
    WheelUp,
115
    /// Wheel down.
116
    WheelDown,
117
    /// Unknown button.
118
    Unknown,
119
}
120
121
/// Active keyboard modifiers attached to an input event.
122
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
123
pub struct Modifiers {
124
    /// Packed modifier flags.
125
    bits: u8,
126
}
127
128
impl Modifiers {
129
    /// No modifiers are active.
130
    pub const NONE: Self = Self { bits: 0 };
131
    /// Shift is active.
132
    pub const SHIFT: Self = Self { bits: 1 << 0 };
133
    /// Alt or Meta is active.
134
    pub const ALT: Self = Self { bits: 1 << 1 };
135
    /// Control is active.
136
    pub const CONTROL: Self = Self { bits: 1 << 2 };
137
138
    /// Returns whether all bits in `modifier` are active.
139
    pub const fn contains(self, modifier: Self) -> bool {
140
        self.bits & modifier.bits == modifier.bits
141
    }
142
143
    /// Returns whether no modifiers are active.
144
    pub const fn is_empty(self) -> bool {
145
        self.bits == 0
146
    }
147
}
148
149
impl std::ops::BitOr for Modifiers {
150
    type Output = Self;
151
152
    fn bitor(self, rhs: Self) -> Self::Output {
153
        Self {
154
            bits: self.bits | rhs.bits,
155
        }
156
    }
157
}
158
159
impl std::ops::BitOrAssign for Modifiers {
160
    fn bitor_assign(&mut self, rhs: Self) {
161
        self.bits |= rhs.bits;
162
    }
163
}