1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! The input event model

use crate::prelude::Point;
use std::collections::HashSet;

/// The AppEvent is an extension point in termit so that
/// you can include your application specific event stream
/// in the input processing.
///
/// This is handy because the additional included stream are executed
/// in parallel with the terminal input and thus provide cheap
/// parallelism to process your application heavy duty async tasks.
///
/// AppEvent is implemented for anything that fulfils the generic [`Event`]
/// requirements.
pub trait AppEvent: std::fmt::Debug + PartialEq + Unpin + 'static {}
impl<T> AppEvent for T where T: std::fmt::Debug + PartialEq + Unpin + 'static {}

/// The main input entity extensible with application specific [`AppEvent`]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event<A: AppEvent = ()> {
    Key(KeyEvent),
    Focus(bool),
    CursorPosition(Point),
    Resize(Point),
    Mouse(MouseEvent),
    Paste(String),
    Unknown(Vec<u8>),
    /// Refresh with n beats passed
    Refresh(usize),
    /// Application specific event
    App(A),
    /// The terminal input has been closed.
    /// This may be a signal to terminate the app (or not)
    InputClosed,
}
#[derive(Hash, Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyCode {
    Esc,
    Left,
    Right,
    Up,
    Down,
    Home,
    End,
    F(u8),
    BackTab,
    Enter,
    Tab,
    Backspace,
    Char(char),
    Insert,
    Delete,
    PageUp,
    PageDown,
    KeypadBegin,
    CapsLock,
    ScrollLock,
    NumLock,
    PrintScreen,
    Pause,
    Menu,
    Media(MediaKeyCode),
    Modifier(ModifierKeyCode),
}
#[derive(Hash, Debug, Clone, Copy, PartialEq, Eq)]
pub enum MediaKeyCode {
    Play,
    Pause,
    PlayPause,
    Reverse,
    Stop,
    FastForward,
    Rewind,
    TrackNext,
    TrackPrevious,
    Record,
    LowerVolume,
    RaiseVolume,
    MuteVolume,
}
#[derive(Hash, Debug, Clone, Copy, PartialEq, Eq)]
pub enum ModifierKeyCode {
    LeftShift,
    LeftControl,
    LeftAlt,
    LeftSuper,
    LeftHyper,
    LeftMeta,
    RightShift,
    RightControl,
    RightAlt,
    RightSuper,
    RightHyper,
    RightMeta,
    IsoLevel3Shift,
    IsoLevel5Shift,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyEvent {
    pub keycode: KeyCode,
    pub kind: KeyEventKind,
    pub modifiers: KeyModifiers,
    pub state: KeyEventStates,
}
impl KeyEvent {
    pub(crate) fn new(
        keycode: KeyCode,
        modifiers: impl IntoIterator<Item = KeyModifier>,
    ) -> KeyEvent {
        KeyEvent::new_with_kind(keycode, modifiers, KeyEventKind::Press)
    }

    pub(crate) fn new_with_kind(
        keycode: KeyCode,
        modifiers: impl IntoIterator<Item = KeyModifier>,
        kind: KeyEventKind,
    ) -> KeyEvent {
        KeyEvent::new_with_kind_and_state(keycode, modifiers, kind, None)
    }

    pub(crate) fn new_with_kind_and_state(
        keycode: KeyCode,
        modifiers: impl IntoIterator<Item = KeyModifier>,
        kind: KeyEventKind,
        state: impl IntoIterator<Item = KeyEventState>,
    ) -> KeyEvent {
        KeyEvent {
            keycode,
            modifiers: modifiers.into_iter().collect(),
            kind,
            state: state.into_iter().collect(),
        }
    }
}
impl From<KeyCode> for KeyEvent {
    /// converts KeyCode to KeyEvent (adds shift modifier in case of uppercase characters)
    fn from(keycode: KeyCode) -> Self {
        let modifiers = match keycode {
            KeyCode::Char(c) if c.is_uppercase() => Some(KeyModifier::Shift),
            _ => None,
        };
        KeyEvent::new(keycode, modifiers)
    }
}
pub type KeyModifiers = HashSet<KeyModifier>;
#[derive(Hash, Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyModifier {
    Alt,
    Control,
    Shift,
    Super,
    Hyper,
    Meta,
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum KeyEventKind {
    Press,
    Release,
    Repeat,
}
pub type KeyEventStates = HashSet<KeyEventState>;

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum KeyEventState {
    CapsLock,
    NumLock,
    Keypad,
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum MouseButton {
    Left,
    Middle,
    Right,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MouseEvent {
    pub column: u16,
    pub row: u16,
    pub kind: MouseEventKind,
    pub modifiers: KeyModifiers,
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum MouseEventKind {
    Moved,
    Up(MouseButton),
    Drag(MouseButton),
    Down(MouseButton),
    ScrollDown,
    ScrollUp,
}