1use crate::{
2 BackGestureEvent, Bounds, Capslock, Context, Empty, IntoElement, Keystroke, LongPressEvent,
3 Modifiers, Pixels, Point, Render, TouchDragEvent, Window, point, seal::Sealed,
4};
5use smallvec::SmallVec;
6use std::{any::Any, fmt::Debug, ops::Deref, path::PathBuf};
7
8pub trait InputEvent: Sealed + 'static {
10 fn to_platform_input(self) -> PlatformInput;
12}
13
14pub trait KeyEvent: InputEvent {}
16
17pub trait MouseEvent: InputEvent {}
19
20pub trait GestureEvent: InputEvent {}
22
23#[derive(Clone, Debug, Eq, PartialEq)]
25pub struct KeyDownEvent {
26 pub keystroke: Keystroke,
28
29 pub is_held: bool,
31
32 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#[derive(Clone, Debug)]
47pub struct KeyUpEvent {
48 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#[derive(Clone, Debug, Default)]
62pub struct ModifiersChangedEvent {
63 pub modifiers: Modifiers,
65 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#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
88pub enum TouchPhase {
89 Started,
91 #[default]
93 Moved,
94 Ended,
96 Cancelled,
100}
101
102#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
109pub struct TouchId(pub u64);
110
111#[derive(Clone, Debug, Default)]
119pub struct TouchEvent {
120 pub id: TouchId,
122 pub phase: TouchPhase,
124 pub position: Point<Pixels>,
126 pub predicted_position: Option<Point<Pixels>>,
135 pub force: Option<f32>,
137}
138
139impl Sealed for TouchEvent {}
140impl InputEvent for TouchEvent {
141 fn to_platform_input(self) -> PlatformInput {
142 PlatformInput::Touch(self)
143 }
144}
145
146#[derive(Clone, Debug, Default)]
148pub struct MouseDownEvent {
149 pub button: MouseButton,
151
152 pub position: Point<Pixels>,
154
155 pub modifiers: Modifiers,
157
158 pub click_count: usize,
160
161 pub first_mouse: bool,
163}
164
165impl Sealed for MouseDownEvent {}
166impl InputEvent for MouseDownEvent {
167 fn to_platform_input(self) -> PlatformInput {
168 PlatformInput::MouseDown(self)
169 }
170}
171impl MouseEvent for MouseDownEvent {}
172
173impl MouseDownEvent {
174 pub fn is_focusing(&self) -> bool {
176 match self.button {
177 MouseButton::Left => true,
178 _ => false,
179 }
180 }
181}
182
183#[derive(Clone, Debug, Default)]
185pub struct MouseUpEvent {
186 pub button: MouseButton,
188
189 pub position: Point<Pixels>,
191
192 pub modifiers: Modifiers,
194
195 pub click_count: usize,
197}
198
199impl Sealed for MouseUpEvent {}
200impl InputEvent for MouseUpEvent {
201 fn to_platform_input(self) -> PlatformInput {
202 PlatformInput::MouseUp(self)
203 }
204}
205
206impl MouseEvent for MouseUpEvent {}
207
208impl MouseUpEvent {
209 pub fn is_focusing(&self) -> bool {
211 match self.button {
212 MouseButton::Left => true,
213 _ => false,
214 }
215 }
216}
217
218#[derive(Clone, Debug, Default)]
220pub struct MouseClickEvent {
221 pub down: MouseDownEvent,
223
224 pub up: MouseUpEvent,
226}
227
228#[derive(Clone, Copy, Debug, Default, PartialEq)]
230pub enum PressureStage {
231 #[default]
233 Zero,
234 Normal,
236 Force,
238}
239
240#[derive(Debug, Clone, Default)]
243pub struct MousePressureEvent {
244 pub pressure: f32,
246 pub stage: PressureStage,
248 pub position: Point<Pixels>,
250 pub modifiers: Modifiers,
252}
253
254impl Sealed for MousePressureEvent {}
255impl InputEvent for MousePressureEvent {
256 fn to_platform_input(self) -> PlatformInput {
257 PlatformInput::MousePressure(self)
258 }
259}
260impl MouseEvent for MousePressureEvent {}
261
262#[derive(Clone, Copy, Debug, Default, PartialEq)]
264pub struct PenSample {
265 pub position: Point<Pixels>,
267 pub pressure: f32,
269 pub tangential_pressure: f32,
271 pub tilt_x: f32,
273 pub tilt_y: f32,
275 pub twist: f32,
277 pub width: f32,
279 pub height: f32,
281 pub time: Option<std::time::Instant>,
283}
284
285#[derive(Clone, Debug, Default, PartialEq)]
287pub struct PenInput {
288 pub pointer_id: u32,
290 pub eraser: bool,
292 pub barrel: bool,
294 pub in_contact: bool,
296 pub sample: PenSample,
298 pub history: Vec<PenSample>,
300}
301
302#[derive(Clone, Debug)]
304pub enum PenAction {
305 Down(MouseDownEvent),
307 Up(MouseUpEvent),
309 Move(MouseMoveEvent),
311 Exited(MouseExitEvent),
313}
314
315#[derive(Clone, Debug)]
318pub struct PenEvent {
319 pub action: PenAction,
321 pub pen: PenInput,
323}
324
325impl PenEvent {
326 pub(crate) fn into_mouse(self) -> (PlatformInput, PenInput) {
327 let input = match self.action {
328 PenAction::Down(e) => PlatformInput::MouseDown(e),
329 PenAction::Up(e) => PlatformInput::MouseUp(e),
330 PenAction::Move(e) => PlatformInput::MouseMove(e),
331 PenAction::Exited(e) => PlatformInput::MouseExited(e),
332 };
333 (input, self.pen)
334 }
335}
336
337impl Sealed for PenEvent {}
338impl InputEvent for PenEvent {
339 fn to_platform_input(self) -> PlatformInput {
340 PlatformInput::Pen(self)
341 }
342}
343
344#[derive(Clone, Debug, Default)]
346pub struct KeyboardClickEvent {
347 pub button: KeyboardButton,
349
350 pub bounds: Bounds<Pixels>,
352}
353
354#[derive(Clone, Debug, Default)]
357pub struct TouchClickEvent {
358 pub position: Point<Pixels>,
360 pub tap_count: usize,
363 pub long_press: bool,
367}
368
369#[derive(Clone, Debug)]
372pub enum ClickEvent {
373 Mouse(MouseClickEvent),
375 Keyboard(KeyboardClickEvent),
377 Touch(TouchClickEvent),
379}
380
381impl Default for ClickEvent {
382 fn default() -> Self {
383 ClickEvent::Keyboard(KeyboardClickEvent::default())
384 }
385}
386
387impl ClickEvent {
388 pub fn modifiers(&self) -> Modifiers {
393 match self {
394 ClickEvent::Keyboard(_) => Modifiers::default(),
396 ClickEvent::Mouse(event) => event.up.modifiers,
400 ClickEvent::Touch(_) => Modifiers::default(),
402 }
403 }
404
405 pub fn position(&self) -> Point<Pixels> {
411 match self {
412 ClickEvent::Keyboard(event) => event.bounds.bottom_left(),
413 ClickEvent::Mouse(event) => event.up.position,
414 ClickEvent::Touch(event) => event.position,
415 }
416 }
417
418 pub fn mouse_position(&self) -> Option<Point<Pixels>> {
424 match self {
425 ClickEvent::Keyboard(_) => None,
426 ClickEvent::Mouse(event) => Some(event.up.position),
427 ClickEvent::Touch(_) => None,
428 }
429 }
430
431 pub fn is_right_click(&self) -> bool {
436 match self {
437 ClickEvent::Keyboard(_) => false,
438 ClickEvent::Mouse(event) => {
439 event.down.button == MouseButton::Right && event.up.button == MouseButton::Right
440 }
441 ClickEvent::Touch(_) => false,
442 }
443 }
444
445 pub fn is_middle_click(&self) -> bool {
450 match self {
451 ClickEvent::Keyboard(_) => false,
452 ClickEvent::Mouse(event) => {
453 event.down.button == MouseButton::Middle && event.up.button == MouseButton::Middle
454 }
455 ClickEvent::Touch(_) => false,
456 }
457 }
458
459 pub fn is_secondary(&self) -> bool {
464 match self {
465 ClickEvent::Keyboard(_) => false,
466 ClickEvent::Mouse(event) => {
467 event.down.button == MouseButton::Right && event.up.button == MouseButton::Right
468 }
469 ClickEvent::Touch(event) => event.long_press,
470 }
471 }
472
473 pub fn standard_click(&self) -> bool {
479 match self {
480 ClickEvent::Keyboard(_) => true,
481 ClickEvent::Mouse(event) => {
482 event.down.button == MouseButton::Left && event.up.button == MouseButton::Left
483 }
484 ClickEvent::Touch(event) => !event.long_press,
485 }
486 }
487
488 pub fn first_focus(&self) -> bool {
494 match self {
495 ClickEvent::Keyboard(_) => false,
496 ClickEvent::Mouse(event) => event.down.first_mouse,
497 ClickEvent::Touch(_) => false,
498 }
499 }
500
501 pub fn click_count(&self) -> usize {
507 match self {
508 ClickEvent::Keyboard(_) => 1,
509 ClickEvent::Mouse(event) => event.up.click_count,
510 ClickEvent::Touch(event) => event.tap_count,
511 }
512 }
513
514 pub fn is_keyboard(&self) -> bool {
516 match self {
517 ClickEvent::Mouse(_) | ClickEvent::Touch(_) => false,
518 ClickEvent::Keyboard(_) => true,
519 }
520 }
521}
522
523#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug, Default)]
525pub enum KeyboardButton {
526 #[default]
528 Enter,
529 Space,
531}
532
533#[derive(Hash, Default, PartialEq, Eq, Copy, Clone, Debug)]
535pub enum MouseButton {
536 #[default]
538 Left,
539
540 Right,
542
543 Middle,
545
546 Navigate(NavigationDirection),
548}
549
550impl MouseButton {
551 pub fn all() -> Vec<Self> {
553 vec![
554 MouseButton::Left,
555 MouseButton::Right,
556 MouseButton::Middle,
557 MouseButton::Navigate(NavigationDirection::Back),
558 MouseButton::Navigate(NavigationDirection::Forward),
559 ]
560 }
561}
562
563#[derive(Hash, Default, PartialEq, Eq, Copy, Clone, Debug)]
565pub enum NavigationDirection {
566 #[default]
568 Back,
569
570 Forward,
572}
573
574#[derive(Clone, Debug, Default)]
576pub struct MouseMoveEvent {
577 pub position: Point<Pixels>,
579
580 pub pressed_button: Option<MouseButton>,
582
583 pub modifiers: Modifiers,
585}
586
587impl Sealed for MouseMoveEvent {}
588impl InputEvent for MouseMoveEvent {
589 fn to_platform_input(self) -> PlatformInput {
590 PlatformInput::MouseMove(self)
591 }
592}
593impl MouseEvent for MouseMoveEvent {}
594
595impl MouseMoveEvent {
596 pub fn dragging(&self) -> bool {
598 self.pressed_button == Some(MouseButton::Left)
599 }
600}
601
602#[derive(Clone, Debug, Default)]
604pub struct ScrollEvent {
605 pub position: Point<Pixels>,
608
609 pub delta: ScrollDelta,
611
612 pub modifiers: Modifiers,
614
615 pub touch_phase: TouchPhase,
617
618 pub source: ScrollSource,
620}
621
622#[deprecated(note = "renamed to `ScrollEvent`")]
624pub type ScrollWheelEvent = ScrollEvent;
625
626#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
628pub enum ScrollSource {
629 #[default]
633 Pointer,
634 Touch,
637}
638
639impl Sealed for ScrollEvent {}
640impl InputEvent for ScrollEvent {
641 fn to_platform_input(self) -> PlatformInput {
642 PlatformInput::Scroll(self)
643 }
644}
645impl MouseEvent for ScrollEvent {}
646
647impl Deref for ScrollEvent {
648 type Target = Modifiers;
649
650 fn deref(&self) -> &Self::Target {
651 &self.modifiers
652 }
653}
654
655#[derive(Clone, Copy, Debug)]
657pub enum ScrollDelta {
658 Pixels(Point<Pixels>),
660 Lines(Point<f32>),
662}
663
664impl Default for ScrollDelta {
665 fn default() -> Self {
666 Self::Lines(Default::default())
667 }
668}
669
670#[derive(Clone, Debug, Default)]
674pub struct PinchEvent {
675 pub position: Point<Pixels>,
677
678 pub delta: f32,
682
683 pub modifiers: Modifiers,
685
686 pub phase: TouchPhase,
688}
689
690impl Sealed for PinchEvent {}
691impl InputEvent for PinchEvent {
692 fn to_platform_input(self) -> PlatformInput {
693 PlatformInput::Pinch(self)
694 }
695}
696impl GestureEvent for PinchEvent {}
697impl MouseEvent for PinchEvent {}
698
699impl Deref for PinchEvent {
700 type Target = Modifiers;
701
702 fn deref(&self) -> &Self::Target {
703 &self.modifiers
704 }
705}
706
707impl ScrollDelta {
708 pub fn precise(&self) -> bool {
710 match self {
711 ScrollDelta::Pixels(_) => true,
712 ScrollDelta::Lines(_) => false,
713 }
714 }
715
716 pub fn pixel_delta(&self, line_height: Pixels) -> Point<Pixels> {
718 match self {
719 ScrollDelta::Pixels(delta) => *delta,
720 ScrollDelta::Lines(delta) => point(line_height * delta.x, line_height * delta.y),
721 }
722 }
723
724 pub fn coalesce(self, other: ScrollDelta) -> ScrollDelta {
729 match (self, other) {
730 (ScrollDelta::Pixels(a), ScrollDelta::Pixels(b)) => {
731 let x = if a.x.signum() == b.x.signum() {
732 a.x + b.x
733 } else {
734 b.x
735 };
736
737 let y = if a.y.signum() == b.y.signum() {
738 a.y + b.y
739 } else {
740 b.y
741 };
742
743 ScrollDelta::Pixels(point(x, y))
744 }
745
746 (ScrollDelta::Lines(a), ScrollDelta::Lines(b)) => {
747 let x = if a.x.signum() == b.x.signum() {
748 a.x + b.x
749 } else {
750 b.x
751 };
752
753 let y = if a.y.signum() == b.y.signum() {
754 a.y + b.y
755 } else {
756 b.y
757 };
758
759 ScrollDelta::Lines(point(x, y))
760 }
761
762 _ => other,
763 }
764 }
765}
766
767#[derive(Clone, Debug, Default)]
769pub struct MouseExitEvent {
770 pub position: Point<Pixels>,
772 pub pressed_button: Option<MouseButton>,
774 pub modifiers: Modifiers,
776}
777
778impl Sealed for MouseExitEvent {}
779impl InputEvent for MouseExitEvent {
780 fn to_platform_input(self) -> PlatformInput {
781 PlatformInput::MouseExited(self)
782 }
783}
784
785impl MouseEvent for MouseExitEvent {}
786
787impl Deref for MouseExitEvent {
788 type Target = Modifiers;
789
790 fn deref(&self) -> &Self::Target {
791 &self.modifiers
792 }
793}
794
795#[derive(Debug, Clone, Default, Eq, PartialEq)]
797pub struct ExternalPaths(pub SmallVec<[PathBuf; 2]>);
798
799impl ExternalPaths {
800 pub fn paths(&self) -> &[PathBuf] {
802 &self.0
803 }
804}
805
806#[derive(Debug, Clone, Eq, PartialEq)]
809pub enum ExternalDragPayload {
810 Files(FileDragPaths),
812}
813
814#[derive(Debug, Clone, Default, Eq, PartialEq)]
817pub struct FileDragPaths(SmallVec<[(PathBuf, bool); 2]>);
818
819impl FileDragPaths {
820 pub fn new(entries: impl IntoIterator<Item = (PathBuf, bool)>) -> Self {
822 Self(entries.into_iter().collect())
823 }
824
825 pub fn entries(&self) -> &[(PathBuf, bool)] {
827 &self.0
828 }
829}
830
831impl Render for ExternalPaths {
832 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
833 Empty
835 }
836}
837
838#[derive(Debug, Clone)]
840pub enum FileDropEvent {
841 Entered {
843 position: Point<Pixels>,
845 paths: ExternalPaths,
847 },
848 Pending {
850 position: Point<Pixels>,
852 },
853 Submit {
855 position: Point<Pixels>,
857 },
858 Exited,
860 Ended,
862}
863
864impl Sealed for FileDropEvent {}
865impl InputEvent for FileDropEvent {
866 fn to_platform_input(self) -> PlatformInput {
867 PlatformInput::FileDrop(self)
868 }
869}
870impl MouseEvent for FileDropEvent {}
871
872#[derive(Clone, Debug)]
874pub enum PlatformInput {
875 KeyDown(KeyDownEvent),
877 KeyUp(KeyUpEvent),
879 ModifiersChanged(ModifiersChangedEvent),
881 MouseDown(MouseDownEvent),
883 MouseUp(MouseUpEvent),
885 MousePressure(MousePressureEvent),
887 MouseMove(MouseMoveEvent),
889 MouseExited(MouseExitEvent),
891 Scroll(ScrollEvent),
894 Pinch(PinchEvent),
896 LongPress(LongPressEvent),
898 TouchDrag(TouchDragEvent),
900 BackGesture(BackGestureEvent),
902 FileDrop(FileDropEvent),
904 Touch(TouchEvent),
906 Pen(PenEvent),
908}
909
910impl PlatformInput {
911 pub(crate) fn mouse_event(&self) -> Option<&dyn Any> {
912 match self {
913 PlatformInput::KeyDown { .. } => None,
914 PlatformInput::KeyUp { .. } => None,
915 PlatformInput::ModifiersChanged { .. } => None,
916 PlatformInput::MouseDown(event) => Some(event),
917 PlatformInput::MouseUp(event) => Some(event),
918 PlatformInput::MouseMove(event) => Some(event),
919 PlatformInput::MousePressure(event) => Some(event),
920 PlatformInput::MouseExited(event) => Some(event),
921 PlatformInput::Scroll(event) => Some(event),
922 PlatformInput::Pinch(event) => Some(event),
923 PlatformInput::LongPress(event) => Some(event),
924 PlatformInput::TouchDrag(event) => Some(event),
925 PlatformInput::BackGesture(event) => Some(event),
926 PlatformInput::FileDrop(event) => Some(event),
927 PlatformInput::Touch(_) => None,
928 PlatformInput::Pen(event) => match &event.action {
929 PenAction::Down(e) => Some(e),
930 PenAction::Up(e) => Some(e),
931 PenAction::Move(e) => Some(e),
932 PenAction::Exited(e) => Some(e),
933 },
934 }
935 }
936
937 pub(crate) fn keyboard_event(&self) -> Option<&dyn Any> {
938 match self {
939 PlatformInput::KeyDown(event) => Some(event),
940 PlatformInput::KeyUp(event) => Some(event),
941 PlatformInput::ModifiersChanged(event) => Some(event),
942 PlatformInput::MouseDown(_) => None,
943 PlatformInput::MouseUp(_) => None,
944 PlatformInput::MouseMove(_) => None,
945 PlatformInput::MousePressure(_) => None,
946 PlatformInput::MouseExited(_) => None,
947 PlatformInput::Scroll(_) => None,
948 PlatformInput::Pinch(_) => None,
949 PlatformInput::LongPress(_) => None,
950 PlatformInput::TouchDrag(_) => None,
951 PlatformInput::BackGesture(_) => None,
952 PlatformInput::FileDrop(_) => None,
953 PlatformInput::Touch(_) => None,
954 PlatformInput::Pen(_) => None,
955 }
956 }
957
958 pub fn kind_name(&self) -> &'static str {
961 match self {
962 PlatformInput::KeyDown(_) => "key_down",
963 PlatformInput::KeyUp(_) => "key_up",
964 PlatformInput::ModifiersChanged(_) => "modifiers_changed",
965 PlatformInput::MouseDown(_) => "mouse_down",
966 PlatformInput::MouseUp(_) => "mouse_up",
967 PlatformInput::MousePressure(_) => "mouse_pressure",
968 PlatformInput::MouseMove(_) => "mouse_move",
969 PlatformInput::MouseExited(_) => "mouse_exited",
970 PlatformInput::Scroll(_) => "scroll",
971 PlatformInput::Pinch(_) => "pinch",
972 PlatformInput::LongPress(_) => "long_press",
973 PlatformInput::TouchDrag(_) => "touch_drag",
974 PlatformInput::BackGesture(_) => "back_gesture",
975 PlatformInput::FileDrop(_) => "file_drop",
976 PlatformInput::Touch(_) => "touch",
977 PlatformInput::Pen(_) => "pen",
978 }
979 }
980
981 pub fn touch_event(&self) -> Option<&TouchEvent> {
983 match self {
984 PlatformInput::Touch(event) => Some(event),
985 _ => None,
986 }
987 }
988}
989
990#[cfg(test)]
991mod test {
992
993 use crate::{
994 self as gpui, AppContext as _, Context, FocusHandle, InteractiveElement, IntoElement,
995 KeyBinding, Keystroke, Modifiers, ParentElement, Render, TestAppContext, Window, div,
996 };
997
998 struct TestView {
999 saw_key_down: bool,
1000 saw_action: bool,
1001 focus_handle: FocusHandle,
1002 }
1003
1004 actions!(test_only, [TestAction]);
1005
1006 impl Render for TestView {
1007 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1008 div().id("testview").child(
1009 div()
1010 .key_context("parent")
1011 .on_key_down(cx.listener(|this, _, _, cx| {
1012 cx.stop_propagation();
1013 this.saw_key_down = true
1014 }))
1015 .on_action(cx.listener(|this: &mut TestView, _: &TestAction, _, _| {
1016 this.saw_action = true
1017 }))
1018 .child(
1019 div()
1020 .key_context("nested")
1021 .track_focus(&self.focus_handle)
1022 .into_element(),
1023 ),
1024 )
1025 }
1026 }
1027
1028 #[gpui::test]
1029 fn test_on_events(cx: &mut TestAppContext) {
1030 let window = cx.update(|cx| {
1031 cx.open_window(Default::default(), |_, cx| {
1032 cx.new(|cx| TestView {
1033 saw_key_down: false,
1034 saw_action: false,
1035 focus_handle: cx.focus_handle(),
1036 })
1037 })
1038 .unwrap()
1039 });
1040
1041 cx.update(|cx| {
1042 cx.bind_keys(vec![KeyBinding::new("ctrl-g", TestAction, Some("parent"))]);
1043 });
1044
1045 window
1046 .update(cx, |test_view, window, cx| {
1047 window.focus(&test_view.focus_handle, cx)
1048 })
1049 .unwrap();
1050
1051 cx.dispatch_keystroke(*window, Keystroke::parse("a").unwrap());
1052 cx.dispatch_keystroke(*window, Keystroke::parse("ctrl-g").unwrap());
1053
1054 window
1055 .update(cx, |test_view, _, _| {
1056 assert!(test_view.saw_key_down || test_view.saw_action);
1057 assert!(test_view.saw_key_down);
1058 assert!(test_view.saw_action);
1059 })
1060 .unwrap();
1061 }
1062
1063 #[gpui::test]
1064 fn test_multi_modifier_gesture_does_not_dispatch_standalone_modifier_binding(
1065 cx: &mut TestAppContext,
1066 ) {
1067 let (test_view, cx) = cx.add_window_view(|_, cx| TestView {
1068 saw_key_down: false,
1069 saw_action: false,
1070 focus_handle: cx.focus_handle(),
1071 });
1072
1073 cx.update(|_, cx| {
1074 cx.bind_keys(vec![KeyBinding::new("shift", TestAction, None)]);
1075 });
1076 test_view.update_in(cx, |test_view, window, cx| {
1077 window.focus(&test_view.focus_handle, cx);
1078 });
1079
1080 cx.simulate_modifiers_change(Modifiers::alt());
1081 cx.simulate_modifiers_change(Modifiers::alt() | Modifiers::shift());
1082 cx.simulate_modifiers_change(Modifiers::shift());
1083 cx.simulate_modifiers_change(Modifiers::none());
1084 assert!(!test_view.read_with(cx, |test_view, _| test_view.saw_action));
1085
1086 cx.simulate_modifiers_change(Modifiers::shift());
1087 cx.simulate_modifiers_change(Modifiers::none());
1088 assert!(test_view.read_with(cx, |test_view, _| test_view.saw_action));
1089 }
1090}