Skip to main content

gpui/
interactive.rs

1use crate::{
2    point, seal::Sealed, Bounds, Capslock, Context, Empty, IntoElement, Keystroke, Modifiers,
3    Pixels, Point, Render, 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    /// The new state of the capslock key
59    pub capslock: Capslock,
60}
61
62impl Sealed for ModifiersChangedEvent {}
63impl InputEvent for ModifiersChangedEvent {
64    fn to_platform_input(self) -> PlatformInput {
65        PlatformInput::ModifiersChanged(self)
66    }
67}
68impl KeyEvent for ModifiersChangedEvent {}
69
70impl Deref for ModifiersChangedEvent {
71    type Target = Modifiers;
72
73    fn deref(&self) -> &Self::Target {
74        &self.modifiers
75    }
76}
77
78/// The phase of a touch motion event.
79/// Based on the winit enum of the same name.
80#[derive(Clone, Copy, Debug, Default)]
81pub enum TouchPhase {
82    /// The touch started.
83    Started,
84    /// The touch event is moving.
85    #[default]
86    Moved,
87    /// The touch phase has ended
88    Ended,
89}
90
91/// A mouse down event from the platform
92#[derive(Clone, Debug, Default)]
93pub struct MouseDownEvent {
94    /// Which mouse button was pressed.
95    pub button: MouseButton,
96
97    /// The position of the mouse on the window.
98    pub position: Point<Pixels>,
99
100    /// The modifiers that were held down when the mouse was pressed.
101    pub modifiers: Modifiers,
102
103    /// The number of times the button has been clicked.
104    pub click_count: usize,
105
106    /// Whether this is the first, focusing click.
107    pub first_mouse: bool,
108}
109
110impl Sealed for MouseDownEvent {}
111impl InputEvent for MouseDownEvent {
112    fn to_platform_input(self) -> PlatformInput {
113        PlatformInput::MouseDown(self)
114    }
115}
116impl MouseEvent for MouseDownEvent {}
117
118/// A mouse up event from the platform
119#[derive(Clone, Debug, Default)]
120pub struct MouseUpEvent {
121    /// Which mouse button was released.
122    pub button: MouseButton,
123
124    /// The position of the mouse on the window.
125    pub position: Point<Pixels>,
126
127    /// The modifiers that were held down when the mouse was released.
128    pub modifiers: Modifiers,
129
130    /// The number of times the button has been clicked.
131    pub click_count: usize,
132}
133
134impl Sealed for MouseUpEvent {}
135impl InputEvent for MouseUpEvent {
136    fn to_platform_input(self) -> PlatformInput {
137        PlatformInput::MouseUp(self)
138    }
139}
140impl MouseEvent for MouseUpEvent {}
141
142/// A click event, generated when a mouse button is pressed and released.
143#[derive(Clone, Debug, Default)]
144pub struct MouseClickEvent {
145    /// The mouse event when the button was pressed.
146    pub down: MouseDownEvent,
147
148    /// The mouse event when the button was released.
149    pub up: MouseUpEvent,
150}
151
152/// A click event that was generated by a keyboard button being pressed and released.
153#[derive(Clone, Debug, Default)]
154pub struct KeyboardClickEvent {
155    /// The keyboard button that was pressed to trigger the click.
156    pub button: KeyboardButton,
157
158    /// The bounds of the element that was clicked.
159    pub bounds: Bounds<Pixels>,
160}
161
162/// A click event, generated when a mouse button or keyboard button is pressed and released.
163#[derive(Clone, Debug)]
164pub enum ClickEvent {
165    /// A click event trigger by a mouse button being pressed and released.
166    Mouse(MouseClickEvent),
167    /// A click event trigger by a keyboard button being pressed and released.
168    Keyboard(KeyboardClickEvent),
169}
170
171impl Default for ClickEvent {
172    fn default() -> Self {
173        ClickEvent::Keyboard(KeyboardClickEvent::default())
174    }
175}
176
177impl ClickEvent {
178    /// Returns the modifiers that were held during the click event
179    ///
180    /// `Keyboard`: The keyboard click events never have modifiers.
181    /// `Mouse`: Modifiers that were held during the mouse key up event.
182    pub fn modifiers(&self) -> Modifiers {
183        match self {
184            // Click events are only generated from keyboard events _without any modifiers_, so we know the modifiers are always Default
185            ClickEvent::Keyboard(_) => Modifiers::default(),
186            // Click events on the web only reflect the modifiers for the keyup event,
187            // tested via observing the behavior of the `ClickEvent.shiftKey` field in Chrome 138
188            // under various combinations of modifiers and keyUp / keyDown events.
189            ClickEvent::Mouse(event) => event.up.modifiers,
190        }
191    }
192
193    /// Returns the position of the click event
194    ///
195    /// `Keyboard`: The bottom left corner of the clicked hitbox
196    /// `Mouse`: The position of the mouse when the button was released.
197    pub fn position(&self) -> Point<Pixels> {
198        match self {
199            ClickEvent::Keyboard(event) => event.bounds.bottom_left(),
200            ClickEvent::Mouse(event) => event.up.position,
201        }
202    }
203
204    /// Returns the mouse position of the click event
205    ///
206    /// `Keyboard`: None
207    /// `Mouse`: The position of the mouse when the button was released.
208    pub fn mouse_position(&self) -> Option<Point<Pixels>> {
209        match self {
210            ClickEvent::Keyboard(_) => None,
211            ClickEvent::Mouse(event) => Some(event.up.position),
212        }
213    }
214
215    /// Returns if this was a right click
216    ///
217    /// `Keyboard`: false
218    /// `Mouse`: Whether the right button was pressed and released
219    pub fn is_right_click(&self) -> bool {
220        match self {
221            ClickEvent::Keyboard(_) => false,
222            ClickEvent::Mouse(event) => {
223                event.down.button == MouseButton::Right && event.up.button == MouseButton::Right
224            }
225        }
226    }
227
228    /// Returns whether the click was a standard click
229    ///
230    /// `Keyboard`: Always true
231    /// `Mouse`: Left button pressed and released
232    pub fn standard_click(&self) -> bool {
233        match self {
234            ClickEvent::Keyboard(_) => true,
235            ClickEvent::Mouse(event) => {
236                event.down.button == MouseButton::Left && event.up.button == MouseButton::Left
237            }
238        }
239    }
240
241    /// Returns whether the click focused the element
242    ///
243    /// `Keyboard`: false, keyboard clicks only work if an element is already focused
244    /// `Mouse`: Whether this was the first focusing click
245    pub fn first_focus(&self) -> bool {
246        match self {
247            ClickEvent::Keyboard(_) => false,
248            ClickEvent::Mouse(event) => event.down.first_mouse,
249        }
250    }
251
252    /// Returns the click count of the click event
253    ///
254    /// `Keyboard`: Always 1
255    /// `Mouse`: Count of clicks from MouseUpEvent
256    pub fn click_count(&self) -> usize {
257        match self {
258            ClickEvent::Keyboard(_) => 1,
259            ClickEvent::Mouse(event) => event.up.click_count,
260        }
261    }
262
263    /// Returns whether the click event is generated by a keyboard event
264    pub fn is_keyboard(&self) -> bool {
265        match self {
266            ClickEvent::Mouse(_) => false,
267            ClickEvent::Keyboard(_) => true,
268        }
269    }
270}
271
272/// An enum representing the keyboard button that was pressed for a click event.
273#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug, Default)]
274pub enum KeyboardButton {
275    /// Enter key was clicked
276    #[default]
277    Enter,
278    /// Space key was clicked
279    Space,
280}
281
282/// An enum representing the mouse button that was pressed.
283#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug, Default)]
284pub enum MouseButton {
285    /// The left mouse button.
286    #[default]
287    Left,
288
289    /// The right mouse button.
290    Right,
291
292    /// The middle mouse button.
293    Middle,
294
295    /// A navigation button, such as back or forward.
296    Navigate(NavigationDirection),
297}
298
299impl MouseButton {
300    /// Get all the mouse buttons in a list.
301    pub fn all() -> Vec<Self> {
302        vec![
303            MouseButton::Left,
304            MouseButton::Right,
305            MouseButton::Middle,
306            MouseButton::Navigate(NavigationDirection::Back),
307            MouseButton::Navigate(NavigationDirection::Forward),
308        ]
309    }
310}
311
312/// A navigation direction, such as back or forward.
313#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug, Default)]
314pub enum NavigationDirection {
315    /// The back button.
316    #[default]
317    Back,
318
319    /// The forward button.
320    Forward,
321}
322
323/// A mouse move event from the platform
324#[derive(Clone, Debug, Default)]
325pub struct MouseMoveEvent {
326    /// The position of the mouse on the window.
327    pub position: Point<Pixels>,
328
329    /// The mouse button that was pressed, if any.
330    pub pressed_button: Option<MouseButton>,
331
332    /// The modifiers that were held down when the mouse was moved.
333    pub modifiers: Modifiers,
334}
335
336impl Sealed for MouseMoveEvent {}
337impl InputEvent for MouseMoveEvent {
338    fn to_platform_input(self) -> PlatformInput {
339        PlatformInput::MouseMove(self)
340    }
341}
342impl MouseEvent for MouseMoveEvent {}
343
344impl MouseMoveEvent {
345    /// Returns true if the left mouse button is currently held down.
346    pub fn dragging(&self) -> bool {
347        self.pressed_button == Some(MouseButton::Left)
348    }
349}
350
351/// A mouse wheel event from the platform
352#[derive(Clone, Debug, Default)]
353pub struct ScrollWheelEvent {
354    /// The position of the mouse on the window.
355    pub position: Point<Pixels>,
356
357    /// The change in scroll wheel position for this event.
358    pub delta: ScrollDelta,
359
360    /// The modifiers that were held down when the mouse was moved.
361    pub modifiers: Modifiers,
362
363    /// The phase of the touch event.
364    pub touch_phase: TouchPhase,
365}
366
367impl Sealed for ScrollWheelEvent {}
368impl InputEvent for ScrollWheelEvent {
369    fn to_platform_input(self) -> PlatformInput {
370        PlatformInput::ScrollWheel(self)
371    }
372}
373impl MouseEvent for ScrollWheelEvent {}
374
375impl Deref for ScrollWheelEvent {
376    type Target = Modifiers;
377
378    fn deref(&self) -> &Self::Target {
379        &self.modifiers
380    }
381}
382
383/// The scroll delta for a scroll wheel event.
384#[derive(Clone, Copy, Debug)]
385pub enum ScrollDelta {
386    /// An exact scroll delta in pixels.
387    Pixels(Point<Pixels>),
388    /// An inexact scroll delta in lines.
389    Lines(Point<f32>),
390}
391
392impl Default for ScrollDelta {
393    fn default() -> Self {
394        Self::Lines(Default::default())
395    }
396}
397
398impl ScrollDelta {
399    /// Returns true if this is a precise scroll delta in pixels.
400    pub fn precise(&self) -> bool {
401        match self {
402            ScrollDelta::Pixels(_) => true,
403            ScrollDelta::Lines(_) => false,
404        }
405    }
406
407    /// Converts this scroll event into exact pixels.
408    pub fn pixel_delta(&self, line_height: Pixels) -> Point<Pixels> {
409        match self {
410            ScrollDelta::Pixels(delta) => *delta,
411            ScrollDelta::Lines(delta) => point(line_height * delta.x, line_height * delta.y),
412        }
413    }
414
415    /// Combines two scroll deltas into one.
416    /// If the signs of the deltas are the same (both positive or both negative),
417    /// the deltas are added together. If the signs are opposite, the second delta
418    /// (other) is used, effectively overriding the first delta.
419    pub fn coalesce(self, other: ScrollDelta) -> ScrollDelta {
420        match (self, other) {
421            (ScrollDelta::Pixels(a), ScrollDelta::Pixels(b)) => {
422                let x = if a.x.signum() == b.x.signum() {
423                    a.x + b.x
424                } else {
425                    b.x
426                };
427
428                let y = if a.y.signum() == b.y.signum() {
429                    a.y + b.y
430                } else {
431                    b.y
432                };
433
434                ScrollDelta::Pixels(point(x, y))
435            }
436
437            (ScrollDelta::Lines(a), ScrollDelta::Lines(b)) => {
438                let x = if a.x.signum() == b.x.signum() {
439                    a.x + b.x
440                } else {
441                    b.x
442                };
443
444                let y = if a.y.signum() == b.y.signum() {
445                    a.y + b.y
446                } else {
447                    b.y
448                };
449
450                ScrollDelta::Lines(point(x, y))
451            }
452
453            _ => other,
454        }
455    }
456}
457
458/// A mouse exit event from the platform, generated when the mouse leaves the window.
459#[derive(Clone, Debug, Default)]
460pub struct MouseExitEvent {
461    /// The position of the mouse relative to the window.
462    pub position: Point<Pixels>,
463    /// The mouse button that was pressed, if any.
464    pub pressed_button: Option<MouseButton>,
465    /// The modifiers that were held down when the mouse was moved.
466    pub modifiers: Modifiers,
467}
468
469impl Sealed for MouseExitEvent {}
470impl InputEvent for MouseExitEvent {
471    fn to_platform_input(self) -> PlatformInput {
472        PlatformInput::MouseExited(self)
473    }
474}
475impl MouseEvent for MouseExitEvent {}
476
477impl Deref for MouseExitEvent {
478    type Target = Modifiers;
479
480    fn deref(&self) -> &Self::Target {
481        &self.modifiers
482    }
483}
484
485/// A collection of paths from the platform, such as from a file drop.
486#[derive(Debug, Clone, Default)]
487pub struct ExternalPaths(pub(crate) SmallVec<[PathBuf; 2]>);
488
489impl ExternalPaths {
490    /// Convert this collection of paths into a slice.
491    pub fn paths(&self) -> &[PathBuf] {
492        &self.0
493    }
494}
495
496impl Render for ExternalPaths {
497    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
498        // the platform will render icons for the dragged files
499        Empty
500    }
501}
502
503/// A file drop event from the platform, generated when files are dragged and dropped onto the window.
504#[derive(Debug, Clone)]
505pub enum FileDropEvent {
506    /// The files have entered the window.
507    Entered {
508        /// The position of the mouse relative to the window.
509        position: Point<Pixels>,
510        /// The paths of the files that are being dragged.
511        paths: ExternalPaths,
512    },
513    /// The files are being dragged over the window
514    Pending {
515        /// The position of the mouse relative to the window.
516        position: Point<Pixels>,
517    },
518    /// The files have been dropped onto the window.
519    Submit {
520        /// The position of the mouse relative to the window.
521        position: Point<Pixels>,
522    },
523    /// The user has stopped dragging the files over the window.
524    Exited,
525}
526
527impl Sealed for FileDropEvent {}
528impl InputEvent for FileDropEvent {
529    fn to_platform_input(self) -> PlatformInput {
530        PlatformInput::FileDrop(self)
531    }
532}
533impl MouseEvent for FileDropEvent {}
534
535/// An enum corresponding to all kinds of platform input events.
536#[derive(Clone, Debug)]
537pub enum PlatformInput {
538    /// A key was pressed.
539    KeyDown(KeyDownEvent),
540    /// A key was released.
541    KeyUp(KeyUpEvent),
542    /// The keyboard modifiers were changed.
543    ModifiersChanged(ModifiersChangedEvent),
544    /// The mouse was pressed.
545    MouseDown(MouseDownEvent),
546    /// The mouse was released.
547    MouseUp(MouseUpEvent),
548    /// The mouse was moved.
549    MouseMove(MouseMoveEvent),
550    /// The mouse exited the window.
551    MouseExited(MouseExitEvent),
552    /// The scroll wheel was used.
553    ScrollWheel(ScrollWheelEvent),
554    /// Files were dragged and dropped onto the window.
555    FileDrop(FileDropEvent),
556}
557
558impl PlatformInput {
559    pub(crate) fn mouse_event(&self) -> Option<&dyn Any> {
560        match self {
561            PlatformInput::KeyDown { .. } => None,
562            PlatformInput::KeyUp { .. } => None,
563            PlatformInput::ModifiersChanged { .. } => None,
564            PlatformInput::MouseDown(event) => Some(event),
565            PlatformInput::MouseUp(event) => Some(event),
566            PlatformInput::MouseMove(event) => Some(event),
567            PlatformInput::MouseExited(event) => Some(event),
568            PlatformInput::ScrollWheel(event) => Some(event),
569            PlatformInput::FileDrop(event) => Some(event),
570        }
571    }
572
573    pub(crate) fn keyboard_event(&self) -> Option<&dyn Any> {
574        match self {
575            PlatformInput::KeyDown(event) => Some(event),
576            PlatformInput::KeyUp(event) => Some(event),
577            PlatformInput::ModifiersChanged(event) => Some(event),
578            PlatformInput::MouseDown(_) => None,
579            PlatformInput::MouseUp(_) => None,
580            PlatformInput::MouseMove(_) => None,
581            PlatformInput::MouseExited(_) => None,
582            PlatformInput::ScrollWheel(_) => None,
583            PlatformInput::FileDrop(_) => None,
584        }
585    }
586}
587
588#[cfg(test)]
589mod test {
590
591    use crate::{
592        self as gpui, div, AppContext as _, Context, FocusHandle, InteractiveElement, IntoElement,
593        KeyBinding, Keystroke, ParentElement, Render, TestAppContext, Window,
594    };
595
596    struct TestView {
597        saw_key_down: bool,
598        saw_action: bool,
599        focus_handle: FocusHandle,
600    }
601
602    actions!(test_only, [TestAction]);
603
604    impl Render for TestView {
605        fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
606            div().id("testview").child(
607                div()
608                    .key_context("parent")
609                    .on_key_down(cx.listener(|this, _, _, cx| {
610                        cx.stop_propagation();
611                        this.saw_key_down = true
612                    }))
613                    .on_action(cx.listener(|this: &mut TestView, _: &TestAction, _, _| {
614                        this.saw_action = true
615                    }))
616                    .child(
617                        div()
618                            .key_context("nested")
619                            .track_focus(&self.focus_handle)
620                            .into_element(),
621                    ),
622            )
623        }
624    }
625
626    #[gpui::test]
627    fn test_on_events(cx: &mut TestAppContext) {
628        let window = cx.update(|cx| {
629            cx.open_window(Default::default(), |_, cx| {
630                cx.new(|cx| TestView {
631                    saw_key_down: false,
632                    saw_action: false,
633                    focus_handle: cx.focus_handle(),
634                })
635            })
636            .unwrap()
637        });
638
639        cx.update(|cx| {
640            cx.bind_keys(vec![KeyBinding::new("ctrl-g", TestAction, Some("parent"))]);
641        });
642
643        window
644            .update(cx, |test_view, window, _cx| {
645                window.focus(&test_view.focus_handle)
646            })
647            .unwrap();
648
649        cx.dispatch_keystroke(*window, Keystroke::parse("a").unwrap());
650        cx.dispatch_keystroke(*window, Keystroke::parse("ctrl-g").unwrap());
651
652        window
653            .update(cx, |test_view, _, _| {
654                assert!(test_view.saw_key_down || test_view.saw_action);
655                assert!(test_view.saw_key_down);
656                assert!(test_view.saw_action);
657            })
658            .unwrap();
659    }
660}