interactive.rs

  1use crate::{
  2    Bounds, Capslock, Context, Empty, IntoElement, Keystroke, Modifiers, Pixels, Point, Render,
  3    Window, point, seal::Sealed,
  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/// A gesture event from the platform.
 21pub trait GestureEvent: InputEvent {}
 22
 23/// The key down event equivalent for the platform.
 24#[derive(Clone, Debug, Eq, PartialEq)]
 25pub struct KeyDownEvent {
 26    /// The keystroke that was generated.
 27    pub keystroke: Keystroke,
 28
 29    /// Whether the key is currently held down.
 30    pub is_held: bool,
 31
 32    /// Whether to prefer character input over keybindings for this keystroke.
 33    /// In some cases, like AltGr on Windows, modifiers are significant for character input.
 34    pub prefer_character_input: bool,
 35}
 36
 37impl Sealed for KeyDownEvent {}
 38impl InputEvent for KeyDownEvent {
 39    fn to_platform_input(self) -> PlatformInput {
 40        PlatformInput::KeyDown(self)
 41    }
 42}
 43impl KeyEvent for KeyDownEvent {}
 44
 45/// The key up event equivalent for the platform.
 46#[derive(Clone, Debug)]
 47pub struct KeyUpEvent {
 48    /// The keystroke that was released.
 49    pub keystroke: Keystroke,
 50}
 51
 52impl Sealed for KeyUpEvent {}
 53impl InputEvent for KeyUpEvent {
 54    fn to_platform_input(self) -> PlatformInput {
 55        PlatformInput::KeyUp(self)
 56    }
 57}
 58impl KeyEvent for KeyUpEvent {}
 59
 60/// The modifiers changed event equivalent for the platform.
 61#[derive(Clone, Debug, Default)]
 62pub struct ModifiersChangedEvent {
 63    /// The new state of the modifier keys
 64    pub modifiers: Modifiers,
 65    /// The new state of the capslock key
 66    pub capslock: Capslock,
 67}
 68
 69impl Sealed for ModifiersChangedEvent {}
 70impl InputEvent for ModifiersChangedEvent {
 71    fn to_platform_input(self) -> PlatformInput {
 72        PlatformInput::ModifiersChanged(self)
 73    }
 74}
 75impl KeyEvent for ModifiersChangedEvent {}
 76
 77impl Deref for ModifiersChangedEvent {
 78    type Target = Modifiers;
 79
 80    fn deref(&self) -> &Self::Target {
 81        &self.modifiers
 82    }
 83}
 84
 85/// The phase of a touch motion event.
 86/// Based on the winit enum of the same name.
 87#[derive(Clone, Copy, Debug, Default)]
 88pub enum TouchPhase {
 89    /// The touch started.
 90    Started,
 91    /// The touch event is moving.
 92    #[default]
 93    Moved,
 94    /// The touch phase has ended
 95    Ended,
 96}
 97
 98/// A mouse down event from the platform
 99#[derive(Clone, Debug, Default)]
100pub struct MouseDownEvent {
101    /// Which mouse button was pressed.
102    pub button: MouseButton,
103
104    /// The position of the mouse on the window.
105    pub position: Point<Pixels>,
106
107    /// The modifiers that were held down when the mouse was pressed.
108    pub modifiers: Modifiers,
109
110    /// The number of times the button has been clicked.
111    pub click_count: usize,
112
113    /// Whether this is the first, focusing click.
114    pub first_mouse: bool,
115}
116
117impl Sealed for MouseDownEvent {}
118impl InputEvent for MouseDownEvent {
119    fn to_platform_input(self) -> PlatformInput {
120        PlatformInput::MouseDown(self)
121    }
122}
123impl MouseEvent for MouseDownEvent {}
124
125impl MouseDownEvent {
126    /// Returns true if this mouse up event should focus the element.
127    pub fn is_focusing(&self) -> bool {
128        match self.button {
129            MouseButton::Left => true,
130            _ => false,
131        }
132    }
133}
134
135/// A mouse up event from the platform
136#[derive(Clone, Debug, Default)]
137pub struct MouseUpEvent {
138    /// Which mouse button was released.
139    pub button: MouseButton,
140
141    /// The position of the mouse on the window.
142    pub position: Point<Pixels>,
143
144    /// The modifiers that were held down when the mouse was released.
145    pub modifiers: Modifiers,
146
147    /// The number of times the button has been clicked.
148    pub click_count: usize,
149}
150
151impl Sealed for MouseUpEvent {}
152impl InputEvent for MouseUpEvent {
153    fn to_platform_input(self) -> PlatformInput {
154        PlatformInput::MouseUp(self)
155    }
156}
157
158impl MouseEvent for MouseUpEvent {}
159
160impl MouseUpEvent {
161    /// Returns true if this mouse up event should focus the element.
162    pub fn is_focusing(&self) -> bool {
163        match self.button {
164            MouseButton::Left => true,
165            _ => false,
166        }
167    }
168}
169
170/// A click event, generated when a mouse button is pressed and released.
171#[derive(Clone, Debug, Default)]
172pub struct MouseClickEvent {
173    /// The mouse event when the button was pressed.
174    pub down: MouseDownEvent,
175
176    /// The mouse event when the button was released.
177    pub up: MouseUpEvent,
178}
179
180/// The stage of a pressure click event.
181#[derive(Clone, Copy, Debug, Default, PartialEq)]
182pub enum PressureStage {
183    /// No pressure.
184    #[default]
185    Zero,
186    /// Normal click pressure.
187    Normal,
188    /// High pressure, enough to trigger a force click.
189    Force,
190}
191
192/// A mouse pressure event from the platform. Generated when a force-sensitive trackpad is pressed hard.
193/// Currently only implemented for macOS trackpads.
194#[derive(Debug, Clone, Default)]
195pub struct MousePressureEvent {
196    /// Pressure of the current stage as a float between 0 and 1
197    pub pressure: f32,
198    /// The pressure stage of the event.
199    pub stage: PressureStage,
200    /// The position of the mouse on the window.
201    pub position: Point<Pixels>,
202    /// The modifiers that were held down when the mouse pressure changed.
203    pub modifiers: Modifiers,
204}
205
206impl Sealed for MousePressureEvent {}
207impl InputEvent for MousePressureEvent {
208    fn to_platform_input(self) -> PlatformInput {
209        PlatformInput::MousePressure(self)
210    }
211}
212impl MouseEvent for MousePressureEvent {}
213
214/// A click event that was generated by a keyboard button being pressed and released.
215#[derive(Clone, Debug, Default)]
216pub struct KeyboardClickEvent {
217    /// The keyboard button that was pressed to trigger the click.
218    pub button: KeyboardButton,
219
220    /// The bounds of the element that was clicked.
221    pub bounds: Bounds<Pixels>,
222}
223
224/// A click event, generated when a mouse button or keyboard button is pressed and released.
225#[derive(Clone, Debug)]
226pub enum ClickEvent {
227    /// A click event trigger by a mouse button being pressed and released.
228    Mouse(MouseClickEvent),
229    /// A click event trigger by a keyboard button being pressed and released.
230    Keyboard(KeyboardClickEvent),
231}
232
233impl Default for ClickEvent {
234    fn default() -> Self {
235        ClickEvent::Keyboard(KeyboardClickEvent::default())
236    }
237}
238
239impl ClickEvent {
240    /// Returns the modifiers that were held during the click event
241    ///
242    /// `Keyboard`: The keyboard click events never have modifiers.
243    /// `Mouse`: Modifiers that were held during the mouse key up event.
244    pub fn modifiers(&self) -> Modifiers {
245        match self {
246            // Click events are only generated from keyboard events _without any modifiers_, so we know the modifiers are always Default
247            ClickEvent::Keyboard(_) => Modifiers::default(),
248            // Click events on the web only reflect the modifiers for the keyup event,
249            // tested via observing the behavior of the `ClickEvent.shiftKey` field in Chrome 138
250            // under various combinations of modifiers and keyUp / keyDown events.
251            ClickEvent::Mouse(event) => event.up.modifiers,
252        }
253    }
254
255    /// Returns the position of the click event
256    ///
257    /// `Keyboard`: The bottom left corner of the clicked hitbox
258    /// `Mouse`: The position of the mouse when the button was released.
259    pub fn position(&self) -> Point<Pixels> {
260        match self {
261            ClickEvent::Keyboard(event) => event.bounds.bottom_left(),
262            ClickEvent::Mouse(event) => event.up.position,
263        }
264    }
265
266    /// Returns the mouse position of the click event
267    ///
268    /// `Keyboard`: None
269    /// `Mouse`: The position of the mouse when the button was released.
270    pub fn mouse_position(&self) -> Option<Point<Pixels>> {
271        match self {
272            ClickEvent::Keyboard(_) => None,
273            ClickEvent::Mouse(event) => Some(event.up.position),
274        }
275    }
276
277    /// Returns if this was a right click
278    ///
279    /// `Keyboard`: false
280    /// `Mouse`: Whether the right button was pressed and released
281    pub fn is_right_click(&self) -> bool {
282        match self {
283            ClickEvent::Keyboard(_) => false,
284            ClickEvent::Mouse(event) => {
285                event.down.button == MouseButton::Right && event.up.button == MouseButton::Right
286            }
287        }
288    }
289
290    /// Returns if this was a middle click
291    ///
292    /// `Keyboard`: false
293    /// `Mouse`: Whether the middle button was pressed and released
294    pub fn is_middle_click(&self) -> bool {
295        match self {
296            ClickEvent::Keyboard(_) => false,
297            ClickEvent::Mouse(event) => {
298                event.down.button == MouseButton::Middle && event.up.button == MouseButton::Middle
299            }
300        }
301    }
302
303    /// Returns whether the click was a standard click
304    ///
305    /// `Keyboard`: Always true
306    /// `Mouse`: Left button pressed and released
307    pub fn standard_click(&self) -> bool {
308        match self {
309            ClickEvent::Keyboard(_) => true,
310            ClickEvent::Mouse(event) => {
311                event.down.button == MouseButton::Left && event.up.button == MouseButton::Left
312            }
313        }
314    }
315
316    /// Returns whether the click focused the element
317    ///
318    /// `Keyboard`: false, keyboard clicks only work if an element is already focused
319    /// `Mouse`: Whether this was the first focusing click
320    pub fn first_focus(&self) -> bool {
321        match self {
322            ClickEvent::Keyboard(_) => false,
323            ClickEvent::Mouse(event) => event.down.first_mouse,
324        }
325    }
326
327    /// Returns the click count of the click event
328    ///
329    /// `Keyboard`: Always 1
330    /// `Mouse`: Count of clicks from MouseUpEvent
331    pub fn click_count(&self) -> usize {
332        match self {
333            ClickEvent::Keyboard(_) => 1,
334            ClickEvent::Mouse(event) => event.up.click_count,
335        }
336    }
337
338    /// Returns whether the click event is generated by a keyboard event
339    pub fn is_keyboard(&self) -> bool {
340        match self {
341            ClickEvent::Mouse(_) => false,
342            ClickEvent::Keyboard(_) => true,
343        }
344    }
345}
346
347/// An enum representing the keyboard button that was pressed for a click event.
348#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug, Default)]
349pub enum KeyboardButton {
350    /// Enter key was clicked
351    #[default]
352    Enter,
353    /// Space key was clicked
354    Space,
355}
356
357/// An enum representing the mouse button that was pressed.
358#[derive(Hash, Default, PartialEq, Eq, Copy, Clone, Debug)]
359pub enum MouseButton {
360    /// The left mouse button.
361    #[default]
362    Left,
363
364    /// The right mouse button.
365    Right,
366
367    /// The middle mouse button.
368    Middle,
369
370    /// A navigation button, such as back or forward.
371    Navigate(NavigationDirection),
372}
373
374impl MouseButton {
375    /// Get all the mouse buttons in a list.
376    pub fn all() -> Vec<Self> {
377        vec![
378            MouseButton::Left,
379            MouseButton::Right,
380            MouseButton::Middle,
381            MouseButton::Navigate(NavigationDirection::Back),
382            MouseButton::Navigate(NavigationDirection::Forward),
383        ]
384    }
385}
386
387/// A navigation direction, such as back or forward.
388#[derive(Hash, Default, PartialEq, Eq, Copy, Clone, Debug)]
389pub enum NavigationDirection {
390    /// The back button.
391    #[default]
392    Back,
393
394    /// The forward button.
395    Forward,
396}
397
398/// A mouse move event from the platform.
399#[derive(Clone, Debug, Default)]
400pub struct MouseMoveEvent {
401    /// The position of the mouse on the window.
402    pub position: Point<Pixels>,
403
404    /// The mouse button that was pressed, if any.
405    pub pressed_button: Option<MouseButton>,
406
407    /// The modifiers that were held down when the mouse was moved.
408    pub modifiers: Modifiers,
409}
410
411impl Sealed for MouseMoveEvent {}
412impl InputEvent for MouseMoveEvent {
413    fn to_platform_input(self) -> PlatformInput {
414        PlatformInput::MouseMove(self)
415    }
416}
417impl MouseEvent for MouseMoveEvent {}
418
419impl MouseMoveEvent {
420    /// Returns true if the left mouse button is currently held down.
421    pub fn dragging(&self) -> bool {
422        self.pressed_button == Some(MouseButton::Left)
423    }
424}
425
426/// A mouse wheel event from the platform.
427#[derive(Clone, Debug, Default)]
428pub struct ScrollWheelEvent {
429    /// The position of the mouse on the window.
430    pub position: Point<Pixels>,
431
432    /// The change in scroll wheel position for this event.
433    pub delta: ScrollDelta,
434
435    /// The modifiers that were held down when the mouse was moved.
436    pub modifiers: Modifiers,
437
438    /// The phase of the touch event.
439    pub touch_phase: TouchPhase,
440}
441
442impl Sealed for ScrollWheelEvent {}
443impl InputEvent for ScrollWheelEvent {
444    fn to_platform_input(self) -> PlatformInput {
445        PlatformInput::ScrollWheel(self)
446    }
447}
448impl MouseEvent for ScrollWheelEvent {}
449
450impl Deref for ScrollWheelEvent {
451    type Target = Modifiers;
452
453    fn deref(&self) -> &Self::Target {
454        &self.modifiers
455    }
456}
457
458/// The scroll delta for a scroll wheel event.
459#[derive(Clone, Copy, Debug)]
460pub enum ScrollDelta {
461    /// An exact scroll delta in pixels.
462    Pixels(Point<Pixels>),
463    /// An inexact scroll delta in lines.
464    Lines(Point<f32>),
465}
466
467impl Default for ScrollDelta {
468    fn default() -> Self {
469        Self::Lines(Default::default())
470    }
471}
472
473/// A pinch gesture event from the platform, generated when the user performs
474/// a pinch-to-zoom gesture (typically on a trackpad).
475///
476/// Note: This event is only available on macOS and Wayland (Linux).
477/// On Windows, pinch gestures are simulated as scroll wheel events with Ctrl held.
478#[derive(Clone, Debug, Default)]
479#[cfg(any(target_os = "linux", target_os = "macos"))]
480pub struct PinchEvent {
481    /// The position of the pinch center on the window.
482    pub position: Point<Pixels>,
483
484    /// The zoom delta for this event.
485    /// Positive values indicate zooming in, negative values indicate zooming out.
486    /// For example, 0.1 represents a 10% zoom increase.
487    pub delta: f32,
488
489    /// The modifiers that were held down during the pinch gesture.
490    pub modifiers: Modifiers,
491
492    /// The phase of the pinch gesture.
493    pub phase: TouchPhase,
494}
495
496#[cfg(any(target_os = "linux", target_os = "macos"))]
497impl Sealed for PinchEvent {}
498#[cfg(any(target_os = "linux", target_os = "macos"))]
499impl InputEvent for PinchEvent {
500    fn to_platform_input(self) -> PlatformInput {
501        PlatformInput::Pinch(self)
502    }
503}
504#[cfg(any(target_os = "linux", target_os = "macos"))]
505impl GestureEvent for PinchEvent {}
506#[cfg(any(target_os = "linux", target_os = "macos"))]
507impl MouseEvent for PinchEvent {}
508
509#[cfg(any(target_os = "linux", target_os = "macos"))]
510impl Deref for PinchEvent {
511    type Target = Modifiers;
512
513    fn deref(&self) -> &Self::Target {
514        &self.modifiers
515    }
516}
517
518impl ScrollDelta {
519    /// Returns true if this is a precise scroll delta in pixels.
520    pub fn precise(&self) -> bool {
521        match self {
522            ScrollDelta::Pixels(_) => true,
523            ScrollDelta::Lines(_) => false,
524        }
525    }
526
527    /// Converts this scroll event into exact pixels.
528    pub fn pixel_delta(&self, line_height: Pixels) -> Point<Pixels> {
529        match self {
530            ScrollDelta::Pixels(delta) => *delta,
531            ScrollDelta::Lines(delta) => point(line_height * delta.x, line_height * delta.y),
532        }
533    }
534
535    /// Combines two scroll deltas into one.
536    /// If the signs of the deltas are the same (both positive or both negative),
537    /// the deltas are added together. If the signs are opposite, the second delta
538    /// (other) is used, effectively overriding the first delta.
539    pub fn coalesce(self, other: ScrollDelta) -> ScrollDelta {
540        match (self, other) {
541            (ScrollDelta::Pixels(a), ScrollDelta::Pixels(b)) => {
542                let x = if a.x.signum() == b.x.signum() {
543                    a.x + b.x
544                } else {
545                    b.x
546                };
547
548                let y = if a.y.signum() == b.y.signum() {
549                    a.y + b.y
550                } else {
551                    b.y
552                };
553
554                ScrollDelta::Pixels(point(x, y))
555            }
556
557            (ScrollDelta::Lines(a), ScrollDelta::Lines(b)) => {
558                let x = if a.x.signum() == b.x.signum() {
559                    a.x + b.x
560                } else {
561                    b.x
562                };
563
564                let y = if a.y.signum() == b.y.signum() {
565                    a.y + b.y
566                } else {
567                    b.y
568                };
569
570                ScrollDelta::Lines(point(x, y))
571            }
572
573            _ => other,
574        }
575    }
576}
577
578/// A mouse exit event from the platform, generated when the mouse leaves the window.
579#[derive(Clone, Debug, Default)]
580pub struct MouseExitEvent {
581    /// The position of the mouse relative to the window.
582    pub position: Point<Pixels>,
583    /// The mouse button that was pressed, if any.
584    pub pressed_button: Option<MouseButton>,
585    /// The modifiers that were held down when the mouse was moved.
586    pub modifiers: Modifiers,
587}
588
589impl Sealed for MouseExitEvent {}
590impl InputEvent for MouseExitEvent {
591    fn to_platform_input(self) -> PlatformInput {
592        PlatformInput::MouseExited(self)
593    }
594}
595
596impl MouseEvent for MouseExitEvent {}
597
598impl Deref for MouseExitEvent {
599    type Target = Modifiers;
600
601    fn deref(&self) -> &Self::Target {
602        &self.modifiers
603    }
604}
605
606/// A collection of paths from the platform, such as from a file drop.
607#[derive(Debug, Clone, Default, Eq, PartialEq)]
608pub struct ExternalPaths(pub SmallVec<[PathBuf; 2]>);
609
610impl ExternalPaths {
611    /// Convert this collection of paths into a slice.
612    pub fn paths(&self) -> &[PathBuf] {
613        &self.0
614    }
615}
616
617impl Render for ExternalPaths {
618    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
619        // the platform will render icons for the dragged files
620        Empty
621    }
622}
623
624/// A file drop event from the platform, generated when files are dragged and dropped onto the window.
625#[derive(Debug, Clone)]
626pub enum FileDropEvent {
627    /// The files have entered the window.
628    Entered {
629        /// The position of the mouse relative to the window.
630        position: Point<Pixels>,
631        /// The paths of the files that are being dragged.
632        paths: ExternalPaths,
633    },
634    /// The files are being dragged over the window
635    Pending {
636        /// The position of the mouse relative to the window.
637        position: Point<Pixels>,
638    },
639    /// The files have been dropped onto the window.
640    Submit {
641        /// The position of the mouse relative to the window.
642        position: Point<Pixels>,
643    },
644    /// The user has stopped dragging the files over the window.
645    Exited,
646}
647
648impl Sealed for FileDropEvent {}
649impl InputEvent for FileDropEvent {
650    fn to_platform_input(self) -> PlatformInput {
651        PlatformInput::FileDrop(self)
652    }
653}
654impl MouseEvent for FileDropEvent {}
655
656/// An enum corresponding to all kinds of platform input events.
657#[derive(Clone, Debug)]
658pub enum PlatformInput {
659    /// A key was pressed.
660    KeyDown(KeyDownEvent),
661    /// A key was released.
662    KeyUp(KeyUpEvent),
663    /// The keyboard modifiers were changed.
664    ModifiersChanged(ModifiersChangedEvent),
665    /// The mouse was pressed.
666    MouseDown(MouseDownEvent),
667    /// The mouse was released.
668    MouseUp(MouseUpEvent),
669    /// Mouse pressure.
670    MousePressure(MousePressureEvent),
671    /// The mouse was moved.
672    MouseMove(MouseMoveEvent),
673    /// The mouse exited the window.
674    MouseExited(MouseExitEvent),
675    /// The scroll wheel was used.
676    ScrollWheel(ScrollWheelEvent),
677    /// A pinch gesture was performed.
678    #[cfg(any(target_os = "linux", target_os = "macos"))]
679    Pinch(PinchEvent),
680    /// Files were dragged and dropped onto the window.
681    FileDrop(FileDropEvent),
682}
683
684impl PlatformInput {
685    pub(crate) fn mouse_event(&self) -> Option<&dyn Any> {
686        match self {
687            PlatformInput::KeyDown { .. } => None,
688            PlatformInput::KeyUp { .. } => None,
689            PlatformInput::ModifiersChanged { .. } => None,
690            PlatformInput::MouseDown(event) => Some(event),
691            PlatformInput::MouseUp(event) => Some(event),
692            PlatformInput::MouseMove(event) => Some(event),
693            PlatformInput::MousePressure(event) => Some(event),
694            PlatformInput::MouseExited(event) => Some(event),
695            PlatformInput::ScrollWheel(event) => Some(event),
696            #[cfg(any(target_os = "linux", target_os = "macos"))]
697            PlatformInput::Pinch(event) => Some(event),
698            PlatformInput::FileDrop(event) => Some(event),
699        }
700    }
701
702    pub(crate) fn keyboard_event(&self) -> Option<&dyn Any> {
703        match self {
704            PlatformInput::KeyDown(event) => Some(event),
705            PlatformInput::KeyUp(event) => Some(event),
706            PlatformInput::ModifiersChanged(event) => Some(event),
707            PlatformInput::MouseDown(_) => None,
708            PlatformInput::MouseUp(_) => None,
709            PlatformInput::MouseMove(_) => None,
710            PlatformInput::MousePressure(_) => None,
711            PlatformInput::MouseExited(_) => None,
712            PlatformInput::ScrollWheel(_) => None,
713            #[cfg(any(target_os = "linux", target_os = "macos"))]
714            PlatformInput::Pinch(_) => None,
715            PlatformInput::FileDrop(_) => None,
716        }
717    }
718}
719
720#[cfg(test)]
721mod test {
722
723    use crate::{
724        self as gpui, AppContext as _, Context, FocusHandle, InteractiveElement, IntoElement,
725        KeyBinding, Keystroke, ParentElement, Render, TestAppContext, Window, div,
726    };
727
728    struct TestView {
729        saw_key_down: bool,
730        saw_action: bool,
731        focus_handle: FocusHandle,
732    }
733
734    actions!(test_only, [TestAction]);
735
736    impl Render for TestView {
737        fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
738            div().id("testview").child(
739                div()
740                    .key_context("parent")
741                    .on_key_down(cx.listener(|this, _, _, cx| {
742                        cx.stop_propagation();
743                        this.saw_key_down = true
744                    }))
745                    .on_action(cx.listener(|this: &mut TestView, _: &TestAction, _, _| {
746                        this.saw_action = true
747                    }))
748                    .child(
749                        div()
750                            .key_context("nested")
751                            .track_focus(&self.focus_handle)
752                            .into_element(),
753                    ),
754            )
755        }
756    }
757
758    #[gpui::test]
759    fn test_on_events(cx: &mut TestAppContext) {
760        let window = cx.update(|cx| {
761            cx.open_window(Default::default(), |_, cx| {
762                cx.new(|cx| TestView {
763                    saw_key_down: false,
764                    saw_action: false,
765                    focus_handle: cx.focus_handle(),
766                })
767            })
768            .unwrap()
769        });
770
771        cx.update(|cx| {
772            cx.bind_keys(vec![KeyBinding::new("ctrl-g", TestAction, Some("parent"))]);
773        });
774
775        window
776            .update(cx, |test_view, window, cx| {
777                window.focus(&test_view.focus_handle, cx)
778            })
779            .unwrap();
780
781        cx.dispatch_keystroke(*window, Keystroke::parse("a").unwrap());
782        cx.dispatch_keystroke(*window, Keystroke::parse("ctrl-g").unwrap());
783
784        window
785            .update(cx, |test_view, _, _| {
786                assert!(test_view.saw_key_down || test_view.saw_action);
787                assert!(test_view.saw_key_down);
788                assert!(test_view.saw_action);
789            })
790            .unwrap();
791    }
792}