1use crate::{
2 point, seal::Sealed, Context, Empty, IntoElement, Keystroke, Modifiers, Pixels, Point, Render,
3 Window,
4};
5use smallvec::SmallVec;
6use std::{any::Any, fmt::Debug, ops::Deref, path::PathBuf};
7
8/// An event from a platform input source.
9pub trait InputEvent: Sealed + 'static {
10 /// Convert this event into the platform input enum.
11 fn to_platform_input(self) -> PlatformInput;
12}
13
14/// A key event from the platform.
15pub trait KeyEvent: InputEvent {}
16
17/// A mouse event from the platform.
18pub trait MouseEvent: InputEvent {}
19
20/// The key down event equivalent for the platform.
21#[derive(Clone, Debug, Eq, PartialEq)]
22pub struct KeyDownEvent {
23 /// The keystroke that was generated.
24 pub keystroke: Keystroke,
25
26 /// Whether the key is currently held down.
27 pub is_held: bool,
28}
29
30impl Sealed for KeyDownEvent {}
31impl InputEvent for KeyDownEvent {
32 fn to_platform_input(self) -> PlatformInput {
33 PlatformInput::KeyDown(self)
34 }
35}
36impl KeyEvent for KeyDownEvent {}
37
38/// The key up event equivalent for the platform.
39#[derive(Clone, Debug)]
40pub struct KeyUpEvent {
41 /// The keystroke that was released.
42 pub keystroke: Keystroke,
43}
44
45impl Sealed for KeyUpEvent {}
46impl InputEvent for KeyUpEvent {
47 fn to_platform_input(self) -> PlatformInput {
48 PlatformInput::KeyUp(self)
49 }
50}
51impl KeyEvent for KeyUpEvent {}
52
53/// The modifiers changed event equivalent for the platform.
54#[derive(Clone, Debug, Default)]
55pub struct ModifiersChangedEvent {
56 /// The new state of the modifier keys
57 pub modifiers: Modifiers,
58}
59
60impl Sealed for ModifiersChangedEvent {}
61impl InputEvent for ModifiersChangedEvent {
62 fn to_platform_input(self) -> PlatformInput {
63 PlatformInput::ModifiersChanged(self)
64 }
65}
66impl KeyEvent for ModifiersChangedEvent {}
67
68impl Deref for ModifiersChangedEvent {
69 type Target = Modifiers;
70
71 fn deref(&self) -> &Self::Target {
72 &self.modifiers
73 }
74}
75
76/// The phase of a touch motion event.
77/// Based on the winit enum of the same name.
78#[derive(Clone, Copy, Debug, Default)]
79pub enum TouchPhase {
80 /// The touch started.
81 Started,
82 /// The touch event is moving.
83 #[default]
84 Moved,
85 /// The touch phase has ended
86 Ended,
87}
88
89/// A mouse down event from the platform
90#[derive(Clone, Debug, Default)]
91pub struct MouseDownEvent {
92 /// Which mouse button was pressed.
93 pub button: MouseButton,
94
95 /// The position of the mouse on the window.
96 pub position: Point<Pixels>,
97
98 /// The modifiers that were held down when the mouse was pressed.
99 pub modifiers: Modifiers,
100
101 /// The number of times the button has been clicked.
102 pub click_count: usize,
103
104 /// Whether this is the first, focusing click.
105 pub first_mouse: bool,
106}
107
108impl Sealed for MouseDownEvent {}
109impl InputEvent for MouseDownEvent {
110 fn to_platform_input(self) -> PlatformInput {
111 PlatformInput::MouseDown(self)
112 }
113}
114impl MouseEvent for MouseDownEvent {}
115
116/// A mouse up event from the platform
117#[derive(Clone, Debug, Default)]
118pub struct MouseUpEvent {
119 /// Which mouse button was released.
120 pub button: MouseButton,
121
122 /// The position of the mouse on the window.
123 pub position: Point<Pixels>,
124
125 /// The modifiers that were held down when the mouse was released.
126 pub modifiers: Modifiers,
127
128 /// The number of times the button has been clicked.
129 pub click_count: usize,
130}
131
132impl Sealed for MouseUpEvent {}
133impl InputEvent for MouseUpEvent {
134 fn to_platform_input(self) -> PlatformInput {
135 PlatformInput::MouseUp(self)
136 }
137}
138impl MouseEvent for MouseUpEvent {}
139
140/// A click event, generated when a mouse button is pressed and released.
141#[derive(Clone, Debug, Default)]
142pub struct ClickEvent {
143 /// The mouse event when the button was pressed.
144 pub down: MouseDownEvent,
145
146 /// The mouse event when the button was released.
147 pub up: MouseUpEvent,
148}
149
150/// An enum representing the mouse button that was pressed.
151#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
152pub enum MouseButton {
153 /// The left mouse button.
154 Left,
155
156 /// The right mouse button.
157 Right,
158
159 /// The middle mouse button.
160 Middle,
161
162 /// A navigation button, such as back or forward.
163 Navigate(NavigationDirection),
164}
165
166impl MouseButton {
167 /// Get all the mouse buttons in a list.
168 pub fn all() -> Vec<Self> {
169 vec![
170 MouseButton::Left,
171 MouseButton::Right,
172 MouseButton::Middle,
173 MouseButton::Navigate(NavigationDirection::Back),
174 MouseButton::Navigate(NavigationDirection::Forward),
175 ]
176 }
177}
178
179impl Default for MouseButton {
180 fn default() -> Self {
181 Self::Left
182 }
183}
184
185/// A navigation direction, such as back or forward.
186#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
187pub enum NavigationDirection {
188 /// The back button.
189 Back,
190
191 /// The forward button.
192 Forward,
193}
194
195impl Default for NavigationDirection {
196 fn default() -> Self {
197 Self::Back
198 }
199}
200
201/// A mouse move event from the platform
202#[derive(Clone, Debug, Default)]
203pub struct MouseMoveEvent {
204 /// The position of the mouse on the window.
205 pub position: Point<Pixels>,
206
207 /// The mouse button that was pressed, if any.
208 pub pressed_button: Option<MouseButton>,
209
210 /// The modifiers that were held down when the mouse was moved.
211 pub modifiers: Modifiers,
212}
213
214impl Sealed for MouseMoveEvent {}
215impl InputEvent for MouseMoveEvent {
216 fn to_platform_input(self) -> PlatformInput {
217 PlatformInput::MouseMove(self)
218 }
219}
220impl MouseEvent for MouseMoveEvent {}
221
222impl MouseMoveEvent {
223 /// Returns true if the left mouse button is currently held down.
224 pub fn dragging(&self) -> bool {
225 self.pressed_button == Some(MouseButton::Left)
226 }
227}
228
229/// A mouse wheel event from the platform
230#[derive(Clone, Debug, Default)]
231pub struct ScrollWheelEvent {
232 /// The position of the mouse on the window.
233 pub position: Point<Pixels>,
234
235 /// The change in scroll wheel position for this event.
236 pub delta: ScrollDelta,
237
238 /// The modifiers that were held down when the mouse was moved.
239 pub modifiers: Modifiers,
240
241 /// The phase of the touch event.
242 pub touch_phase: TouchPhase,
243}
244
245impl Sealed for ScrollWheelEvent {}
246impl InputEvent for ScrollWheelEvent {
247 fn to_platform_input(self) -> PlatformInput {
248 PlatformInput::ScrollWheel(self)
249 }
250}
251impl MouseEvent for ScrollWheelEvent {}
252
253impl Deref for ScrollWheelEvent {
254 type Target = Modifiers;
255
256 fn deref(&self) -> &Self::Target {
257 &self.modifiers
258 }
259}
260
261/// The scroll delta for a scroll wheel event.
262#[derive(Clone, Copy, Debug)]
263pub enum ScrollDelta {
264 /// An exact scroll delta in pixels.
265 Pixels(Point<Pixels>),
266 /// An inexact scroll delta in lines.
267 Lines(Point<f32>),
268}
269
270impl Default for ScrollDelta {
271 fn default() -> Self {
272 Self::Lines(Default::default())
273 }
274}
275
276impl ScrollDelta {
277 /// Returns true if this is a precise scroll delta in pixels.
278 pub fn precise(&self) -> bool {
279 match self {
280 ScrollDelta::Pixels(_) => true,
281 ScrollDelta::Lines(_) => false,
282 }
283 }
284
285 /// Converts this scroll event into exact pixels.
286 pub fn pixel_delta(&self, line_height: Pixels) -> Point<Pixels> {
287 match self {
288 ScrollDelta::Pixels(delta) => *delta,
289 ScrollDelta::Lines(delta) => point(line_height * delta.x, line_height * delta.y),
290 }
291 }
292
293 /// Combines two scroll deltas into one.
294 /// If the signs of the deltas are the same (both positive or both negative),
295 /// the deltas are added together. If the signs are opposite, the second delta
296 /// (other) is used, effectively overriding the first delta.
297 pub fn coalesce(self, other: ScrollDelta) -> ScrollDelta {
298 match (self, other) {
299 (ScrollDelta::Pixels(a), ScrollDelta::Pixels(b)) => {
300 let x = if a.x.signum() * b.x.signum() >= 0. {
301 a.x + b.x
302 } else {
303 b.x
304 };
305
306 let y = if a.y.signum() * b.y.signum() >= 0. {
307 a.y + b.y
308 } else {
309 b.y
310 };
311
312 ScrollDelta::Pixels(point(x, y))
313 }
314
315 (ScrollDelta::Lines(a), ScrollDelta::Lines(b)) => {
316 let x = if a.x.signum() * b.x.signum() >= 0. {
317 a.x + b.x
318 } else {
319 b.x
320 };
321
322 let y = if a.y.signum() * b.y.signum() >= 0. {
323 a.y + b.y
324 } else {
325 b.y
326 };
327
328 ScrollDelta::Lines(point(x, y))
329 }
330
331 _ => other,
332 }
333 }
334}
335
336/// A mouse exit event from the platform, generated when the mouse leaves the window.
337#[derive(Clone, Debug, Default)]
338pub struct MouseExitEvent {
339 /// The position of the mouse relative to the window.
340 pub position: Point<Pixels>,
341 /// The mouse button that was pressed, if any.
342 pub pressed_button: Option<MouseButton>,
343 /// The modifiers that were held down when the mouse was moved.
344 pub modifiers: Modifiers,
345}
346
347impl Sealed for MouseExitEvent {}
348impl InputEvent for MouseExitEvent {
349 fn to_platform_input(self) -> PlatformInput {
350 PlatformInput::MouseExited(self)
351 }
352}
353impl MouseEvent for MouseExitEvent {}
354
355impl Deref for MouseExitEvent {
356 type Target = Modifiers;
357
358 fn deref(&self) -> &Self::Target {
359 &self.modifiers
360 }
361}
362
363/// A collection of paths from the platform, such as from a file drop.
364#[derive(Debug, Clone, Default)]
365pub struct ExternalPaths(pub(crate) SmallVec<[PathBuf; 2]>);
366
367impl ExternalPaths {
368 /// Convert this collection of paths into a slice.
369 pub fn paths(&self) -> &[PathBuf] {
370 &self.0
371 }
372}
373
374impl Render for ExternalPaths {
375 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
376 // the platform will render icons for the dragged files
377 Empty
378 }
379}
380
381/// A file drop event from the platform, generated when files are dragged and dropped onto the window.
382#[derive(Debug, Clone)]
383pub enum FileDropEvent {
384 /// The files have entered the window.
385 Entered {
386 /// The position of the mouse relative to the window.
387 position: Point<Pixels>,
388 /// The paths of the files that are being dragged.
389 paths: ExternalPaths,
390 },
391 /// The files are being dragged over the window
392 Pending {
393 /// The position of the mouse relative to the window.
394 position: Point<Pixels>,
395 },
396 /// The files have been dropped onto the window.
397 Submit {
398 /// The position of the mouse relative to the window.
399 position: Point<Pixels>,
400 },
401 /// The user has stopped dragging the files over the window.
402 Exited,
403}
404
405impl Sealed for FileDropEvent {}
406impl InputEvent for FileDropEvent {
407 fn to_platform_input(self) -> PlatformInput {
408 PlatformInput::FileDrop(self)
409 }
410}
411impl MouseEvent for FileDropEvent {}
412
413/// An enum corresponding to all kinds of platform input events.
414#[derive(Clone, Debug)]
415pub enum PlatformInput {
416 /// A key was pressed.
417 KeyDown(KeyDownEvent),
418 /// A key was released.
419 KeyUp(KeyUpEvent),
420 /// The keyboard modifiers were changed.
421 ModifiersChanged(ModifiersChangedEvent),
422 /// The mouse was pressed.
423 MouseDown(MouseDownEvent),
424 /// The mouse was released.
425 MouseUp(MouseUpEvent),
426 /// The mouse was moved.
427 MouseMove(MouseMoveEvent),
428 /// The mouse exited the window.
429 MouseExited(MouseExitEvent),
430 /// The scroll wheel was used.
431 ScrollWheel(ScrollWheelEvent),
432 /// Files were dragged and dropped onto the window.
433 FileDrop(FileDropEvent),
434}
435
436impl PlatformInput {
437 pub(crate) fn mouse_event(&self) -> Option<&dyn Any> {
438 match self {
439 PlatformInput::KeyDown { .. } => None,
440 PlatformInput::KeyUp { .. } => None,
441 PlatformInput::ModifiersChanged { .. } => None,
442 PlatformInput::MouseDown(event) => Some(event),
443 PlatformInput::MouseUp(event) => Some(event),
444 PlatformInput::MouseMove(event) => Some(event),
445 PlatformInput::MouseExited(event) => Some(event),
446 PlatformInput::ScrollWheel(event) => Some(event),
447 PlatformInput::FileDrop(event) => Some(event),
448 }
449 }
450
451 pub(crate) fn keyboard_event(&self) -> Option<&dyn Any> {
452 match self {
453 PlatformInput::KeyDown(event) => Some(event),
454 PlatformInput::KeyUp(event) => Some(event),
455 PlatformInput::ModifiersChanged(event) => Some(event),
456 PlatformInput::MouseDown(_) => None,
457 PlatformInput::MouseUp(_) => None,
458 PlatformInput::MouseMove(_) => None,
459 PlatformInput::MouseExited(_) => None,
460 PlatformInput::ScrollWheel(_) => None,
461 PlatformInput::FileDrop(_) => None,
462 }
463 }
464}
465
466#[cfg(test)]
467mod test {
468
469 use crate::{
470 self as gpui, div, AppContext as _, Context, FocusHandle, InteractiveElement, IntoElement,
471 KeyBinding, Keystroke, ParentElement, Render, TestAppContext, Window,
472 };
473
474 struct TestView {
475 saw_key_down: bool,
476 saw_action: bool,
477 focus_handle: FocusHandle,
478 }
479
480 actions!(test, [TestAction]);
481
482 impl Render for TestView {
483 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
484 div().id("testview").child(
485 div()
486 .key_context("parent")
487 .on_key_down(cx.listener(|this, _, _, cx| {
488 cx.stop_propagation();
489 this.saw_key_down = true
490 }))
491 .on_action(cx.listener(|this: &mut TestView, _: &TestAction, _, _| {
492 this.saw_action = true
493 }))
494 .child(
495 div()
496 .key_context("nested")
497 .track_focus(&self.focus_handle)
498 .into_element(),
499 ),
500 )
501 }
502 }
503
504 #[gpui::test]
505 fn test_on_events(cx: &mut TestAppContext) {
506 let window = cx.update(|cx| {
507 cx.open_window(Default::default(), |_, cx| {
508 cx.new(|cx| TestView {
509 saw_key_down: false,
510 saw_action: false,
511 focus_handle: cx.focus_handle(),
512 })
513 })
514 .unwrap()
515 });
516
517 cx.update(|cx| {
518 cx.bind_keys(vec![KeyBinding::new("ctrl-g", TestAction, Some("parent"))]);
519 });
520
521 window
522 .update(cx, |test_view, window, _cx| {
523 window.focus(&test_view.focus_handle)
524 })
525 .unwrap();
526
527 cx.dispatch_keystroke(*window, Keystroke::parse("a").unwrap());
528 cx.dispatch_keystroke(*window, Keystroke::parse("ctrl-g").unwrap());
529
530 window
531 .update(cx, |test_view, _, _| {
532 assert!(test_view.saw_key_down || test_view.saw_action);
533 assert!(test_view.saw_key_down);
534 assert!(test_view.saw_action);
535 })
536 .unwrap();
537 }
538}