gpui_cn/terminal/
input.rs1#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
10#[non_exhaustive]
11pub struct Modifiers {
12 pub shift: bool,
14 pub control: bool,
16 pub alt: bool,
18 pub super_key: bool,
20}
21
22#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
24pub enum KeyAction {
25 #[default]
27 Press,
28 Repeat,
30 Release,
32}
33
34#[derive(Clone, Debug, Default, PartialEq, Eq)]
36#[non_exhaustive]
37pub struct KeyInput {
38 pub key: String,
40 pub text: Option<String>,
42 pub modifiers: Modifiers,
44 pub action: KeyAction,
46}
47
48impl KeyInput {
49 #[must_use]
51 pub fn new(key: impl Into<String>) -> Self {
52 Self {
53 key: key.into(),
54 ..Self::default()
55 }
56 }
57
58 #[must_use]
60 pub fn with_text(mut self, text: impl Into<String>) -> Self {
61 self.text = Some(text.into());
62 self
63 }
64
65 #[must_use]
67 pub fn with_modifiers(mut self, modifiers: Modifiers) -> Self {
68 self.modifiers = modifiers;
69 self
70 }
71
72 #[must_use]
74 pub fn with_action(mut self, action: KeyAction) -> Self {
75 self.action = action;
76 self
77 }
78}
79
80#[derive(Clone, Copy, Debug, PartialEq, Eq)]
82pub enum MouseAction {
83 Press,
85 Release,
87 Move,
89 ScrollUp,
91 ScrollDown,
93}
94
95#[derive(Clone, Copy, Debug, PartialEq, Eq)]
97pub enum MouseButton {
98 Left,
100 Middle,
102 Right,
104}
105
106#[derive(Clone, Copy, Debug, PartialEq)]
108#[non_exhaustive]
109pub struct MouseInput {
110 pub action: MouseAction,
112 pub button: Option<MouseButton>,
114 pub modifiers: Modifiers,
116 pub x: f32,
118 pub y: f32,
120}
121
122impl MouseInput {
123 #[must_use]
125 pub fn new(action: MouseAction, x: f32, y: f32) -> Self {
126 Self {
127 action,
128 button: None,
129 modifiers: Modifiers::default(),
130 x,
131 y,
132 }
133 }
134
135 #[must_use]
137 pub fn with_button(mut self, button: MouseButton) -> Self {
138 self.button = Some(button);
139 self
140 }
141
142 #[must_use]
144 pub fn with_modifiers(mut self, modifiers: Modifiers) -> Self {
145 self.modifiers = modifiers;
146 self
147 }
148}
149
150#[derive(Clone, Copy, Debug, PartialEq, Eq)]
152pub enum ScrollRequest {
153 Lines(isize),
155 Top,
157 Bottom,
159}
160
161#[derive(Clone, Debug, PartialEq)]
163#[non_exhaustive]
164pub enum TerminalInput {
165 Text(String),
167 Paste(String),
169 Key(KeyInput),
171 Mouse(MouseInput),
173 Scroll(ScrollRequest),
175 Focus(bool),
177}
178
179impl TerminalInput {
180 #[must_use]
183 pub fn is_interrupt(&self) -> bool {
184 match self {
185 Self::Key(key) => {
186 key.key.eq_ignore_ascii_case("c")
187 && key.modifiers.control
188 && !key.modifiers.alt
189 && !key.modifiers.super_key
190 && key.action != KeyAction::Release
191 }
192 Self::Text(text) => text == "\x03",
193 _ => false,
194 }
195 }
196
197 #[must_use]
199 pub fn budget(&self) -> usize {
200 match self {
201 Self::Text(s) | Self::Paste(s) => s.len(),
202 Self::Key(k) => k.key.len() + k.text.as_ref().map_or(0, String::len),
203 _ => 0,
204 }
205 }
206}