878 lines · 26.9 KB · Rust
RawDownload
use crate::{    Bounds, Capslock, Context, Empty, IntoElement, Keystroke, Modifiers, Pixels, Point, Render,    Window, point, seal::Sealed,};use smallvec::SmallVec;use std::{any::Any, fmt::Debug, ops::Deref, path::PathBuf}; /// An event from a platform input source.pub trait InputEvent: Sealed + 'static {    /// Convert this event into the platform input enum.    fn to_platform_input(self) -> PlatformInput;} /// A key event from the platform.pub trait KeyEvent: InputEvent {} /// A mouse event from the platform.pub trait MouseEvent: InputEvent {} /// A gesture event from the platform.pub trait GestureEvent: InputEvent {} /// The key down event equivalent for the platform.#[derive(Clone, Debug, Eq, PartialEq)]pub struct KeyDownEvent {    /// The keystroke that was generated.    pub keystroke: Keystroke,     /// Whether the key is currently held down.    pub is_held: bool,     /// Whether to prefer character input over keybindings for this keystroke.    /// In some cases, like AltGr on Windows, modifiers are significant for character input.    pub prefer_character_input: bool,} impl Sealed for KeyDownEvent {}impl InputEvent for KeyDownEvent {    fn to_platform_input(self) -> PlatformInput {        PlatformInput::KeyDown(self)    }}impl KeyEvent for KeyDownEvent {} /// The key up event equivalent for the platform.#[derive(Clone, Debug)]pub struct KeyUpEvent {    /// The keystroke that was released.    pub keystroke: Keystroke,} impl Sealed for KeyUpEvent {}impl InputEvent for KeyUpEvent {    fn to_platform_input(self) -> PlatformInput {        PlatformInput::KeyUp(self)    }}impl KeyEvent for KeyUpEvent {} /// The modifiers changed event equivalent for the platform.#[derive(Clone, Debug, Default)]pub struct ModifiersChangedEvent {    /// The new state of the modifier keys    pub modifiers: Modifiers,    /// The new state of the capslock key    pub capslock: Capslock,} impl Sealed for ModifiersChangedEvent {}impl InputEvent for ModifiersChangedEvent {    fn to_platform_input(self) -> PlatformInput {        PlatformInput::ModifiersChanged(self)    }}impl KeyEvent for ModifiersChangedEvent {} impl Deref for ModifiersChangedEvent {    type Target = Modifiers;     fn deref(&self) -> &Self::Target {        &self.modifiers    }} /// The phase of a touch motion event./// Based on the winit enum of the same name.#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]pub enum TouchPhase {    /// The touch started.    Started,    /// The touch event is moving.    #[default]    Moved,    /// The touch phase has ended    Ended,    /// The touch was cancelled: the system took it and it will not end    /// normally. Consumers must fully unwind any in-progress interaction,    /// treating the touch as if it never committed.    Cancelled,} /// Identifies one touch (finger or stylus contact) for its lifetime, from/// [`TouchPhase::Started`] through [`TouchPhase::Ended`] or/// [`TouchPhase::Cancelled`].////// The value is opaque and platform-defined; it is only guaranteed to be/// stable for the duration of the touch and unique among concurrent touches.#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]pub struct TouchId(pub u64); /// A raw touch event from the platform.///////// Dispatch contract (core implementation pending): a touch is hit-tested/// once, at [`TouchPhase::Started`], occlusion-aware; all subsequent events/// for the same [`TouchId`] are delivered to the elements under the starting/// position, even after the touch moves outside them.#[derive(Clone, Debug, Default)]pub struct TouchEvent {    /// Which touch this event belongs to.    pub id: TouchId,    /// The phase of the touch.    pub phase: TouchPhase,    /// The position of the touch in window coordinates.    pub position: Point<Pixels>,    /// Normalized touch force in `0.0..=1.0`, if the hardware reports it.    pub force: Option<f32>,} impl Sealed for TouchEvent {}impl InputEvent for TouchEvent {    fn to_platform_input(self) -> PlatformInput {        PlatformInput::Touch(self)    }} /// A mouse down event from the platform#[derive(Clone, Debug, Default)]pub struct MouseDownEvent {    /// Which mouse button was pressed.    pub button: MouseButton,     /// The position of the mouse on the window.    pub position: Point<Pixels>,     /// The modifiers that were held down when the mouse was pressed.    pub modifiers: Modifiers,     /// The number of times the button has been clicked.    pub click_count: usize,     /// Whether this is the first, focusing click.    pub first_mouse: bool,} impl Sealed for MouseDownEvent {}impl InputEvent for MouseDownEvent {    fn to_platform_input(self) -> PlatformInput {        PlatformInput::MouseDown(self)    }}impl MouseEvent for MouseDownEvent {} impl MouseDownEvent {    /// Returns true if this mouse up event should focus the element.    pub fn is_focusing(&self) -> bool {        match self.button {            MouseButton::Left => true,            _ => false,        }    }} /// A mouse up event from the platform#[derive(Clone, Debug, Default)]pub struct MouseUpEvent {    /// Which mouse button was released.    pub button: MouseButton,     /// The position of the mouse on the window.    pub position: Point<Pixels>,     /// The modifiers that were held down when the mouse was released.    pub modifiers: Modifiers,     /// The number of times the button has been clicked.    pub click_count: usize,} impl Sealed for MouseUpEvent {}impl InputEvent for MouseUpEvent {    fn to_platform_input(self) -> PlatformInput {        PlatformInput::MouseUp(self)    }} impl MouseEvent for MouseUpEvent {} impl MouseUpEvent {    /// Returns true if this mouse up event should focus the element.    pub fn is_focusing(&self) -> bool {        match self.button {            MouseButton::Left => true,            _ => false,        }    }} /// A click event, generated when a mouse button is pressed and released.#[derive(Clone, Debug, Default)]pub struct MouseClickEvent {    /// The mouse event when the button was pressed.    pub down: MouseDownEvent,     /// The mouse event when the button was released.    pub up: MouseUpEvent,} /// The stage of a pressure click event.#[derive(Clone, Copy, Debug, Default, PartialEq)]pub enum PressureStage {    /// No pressure.    #[default]    Zero,    /// Normal click pressure.    Normal,    /// High pressure, enough to trigger a force click.    Force,} /// A mouse pressure event from the platform. Generated when a force-sensitive trackpad is pressed hard./// Currently only implemented for macOS trackpads.#[derive(Debug, Clone, Default)]pub struct MousePressureEvent {    /// Pressure of the current stage as a float between 0 and 1    pub pressure: f32,    /// The pressure stage of the event.    pub stage: PressureStage,    /// The position of the mouse on the window.    pub position: Point<Pixels>,    /// The modifiers that were held down when the mouse pressure changed.    pub modifiers: Modifiers,} impl Sealed for MousePressureEvent {}impl InputEvent for MousePressureEvent {    fn to_platform_input(self) -> PlatformInput {        PlatformInput::MousePressure(self)    }}impl MouseEvent for MousePressureEvent {} /// A click event that was generated by a keyboard button being pressed and released.#[derive(Clone, Debug, Default)]pub struct KeyboardClickEvent {    /// The keyboard button that was pressed to trigger the click.    pub button: KeyboardButton,     /// The bounds of the element that was clicked.    pub bounds: Bounds<Pixels>,} /// A click event that was generated by a recognized tap gesture on a touch/// screen.#[derive(Clone, Debug, Default)]pub struct TouchClickEvent {    /// The position of the tap in window coordinates.    pub position: Point<Pixels>,    /// The number of consecutive taps at this location (double tap = 2),    /// analogous to the mouse `click_count`.    pub tap_count: usize,    /// Whether this was a long press rather than a tap. Long presses are    /// touch's secondary activation: they are delivered to aux-click    /// listeners alongside right clicks, not to primary click listeners.    pub long_press: bool,} /// A click event, generated when a mouse button or keyboard button is pressed and released,/// or when a tap gesture is recognized on a touch screen.#[derive(Clone, Debug)]pub enum ClickEvent {    /// A click event trigger by a mouse button being pressed and released.    Mouse(MouseClickEvent),    /// A click event trigger by a keyboard button being pressed and released.    Keyboard(KeyboardClickEvent),    /// A click event triggered by a recognized tap gesture on a touch screen.    Touch(TouchClickEvent),} impl Default for ClickEvent {    fn default() -> Self {        ClickEvent::Keyboard(KeyboardClickEvent::default())    }} impl ClickEvent {    /// Returns the modifiers that were held during the click event    ///    /// `Keyboard`: The keyboard click events never have modifiers.    /// `Mouse`: Modifiers that were held during the mouse key up event.    pub fn modifiers(&self) -> Modifiers {        match self {            // Click events are only generated from keyboard events _without any modifiers_, so we know the modifiers are always Default            ClickEvent::Keyboard(_) => Modifiers::default(),            // Click events on the web only reflect the modifiers for the keyup event,            // tested via observing the behavior of the `ClickEvent.shiftKey` field in Chrome 138            // under various combinations of modifiers and keyUp / keyDown events.            ClickEvent::Mouse(event) => event.up.modifiers,            // Touch screens have no modifier keys.            ClickEvent::Touch(_) => Modifiers::default(),        }    }     /// Returns the position of the click event    ///    /// `Keyboard`: The bottom left corner of the clicked hitbox    /// `Mouse`: The position of the mouse when the button was released.    /// `Touch`: The position of the tap.    pub fn position(&self) -> Point<Pixels> {        match self {            ClickEvent::Keyboard(event) => event.bounds.bottom_left(),            ClickEvent::Mouse(event) => event.up.position,            ClickEvent::Touch(event) => event.position,        }    }     /// Returns the mouse position of the click event    ///    /// `Keyboard`: None    /// `Mouse`: The position of the mouse when the button was released.    /// `Touch`: None, touches are not mouse input and there is no cursor.    pub fn mouse_position(&self) -> Option<Point<Pixels>> {        match self {            ClickEvent::Keyboard(_) => None,            ClickEvent::Mouse(event) => Some(event.up.position),            ClickEvent::Touch(_) => None,        }    }     /// Returns if this was a right click    ///    /// `Keyboard`: false    /// `Mouse`: Whether the right button was pressed and released    pub fn is_right_click(&self) -> bool {        match self {            ClickEvent::Keyboard(_) => false,            ClickEvent::Mouse(event) => {                event.down.button == MouseButton::Right && event.up.button == MouseButton::Right            }            ClickEvent::Touch(_) => false,        }    }     /// Returns if this was a middle click    ///    /// `Keyboard`: false    /// `Mouse`: Whether the middle button was pressed and released    pub fn is_middle_click(&self) -> bool {        match self {            ClickEvent::Keyboard(_) => false,            ClickEvent::Mouse(event) => {                event.down.button == MouseButton::Middle && event.up.button == MouseButton::Middle            }            ClickEvent::Touch(_) => false,        }    }     /// Returns whether the click is a secondary activation, i.e. a context    /// menu trigger: a right click from a mouse (macOS ctrl-clicks arrive    /// already converted to right clicks by the platform layer), or a long    /// press on a touch screen.    pub fn is_secondary(&self) -> bool {        match self {            ClickEvent::Keyboard(_) => false,            ClickEvent::Mouse(event) => {                event.down.button == MouseButton::Right && event.up.button == MouseButton::Right            }            ClickEvent::Touch(event) => event.long_press,        }    }     /// Returns whether the click was a standard click    ///    /// `Keyboard`: Always true    /// `Mouse`: Left button pressed and released    /// `Touch`: A tap, but not a long press    pub fn standard_click(&self) -> bool {        match self {            ClickEvent::Keyboard(_) => true,            ClickEvent::Mouse(event) => {                event.down.button == MouseButton::Left && event.up.button == MouseButton::Left            }            ClickEvent::Touch(event) => !event.long_press,        }    }     /// Returns whether the click focused the element    ///    /// `Keyboard`: false, keyboard clicks only work if an element is already focused    /// `Mouse`: Whether this was the first focusing click    /// `Touch`: false, mobile windows are already active when tappable    pub fn first_focus(&self) -> bool {        match self {            ClickEvent::Keyboard(_) => false,            ClickEvent::Mouse(event) => event.down.first_mouse,            ClickEvent::Touch(_) => false,        }    }     /// Returns the click count of the click event    ///    /// `Keyboard`: Always 1    /// `Mouse`: Count of clicks from MouseUpEvent    /// `Touch`: Count of consecutive taps    pub fn click_count(&self) -> usize {        match self {            ClickEvent::Keyboard(_) => 1,            ClickEvent::Mouse(event) => event.up.click_count,            ClickEvent::Touch(event) => event.tap_count,        }    }     /// Returns whether the click event is generated by a keyboard event    pub fn is_keyboard(&self) -> bool {        match self {            ClickEvent::Mouse(_) | ClickEvent::Touch(_) => false,            ClickEvent::Keyboard(_) => true,        }    }} /// An enum representing the keyboard button that was pressed for a click event.#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug, Default)]pub enum KeyboardButton {    /// Enter key was clicked    #[default]    Enter,    /// Space key was clicked    Space,} /// An enum representing the mouse button that was pressed.#[derive(Hash, Default, PartialEq, Eq, Copy, Clone, Debug)]pub enum MouseButton {    /// The left mouse button.    #[default]    Left,     /// The right mouse button.    Right,     /// The middle mouse button.    Middle,     /// A navigation button, such as back or forward.    Navigate(NavigationDirection),} impl MouseButton {    /// Get all the mouse buttons in a list.    pub fn all() -> Vec<Self> {        vec![            MouseButton::Left,            MouseButton::Right,            MouseButton::Middle,            MouseButton::Navigate(NavigationDirection::Back),            MouseButton::Navigate(NavigationDirection::Forward),        ]    }} /// A navigation direction, such as back or forward.#[derive(Hash, Default, PartialEq, Eq, Copy, Clone, Debug)]pub enum NavigationDirection {    /// The back button.    #[default]    Back,     /// The forward button.    Forward,} /// A mouse move event from the platform.#[derive(Clone, Debug, Default)]pub struct MouseMoveEvent {    /// The position of the mouse on the window.    pub position: Point<Pixels>,     /// The mouse button that was pressed, if any.    pub pressed_button: Option<MouseButton>,     /// The modifiers that were held down when the mouse was moved.    pub modifiers: Modifiers,} impl Sealed for MouseMoveEvent {}impl InputEvent for MouseMoveEvent {    fn to_platform_input(self) -> PlatformInput {        PlatformInput::MouseMove(self)    }

Lines 1–500 of 878.