kova-native-input 0.1.0

Input event model, keystrokes, keymaps and propagation for Kova Native.
Documentation
//! Platform independent input events.

use crate::{Keystroke, Modifiers};
use kova_native_core::Point;

/// A mouse button.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MouseButton {
    Left,
    Right,
    Middle,
    Back,
    Forward,
    Other(u16),
}

/// Scroll amount reported by the device.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ScrollDelta {
    /// Discrete wheel notches (positive y = scroll content up / wheel away).
    Lines(Point),
    /// Precise pixel deltas from touchpads.
    Pixels(Point),
}

impl ScrollDelta {
    /// Converts to logical pixels, using `line_height` for line based deltas.
    pub fn pixels(&self, line_height: f32) -> Point {
        match *self {
            ScrollDelta::Lines(p) => p * line_height,
            ScrollDelta::Pixels(p) => p,
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct MouseDownEvent {
    pub button: MouseButton,
    pub position: Point,
    pub modifiers: Modifiers,
    /// 1 for single click, 2 for double click, ...
    pub click_count: u32,
}

#[derive(Clone, Debug, PartialEq)]
pub struct MouseUpEvent {
    pub button: MouseButton,
    pub position: Point,
    pub modifiers: Modifiers,
    pub click_count: u32,
}

#[derive(Clone, Debug, PartialEq)]
pub struct MouseMoveEvent {
    pub position: Point,
    /// The button held down while moving, if any.
    pub pressed_button: Option<MouseButton>,
    pub modifiers: Modifiers,
}

#[derive(Clone, Debug, PartialEq)]
pub struct ScrollWheelEvent {
    pub position: Point,
    pub delta: ScrollDelta,
    pub modifiers: Modifiers,
}

#[derive(Clone, Debug, PartialEq)]
pub struct KeyDownEvent {
    pub keystroke: Keystroke,
    /// True when generated by key auto-repeat.
    pub is_repeat: bool,
}

#[derive(Clone, Debug, PartialEq)]
pub struct KeyUpEvent {
    pub keystroke: Keystroke,
}

/// Input method editor events (composition of CJK text, dead keys, emoji picker...).
#[derive(Clone, Debug, PartialEq)]
pub enum ImeEvent {
    Enabled,
    /// Composition in progress: text and optional selected byte range within it.
    Preedit {
        text: String,
        cursor: Option<(usize, usize)>,
    },
    /// Final text to insert.
    Commit(String),
    Disabled,
}

/// Any input event delivered to a window.
#[derive(Clone, Debug, PartialEq)]
pub enum InputEvent {
    MouseDown(MouseDownEvent),
    MouseUp(MouseUpEvent),
    MouseMove(MouseMoveEvent),
    /// The pointer left the window.
    MouseExit,
    ScrollWheel(ScrollWheelEvent),
    KeyDown(KeyDownEvent),
    KeyUp(KeyUpEvent),
    ModifiersChanged(Modifiers),
    /// Text produced by the keyboard (after layout and dead-key processing).
    TextInput(String),
    Ime(ImeEvent),
}

impl InputEvent {
    pub fn position(&self) -> Option<Point> {
        match self {
            InputEvent::MouseDown(e) => Some(e.position),
            InputEvent::MouseUp(e) => Some(e.position),
            InputEvent::MouseMove(e) => Some(e.position),
            InputEvent::ScrollWheel(e) => Some(e.position),
            _ => None,
        }
    }

    pub fn is_keyboard(&self) -> bool {
        matches!(
            self,
            InputEvent::KeyDown(_)
                | InputEvent::KeyUp(_)
                | InputEvent::TextInput(_)
                | InputEvent::Ime(_)
        )
    }
}

/// The phase in which an event handler runs.
///
/// Events first travel from the root to the target (`Capture`), then back up
/// from the target to the root (`Bubble`). Any handler can stop propagation.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DispatchPhase {
    Capture,
    Bubble,
}

/// Mouse cursor shapes.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum CursorStyle {
    #[default]
    Arrow,
    PointingHand,
    IBeam,
    Crosshair,
    ClosedHand,
    OpenHand,
    NotAllowed,
    ResizeLeftRight,
    ResizeUpDown,
    ResizeNwse,
    ResizeNesw,
    Move,
    Wait,
    Progress,
}