Skip to main content

strop_core/
frontend_input.rs

1//! Physical/logical input facts before an editor or terminal chooses semantics.
2//! No toolkit types, grammar normalization, native work or implicit key expansion.
3use serde::{Deserialize, Serialize};
4
5pub mod notation;
6
7/// Normalized editor keys for admitted semantic input. Physical frontend input
8/// retains its modifiers until the engine selects its owner.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
10pub enum Key {
11    Char(char),
12    Esc,
13    Enter,
14    Backspace,
15    Up,
16    Down,
17    Left,
18    Right,
19    Tab,
20    Backtab,
21    /// Replace picker: exclude the row's whole file (vscode's toggle).
22    CtrlD,
23    CtrlR,
24    /// vim's jump-back (ctrl-i forward is Tab in a terminal).
25    CtrlO,
26    /// ctrl-space: query suggestions in a query field (0051 R02).
27    CtrlSpace,
28    /// Insert completion selection/acceptance; fields retain their own keys.
29    CtrlN,
30    CtrlP,
31    CtrlY,
32    CtrlE,
33    CtrlW,
34    /// Replace picker: exclude/include the selected match (0007 ยง2).
35    CtrlX,
36    /// vim ctrl-u/ctrl-f/ctrl-b: half/full page up.
37    CtrlU,
38    CtrlF,
39    CtrlB,
40    /// vim ctrl-^: alternate buffer.
41    CtrlCaret,
42    /// vim ctrl-v: visual block mode.
43    CtrlV,
44    /// vim ctrl-l: force a full terminal repaint (desync recovery).
45    CtrlL,
46}
47
48#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
49pub enum KeyCode {
50    Char(char),
51    Function(u8),
52    Escape,
53    Enter,
54    Backspace,
55    Up,
56    Down,
57    Left,
58    Right,
59    Home,
60    End,
61    PageUp,
62    PageDown,
63    Tab,
64    BackTab,
65    Delete,
66    Insert,
67    Null,
68    CapsLock,
69    ScrollLock,
70    NumLock,
71    PrintScreen,
72    Pause,
73    Menu,
74    KeypadBegin,
75    Media(MediaKey),
76    Modifier(ModifierKey),
77}
78
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
80pub enum MediaKey {
81    Play,
82    Pause,
83    PlayPause,
84    Reverse,
85    Stop,
86    FastForward,
87    Rewind,
88    TrackNext,
89    TrackPrevious,
90    Record,
91    LowerVolume,
92    RaiseVolume,
93    MuteVolume,
94}
95
96#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
97pub enum ModifierKey {
98    LeftShift,
99    LeftControl,
100    LeftAlt,
101    LeftSuper,
102    LeftHyper,
103    LeftMeta,
104    RightShift,
105    RightControl,
106    RightAlt,
107    RightSuper,
108    RightHyper,
109    RightMeta,
110    IsoLevel3Shift,
111    IsoLevel5Shift,
112}
113
114#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
115pub struct Modifiers {
116    pub shift: bool,
117    pub control: bool,
118    pub alt: bool,
119    pub super_key: bool,
120    pub hyper: bool,
121    pub meta: bool,
122}
123
124#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
125pub enum KeyKind {
126    #[default]
127    Press,
128    Repeat,
129    Release,
130}
131
132#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
133pub struct KeyState {
134    pub keypad: bool,
135    pub caps_lock: bool,
136    pub num_lock: bool,
137}
138
139#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
140pub struct KeyEvent {
141    pub code: KeyCode,
142    pub modifiers: Modifiers,
143    pub kind: KeyKind,
144    pub state: KeyState,
145}
146
147impl KeyEvent {
148    pub const fn press(code: KeyCode) -> Self {
149        Self {
150            code,
151            modifiers: Modifiers {
152                shift: false,
153                control: false,
154                alt: false,
155                super_key: false,
156                hyper: false,
157                meta: false,
158            },
159            kind: KeyKind::Press,
160            state: KeyState {
161                keypad: false,
162                caps_lock: false,
163                num_lock: false,
164            },
165        }
166    }
167}
168
169#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
170pub enum Input {
171    Key(KeyEvent),
172    /// Committed text, not a physical-key reconstruction or IME preedit.
173    Text(String),
174    Paste(String),
175}