Skip to main content

gpui_types/
input.rs

1//! Input event value types shared between GPUI and its platform backends.
2//!
3//! These are the passive event payloads that platform backends produce and
4//! that GPUI consumes. The `InputEvent` and `Sealed` implementations that
5//! convert them into `gpui::PlatformInput` stay in the `gpui` crate.
6
7use crate::{Keystroke, Modifiers, Pixels, Point};
8
9/// An enum representing the mouse button that was pressed.
10#[derive(Hash, Default, PartialEq, Eq, Copy, Clone, Debug)]
11pub enum MouseButton {
12    /// The left mouse button.
13    #[default]
14    Left,
15
16    /// The right mouse button.
17    Right,
18
19    /// The middle mouse button.
20    Middle,
21
22    /// A navigation button, such as back or forward.
23    Navigate(NavigationDirection),
24}
25
26impl MouseButton {
27    /// Get all the mouse buttons in a list.
28    pub fn all() -> Vec<Self> {
29        vec![
30            MouseButton::Left,
31            MouseButton::Right,
32            MouseButton::Middle,
33            MouseButton::Navigate(NavigationDirection::Back),
34            MouseButton::Navigate(NavigationDirection::Forward),
35        ]
36    }
37}
38
39/// A navigation direction, such as back or forward.
40#[derive(Hash, Default, PartialEq, Eq, Copy, Clone, Debug)]
41pub enum NavigationDirection {
42    /// The back button.
43    #[default]
44    Back,
45
46    /// The forward button.
47    Forward,
48}
49
50/// The key down event equivalent for the platform.
51#[derive(Clone, Debug, Eq, PartialEq)]
52pub struct KeyDownEvent {
53    /// The keystroke that was generated.
54    pub keystroke: Keystroke,
55
56    /// Whether the key is currently held down.
57    pub is_held: bool,
58
59    /// Whether to prefer character input over keybindings for this keystroke.
60    /// In some cases, like AltGr on Windows, modifiers are significant for character input.
61    pub prefer_character_input: bool,
62}
63
64/// The key up event equivalent for the platform.
65#[derive(Clone, Debug)]
66pub struct KeyUpEvent {
67    /// The keystroke that was released.
68    pub keystroke: Keystroke,
69}
70
71/// A mouse down event from the platform
72#[derive(Clone, Debug, Default)]
73pub struct MouseDownEvent {
74    /// Which mouse button was pressed.
75    pub button: MouseButton,
76
77    /// The position of the mouse on the window.
78    pub position: Point<Pixels>,
79
80    /// The modifiers that were held down when the mouse was pressed.
81    pub modifiers: Modifiers,
82
83    /// The number of times the button has been clicked.
84    pub click_count: usize,
85
86    /// Whether this is the first, focusing click.
87    pub first_mouse: bool,
88}
89
90impl MouseDownEvent {
91    /// Returns true if this mouse up event should focus the element.
92    pub fn is_focusing(&self) -> bool {
93        match self.button {
94            MouseButton::Left => true,
95            _ => false,
96        }
97    }
98}
99
100/// A mouse move event from the platform.
101#[derive(Clone, Debug, Default)]
102pub struct MouseMoveEvent {
103    /// The position of the mouse on the window.
104    pub position: Point<Pixels>,
105
106    /// The mouse button that was pressed, if any.
107    pub pressed_button: Option<MouseButton>,
108
109    /// The modifiers that were held down when the mouse was moved.
110    pub modifiers: Modifiers,
111}
112
113impl MouseMoveEvent {
114    /// Returns true if the left mouse button is currently held down.
115    pub fn dragging(&self) -> bool {
116        self.pressed_button == Some(MouseButton::Left)
117    }
118}