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
8pub trait InputEvent: Sealed + 'static {
10 fn to_platform_input(self) -> PlatformInput;
12}
13
14pub trait KeyEvent: InputEvent {}
16
17pub trait MouseEvent: InputEvent {}
19
20#[derive(Clone, Debug, Eq, PartialEq)]
22pub struct KeyDownEvent {
23 pub keystroke: Keystroke,
25
26 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#[derive(Clone, Debug)]
40pub struct KeyUpEvent {
41 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#[derive(Clone, Debug, Default)]
55pub struct ModifiersChangedEvent {
56 pub modifiers: Modifiers,
58 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#[derive(Clone, Copy, Debug, Default)]
81pub enum TouchPhase {
82 Started,
84 #[default]
86 Moved,
87 Ended,
89}
90
91#[derive(Clone, Debug, Default)]
93pub struct MouseDownEvent {
94 pub button: MouseButton,
96
97 pub position: Point<Pixels>,
99
100 pub modifiers: Modifiers,
102
103 pub click_count: usize,
105
106 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#[derive(Clone, Debug, Default)]
120pub struct MouseUpEvent {
121 pub button: MouseButton,
123
124 pub position: Point<Pixels>,
126
127 pub modifiers: Modifiers,
129
130 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#[derive(Clone, Debug, Default)]
144pub struct MouseClickEvent {
145 pub down: MouseDownEvent,
147
148 pub up: MouseUpEvent,
150}
151
152#[derive(Clone, Debug, Default)]
154pub struct KeyboardClickEvent {
155 pub button: KeyboardButton,
157
158 pub bounds: Bounds<Pixels>,
160}
161
162#[derive(Clone, Debug)]
164pub enum ClickEvent {
165 Mouse(MouseClickEvent),
167 Keyboard(KeyboardClickEvent),
169}
170
171impl Default for ClickEvent {
172 fn default() -> Self {
173 ClickEvent::Keyboard(KeyboardClickEvent::default())
174 }
175}
176
177impl ClickEvent {
178 pub fn modifiers(&self) -> Modifiers {
183 match self {
184 ClickEvent::Keyboard(_) => Modifiers::default(),
186 ClickEvent::Mouse(event) => event.up.modifiers,
190 }
191 }
192
193 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 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 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 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 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 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 pub fn is_keyboard(&self) -> bool {
265 match self {
266 ClickEvent::Mouse(_) => false,
267 ClickEvent::Keyboard(_) => true,
268 }
269 }
270}
271
272#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug, Default)]
274pub enum KeyboardButton {
275 #[default]
277 Enter,
278 Space,
280}
281
282#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug, Default)]
284pub enum MouseButton {
285 #[default]
287 Left,
288
289 Right,
291
292 Middle,
294
295 Navigate(NavigationDirection),
297}
298
299impl MouseButton {
300 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#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug, Default)]
314pub enum NavigationDirection {
315 #[default]
317 Back,
318
319 Forward,
321}
322
323#[derive(Clone, Debug, Default)]
325pub struct MouseMoveEvent {
326 pub position: Point<Pixels>,
328
329 pub pressed_button: Option<MouseButton>,
331
332 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 pub fn dragging(&self) -> bool {
347 self.pressed_button == Some(MouseButton::Left)
348 }
349}
350
351#[derive(Clone, Debug, Default)]
353pub struct ScrollWheelEvent {
354 pub position: Point<Pixels>,
356
357 pub delta: ScrollDelta,
359
360 pub modifiers: Modifiers,
362
363 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#[derive(Clone, Copy, Debug)]
385pub enum ScrollDelta {
386 Pixels(Point<Pixels>),
388 Lines(Point<f32>),
390}
391
392impl Default for ScrollDelta {
393 fn default() -> Self {
394 Self::Lines(Default::default())
395 }
396}
397
398impl ScrollDelta {
399 pub fn precise(&self) -> bool {
401 match self {
402 ScrollDelta::Pixels(_) => true,
403 ScrollDelta::Lines(_) => false,
404 }
405 }
406
407 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 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#[derive(Clone, Debug, Default)]
460pub struct MouseExitEvent {
461 pub position: Point<Pixels>,
463 pub pressed_button: Option<MouseButton>,
465 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#[derive(Debug, Clone, Default)]
487pub struct ExternalPaths(pub(crate) SmallVec<[PathBuf; 2]>);
488
489impl ExternalPaths {
490 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 Empty
500 }
501}
502
503#[derive(Debug, Clone)]
505pub enum FileDropEvent {
506 Entered {
508 position: Point<Pixels>,
510 paths: ExternalPaths,
512 },
513 Pending {
515 position: Point<Pixels>,
517 },
518 Submit {
520 position: Point<Pixels>,
522 },
523 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#[derive(Clone, Debug)]
537pub enum PlatformInput {
538 KeyDown(KeyDownEvent),
540 KeyUp(KeyUpEvent),
542 ModifiersChanged(ModifiersChangedEvent),
544 MouseDown(MouseDownEvent),
546 MouseUp(MouseUpEvent),
548 MouseMove(MouseMoveEvent),
550 MouseExited(MouseExitEvent),
552 ScrollWheel(ScrollWheelEvent),
554 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}