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