floem_winit/event.rs
1//! The [`Event`] enum and assorted supporting types.
2//!
3//! These are sent to the closure given to [`EventLoop::run(...)`], where they get
4//! processed and used to modify the program state. For more details, see the root-level documentation.
5//!
6//! Some of these events represent different "parts" of a traditional event-handling loop. You could
7//! approximate the basic ordering loop of [`EventLoop::run(...)`] like this:
8//!
9//! ```rust,ignore
10//! let mut start_cause = StartCause::Init;
11//!
12//! while !elwt.exiting() {
13//! event_handler(NewEvents(start_cause), elwt);
14//!
15//! for e in (window events, user events, device events) {
16//! event_handler(e, elwt);
17//! }
18//!
19//! for w in (redraw windows) {
20//! event_handler(RedrawRequested(w), elwt);
21//! }
22//!
23//! event_handler(AboutToWait, elwt);
24//! start_cause = wait_if_necessary();
25//! }
26//!
27//! event_handler(LoopExiting, elwt);
28//! ```
29//!
30//! This leaves out timing details like [`ControlFlow::WaitUntil`] but hopefully
31//! describes what happens in what order.
32//!
33//! [`EventLoop::run(...)`]: crate::event_loop::EventLoop::run
34//! [`ControlFlow::WaitUntil`]: crate::event_loop::ControlFlow::WaitUntil
35use std::path::PathBuf;
36use std::sync::{Mutex, Weak};
37#[cfg(not(wasm_platform))]
38use std::time::Instant;
39
40use smol_str::SmolStr;
41#[cfg(wasm_platform)]
42use web_time::Instant;
43
44use crate::error::ExternalError;
45#[cfg(doc)]
46use crate::window::Window;
47use crate::{
48 dpi::{PhysicalPosition, PhysicalSize},
49 event_loop::AsyncRequestSerial,
50 keyboard::{self, ModifiersKeyState, ModifiersKeys, ModifiersState},
51 platform_impl,
52 window::{ActivationToken, Theme, WindowId},
53};
54
55/// Describes a generic event.
56///
57/// See the module-level docs for more information on the event loop manages each event.
58#[derive(Debug, Clone, PartialEq)]
59pub enum Event<T: 'static> {
60 /// Emitted when new events arrive from the OS to be processed.
61 ///
62 /// This event type is useful as a place to put code that should be done before you start
63 /// processing events, such as updating frame timing information for benchmarking or checking
64 /// the [`StartCause`] to see if a timer set by
65 /// [`ControlFlow::WaitUntil`](crate::event_loop::ControlFlow::WaitUntil) has elapsed.
66 NewEvents(StartCause),
67
68 /// Emitted when the OS sends an event to a winit window.
69 WindowEvent {
70 window_id: WindowId,
71 event: WindowEvent,
72 },
73
74 /// Emitted when the OS sends an event to a device.
75 DeviceEvent {
76 device_id: DeviceId,
77 event: DeviceEvent,
78 },
79
80 /// Emitted when an event is sent from [`EventLoopProxy::send_event`](crate::event_loop::EventLoopProxy::send_event)
81 UserEvent(T),
82
83 /// Emitted when the application has been suspended.
84 ///
85 /// # Portability
86 ///
87 /// Not all platforms support the notion of suspending applications, and there may be no
88 /// technical way to guarantee being able to emit a `Suspended` event if the OS has
89 /// no formal application lifecycle (currently only Android, iOS, and Web do). For this reason,
90 /// Winit does not currently try to emit pseudo `Suspended` events before the application
91 /// quits on platforms without an application lifecycle.
92 ///
93 /// Considering that the implementation of `Suspended` and [`Resumed`] events may be internally
94 /// driven by multiple platform-specific events, and that there may be subtle differences across
95 /// platforms with how these internal events are delivered, it's recommended that applications
96 /// be able to gracefully handle redundant (i.e. back-to-back) `Suspended` or [`Resumed`] events.
97 ///
98 /// Also see [`Resumed`] notes.
99 ///
100 /// ## Android
101 ///
102 /// On Android, the `Suspended` event is only sent when the application's associated
103 /// [`SurfaceView`] is destroyed. This is expected to closely correlate with the [`onPause`]
104 /// lifecycle event but there may technically be a discrepancy.
105 ///
106 /// [`onPause`]: https://developer.android.com/reference/android/app/Activity#onPause()
107 ///
108 /// Applications that need to run on Android should assume their [`SurfaceView`] has been
109 /// destroyed, which indirectly invalidates any existing render surfaces that may have been
110 /// created outside of Winit (such as an `EGLSurface`, [`VkSurfaceKHR`] or [`wgpu::Surface`]).
111 ///
112 /// After being `Suspended` on Android applications must drop all render surfaces before
113 /// the event callback completes, which may be re-created when the application is next [`Resumed`].
114 ///
115 /// [`SurfaceView`]: https://developer.android.com/reference/android/view/SurfaceView
116 /// [Activity lifecycle]: https://developer.android.com/guide/components/activities/activity-lifecycle
117 /// [`VkSurfaceKHR`]: https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VkSurfaceKHR.html
118 /// [`wgpu::Surface`]: https://docs.rs/wgpu/latest/wgpu/struct.Surface.html
119 ///
120 /// ## iOS
121 ///
122 /// On iOS, the `Suspended` event is currently emitted in response to an
123 /// [`applicationWillResignActive`] callback which means that the application is
124 /// about to transition from the active to inactive state (according to the
125 /// [iOS application lifecycle]).
126 ///
127 /// [`applicationWillResignActive`]: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622950-applicationwillresignactive
128 /// [iOS application lifecycle]: https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle
129 ///
130 /// ## Web
131 ///
132 /// On Web, the `Suspended` event is emitted in response to a [`pagehide`] event
133 /// with the property [`persisted`] being true, which means that the page is being
134 /// put in the [`bfcache`] (back/forward cache) - an in-memory cache that stores a
135 /// complete snapshot of a page (including the JavaScript heap) as the user is
136 /// navigating away.
137 ///
138 /// [`pagehide`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/pagehide_event
139 /// [`persisted`]: https://developer.mozilla.org/en-US/docs/Web/API/PageTransitionEvent/persisted
140 /// [`bfcache`]: https://web.dev/bfcache/
141 ///
142 /// [`Resumed`]: Self::Resumed
143 Suspended,
144
145 /// Emitted when the application has been resumed.
146 ///
147 /// For consistency, all platforms emit a `Resumed` event even if they don't themselves have a
148 /// formal suspend/resume lifecycle. For systems without a standard suspend/resume lifecycle
149 /// the `Resumed` event is always emitted after the [`NewEvents(StartCause::Init)`][StartCause::Init]
150 /// event.
151 ///
152 /// # Portability
153 ///
154 /// It's recommended that applications should only initialize their graphics context and create
155 /// a window after they have received their first `Resumed` event. Some systems
156 /// (specifically Android) won't allow applications to create a render surface until they are
157 /// resumed.
158 ///
159 /// Considering that the implementation of [`Suspended`] and `Resumed` events may be internally
160 /// driven by multiple platform-specific events, and that there may be subtle differences across
161 /// platforms with how these internal events are delivered, it's recommended that applications
162 /// be able to gracefully handle redundant (i.e. back-to-back) [`Suspended`] or `Resumed` events.
163 ///
164 /// Also see [`Suspended`] notes.
165 ///
166 /// ## Android
167 ///
168 /// On Android, the `Resumed` event is sent when a new [`SurfaceView`] has been created. This is
169 /// expected to closely correlate with the [`onResume`] lifecycle event but there may technically
170 /// be a discrepancy.
171 ///
172 /// [`onResume`]: https://developer.android.com/reference/android/app/Activity#onResume()
173 ///
174 /// Applications that need to run on Android must wait until they have been `Resumed`
175 /// before they will be able to create a render surface (such as an `EGLSurface`,
176 /// [`VkSurfaceKHR`] or [`wgpu::Surface`]) which depend on having a
177 /// [`SurfaceView`]. Applications must also assume that if they are [`Suspended`], then their
178 /// render surfaces are invalid and should be dropped.
179 ///
180 /// Also see [`Suspended`] notes.
181 ///
182 /// [`SurfaceView`]: https://developer.android.com/reference/android/view/SurfaceView
183 /// [Activity lifecycle]: https://developer.android.com/guide/components/activities/activity-lifecycle
184 /// [`VkSurfaceKHR`]: https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VkSurfaceKHR.html
185 /// [`wgpu::Surface`]: https://docs.rs/wgpu/latest/wgpu/struct.Surface.html
186 ///
187 /// ## iOS
188 ///
189 /// On iOS, the `Resumed` event is emitted in response to an [`applicationDidBecomeActive`]
190 /// callback which means the application is "active" (according to the
191 /// [iOS application lifecycle]).
192 ///
193 /// [`applicationDidBecomeActive`]: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622956-applicationdidbecomeactive
194 /// [iOS application lifecycle]: https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle
195 ///
196 /// ## Web
197 ///
198 /// On Web, the `Resumed` event is emitted in response to a [`pageshow`] event
199 /// with the property [`persisted`] being true, which means that the page is being
200 /// restored from the [`bfcache`] (back/forward cache) - an in-memory cache that
201 /// stores a complete snapshot of a page (including the JavaScript heap) as the
202 /// user is navigating away.
203 ///
204 /// [`pageshow`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/pageshow_event
205 /// [`persisted`]: https://developer.mozilla.org/en-US/docs/Web/API/PageTransitionEvent/persisted
206 /// [`bfcache`]: https://web.dev/bfcache/
207 ///
208 /// [`Suspended`]: Self::Suspended
209 Resumed,
210
211 /// Emitted when the event loop is about to block and wait for new events.
212 ///
213 /// Most applications shouldn't need to hook into this event since there is no real relationship
214 /// between how often the event loop needs to wake up and the dispatching of any specific events.
215 ///
216 /// High frequency event sources, such as input devices could potentially lead to lots of wake
217 /// ups and also lots of corresponding `AboutToWait` events.
218 ///
219 /// This is not an ideal event to drive application rendering from and instead applications
220 /// should render in response to [`WindowEvent::RedrawRequested`] events.
221 AboutToWait,
222
223 /// Emitted when the event loop is being shut down.
224 ///
225 /// This is irreversible - if this event is emitted, it is guaranteed to be the last event that
226 /// gets emitted. You generally want to treat this as a "do on quit" event.
227 LoopExiting,
228
229 /// Emitted when the application has received a memory warning.
230 ///
231 /// ## Platform-specific
232 ///
233 /// ### Android
234 ///
235 /// On Android, the `MemoryWarning` event is sent when [`onLowMemory`] was called. The application
236 /// must [release memory] or risk being killed.
237 ///
238 /// [`onLowMemory`]: https://developer.android.com/reference/android/app/Application.html#onLowMemory()
239 /// [release memory]: https://developer.android.com/topic/performance/memory#release
240 ///
241 /// ### iOS
242 ///
243 /// On iOS, the `MemoryWarning` event is emitted in response to an [`applicationDidReceiveMemoryWarning`]
244 /// callback. The application must free as much memory as possible or risk being terminated, see
245 /// [how to respond to memory warnings].
246 ///
247 /// [`applicationDidReceiveMemoryWarning`]: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623063-applicationdidreceivememorywarni
248 /// [how to respond to memory warnings]: https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle/responding_to_memory_warnings
249 ///
250 /// ### Others
251 ///
252 /// - **macOS / Wayland / Windows / Orbital:** Unsupported.
253 MemoryWarning,
254
255 /// Emitted when the application has received a reopen request from OS.
256 ///
257 /// ## Platform-specific
258 ///
259 /// ### macOS
260 ///
261 /// On macOS, the `Reopen` event is emitted in response to an [`applicationShouldHandleReopen`]
262 /// callback, which is usually called whenever the Finder reactivates an already running
263 /// application because the user double-clicked it again or used the dock to activate it.
264 /// Usually, the user would expect you to create a new window if there isn't any.
265 ///
266 /// [`applicationShouldHandleReopen`]: https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428638-applicationshouldhandlereopen
267 ///
268 /// ### Others
269 ///
270 /// - **Android / iOS / Web / Wayland / Windows / Orbital:** Unsupported.
271 Reopen,
272}
273
274impl<T> Event<T> {
275 #[allow(clippy::result_large_err)]
276 pub fn map_nonuser_event<U>(self) -> Result<Event<U>, Event<T>> {
277 use self::Event::*;
278 match self {
279 UserEvent(_) => Err(self),
280 WindowEvent { window_id, event } => Ok(WindowEvent { window_id, event }),
281 DeviceEvent { device_id, event } => Ok(DeviceEvent { device_id, event }),
282 NewEvents(cause) => Ok(NewEvents(cause)),
283 AboutToWait => Ok(AboutToWait),
284 LoopExiting => Ok(LoopExiting),
285 Suspended => Ok(Suspended),
286 Resumed => Ok(Resumed),
287 MemoryWarning => Ok(MemoryWarning),
288 Reopen => Ok(Reopen),
289 }
290 }
291}
292
293/// Describes the reason the event loop is resuming.
294#[derive(Debug, Clone, Copy, PartialEq, Eq)]
295pub enum StartCause {
296 /// Sent if the time specified by [`ControlFlow::WaitUntil`] has been reached. Contains the
297 /// moment the timeout was requested and the requested resume time. The actual resume time is
298 /// guaranteed to be equal to or after the requested resume time.
299 ///
300 /// [`ControlFlow::WaitUntil`]: crate::event_loop::ControlFlow::WaitUntil
301 ResumeTimeReached {
302 start: Instant,
303 requested_resume: Instant,
304 },
305
306 /// Sent if the OS has new events to send to the window, after a wait was requested. Contains
307 /// the moment the wait was requested and the resume time, if requested.
308 WaitCancelled {
309 start: Instant,
310 requested_resume: Option<Instant>,
311 },
312
313 /// Sent if the event loop is being resumed after the loop's control flow was set to
314 /// [`ControlFlow::Poll`].
315 ///
316 /// [`ControlFlow::Poll`]: crate::event_loop::ControlFlow::Poll
317 Poll,
318
319 /// Sent once, immediately after `run` is called. Indicates that the loop was just initialized.
320 Init,
321}
322
323/// Describes an event from a [`Window`].
324#[derive(Debug, Clone, PartialEq)]
325pub enum WindowEvent {
326 /// The activation token was delivered back and now could be used.
327 ///
328 #[cfg_attr(
329 not(any(x11_platform, wayland_platfrom)),
330 allow(rustdoc::broken_intra_doc_links)
331 )]
332 /// Delivered in response to [`request_activation_token`].
333 ///
334 /// [`request_activation_token`]: crate::platform::startup_notify::WindowExtStartupNotify::request_activation_token
335 ActivationTokenDone {
336 serial: AsyncRequestSerial,
337 token: ActivationToken,
338 },
339
340 /// The size of the window has changed. Contains the client area's new dimensions.
341 Resized(PhysicalSize<u32>),
342
343 /// The position of the window has changed. Contains the window's new position.
344 ///
345 /// ## Platform-specific
346 ///
347 /// - **iOS / Android / Web / Wayland:** Unsupported.
348 Moved(PhysicalPosition<i32>),
349
350 /// The window has been requested to close.
351 CloseRequested,
352
353 /// The window has been destroyed.
354 Destroyed,
355
356 /// A file has been dropped into the window.
357 ///
358 /// When the user drops multiple files at once, this event will be emitted for each file
359 /// separately.
360 DroppedFile(PathBuf),
361
362 /// A file is being hovered over the window.
363 ///
364 /// When the user hovers multiple files at once, this event will be emitted for each file
365 /// separately.
366 HoveredFile(PathBuf),
367
368 /// A file was hovered, but has exited the window.
369 ///
370 /// There will be a single `HoveredFileCancelled` event triggered even if multiple files were
371 /// hovered.
372 HoveredFileCancelled,
373
374 /// The window gained or lost focus.
375 ///
376 /// The parameter is true if the window has gained focus, and false if it has lost focus.
377 Focused(bool),
378
379 /// An event from the keyboard has been received.
380 ///
381 /// ## Platform-specific
382 /// - **Windows:** The shift key overrides NumLock. In other words, while shift is held down,
383 /// numpad keys act as if NumLock wasn't active. When this is used, the OS sends fake key
384 /// events which are not marked as `is_synthetic`.
385 KeyboardInput {
386 device_id: DeviceId,
387 event: KeyEvent,
388
389 /// If `true`, the event was generated synthetically by winit
390 /// in one of the following circumstances:
391 ///
392 /// * Synthetic key press events are generated for all keys pressed
393 /// when a window gains focus. Likewise, synthetic key release events
394 /// are generated for all keys pressed when a window goes out of focus.
395 /// ***Currently, this is only functional on X11 and Windows***
396 ///
397 /// Otherwise, this value is always `false`.
398 is_synthetic: bool,
399 },
400
401 /// The keyboard modifiers have changed.
402 ModifiersChanged(Modifiers),
403
404 /// An event from an input method.
405 ///
406 /// **Note:** You have to explicitly enable this event using [`Window::set_ime_allowed`].
407 ///
408 /// ## Platform-specific
409 ///
410 /// - **iOS / Android / Web / Orbital:** Unsupported.
411 Ime(Ime),
412
413 /// The cursor has moved on the window.
414 ///
415 /// ## Platform-specific
416 ///
417 /// - **Web:** Doesn't take into account CSS [`border`], [`padding`], or [`transform`].
418 ///
419 /// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
420 /// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
421 /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
422 CursorMoved {
423 device_id: DeviceId,
424
425 /// (x,y) coords in pixels relative to the top-left corner of the window. Because the range of this data is
426 /// limited by the display area and it may have been transformed by the OS to implement effects such as cursor
427 /// acceleration, it should not be used to implement non-cursor-like interactions such as 3D camera control.
428 position: PhysicalPosition<f64>,
429 },
430
431 /// The cursor has entered the window.
432 ///
433 /// ## Platform-specific
434 ///
435 /// - **Web:** Doesn't take into account CSS [`border`], [`padding`], or [`transform`].
436 ///
437 /// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
438 /// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
439 /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
440 CursorEntered { device_id: DeviceId },
441
442 /// The cursor has left the window.
443 ///
444 /// ## Platform-specific
445 ///
446 /// - **Web:** Doesn't take into account CSS [`border`], [`padding`], or [`transform`].
447 ///
448 /// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
449 /// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
450 /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
451 CursorLeft { device_id: DeviceId },
452
453 /// A mouse wheel movement or touchpad scroll occurred.
454 MouseWheel {
455 device_id: DeviceId,
456 delta: MouseScrollDelta,
457 phase: TouchPhase,
458 },
459
460 /// An mouse button press has been received.
461 MouseInput {
462 device_id: DeviceId,
463 state: ElementState,
464 button: MouseButton,
465 },
466
467 /// Touchpad magnification event with two-finger pinch gesture.
468 ///
469 /// Positive delta values indicate magnification (zooming in) and
470 /// negative delta values indicate shrinking (zooming out).
471 ///
472 /// ## Platform-specific
473 ///
474 /// - Only available on **macOS**.
475 TouchpadMagnify {
476 device_id: DeviceId,
477 delta: f64,
478 phase: TouchPhase,
479 },
480
481 /// Smart magnification event.
482 ///
483 /// On a Mac, smart magnification is triggered by a double tap with two fingers
484 /// on the trackpad and is commonly used to zoom on a certain object
485 /// (e.g. a paragraph of a PDF) or (sort of like a toggle) to reset any zoom.
486 /// The gesture is also supported in Safari, Pages, etc.
487 ///
488 /// The event is general enough that its generating gesture is allowed to vary
489 /// across platforms. It could also be generated by another device.
490 ///
491 /// Unfortunatly, neither [Windows](https://support.microsoft.com/en-us/windows/touch-gestures-for-windows-a9d28305-4818-a5df-4e2b-e5590f850741)
492 /// nor [Wayland](https://wayland.freedesktop.org/libinput/doc/latest/gestures.html)
493 /// support this gesture or any other gesture with the same effect.
494 ///
495 /// ## Platform-specific
496 ///
497 /// - Only available on **macOS 10.8** and later.
498 SmartMagnify { device_id: DeviceId },
499
500 /// Touchpad rotation event with two-finger rotation gesture.
501 ///
502 /// Positive delta values indicate rotation counterclockwise and
503 /// negative delta values indicate rotation clockwise.
504 ///
505 /// ## Platform-specific
506 ///
507 /// - Only available on **macOS**.
508 TouchpadRotate {
509 device_id: DeviceId,
510 delta: f32,
511 phase: TouchPhase,
512 },
513
514 /// Touchpad pressure event.
515 ///
516 /// At the moment, only supported on Apple forcetouch-capable macbooks.
517 /// The parameters are: pressure level (value between 0 and 1 representing how hard the touchpad
518 /// is being pressed) and stage (integer representing the click level).
519 TouchpadPressure {
520 device_id: DeviceId,
521 pressure: f32,
522 stage: i64,
523 },
524
525 /// Motion on some analog axis. May report data redundant to other, more specific events.
526 AxisMotion {
527 device_id: DeviceId,
528 axis: AxisId,
529 value: f64,
530 },
531
532 /// Touch event has been received
533 ///
534 /// ## Platform-specific
535 ///
536 /// - **Web:** Doesn't take into account CSS [`border`], [`padding`], or [`transform`].
537 /// - **macOS:** Unsupported.
538 ///
539 /// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
540 /// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
541 /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
542 Touch(Touch),
543
544 /// The window's scale factor has changed.
545 ///
546 /// The following user actions can cause DPI changes:
547 ///
548 /// * Changing the display's resolution.
549 /// * Changing the display's scale factor (e.g. in Control Panel on Windows).
550 /// * Moving the window to a display with a different scale factor.
551 ///
552 /// After this event callback has been processed, the window will be resized to whatever value
553 /// is pointed to by the `new_inner_size` reference. By default, this will contain the size suggested
554 /// by the OS, but it can be changed to any value.
555 ///
556 /// For more information about DPI in general, see the [`dpi`](crate::dpi) module.
557 ScaleFactorChanged {
558 scale_factor: f64,
559 /// Handle to update inner size during scale changes.
560 ///
561 /// See [`InnerSizeWriter`] docs for more details.
562 inner_size_writer: InnerSizeWriter,
563 },
564
565 /// The system window theme has changed.
566 ///
567 /// Applications might wish to react to this to change the theme of the content of the window
568 /// when the system changes the window theme.
569 ///
570 /// ## Platform-specific
571 ///
572 /// - **iOS / Android / X11 / Wayland / Orbital:** Unsupported.
573 ThemeChanged(Theme),
574
575 /// The window has been occluded (completely hidden from view).
576 ///
577 /// This is different to window visibility as it depends on whether the window is closed,
578 /// minimised, set invisible, or fully occluded by another window.
579 ///
580 /// ## Platform-specific
581 ///
582 /// ### iOS
583 ///
584 /// On iOS, the `Occluded(false)` event is emitted in response to an [`applicationWillEnterForeground`]
585 /// callback which means the application should start preparing its data. The `Occluded(true)` event is
586 /// emitted in response to an [`applicationDidEnterBackground`] callback which means the application
587 /// should free resources (according to the [iOS application lifecycle]).
588 ///
589 /// [`applicationWillEnterForeground`]: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623076-applicationwillenterforeground
590 /// [`applicationDidEnterBackground`]: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622997-applicationdidenterbackground
591 /// [iOS application lifecycle]: https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle
592 ///
593 /// ### Others
594 ///
595 /// - **Web:** Doesn't take into account CSS [`border`], [`padding`], or [`transform`].
596 /// - **Android / Windows / Orbital:** Unsupported.
597 ///
598 /// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
599 /// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
600 /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
601 Occluded(bool),
602
603 /// A menu item was pressed
604 MenuAction(usize),
605
606 /// Emitted when a window should be redrawn.
607 ///
608 /// This gets triggered in two scenarios:
609 /// - The OS has performed an operation that's invalidated the window's contents (such as
610 /// resizing the window).
611 /// - The application has explicitly requested a redraw via [`Window::request_redraw`].
612 ///
613 /// Winit will aggregate duplicate redraw requests into a single event, to
614 /// help avoid duplicating rendering work.
615 RedrawRequested,
616}
617
618/// Identifier of an input device.
619///
620/// Whenever you receive an event arising from a particular input device, this event contains a `DeviceId` which
621/// identifies its origin. Note that devices may be virtual (representing an on-screen cursor and keyboard focus) or
622/// physical. Virtual devices typically aggregate inputs from multiple physical devices.
623#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
624pub struct DeviceId(pub(crate) platform_impl::DeviceId);
625
626impl DeviceId {
627 /// Returns a dummy id, useful for unit testing.
628 ///
629 /// # Safety
630 ///
631 /// The only guarantee made about the return value of this function is that
632 /// it will always be equal to itself and to future values returned by this function.
633 /// No other guarantees are made. This may be equal to a real `DeviceId`.
634 ///
635 /// **Passing this into a winit function will result in undefined behavior.**
636 pub const unsafe fn dummy() -> Self {
637 #[allow(unused_unsafe)]
638 DeviceId(unsafe { platform_impl::DeviceId::dummy() })
639 }
640}
641
642/// Represents raw hardware events that are not associated with any particular window.
643///
644/// Useful for interactions that diverge significantly from a conventional 2D GUI, such as 3D camera or first-person
645/// game controls. Many physical actions, such as mouse movement, can produce both device and window events. Because
646/// window events typically arise from virtual devices (corresponding to GUI cursors and keyboard focus) the device IDs
647/// may not match.
648///
649/// Note that these events are delivered regardless of input focus.
650#[derive(Clone, Debug, PartialEq)]
651pub enum DeviceEvent {
652 Added,
653 Removed,
654
655 /// Change in physical position of a pointing device.
656 ///
657 /// This represents raw, unfiltered physical motion. Not to be confused with [`WindowEvent::CursorMoved`].
658 MouseMotion {
659 /// (x, y) change in position in unspecified units.
660 ///
661 /// Different devices may use different units.
662 delta: (f64, f64),
663 },
664
665 /// Physical scroll event
666 MouseWheel {
667 delta: MouseScrollDelta,
668 },
669
670 /// Motion on some analog axis. This event will be reported for all arbitrary input devices
671 /// that winit supports on this platform, including mouse devices. If the device is a mouse
672 /// device then this will be reported alongside the MouseMotion event.
673 Motion {
674 axis: AxisId,
675 value: f64,
676 },
677
678 Button {
679 button: ButtonId,
680 state: ElementState,
681 },
682
683 Key(RawKeyEvent),
684}
685
686/// Describes a keyboard input as a raw device event.
687///
688/// Note that holding down a key may produce repeated `RawKeyEvent`s. The
689/// operating system doesn't provide information whether such an event is a
690/// repeat or the initial keypress. An application may emulate this by, for
691/// example keeping a Map/Set of pressed keys and determining whether a keypress
692/// corresponds to an already pressed key.
693#[derive(Debug, Clone, Eq, PartialEq, Hash)]
694#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
695pub struct RawKeyEvent {
696 pub physical_key: keyboard::PhysicalKey,
697 pub state: ElementState,
698}
699
700/// Describes a keyboard input targeting a window.
701#[derive(Debug, Clone, Eq, PartialEq, Hash)]
702pub struct KeyEvent {
703 /// Represents the position of a key independent of the currently active layout.
704 ///
705 /// It also uniquely identifies the physical key (i.e. it's mostly synonymous with a scancode).
706 /// The most prevalent use case for this is games. For example the default keys for the player
707 /// to move around might be the W, A, S, and D keys on a US layout. The position of these keys
708 /// is more important than their label, so they should map to Z, Q, S, and D on an "AZERTY"
709 /// layout. (This value is `KeyCode::KeyW` for the Z key on an AZERTY layout.)
710 ///
711 /// ## Caveats
712 ///
713 /// - Certain niche hardware will shuffle around physical key positions, e.g. a keyboard that
714 /// implements DVORAK in hardware (or firmware)
715 /// - Your application will likely have to handle keyboards which are missing keys that your
716 /// own keyboard has.
717 /// - Certain `KeyCode`s will move between a couple of different positions depending on what
718 /// layout the keyboard was manufactured to support.
719 ///
720 /// **Because of these caveats, it is important that you provide users with a way to configure
721 /// most (if not all) keybinds in your application.**
722 ///
723 /// ## `Fn` and `FnLock`
724 ///
725 /// `Fn` and `FnLock` key events are *exceedingly unlikely* to be emitted by Winit. These keys
726 /// are usually handled at the hardware or OS level, and aren't surfaced to applications. If
727 /// you somehow see this in the wild, we'd like to know :)
728 pub physical_key: keyboard::PhysicalKey,
729
730 // Allowing `broken_intra_doc_links` for `logical_key`, because
731 // `key_without_modifiers` is not available on all platforms
732 #[cfg_attr(
733 not(any(windows_platform, macos_platform, x11_platform, wayland_platform)),
734 allow(rustdoc::broken_intra_doc_links)
735 )]
736 /// This value is affected by all modifiers except <kbd>Ctrl</kbd>.
737 ///
738 /// This has two use cases:
739 /// - Allows querying whether the current input is a Dead key.
740 /// - Allows handling key-bindings on platforms which don't
741 /// support [`key_without_modifiers`].
742 ///
743 /// If you use this field (or [`key_without_modifiers`] for that matter) for keyboard
744 /// shortcuts, **it is important that you provide users with a way to configure your
745 /// application's shortcuts so you don't render your application unusable for users with an
746 /// incompatible keyboard layout.**
747 ///
748 /// ## Platform-specific
749 /// - **Web:** Dead keys might be reported as the real key instead
750 /// of `Dead` depending on the browser/OS.
751 ///
752 /// [`key_without_modifiers`]: crate::platform::modifier_supplement::KeyEventExtModifierSupplement::key_without_modifiers
753 pub logical_key: keyboard::Key,
754
755 /// Contains the text produced by this keypress.
756 ///
757 /// In most cases this is identical to the content
758 /// of the `Character` variant of `logical_key`.
759 /// However, on Windows when a dead key was pressed earlier
760 /// but cannot be combined with the character from this
761 /// keypress, the produced text will consist of two characters:
762 /// the dead-key-character followed by the character resulting
763 /// from this keypress.
764 ///
765 /// An additional difference from `logical_key` is that
766 /// this field stores the text representation of any key
767 /// that has such a representation. For example when
768 /// `logical_key` is `Key::Named(NamedKey::Enter)`, this field is `Some("\r")`.
769 ///
770 /// This is `None` if the current keypress cannot
771 /// be interpreted as text.
772 ///
773 /// See also: `text_with_all_modifiers()`
774 pub text: Option<SmolStr>,
775
776 /// Contains the location of this key on the keyboard.
777 ///
778 /// Certain keys on the keyboard may appear in more than once place. For example, the "Shift" key
779 /// appears on the left side of the QWERTY keyboard as well as the right side. However, both keys
780 /// have the same symbolic value. Another example of this phenomenon is the "1" key, which appears
781 /// both above the "Q" key and as the "Keypad 1" key.
782 ///
783 /// This field allows the user to differentiate between keys like this that have the same symbolic
784 /// value but different locations on the keyboard.
785 ///
786 /// See the [`KeyLocation`] type for more details.
787 ///
788 /// [`KeyLocation`]: crate::keyboard::KeyLocation
789 pub location: keyboard::KeyLocation,
790
791 /// Whether the key is being pressed or released.
792 ///
793 /// See the [`ElementState`] type for more details.
794 pub state: ElementState,
795
796 /// Whether or not this key is a key repeat event.
797 ///
798 /// On some systems, holding down a key for some period of time causes that key to be repeated
799 /// as though it were being pressed and released repeatedly. This field is `true` if and only if
800 /// this event is the result of one of those repeats.
801 pub repeat: bool,
802
803 /// Platform-specific key event information.
804 ///
805 /// On Windows, Linux and macOS, this type contains the key without modifiers and the text with all
806 /// modifiers applied.
807 ///
808 /// On Android, iOS, Redox and Web, this type is a no-op.
809 pub(crate) platform_specific: platform_impl::KeyEventExtra,
810}
811
812/// Describes keyboard modifiers event.
813#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
814pub struct Modifiers {
815 pub(crate) state: ModifiersState,
816
817 // NOTE: Currently pressed modifiers keys.
818 //
819 // The field providing a metadata, it shouldn't be used as a source of truth.
820 pub(crate) pressed_mods: ModifiersKeys,
821}
822
823impl Modifiers {
824 /// The state of the modifiers.
825 pub fn state(&self) -> ModifiersState {
826 self.state
827 }
828
829 /// The state of the left shift key.
830 pub fn lshift_state(&self) -> ModifiersKeyState {
831 self.mod_state(ModifiersKeys::LSHIFT)
832 }
833
834 /// The state of the right shift key.
835 pub fn rshift_state(&self) -> ModifiersKeyState {
836 self.mod_state(ModifiersKeys::RSHIFT)
837 }
838
839 /// The state of the left alt key.
840 pub fn lalt_state(&self) -> ModifiersKeyState {
841 self.mod_state(ModifiersKeys::LALT)
842 }
843
844 /// The state of the right alt key.
845 pub fn ralt_state(&self) -> ModifiersKeyState {
846 self.mod_state(ModifiersKeys::RALT)
847 }
848
849 /// The state of the left control key.
850 pub fn lcontrol_state(&self) -> ModifiersKeyState {
851 self.mod_state(ModifiersKeys::LCONTROL)
852 }
853
854 /// The state of the right control key.
855 pub fn rcontrol_state(&self) -> ModifiersKeyState {
856 self.mod_state(ModifiersKeys::RCONTROL)
857 }
858
859 /// The state of the left super key.
860 pub fn lsuper_state(&self) -> ModifiersKeyState {
861 self.mod_state(ModifiersKeys::LSUPER)
862 }
863
864 /// The state of the right super key.
865 pub fn rsuper_state(&self) -> ModifiersKeyState {
866 self.mod_state(ModifiersKeys::RSUPER)
867 }
868
869 fn mod_state(&self, modifier: ModifiersKeys) -> ModifiersKeyState {
870 if self.pressed_mods.contains(modifier) {
871 ModifiersKeyState::Pressed
872 } else {
873 ModifiersKeyState::Unknown
874 }
875 }
876}
877
878impl From<ModifiersState> for Modifiers {
879 fn from(value: ModifiersState) -> Self {
880 Self {
881 state: value,
882 pressed_mods: Default::default(),
883 }
884 }
885}
886
887/// Describes [input method](https://en.wikipedia.org/wiki/Input_method) events.
888///
889/// This is also called a "composition event".
890///
891/// Most keypresses using a latin-like keyboard layout simply generate a [`WindowEvent::KeyboardInput`].
892/// However, one couldn't possibly have a key for every single unicode character that the user might want to type
893/// - so the solution operating systems employ is to allow the user to type these using _a sequence of keypresses_ instead.
894///
895/// A prominent example of this is accents - many keyboard layouts allow you to first click the "accent key", and then
896/// the character you want to apply the accent to. In this case, some platforms will generate the following event sequence:
897/// ```ignore
898/// // Press "`" key
899/// Ime::Preedit("`", Some((0, 0)))
900/// // Press "E" key
901/// Ime::Preedit("", None) // Synthetic event generated by winit to clear preedit.
902/// Ime::Commit("é")
903/// ```
904///
905/// Additionally, certain input devices are configured to display a candidate box that allow the user to select the
906/// desired character interactively. (To properly position this box, you must use [`Window::set_ime_cursor_area`].)
907///
908/// An example of a keyboard layout which uses candidate boxes is pinyin. On a latin keyboard the following event
909/// sequence could be obtained:
910/// ```ignore
911/// // Press "A" key
912/// Ime::Preedit("a", Some((1, 1)))
913/// // Press "B" key
914/// Ime::Preedit("a b", Some((3, 3)))
915/// // Press left arrow key
916/// Ime::Preedit("a b", Some((1, 1)))
917/// // Press space key
918/// Ime::Preedit("啊b", Some((3, 3)))
919/// // Press space key
920/// Ime::Preedit("", None) // Synthetic event generated by winit to clear preedit.
921/// Ime::Commit("啊不")
922/// ```
923#[derive(Debug, Clone, PartialEq, Eq, Hash)]
924#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
925pub enum Ime {
926 /// Notifies when the IME was enabled.
927 ///
928 /// After getting this event you could receive [`Preedit`](Self::Preedit) and
929 /// [`Commit`](Self::Commit) events. You should also start performing IME related requests
930 /// like [`Window::set_ime_cursor_area`].
931 Enabled,
932
933 /// Notifies when a new composing text should be set at the cursor position.
934 ///
935 /// The value represents a pair of the preedit string and the cursor begin position and end
936 /// position. When it's `None`, the cursor should be hidden. When `String` is an empty string
937 /// this indicates that preedit was cleared.
938 ///
939 /// The cursor position is byte-wise indexed.
940 Preedit(String, Option<(usize, usize)>),
941
942 /// Notifies when text should be inserted into the editor widget.
943 ///
944 /// Right before this event winit will send empty [`Self::Preedit`] event.
945 Commit(String),
946
947 /// Notifies when the IME was disabled.
948 ///
949 /// After receiving this event you won't get any more [`Preedit`](Self::Preedit) or
950 /// [`Commit`](Self::Commit) events until the next [`Enabled`](Self::Enabled) event. You should
951 /// also stop issuing IME related requests like [`Window::set_ime_cursor_area`] and clear pending
952 /// preedit text.
953 Disabled,
954}
955
956/// Describes touch-screen input state.
957#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
958#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
959pub enum TouchPhase {
960 Started,
961 Moved,
962 Ended,
963 Cancelled,
964}
965
966/// Represents a touch event
967///
968/// Every time the user touches the screen, a new [`TouchPhase::Started`] event with an unique
969/// identifier for the finger is generated. When the finger is lifted, an [`TouchPhase::Ended`]
970/// event is generated with the same finger id.
971///
972/// After a `Started` event has been emitted, there may be zero or more `Move`
973/// events when the finger is moved or the touch pressure changes.
974///
975/// The finger id may be reused by the system after an `Ended` event. The user
976/// should assume that a new `Started` event received with the same id has nothing
977/// to do with the old finger and is a new finger.
978///
979/// A [`TouchPhase::Cancelled`] event is emitted when the system has canceled tracking this
980/// touch, such as when the window loses focus, or on iOS if the user moves the
981/// device against their face.
982///
983/// ## Platform-specific
984///
985/// - **Web:** Doesn't take into account CSS [`border`], [`padding`], or [`transform`].
986/// - **macOS:** Unsupported.
987///
988/// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
989/// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
990/// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
991#[derive(Debug, Clone, Copy, PartialEq)]
992pub struct Touch {
993 pub device_id: DeviceId,
994 pub phase: TouchPhase,
995 pub location: PhysicalPosition<f64>,
996 /// Describes how hard the screen was pressed. May be `None` if the platform
997 /// does not support pressure sensitivity.
998 ///
999 /// ## Platform-specific
1000 ///
1001 /// - Only available on **iOS** 9.0+, **Windows** 8+, **Web**, and **Android**.
1002 /// - **Android**: This will never be [None]. If the device doesn't support pressure
1003 /// sensitivity, force will either be 0.0 or 1.0. Also see the
1004 /// [android documentation](https://developer.android.com/reference/android/view/MotionEvent#AXIS_PRESSURE).
1005 pub force: Option<Force>,
1006 /// Unique identifier of a finger.
1007 pub id: u64,
1008}
1009
1010/// Describes the force of a touch event
1011#[derive(Debug, Clone, Copy, PartialEq)]
1012pub enum Force {
1013 /// On iOS, the force is calibrated so that the same number corresponds to
1014 /// roughly the same amount of pressure on the screen regardless of the
1015 /// device.
1016 Calibrated {
1017 /// The force of the touch, where a value of 1.0 represents the force of
1018 /// an average touch (predetermined by the system, not user-specific).
1019 ///
1020 /// The force reported by Apple Pencil is measured along the axis of the
1021 /// pencil. If you want a force perpendicular to the device, you need to
1022 /// calculate this value using the `altitude_angle` value.
1023 force: f64,
1024 /// The maximum possible force for a touch.
1025 ///
1026 /// The value of this field is sufficiently high to provide a wide
1027 /// dynamic range for values of the `force` field.
1028 max_possible_force: f64,
1029 /// The altitude (in radians) of the stylus.
1030 ///
1031 /// A value of 0 radians indicates that the stylus is parallel to the
1032 /// surface. The value of this property is Pi/2 when the stylus is
1033 /// perpendicular to the surface.
1034 altitude_angle: Option<f64>,
1035 },
1036 /// If the platform reports the force as normalized, we have no way of
1037 /// knowing how much pressure 1.0 corresponds to – we know it's the maximum
1038 /// amount of force, but as to how much force, you might either have to
1039 /// press really really hard, or not hard at all, depending on the device.
1040 Normalized(f64),
1041}
1042
1043impl Force {
1044 /// Returns the force normalized to the range between 0.0 and 1.0 inclusive.
1045 ///
1046 /// Instead of normalizing the force, you should prefer to handle
1047 /// [`Force::Calibrated`] so that the amount of force the user has to apply is
1048 /// consistent across devices.
1049 pub fn normalized(&self) -> f64 {
1050 match self {
1051 Force::Calibrated {
1052 force,
1053 max_possible_force,
1054 altitude_angle,
1055 } => {
1056 let force = match altitude_angle {
1057 Some(altitude_angle) => force / altitude_angle.sin(),
1058 None => *force,
1059 };
1060 force / max_possible_force
1061 }
1062 Force::Normalized(force) => *force,
1063 }
1064 }
1065}
1066
1067/// Identifier for a specific analog axis on some device.
1068pub type AxisId = u32;
1069
1070/// Identifier for a specific button on some device.
1071pub type ButtonId = u32;
1072
1073/// Describes the input state of a key.
1074#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
1075#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1076pub enum ElementState {
1077 Pressed,
1078 Released,
1079}
1080
1081impl ElementState {
1082 /// True if `self == Pressed`.
1083 pub fn is_pressed(self) -> bool {
1084 self == ElementState::Pressed
1085 }
1086}
1087
1088/// Describes a button of a mouse controller.
1089///
1090/// ## Platform-specific
1091///
1092/// **macOS:** `Back` and `Forward` might not work with all hardware.
1093/// **Orbital:** `Back` and `Forward` are unsupported due to orbital not supporting them.
1094#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
1095#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1096pub enum MouseButton {
1097 Left,
1098 Right,
1099 Middle,
1100 Back,
1101 Forward,
1102 Other(u16),
1103}
1104
1105/// Describes a difference in the mouse scroll wheel state.
1106#[derive(Debug, Clone, Copy, PartialEq)]
1107#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1108pub enum MouseScrollDelta {
1109 /// Amount in lines or rows to scroll in the horizontal
1110 /// and vertical directions.
1111 ///
1112 /// Positive values indicate that the content that is being scrolled should move
1113 /// right and down (revealing more content left and up).
1114 LineDelta(f32, f32),
1115
1116 /// Amount in pixels to scroll in the horizontal and
1117 /// vertical direction.
1118 ///
1119 /// Scroll events are expressed as a `PixelDelta` if
1120 /// supported by the device (eg. a touchpad) and
1121 /// platform.
1122 ///
1123 /// Positive values indicate that the content being scrolled should
1124 /// move right/down.
1125 ///
1126 /// For a 'natural scrolling' touch pad (that acts like a touch screen)
1127 /// this means moving your fingers right and down should give positive values,
1128 /// and move the content right and down (to reveal more things left and up).
1129 PixelDelta(PhysicalPosition<f64>),
1130}
1131
1132/// Handle to synchroniously change the size of the window from the
1133/// [`WindowEvent`].
1134#[derive(Debug, Clone)]
1135pub struct InnerSizeWriter {
1136 pub(crate) new_inner_size: Weak<Mutex<PhysicalSize<u32>>>,
1137}
1138
1139impl InnerSizeWriter {
1140 #[cfg(not(orbital_platform))]
1141 pub(crate) fn new(new_inner_size: Weak<Mutex<PhysicalSize<u32>>>) -> Self {
1142 Self { new_inner_size }
1143 }
1144
1145 /// Try to request inner size which will be set synchroniously on the window.
1146 pub fn request_inner_size(
1147 &mut self,
1148 new_inner_size: PhysicalSize<u32>,
1149 ) -> Result<(), ExternalError> {
1150 if let Some(inner) = self.new_inner_size.upgrade() {
1151 *inner.lock().unwrap() = new_inner_size;
1152 Ok(())
1153 } else {
1154 Err(ExternalError::Ignored)
1155 }
1156 }
1157}
1158
1159impl PartialEq for InnerSizeWriter {
1160 fn eq(&self, other: &Self) -> bool {
1161 self.new_inner_size.as_ptr() == other.new_inner_size.as_ptr()
1162 }
1163}
1164
1165#[cfg(test)]
1166mod tests {
1167 use crate::event;
1168 use std::collections::{BTreeSet, HashSet};
1169
1170 macro_rules! foreach_event {
1171 ($closure:expr) => {{
1172 #[allow(unused_mut)]
1173 let mut x = $closure;
1174 let did = unsafe { event::DeviceId::dummy() };
1175
1176 #[allow(deprecated)]
1177 {
1178 use crate::event::{Event::*, Ime::Enabled, WindowEvent::*};
1179 use crate::window::WindowId;
1180
1181 // Mainline events.
1182 let wid = unsafe { WindowId::dummy() };
1183 x(UserEvent(()));
1184 x(NewEvents(event::StartCause::Init));
1185 x(AboutToWait);
1186 x(LoopExiting);
1187 x(Suspended);
1188 x(Resumed);
1189
1190 // Window events.
1191 let with_window_event = |wev| {
1192 x(WindowEvent {
1193 window_id: wid,
1194 event: wev,
1195 })
1196 };
1197
1198 with_window_event(CloseRequested);
1199 with_window_event(Destroyed);
1200 with_window_event(Focused(true));
1201 with_window_event(Moved((0, 0).into()));
1202 with_window_event(Resized((0, 0).into()));
1203 with_window_event(DroppedFile("x.txt".into()));
1204 with_window_event(HoveredFile("x.txt".into()));
1205 with_window_event(HoveredFileCancelled);
1206 with_window_event(Ime(Enabled));
1207 with_window_event(CursorMoved {
1208 device_id: did,
1209 position: (0, 0).into(),
1210 });
1211 with_window_event(ModifiersChanged(event::Modifiers::default()));
1212 with_window_event(CursorEntered { device_id: did });
1213 with_window_event(CursorLeft { device_id: did });
1214 with_window_event(MouseWheel {
1215 device_id: did,
1216 delta: event::MouseScrollDelta::LineDelta(0.0, 0.0),
1217 phase: event::TouchPhase::Started,
1218 });
1219 with_window_event(MouseInput {
1220 device_id: did,
1221 state: event::ElementState::Pressed,
1222 button: event::MouseButton::Other(0),
1223 });
1224 with_window_event(TouchpadMagnify {
1225 device_id: did,
1226 delta: 0.0,
1227 phase: event::TouchPhase::Started,
1228 });
1229 with_window_event(SmartMagnify { device_id: did });
1230 with_window_event(TouchpadRotate {
1231 device_id: did,
1232 delta: 0.0,
1233 phase: event::TouchPhase::Started,
1234 });
1235 with_window_event(TouchpadPressure {
1236 device_id: did,
1237 pressure: 0.0,
1238 stage: 0,
1239 });
1240 with_window_event(AxisMotion {
1241 device_id: did,
1242 axis: 0,
1243 value: 0.0,
1244 });
1245 with_window_event(Touch(event::Touch {
1246 device_id: did,
1247 phase: event::TouchPhase::Started,
1248 location: (0.0, 0.0).into(),
1249 id: 0,
1250 force: Some(event::Force::Normalized(0.0)),
1251 }));
1252 with_window_event(ThemeChanged(crate::window::Theme::Light));
1253 with_window_event(Occluded(true));
1254 }
1255
1256 #[allow(deprecated)]
1257 {
1258 use event::DeviceEvent::*;
1259
1260 let with_device_event = |dev_ev| {
1261 x(event::Event::DeviceEvent {
1262 device_id: did,
1263 event: dev_ev,
1264 })
1265 };
1266
1267 with_device_event(Added);
1268 with_device_event(Removed);
1269 with_device_event(MouseMotion {
1270 delta: (0.0, 0.0).into(),
1271 });
1272 with_device_event(MouseWheel {
1273 delta: event::MouseScrollDelta::LineDelta(0.0, 0.0),
1274 });
1275 with_device_event(Motion {
1276 axis: 0,
1277 value: 0.0,
1278 });
1279 with_device_event(Button {
1280 button: 0,
1281 state: event::ElementState::Pressed,
1282 });
1283 }
1284 }};
1285 }
1286
1287 #[allow(clippy::redundant_clone)]
1288 #[test]
1289 fn test_event_clone() {
1290 foreach_event!(|event: event::Event<()>| {
1291 let event2 = event.clone();
1292 assert_eq!(event, event2);
1293 })
1294 }
1295
1296 #[test]
1297 fn test_map_nonuser_event() {
1298 foreach_event!(|event: event::Event<()>| {
1299 let is_user = matches!(event, event::Event::UserEvent(()));
1300 let event2 = event.map_nonuser_event::<()>();
1301 if is_user {
1302 assert_eq!(event2, Err(event::Event::UserEvent(())));
1303 } else {
1304 assert!(event2.is_ok());
1305 }
1306 })
1307 }
1308
1309 #[test]
1310 fn test_force_normalize() {
1311 let force = event::Force::Normalized(0.0);
1312 assert_eq!(force.normalized(), 0.0);
1313
1314 let force2 = event::Force::Calibrated {
1315 force: 5.0,
1316 max_possible_force: 2.5,
1317 altitude_angle: None,
1318 };
1319 assert_eq!(force2.normalized(), 2.0);
1320
1321 let force3 = event::Force::Calibrated {
1322 force: 5.0,
1323 max_possible_force: 2.5,
1324 altitude_angle: Some(std::f64::consts::PI / 2.0),
1325 };
1326 assert_eq!(force3.normalized(), 2.0);
1327 }
1328
1329 #[allow(clippy::clone_on_copy)]
1330 #[test]
1331 fn ensure_attrs_do_not_panic() {
1332 foreach_event!(|event: event::Event<()>| {
1333 let _ = format!("{:?}", event);
1334 });
1335 let _ = event::StartCause::Init.clone();
1336
1337 let did = unsafe { crate::event::DeviceId::dummy() }.clone();
1338 HashSet::new().insert(did);
1339 let mut set = [did, did, did];
1340 set.sort_unstable();
1341 let mut set2 = BTreeSet::new();
1342 set2.insert(did);
1343 set2.insert(did);
1344
1345 HashSet::new().insert(event::TouchPhase::Started.clone());
1346 HashSet::new().insert(event::MouseButton::Left.clone());
1347 HashSet::new().insert(event::Ime::Enabled);
1348
1349 let _ = event::Touch {
1350 device_id: did,
1351 phase: event::TouchPhase::Started,
1352 location: (0.0, 0.0).into(),
1353 id: 0,
1354 force: Some(event::Force::Normalized(0.0)),
1355 }
1356 .clone();
1357 let _ = event::Force::Calibrated {
1358 force: 0.0,
1359 max_possible_force: 0.0,
1360 altitude_angle: None,
1361 }
1362 .clone();
1363 }
1364}