event.rs

  1use std::{any::Any, ops::Deref};
  2
  3use pathfinder_geometry::vector::vec2f;
  4
  5use crate::{geometry::vector::Vector2F, keymap_matcher::Keystroke};
  6
  7#[derive(Clone, Debug, Eq, PartialEq)]
  8pub struct KeyDownEvent {
  9    pub keystroke: Keystroke,
 10    pub is_held: bool,
 11}
 12
 13#[derive(Clone, Debug)]
 14pub struct KeyUpEvent {
 15    pub keystroke: Keystroke,
 16}
 17
 18#[derive(Clone, Copy, Debug, Default, PartialEq)]
 19pub struct Modifiers {
 20    pub ctrl: bool,
 21    pub alt: bool,
 22    pub shift: bool,
 23    pub cmd: bool,
 24    pub fun: bool,
 25}
 26
 27#[derive(Clone, Copy, Debug, Default)]
 28pub struct ModifiersChangedEvent {
 29    pub modifiers: Modifiers,
 30}
 31
 32impl Deref for ModifiersChangedEvent {
 33    type Target = Modifiers;
 34
 35    fn deref(&self) -> &Self::Target {
 36        &self.modifiers
 37    }
 38}
 39
 40/// The phase of a touch motion event.
 41/// Based on the winit enum of the same name,
 42#[derive(Clone, Copy, Debug)]
 43pub enum TouchPhase {
 44    Started,
 45    Moved,
 46    Ended,
 47}
 48
 49#[derive(Clone, Copy, Debug)]
 50pub enum ScrollDelta {
 51    Pixels(Vector2F),
 52    Lines(Vector2F),
 53}
 54
 55impl Default for ScrollDelta {
 56    fn default() -> Self {
 57        Self::Lines(Default::default())
 58    }
 59}
 60
 61impl ScrollDelta {
 62    pub fn raw(&self) -> &Vector2F {
 63        match self {
 64            ScrollDelta::Pixels(v) => v,
 65            ScrollDelta::Lines(v) => v,
 66        }
 67    }
 68
 69    pub fn precise(&self) -> bool {
 70        match self {
 71            ScrollDelta::Pixels(_) => true,
 72            ScrollDelta::Lines(_) => false,
 73        }
 74    }
 75
 76    pub fn pixel_delta(&self, line_height: f32) -> Vector2F {
 77        match self {
 78            ScrollDelta::Pixels(delta) => *delta,
 79            ScrollDelta::Lines(delta) => vec2f(delta.x() * line_height, delta.y() * line_height),
 80        }
 81    }
 82}
 83
 84#[derive(Clone, Copy, Debug, Default)]
 85pub struct ScrollWheelEvent {
 86    pub position: Vector2F,
 87    pub delta: ScrollDelta,
 88    pub modifiers: Modifiers,
 89    /// If the platform supports returning the phase of a scroll wheel event, it will be stored here
 90    pub phase: Option<TouchPhase>,
 91}
 92
 93impl Deref for ScrollWheelEvent {
 94    type Target = Modifiers;
 95
 96    fn deref(&self) -> &Self::Target {
 97        &self.modifiers
 98    }
 99}
100
101#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
102pub enum NavigationDirection {
103    Back,
104    Forward,
105}
106
107impl Default for NavigationDirection {
108    fn default() -> Self {
109        Self::Back
110    }
111}
112
113#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
114pub enum MouseButton {
115    Left,
116    Right,
117    Middle,
118    Navigate(NavigationDirection),
119}
120
121impl MouseButton {
122    pub fn all() -> Vec<Self> {
123        vec![
124            MouseButton::Left,
125            MouseButton::Right,
126            MouseButton::Middle,
127            MouseButton::Navigate(NavigationDirection::Back),
128            MouseButton::Navigate(NavigationDirection::Forward),
129        ]
130    }
131}
132
133impl Default for MouseButton {
134    fn default() -> Self {
135        Self::Left
136    }
137}
138
139#[derive(Clone, Copy, Debug, Default)]
140pub struct MouseButtonEvent {
141    pub button: MouseButton,
142    pub position: Vector2F,
143    pub modifiers: Modifiers,
144    pub click_count: usize,
145    pub is_down: bool,
146}
147
148impl Deref for MouseButtonEvent {
149    type Target = Modifiers;
150
151    fn deref(&self) -> &Self::Target {
152        &self.modifiers
153    }
154}
155
156#[derive(Clone, Copy, Debug, Default)]
157pub struct MouseMovedEvent {
158    pub position: Vector2F,
159    pub pressed_button: Option<MouseButton>,
160    pub modifiers: Modifiers,
161}
162
163impl Deref for MouseMovedEvent {
164    type Target = Modifiers;
165
166    fn deref(&self) -> &Self::Target {
167        &self.modifiers
168    }
169}
170
171impl MouseMovedEvent {
172    pub fn to_button_event(&self, button: MouseButton) -> MouseButtonEvent {
173        MouseButtonEvent {
174            position: self.position,
175            button: self.pressed_button.unwrap_or(button),
176            modifiers: self.modifiers,
177            click_count: 0,
178            is_down: self.pressed_button.is_some(),
179        }
180    }
181}
182
183#[derive(Clone, Copy, Debug, Default)]
184pub struct MouseExitedEvent {
185    pub position: Vector2F,
186    pub pressed_button: Option<MouseButton>,
187    pub modifiers: Modifiers,
188}
189
190impl Deref for MouseExitedEvent {
191    type Target = Modifiers;
192
193    fn deref(&self) -> &Self::Target {
194        &self.modifiers
195    }
196}
197
198#[derive(Clone, Debug)]
199pub enum Event {
200    KeyDown(KeyDownEvent),
201    KeyUp(KeyUpEvent),
202    ModifiersChanged(ModifiersChangedEvent),
203    MouseDown(MouseButtonEvent),
204    MouseUp(MouseButtonEvent),
205    MouseMoved(MouseMovedEvent),
206    MouseExited(MouseExitedEvent),
207    ScrollWheel(ScrollWheelEvent),
208}
209
210impl Event {
211    pub fn position(&self) -> Option<Vector2F> {
212        match self {
213            Event::KeyDown { .. } => None,
214            Event::KeyUp { .. } => None,
215            Event::ModifiersChanged { .. } => None,
216            Event::MouseDown(event) => Some(event.position),
217            Event::MouseUp(event) => Some(event.position),
218            Event::MouseMoved(event) => Some(event.position),
219            Event::MouseExited(event) => Some(event.position),
220            Event::ScrollWheel(event) => Some(event.position),
221        }
222    }
223
224    pub fn mouse_event<'a>(&'a self) -> Option<&'a dyn Any> {
225        match self {
226            Event::KeyDown { .. } => None,
227            Event::KeyUp { .. } => None,
228            Event::ModifiersChanged { .. } => None,
229            Event::MouseDown(event) => Some(event),
230            Event::MouseUp(event) => Some(event),
231            Event::MouseMoved(event) => Some(event),
232            Event::MouseExited(event) => Some(event),
233            Event::ScrollWheel(event) => Some(event),
234        }
235    }
236}