Skip to main content

kova_native_input/
event.rs

1//! Platform independent input events.
2
3use crate::{Keystroke, Modifiers};
4use kova_native_core::Point;
5
6/// A mouse button.
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
8pub enum MouseButton {
9    Left,
10    Right,
11    Middle,
12    Back,
13    Forward,
14    Other(u16),
15}
16
17/// Scroll amount reported by the device.
18#[derive(Clone, Copy, Debug, PartialEq)]
19pub enum ScrollDelta {
20    /// Discrete wheel notches (positive y = scroll content up / wheel away).
21    Lines(Point),
22    /// Precise pixel deltas from touchpads.
23    Pixels(Point),
24}
25
26impl ScrollDelta {
27    /// Converts to logical pixels, using `line_height` for line based deltas.
28    pub fn pixels(&self, line_height: f32) -> Point {
29        match *self {
30            ScrollDelta::Lines(p) => p * line_height,
31            ScrollDelta::Pixels(p) => p,
32        }
33    }
34}
35
36#[derive(Clone, Debug, PartialEq)]
37pub struct MouseDownEvent {
38    pub button: MouseButton,
39    pub position: Point,
40    pub modifiers: Modifiers,
41    /// 1 for single click, 2 for double click, ...
42    pub click_count: u32,
43}
44
45#[derive(Clone, Debug, PartialEq)]
46pub struct MouseUpEvent {
47    pub button: MouseButton,
48    pub position: Point,
49    pub modifiers: Modifiers,
50    pub click_count: u32,
51}
52
53#[derive(Clone, Debug, PartialEq)]
54pub struct MouseMoveEvent {
55    pub position: Point,
56    /// The button held down while moving, if any.
57    pub pressed_button: Option<MouseButton>,
58    pub modifiers: Modifiers,
59}
60
61/// Phase of a pressure-sensitive pointer (for example, a stylus).
62#[derive(Clone, Copy, Debug, PartialEq, Eq)]
63pub enum PenPhase {
64    Started,
65    Moved,
66    Ended,
67    Cancelled,
68}
69
70/// Pressure-sensitive pointer event in logical window pixels.
71#[derive(Clone, Copy, Debug, PartialEq)]
72pub struct PenEvent {
73    /// Touch/pointer identifier supplied by the platform.
74    pub pointer_id: u64,
75    pub phase: PenPhase,
76    pub position: Point,
77    /// Normalized pressure, clamped to `0.0..=1.0`.
78    pub pressure: f32,
79}
80
81#[derive(Clone, Debug, PartialEq)]
82pub struct ScrollWheelEvent {
83    pub position: Point,
84    pub delta: ScrollDelta,
85    pub modifiers: Modifiers,
86}
87
88#[derive(Clone, Debug, PartialEq)]
89pub struct KeyDownEvent {
90    pub keystroke: Keystroke,
91    /// True when generated by key auto-repeat.
92    pub is_repeat: bool,
93}
94
95#[derive(Clone, Debug, PartialEq)]
96pub struct KeyUpEvent {
97    pub keystroke: Keystroke,
98}
99
100/// Input method editor events (composition of CJK text, dead keys, emoji picker...).
101#[derive(Clone, Debug, PartialEq)]
102pub enum ImeEvent {
103    Enabled,
104    /// Composition in progress: text and optional selected byte range within it.
105    Preedit {
106        text: String,
107        cursor: Option<(usize, usize)>,
108    },
109    /// Final text to insert.
110    Commit(String),
111    Disabled,
112}
113
114/// Any input event delivered to a window.
115#[derive(Clone, Debug, PartialEq)]
116pub enum InputEvent {
117    MouseDown(MouseDownEvent),
118    MouseUp(MouseUpEvent),
119    MouseMove(MouseMoveEvent),
120    /// Pressure-aware pointer input from platforms that report touch force.
121    Pen(PenEvent),
122    /// The pointer left the window.
123    MouseExit,
124    ScrollWheel(ScrollWheelEvent),
125    KeyDown(KeyDownEvent),
126    KeyUp(KeyUpEvent),
127    ModifiersChanged(Modifiers),
128    /// Text produced by the keyboard (after layout and dead-key processing).
129    TextInput(String),
130    Ime(ImeEvent),
131}
132
133impl InputEvent {
134    pub fn position(&self) -> Option<Point> {
135        match self {
136            InputEvent::MouseDown(e) => Some(e.position),
137            InputEvent::MouseUp(e) => Some(e.position),
138            InputEvent::MouseMove(e) => Some(e.position),
139            InputEvent::Pen(e) => Some(e.position),
140            InputEvent::ScrollWheel(e) => Some(e.position),
141            _ => None,
142        }
143    }
144
145    pub fn is_keyboard(&self) -> bool {
146        matches!(
147            self,
148            InputEvent::KeyDown(_)
149                | InputEvent::KeyUp(_)
150                | InputEvent::TextInput(_)
151                | InputEvent::Ime(_)
152        )
153    }
154}
155
156/// The phase in which an event handler runs.
157///
158/// Events first travel from the root to the target (`Capture`), then back up
159/// from the target to the root (`Bubble`). Any handler can stop propagation.
160#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
161pub enum DispatchPhase {
162    Capture,
163    Bubble,
164}
165
166/// Mouse cursor shapes.
167#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
168pub enum CursorStyle {
169    #[default]
170    Arrow,
171    PointingHand,
172    IBeam,
173    Crosshair,
174    ClosedHand,
175    OpenHand,
176    NotAllowed,
177    ResizeLeftRight,
178    ResizeUpDown,
179    ResizeNwse,
180    ResizeNesw,
181    Move,
182    Wait,
183    Progress,
184}