1use std::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}
146
147impl Deref for MouseButtonEvent {
148 type Target = Modifiers;
149
150 fn deref(&self) -> &Self::Target {
151 &self.modifiers
152 }
153}
154
155#[derive(Clone, Copy, Debug, Default)]
156pub struct MouseMovedEvent {
157 pub position: Vector2F,
158 pub pressed_button: Option<MouseButton>,
159 pub modifiers: Modifiers,
160}
161
162impl Deref for MouseMovedEvent {
163 type Target = Modifiers;
164
165 fn deref(&self) -> &Self::Target {
166 &self.modifiers
167 }
168}
169
170impl MouseMovedEvent {
171 pub fn to_button_event(&self, button: MouseButton) -> MouseButtonEvent {
172 MouseButtonEvent {
173 position: self.position,
174 button: self.pressed_button.unwrap_or(button),
175 modifiers: self.modifiers,
176 click_count: 0,
177 }
178 }
179}
180
181#[derive(Clone, Copy, Debug, Default)]
182pub struct MouseExitedEvent {
183 pub position: Vector2F,
184 pub pressed_button: Option<MouseButton>,
185 pub modifiers: Modifiers,
186}
187
188impl Deref for MouseExitedEvent {
189 type Target = Modifiers;
190
191 fn deref(&self) -> &Self::Target {
192 &self.modifiers
193 }
194}
195
196#[derive(Clone, Debug)]
197pub enum Event {
198 KeyDown(KeyDownEvent),
199 KeyUp(KeyUpEvent),
200 ModifiersChanged(ModifiersChangedEvent),
201 MouseDown(MouseButtonEvent),
202 MouseUp(MouseButtonEvent),
203 MouseMoved(MouseMovedEvent),
204 MouseExited(MouseExitedEvent),
205 ScrollWheel(ScrollWheelEvent),
206}
207
208impl Event {
209 pub fn position(&self) -> Option<Vector2F> {
210 match self {
211 Event::KeyDown { .. } => None,
212 Event::KeyUp { .. } => None,
213 Event::ModifiersChanged { .. } => None,
214 Event::MouseDown(event) | Event::MouseUp(event) => Some(event.position),
215 Event::MouseMoved(event) => Some(event.position),
216 Event::MouseExited(event) => Some(event.position),
217 Event::ScrollWheel(event) => Some(event.position),
218 }
219 }
220}