interactive.rs

  1use crate::{
  2    point, seal::Sealed, Empty, IntoElement, Keystroke, Modifiers, Pixels, Point, Render,
  3    ViewContext,
  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
105impl Sealed for MouseDownEvent {}
106impl InputEvent for MouseDownEvent {
107    fn to_platform_input(self) -> PlatformInput {
108        PlatformInput::MouseDown(self)
109    }
110}
111impl MouseEvent for MouseDownEvent {}
112
113/// A mouse up event from the platform
114#[derive(Clone, Debug, Default)]
115pub struct MouseUpEvent {
116    /// Which mouse button was released.
117    pub button: MouseButton,
118
119    /// The position of the mouse on the window.
120    pub position: Point<Pixels>,
121
122    /// The modifiers that were held down when the mouse was released.
123    pub modifiers: Modifiers,
124
125    /// The number of times the button has been clicked.
126    pub click_count: usize,
127}
128
129impl Sealed for MouseUpEvent {}
130impl InputEvent for MouseUpEvent {
131    fn to_platform_input(self) -> PlatformInput {
132        PlatformInput::MouseUp(self)
133    }
134}
135impl MouseEvent for MouseUpEvent {}
136
137/// A click event, generated when a mouse button is pressed and released.
138#[derive(Clone, Debug, Default)]
139pub struct ClickEvent {
140    /// The mouse event when the button was pressed.
141    pub down: MouseDownEvent,
142
143    /// The mouse event when the button was released.
144    pub up: MouseUpEvent,
145}
146
147/// An enum representing the mouse button that was pressed.
148#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
149pub enum MouseButton {
150    /// The left mouse button.
151    Left,
152
153    /// The right mouse button.
154    Right,
155
156    /// The middle mouse button.
157    Middle,
158
159    /// A navigation button, such as back or forward.
160    Navigate(NavigationDirection),
161}
162
163impl MouseButton {
164    /// Get all the mouse buttons in a list.
165    pub fn all() -> Vec<Self> {
166        vec![
167            MouseButton::Left,
168            MouseButton::Right,
169            MouseButton::Middle,
170            MouseButton::Navigate(NavigationDirection::Back),
171            MouseButton::Navigate(NavigationDirection::Forward),
172        ]
173    }
174}
175
176impl Default for MouseButton {
177    fn default() -> Self {
178        Self::Left
179    }
180}
181
182/// A navigation direction, such as back or forward.
183#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
184pub enum NavigationDirection {
185    /// The back button.
186    Back,
187
188    /// The forward button.
189    Forward,
190}
191
192impl Default for NavigationDirection {
193    fn default() -> Self {
194        Self::Back
195    }
196}
197
198/// A mouse move event from the platform
199#[derive(Clone, Debug, Default)]
200pub struct MouseMoveEvent {
201    /// The position of the mouse on the window.
202    pub position: Point<Pixels>,
203
204    /// The mouse button that was pressed, if any.
205    pub pressed_button: Option<MouseButton>,
206
207    /// The modifiers that were held down when the mouse was moved.
208    pub modifiers: Modifiers,
209}
210
211impl Sealed for MouseMoveEvent {}
212impl InputEvent for MouseMoveEvent {
213    fn to_platform_input(self) -> PlatformInput {
214        PlatformInput::MouseMove(self)
215    }
216}
217impl MouseEvent for MouseMoveEvent {}
218
219impl MouseMoveEvent {
220    /// Returns true if the left mouse button is currently held down.
221    pub fn dragging(&self) -> bool {
222        self.pressed_button == Some(MouseButton::Left)
223    }
224}
225
226/// A mouse wheel event from the platform
227#[derive(Clone, Debug, Default)]
228pub struct ScrollWheelEvent {
229    /// The position of the mouse on the window.
230    pub position: Point<Pixels>,
231
232    /// The change in scroll wheel position for this event.
233    pub delta: ScrollDelta,
234
235    /// The modifiers that were held down when the mouse was moved.
236    pub modifiers: Modifiers,
237
238    /// The phase of the touch event.
239    pub touch_phase: TouchPhase,
240}
241
242impl Sealed for ScrollWheelEvent {}
243impl InputEvent for ScrollWheelEvent {
244    fn to_platform_input(self) -> PlatformInput {
245        PlatformInput::ScrollWheel(self)
246    }
247}
248impl MouseEvent for ScrollWheelEvent {}
249
250impl Deref for ScrollWheelEvent {
251    type Target = Modifiers;
252
253    fn deref(&self) -> &Self::Target {
254        &self.modifiers
255    }
256}
257
258/// The scroll delta for a scroll wheel event.
259#[derive(Clone, Copy, Debug)]
260pub enum ScrollDelta {
261    /// An exact scroll delta in pixels.
262    Pixels(Point<Pixels>),
263    /// An inexact scroll delta in lines.
264    Lines(Point<f32>),
265}
266
267impl Default for ScrollDelta {
268    fn default() -> Self {
269        Self::Lines(Default::default())
270    }
271}
272
273impl ScrollDelta {
274    /// Returns true if this is a precise scroll delta in pixels.
275    pub fn precise(&self) -> bool {
276        match self {
277            ScrollDelta::Pixels(_) => true,
278            ScrollDelta::Lines(_) => false,
279        }
280    }
281
282    /// Converts this scroll event into exact pixels.
283    pub fn pixel_delta(&self, line_height: Pixels) -> Point<Pixels> {
284        match self {
285            ScrollDelta::Pixels(delta) => *delta,
286            ScrollDelta::Lines(delta) => point(line_height * delta.x, line_height * delta.y),
287        }
288    }
289
290    /// Combines two scroll deltas into one.
291    pub fn coalesce(self, other: ScrollDelta) -> ScrollDelta {
292        match (self, other) {
293            (ScrollDelta::Pixels(px_a), ScrollDelta::Pixels(px_b)) => {
294                ScrollDelta::Pixels(px_a + px_b)
295            }
296
297            (ScrollDelta::Lines(lines_a), ScrollDelta::Lines(lines_b)) => {
298                ScrollDelta::Lines(lines_a + lines_b)
299            }
300
301            _ => other,
302        }
303    }
304}
305
306/// A mouse exit event from the platform, generated when the mouse leaves the window.
307/// The position generated should be just outside of the window's bounds.
308#[derive(Clone, Debug, Default)]
309pub struct MouseExitEvent {
310    /// The position of the mouse relative to the window.
311    pub position: Point<Pixels>,
312    /// The mouse button that was pressed, if any.
313    pub pressed_button: Option<MouseButton>,
314    /// The modifiers that were held down when the mouse was moved.
315    pub modifiers: Modifiers,
316}
317
318impl Sealed for MouseExitEvent {}
319impl InputEvent for MouseExitEvent {
320    fn to_platform_input(self) -> PlatformInput {
321        PlatformInput::MouseExited(self)
322    }
323}
324impl MouseEvent for MouseExitEvent {}
325
326impl Deref for MouseExitEvent {
327    type Target = Modifiers;
328
329    fn deref(&self) -> &Self::Target {
330        &self.modifiers
331    }
332}
333
334/// A collection of paths from the platform, such as from a file drop.
335#[derive(Debug, Clone, Default)]
336pub struct ExternalPaths(pub(crate) SmallVec<[PathBuf; 2]>);
337
338impl ExternalPaths {
339    /// Convert this collection of paths into a slice.
340    pub fn paths(&self) -> &[PathBuf] {
341        &self.0
342    }
343}
344
345impl Render for ExternalPaths {
346    fn render(&mut self, _: &mut ViewContext<Self>) -> impl IntoElement {
347        // the platform will render icons for the dragged files
348        Empty
349    }
350}
351
352/// A file drop event from the platform, generated when files are dragged and dropped onto the window.
353#[derive(Debug, Clone)]
354pub enum FileDropEvent {
355    /// The files have entered the window.
356    Entered {
357        /// The position of the mouse relative to the window.
358        position: Point<Pixels>,
359        /// The paths of the files that are being dragged.
360        paths: ExternalPaths,
361    },
362    /// The files are being dragged over the window
363    Pending {
364        /// The position of the mouse relative to the window.
365        position: Point<Pixels>,
366    },
367    /// The files have been dropped onto the window.
368    Submit {
369        /// The position of the mouse relative to the window.
370        position: Point<Pixels>,
371    },
372    /// The user has stopped dragging the files over the window.
373    Exited,
374}
375
376impl Sealed for FileDropEvent {}
377impl InputEvent for FileDropEvent {
378    fn to_platform_input(self) -> PlatformInput {
379        PlatformInput::FileDrop(self)
380    }
381}
382impl MouseEvent for FileDropEvent {}
383
384/// An enum corresponding to all kinds of platform input events.
385#[derive(Clone, Debug)]
386pub enum PlatformInput {
387    /// A key was pressed.
388    KeyDown(KeyDownEvent),
389    /// A key was released.
390    KeyUp(KeyUpEvent),
391    /// The keyboard modifiers were changed.
392    ModifiersChanged(ModifiersChangedEvent),
393    /// The mouse was pressed.
394    MouseDown(MouseDownEvent),
395    /// The mouse was released.
396    MouseUp(MouseUpEvent),
397    /// The mouse was moved.
398    MouseMove(MouseMoveEvent),
399    /// The mouse exited the window.
400    MouseExited(MouseExitEvent),
401    /// The scroll wheel was used.
402    ScrollWheel(ScrollWheelEvent),
403    /// Files were dragged and dropped onto the window.
404    FileDrop(FileDropEvent),
405}
406
407impl PlatformInput {
408    pub(crate) fn mouse_event(&self) -> Option<&dyn Any> {
409        match self {
410            PlatformInput::KeyDown { .. } => None,
411            PlatformInput::KeyUp { .. } => None,
412            PlatformInput::ModifiersChanged { .. } => None,
413            PlatformInput::MouseDown(event) => Some(event),
414            PlatformInput::MouseUp(event) => Some(event),
415            PlatformInput::MouseMove(event) => Some(event),
416            PlatformInput::MouseExited(event) => Some(event),
417            PlatformInput::ScrollWheel(event) => Some(event),
418            PlatformInput::FileDrop(event) => Some(event),
419        }
420    }
421
422    pub(crate) fn keyboard_event(&self) -> Option<&dyn Any> {
423        match self {
424            PlatformInput::KeyDown(event) => Some(event),
425            PlatformInput::KeyUp(event) => Some(event),
426            PlatformInput::ModifiersChanged(event) => Some(event),
427            PlatformInput::MouseDown(_) => None,
428            PlatformInput::MouseUp(_) => None,
429            PlatformInput::MouseMove(_) => None,
430            PlatformInput::MouseExited(_) => None,
431            PlatformInput::ScrollWheel(_) => None,
432            PlatformInput::FileDrop(_) => None,
433        }
434    }
435}
436
437#[cfg(test)]
438mod test {
439
440    use crate::{
441        self as gpui, div, Element, FocusHandle, InteractiveElement, IntoElement, KeyBinding,
442        Keystroke, ParentElement, Render, TestAppContext, VisualContext,
443    };
444
445    struct TestView {
446        saw_key_down: bool,
447        saw_action: bool,
448        focus_handle: FocusHandle,
449    }
450
451    actions!(test, [TestAction]);
452
453    impl Render for TestView {
454        fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl Element {
455            div().id("testview").child(
456                div()
457                    .key_context("parent")
458                    .on_key_down(cx.listener(|this, _, cx| {
459                        cx.stop_propagation();
460                        this.saw_key_down = true
461                    }))
462                    .on_action(
463                        cx.listener(|this: &mut TestView, _: &TestAction, _| {
464                            this.saw_action = true
465                        }),
466                    )
467                    .child(
468                        div()
469                            .key_context("nested")
470                            .track_focus(&self.focus_handle)
471                            .into_element(),
472                    ),
473            )
474        }
475    }
476
477    #[gpui::test]
478    fn test_on_events(cx: &mut TestAppContext) {
479        let window = cx.update(|cx| {
480            cx.open_window(Default::default(), |cx| {
481                cx.new_view(|cx| TestView {
482                    saw_key_down: false,
483                    saw_action: false,
484                    focus_handle: cx.focus_handle(),
485                })
486            })
487        });
488
489        cx.update(|cx| {
490            cx.bind_keys(vec![KeyBinding::new("ctrl-g", TestAction, Some("parent"))]);
491        });
492
493        window
494            .update(cx, |test_view, cx| cx.focus(&test_view.focus_handle))
495            .unwrap();
496
497        cx.dispatch_keystroke(*window, Keystroke::parse("a").unwrap());
498        cx.dispatch_keystroke(*window, Keystroke::parse("ctrl-g").unwrap());
499
500        window
501            .update(cx, |test_view, _| {
502                assert!(test_view.saw_key_down || test_view.saw_action);
503                assert!(test_view.saw_key_down);
504                assert!(test_view.saw_action);
505            })
506            .unwrap();
507    }
508}