1use std::{ops::Range, path::PathBuf};
2
3use crate::{
4 AsyncWakeToken, DragEvent, Point, SafeAreaInsets, SemanticsActionRequest, Size, TimerToken,
5 Vector, WidgetId,
6};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub struct Modifiers {
10 pub shift: bool,
11 pub control: bool,
12 pub alt: bool,
13 pub meta: bool,
14}
15
16impl Modifiers {
17 pub const NONE: Self = Self {
18 shift: false,
19 control: false,
20 alt: false,
21 meta: false,
22 };
23
24 pub const fn any(self) -> bool {
25 self.shift || self.control || self.alt || self.meta
26 }
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum PointerButton {
31 Primary,
32 Secondary,
33 Middle,
34 Back,
35 Forward,
36 Other(u16),
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
40pub struct PointerButtons(u8);
41
42impl PointerButtons {
43 const PRIMARY: u8 = 1 << 0;
44 const SECONDARY: u8 = 1 << 1;
45 const MIDDLE: u8 = 1 << 2;
46 const BACK: u8 = 1 << 3;
47 const FORWARD: u8 = 1 << 4;
48
49 pub const NONE: Self = Self(0);
50
51 pub const fn new(bits: u8) -> Self {
52 Self(bits)
53 }
54
55 pub const fn bits(self) -> u8 {
56 self.0
57 }
58
59 pub const fn is_empty(self) -> bool {
60 self.0 == 0
61 }
62
63 pub const fn contains(self, button: PointerButton) -> bool {
64 match Self::mask(button) {
65 Some(mask) => self.0 & mask != 0,
66 None => false,
67 }
68 }
69
70 pub fn insert(&mut self, button: PointerButton) {
71 if let Some(mask) = Self::mask(button) {
72 self.0 |= mask;
73 }
74 }
75
76 const fn mask(button: PointerButton) -> Option<u8> {
77 match button {
78 PointerButton::Primary => Some(Self::PRIMARY),
79 PointerButton::Secondary => Some(Self::SECONDARY),
80 PointerButton::Middle => Some(Self::MIDDLE),
81 PointerButton::Back => Some(Self::BACK),
82 PointerButton::Forward => Some(Self::FORWARD),
83 PointerButton::Other(_) => None,
84 }
85 }
86}
87
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
89pub enum PointerKind {
90 #[default]
91 Mouse,
92 Touch,
93 Pen,
94 Unknown,
95}
96
97#[derive(Debug, Clone, Copy, PartialEq, Eq)]
98pub enum PointerEventKind {
99 Down,
100 Up,
101 Move,
102 Scroll,
103 Enter,
104 Leave,
105 Cancel,
106}
107
108#[derive(Debug, Clone, Copy, PartialEq)]
109pub enum ScrollDelta {
110 Lines(Vector),
111 Pixels(Vector),
112}
113
114#[derive(Debug, Clone, PartialEq)]
115pub struct PointerEvent {
116 pub pointer_id: u64,
117 pub kind: PointerEventKind,
118 pub position: Point,
119 pub delta: Vector,
120 pub scroll_delta: Option<ScrollDelta>,
121 pub button: Option<PointerButton>,
122 pub buttons: PointerButtons,
123 pub modifiers: Modifiers,
124 pub pointer_kind: PointerKind,
125 pub is_primary: bool,
126}
127
128impl PointerEvent {
129 pub fn new(kind: PointerEventKind, position: Point) -> Self {
130 Self {
131 pointer_id: 0,
132 kind,
133 position,
134 delta: Vector::ZERO,
135 scroll_delta: None,
136 button: None,
137 buttons: PointerButtons::NONE,
138 modifiers: Modifiers::NONE,
139 pointer_kind: PointerKind::Mouse,
140 is_primary: true,
141 }
142 }
143}
144
145#[derive(Debug, Clone, Copy, PartialEq)]
146pub enum WakeEvent {
147 Timer {
148 token: TimerToken,
149 time: f64,
150 deadline: f64,
151 },
152 Async {
153 token: AsyncWakeToken,
154 time: f64,
155 },
156 AnimationFrame {
157 time: f64,
158 delta: f64,
159 frame_index: u64,
160 },
161}
162
163#[derive(Debug, Clone, Copy, PartialEq, Eq)]
164pub enum KeyState {
165 Pressed,
166 Released,
167}
168
169#[derive(Debug, Clone, PartialEq)]
170pub struct KeyboardEvent {
171 pub key: String,
172 pub code: String,
173 pub text: Option<String>,
174 pub state: KeyState,
175 pub modifiers: Modifiers,
176 pub repeat: bool,
177 pub is_composing: bool,
178}
179
180impl KeyboardEvent {
181 pub fn new(key: impl Into<String>, state: KeyState) -> Self {
182 let key = key.into();
183
184 Self {
185 code: key.clone(),
186 text: keyboard_text_for_key(&key, state),
187 key,
188 state,
189 modifiers: Modifiers::NONE,
190 repeat: false,
191 is_composing: false,
192 }
193 }
194}
195
196fn keyboard_text_for_key(key: &str, state: KeyState) -> Option<String> {
197 if state != KeyState::Pressed {
198 return None;
199 }
200
201 if key.is_empty() || key.chars().any(char::is_control) {
202 return None;
203 }
204
205 if key.chars().count() == 1 {
206 Some(key.to_string())
207 } else {
208 None
209 }
210}
211
212#[derive(Debug, Clone, PartialEq)]
213pub enum ImeEvent {
214 CompositionStart,
215 CompositionUpdate {
216 text: String,
217 cursor_range: Option<Range<usize>>,
218 },
219 CompositionCommit {
220 text: String,
221 },
222 CompositionEnd,
223}
224
225#[derive(Debug, Clone, PartialEq)]
226pub enum WindowEvent {
227 CloseRequested,
230 Resized(Size),
233 ScaleFactorChanged {
234 scale_factor: f64,
235 raw_dpi: Option<f32>,
236 suggested_size: Option<Size>,
237 },
238 SafeAreaChanged(SafeAreaInsets),
241 Focused(bool),
242 Occluded(bool),
243 ExternalFileHovered(PathBuf),
247 ExternalFileHoverCancelled,
250 ExternalFileDropped(PathBuf),
254 RedrawRequested,
255}
256
257#[derive(Debug, Clone, PartialEq)]
258pub struct CustomEvent {
259 pub kind: String,
260 pub payload: Option<String>,
261}
262
263#[derive(Debug, Clone, PartialEq)]
270pub struct SemanticsEvent {
271 pub target: WidgetId,
272 pub action: SemanticsActionRequest,
273}
274
275impl SemanticsEvent {
276 pub fn new(target: WidgetId, action: SemanticsActionRequest) -> Self {
277 Self { target, action }
278 }
279}
280
281impl CustomEvent {
282 pub fn new(kind: impl Into<String>) -> Self {
283 Self {
284 kind: kind.into(),
285 payload: None,
286 }
287 }
288}
289
290#[derive(Debug, Clone, PartialEq)]
291pub enum Event {
292 Pointer(PointerEvent),
293 Drag(DragEvent),
294 Keyboard(KeyboardEvent),
295 Ime(ImeEvent),
296 Semantics(SemanticsEvent),
297 Wake(WakeEvent),
298 Window(WindowEvent),
301 Custom(CustomEvent),
302}
303
304#[cfg(test)]
305mod tests {
306 use std::path::PathBuf;
307
308 use super::{WakeEvent, WindowEvent};
309
310 #[test]
311 fn wake_event_animation_frame_carries_time_and_delta() {
312 let event = WakeEvent::AnimationFrame {
313 time: 42.5,
314 delta: 1.0 / 120.0,
315 frame_index: 7,
316 };
317
318 assert!(matches!(
319 event,
320 WakeEvent::AnimationFrame {
321 time,
322 delta,
323 frame_index
324 } if time == 42.5 && delta == 1.0 / 120.0 && frame_index == 7
325 ));
326 }
327
328 #[test]
329 fn external_file_window_events_preserve_paths_and_cancellation() {
330 let path = PathBuf::from(r"C:\workspace\candidate.json");
331
332 let hovered = WindowEvent::ExternalFileHovered(path.clone());
333 let dropped = WindowEvent::ExternalFileDropped(path.clone());
334
335 assert!(matches!(
336 hovered,
337 WindowEvent::ExternalFileHovered(event_path) if event_path == path
338 ));
339 assert!(matches!(
340 dropped,
341 WindowEvent::ExternalFileDropped(event_path) if event_path == path
342 ));
343 assert!(matches!(
344 WindowEvent::ExternalFileHoverCancelled,
345 WindowEvent::ExternalFileHoverCancelled
346 ));
347 }
348}