Skip to main content

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