winit/
event.rs

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