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
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)]
88pub enum TouchPhase {
89 Started,
91 #[default]
93 Moved,
94 Ended,
96}
97
98#[derive(Clone, Debug, Default)]
100pub struct MouseDownEvent {
101 pub button: MouseButton,
103
104 pub position: Point<Pixels>,
106
107 pub modifiers: Modifiers,
109
110 pub click_count: usize,
112
113 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 pub fn is_focusing(&self) -> bool {
128 match self.button {
129 MouseButton::Left => true,
130 _ => false,
131 }
132 }
133}
134
135#[derive(Clone, Debug, Default)]
137pub struct MouseUpEvent {
138 pub button: MouseButton,
140
141 pub position: Point<Pixels>,
143
144 pub modifiers: Modifiers,
146
147 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 pub fn is_focusing(&self) -> bool {
163 match self.button {
164 MouseButton::Left => true,
165 _ => false,
166 }
167 }
168}
169
170#[derive(Clone, Debug, Default)]
172pub struct MouseClickEvent {
173 pub down: MouseDownEvent,
175
176 pub up: MouseUpEvent,
178}
179
180#[derive(Clone, Copy, Debug, Default, PartialEq)]
182pub enum PressureStage {
183 #[default]
185 Zero,
186 Normal,
188 Force,
190}
191
192#[derive(Debug, Clone, Default)]
195pub struct MousePressureEvent {
196 pub pressure: f32,
198 pub stage: PressureStage,
200 pub position: Point<Pixels>,
202 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#[derive(Clone, Debug, Default)]
216pub struct KeyboardClickEvent {
217 pub button: KeyboardButton,
219
220 pub bounds: Bounds<Pixels>,
222}
223
224#[derive(Clone, Debug)]
226pub enum ClickEvent {
227 Mouse(MouseClickEvent),
229 Keyboard(KeyboardClickEvent),
231}
232
233impl Default for ClickEvent {
234 fn default() -> Self {
235 ClickEvent::Keyboard(KeyboardClickEvent::default())
236 }
237}
238
239impl ClickEvent {
240 pub fn modifiers(&self) -> Modifiers {
245 match self {
246 ClickEvent::Keyboard(_) => Modifiers::default(),
248 ClickEvent::Mouse(event) => event.up.modifiers,
252 }
253 }
254
255 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 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 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 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 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 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 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 pub fn is_keyboard(&self) -> bool {
340 match self {
341 ClickEvent::Mouse(_) => false,
342 ClickEvent::Keyboard(_) => true,
343 }
344 }
345}
346
347#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug, Default)]
349pub enum KeyboardButton {
350 #[default]
352 Enter,
353 Space,
355}
356
357#[derive(Hash, Default, PartialEq, Eq, Copy, Clone, Debug)]
359pub enum MouseButton {
360 #[default]
362 Left,
363
364 Right,
366
367 Middle,
369
370 Navigate(NavigationDirection),
372}
373
374impl MouseButton {
375 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#[derive(Hash, Default, PartialEq, Eq, Copy, Clone, Debug)]
389pub enum NavigationDirection {
390 #[default]
392 Back,
393
394 Forward,
396}
397
398#[derive(Clone, Debug, Default)]
400pub struct MouseMoveEvent {
401 pub position: Point<Pixels>,
403
404 pub pressed_button: Option<MouseButton>,
406
407 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 pub fn dragging(&self) -> bool {
422 self.pressed_button == Some(MouseButton::Left)
423 }
424}
425
426#[derive(Clone, Debug, Default)]
428pub struct ScrollWheelEvent {
429 pub position: Point<Pixels>,
431
432 pub delta: ScrollDelta,
434
435 pub modifiers: Modifiers,
437
438 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#[derive(Clone, Copy, Debug)]
460pub enum ScrollDelta {
461 Pixels(Point<Pixels>),
463 Lines(Point<f32>),
465}
466
467impl Default for ScrollDelta {
468 fn default() -> Self {
469 Self::Lines(Default::default())
470 }
471}
472
473#[derive(Clone, Debug, Default)]
477pub struct PinchEvent {
478 pub position: Point<Pixels>,
480
481 pub delta: f32,
485
486 pub modifiers: Modifiers,
488
489 pub phase: TouchPhase,
491}
492
493impl Sealed for PinchEvent {}
494impl InputEvent for PinchEvent {
495 fn to_platform_input(self) -> PlatformInput {
496 PlatformInput::Pinch(self)
497 }
498}
499impl GestureEvent for PinchEvent {}
500impl MouseEvent for PinchEvent {}
501
502impl Deref for PinchEvent {
503 type Target = Modifiers;
504
505 fn deref(&self) -> &Self::Target {
506 &self.modifiers
507 }
508}
509
510impl ScrollDelta {
511 pub fn precise(&self) -> bool {
513 match self {
514 ScrollDelta::Pixels(_) => true,
515 ScrollDelta::Lines(_) => false,
516 }
517 }
518
519 pub fn pixel_delta(&self, line_height: Pixels) -> Point<Pixels> {
521 match self {
522 ScrollDelta::Pixels(delta) => *delta,
523 ScrollDelta::Lines(delta) => point(line_height * delta.x, line_height * delta.y),
524 }
525 }
526
527 pub fn coalesce(self, other: ScrollDelta) -> ScrollDelta {
532 match (self, other) {
533 (ScrollDelta::Pixels(a), ScrollDelta::Pixels(b)) => {
534 let x = if a.x.signum() == b.x.signum() {
535 a.x + b.x
536 } else {
537 b.x
538 };
539
540 let y = if a.y.signum() == b.y.signum() {
541 a.y + b.y
542 } else {
543 b.y
544 };
545
546 ScrollDelta::Pixels(point(x, y))
547 }
548
549 (ScrollDelta::Lines(a), ScrollDelta::Lines(b)) => {
550 let x = if a.x.signum() == b.x.signum() {
551 a.x + b.x
552 } else {
553 b.x
554 };
555
556 let y = if a.y.signum() == b.y.signum() {
557 a.y + b.y
558 } else {
559 b.y
560 };
561
562 ScrollDelta::Lines(point(x, y))
563 }
564
565 _ => other,
566 }
567 }
568}
569
570#[derive(Clone, Debug, Default)]
572pub struct MouseExitEvent {
573 pub position: Point<Pixels>,
575 pub pressed_button: Option<MouseButton>,
577 pub modifiers: Modifiers,
579}
580
581impl Sealed for MouseExitEvent {}
582impl InputEvent for MouseExitEvent {
583 fn to_platform_input(self) -> PlatformInput {
584 PlatformInput::MouseExited(self)
585 }
586}
587
588impl MouseEvent for MouseExitEvent {}
589
590impl Deref for MouseExitEvent {
591 type Target = Modifiers;
592
593 fn deref(&self) -> &Self::Target {
594 &self.modifiers
595 }
596}
597
598#[derive(Debug, Clone, Default, Eq, PartialEq)]
600pub struct ExternalPaths(pub SmallVec<[PathBuf; 2]>);
601
602impl ExternalPaths {
603 pub fn paths(&self) -> &[PathBuf] {
605 &self.0
606 }
607}
608
609impl Render for ExternalPaths {
610 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
611 Empty
613 }
614}
615
616#[derive(Debug, Clone)]
618pub enum FileDropEvent {
619 Entered {
621 position: Point<Pixels>,
623 paths: ExternalPaths,
625 },
626 Pending {
628 position: Point<Pixels>,
630 },
631 Submit {
633 position: Point<Pixels>,
635 },
636 Exited,
638}
639
640impl Sealed for FileDropEvent {}
641impl InputEvent for FileDropEvent {
642 fn to_platform_input(self) -> PlatformInput {
643 PlatformInput::FileDrop(self)
644 }
645}
646impl MouseEvent for FileDropEvent {}
647
648#[derive(Clone, Debug)]
650pub enum PlatformInput {
651 KeyDown(KeyDownEvent),
653 KeyUp(KeyUpEvent),
655 ModifiersChanged(ModifiersChangedEvent),
657 MouseDown(MouseDownEvent),
659 MouseUp(MouseUpEvent),
661 MousePressure(MousePressureEvent),
663 MouseMove(MouseMoveEvent),
665 MouseExited(MouseExitEvent),
667 ScrollWheel(ScrollWheelEvent),
669 Pinch(PinchEvent),
671 FileDrop(FileDropEvent),
673}
674
675impl PlatformInput {
676 pub(crate) fn mouse_event(&self) -> Option<&dyn Any> {
677 match self {
678 PlatformInput::KeyDown { .. } => None,
679 PlatformInput::KeyUp { .. } => None,
680 PlatformInput::ModifiersChanged { .. } => None,
681 PlatformInput::MouseDown(event) => Some(event),
682 PlatformInput::MouseUp(event) => Some(event),
683 PlatformInput::MouseMove(event) => Some(event),
684 PlatformInput::MousePressure(event) => Some(event),
685 PlatformInput::MouseExited(event) => Some(event),
686 PlatformInput::ScrollWheel(event) => Some(event),
687 PlatformInput::Pinch(event) => Some(event),
688 PlatformInput::FileDrop(event) => Some(event),
689 }
690 }
691
692 pub(crate) fn keyboard_event(&self) -> Option<&dyn Any> {
693 match self {
694 PlatformInput::KeyDown(event) => Some(event),
695 PlatformInput::KeyUp(event) => Some(event),
696 PlatformInput::ModifiersChanged(event) => Some(event),
697 PlatformInput::MouseDown(_) => None,
698 PlatformInput::MouseUp(_) => None,
699 PlatformInput::MouseMove(_) => None,
700 PlatformInput::MousePressure(_) => None,
701 PlatformInput::MouseExited(_) => None,
702 PlatformInput::ScrollWheel(_) => None,
703 PlatformInput::Pinch(_) => None,
704 PlatformInput::FileDrop(_) => None,
705 }
706 }
707}
708
709#[cfg(test)]
710mod test {
711
712 use crate::{
713 self as gpui, AppContext as _, Context, FocusHandle, InteractiveElement, IntoElement,
714 KeyBinding, Keystroke, ParentElement, Render, TestAppContext, Window, div,
715 };
716
717 struct TestView {
718 saw_key_down: bool,
719 saw_action: bool,
720 focus_handle: FocusHandle,
721 }
722
723 actions!(test_only, [TestAction]);
724
725 impl Render for TestView {
726 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
727 div().id("testview").child(
728 div()
729 .key_context("parent")
730 .on_key_down(cx.listener(|this, _, _, cx| {
731 cx.stop_propagation();
732 this.saw_key_down = true
733 }))
734 .on_action(cx.listener(|this: &mut TestView, _: &TestAction, _, _| {
735 this.saw_action = true
736 }))
737 .child(
738 div()
739 .key_context("nested")
740 .track_focus(&self.focus_handle)
741 .into_element(),
742 ),
743 )
744 }
745 }
746
747 #[gpui::test]
748 fn test_on_events(cx: &mut TestAppContext) {
749 let window = cx.update(|cx| {
750 cx.open_window(Default::default(), |_, cx| {
751 cx.new(|cx| TestView {
752 saw_key_down: false,
753 saw_action: false,
754 focus_handle: cx.focus_handle(),
755 })
756 })
757 .unwrap()
758 });
759
760 cx.update(|cx| {
761 cx.bind_keys(vec![KeyBinding::new("ctrl-g", TestAction, Some("parent"))]);
762 });
763
764 window
765 .update(cx, |test_view, window, cx| {
766 window.focus(&test_view.focus_handle, cx)
767 })
768 .unwrap();
769
770 cx.dispatch_keystroke(*window, Keystroke::parse("a").unwrap());
771 cx.dispatch_keystroke(*window, Keystroke::parse("ctrl-g").unwrap());
772
773 window
774 .update(cx, |test_view, _, _| {
775 assert!(test_view.saw_key_down || test_view.saw_action);
776 assert!(test_view.saw_key_down);
777 assert!(test_view.saw_action);
778 })
779 .unwrap();
780 }
781}