Skip to main content

fission_core/
event.rs

1//! Input events consumed by the [`Runtime`](crate::Runtime).
2//!
3//! Platform shells convert native OS events into the types defined here and
4//! pass them to [`Runtime::handle_input`](crate::Runtime::handle_input).
5
6use fission_layout::{LayoutPoint, LayoutSize};
7use serde::{Deserialize, Serialize};
8
9/// Identifies which mouse button or touch produced a pointer event.
10///
11/// # Variants
12///
13/// - `Primary` -- left mouse button or primary touch contact.
14/// - `Secondary` -- right mouse button.
15/// - `Middle` -- middle mouse button (scroll wheel click).
16/// - `Other(u8)` -- auxiliary buttons (back, forward, etc.).
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18pub enum PointerButton {
19    /// Left mouse button or primary touch.
20    Primary,
21    /// Right mouse button.
22    Secondary,
23    /// Middle mouse button.
24    Middle,
25    /// Auxiliary buttons identified by index.
26    Other(u8),
27}
28
29/// A pointer (mouse / touch / stylus) event in layout coordinates.
30///
31/// # Example
32///
33/// ```rust,ignore
34/// let event = InputEvent::Pointer(PointerEvent::Down {
35///     point: LayoutPoint::new(100.0, 200.0),
36///     button: PointerButton::Primary,
37/// });
38/// runtime.handle_input(event, &ir, &layout)?;
39/// ```
40#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
41pub enum PointerEvent {
42    /// A button was pressed at the given point.
43    Down {
44        point: LayoutPoint,
45        button: PointerButton,
46        /// Modifier bitmask (Shift=1, Alt=2, Ctrl=4, Super=8).
47        modifiers: u8,
48    },
49    /// A button was released at the given point.
50    Up {
51        point: LayoutPoint,
52        button: PointerButton,
53        /// Modifier bitmask (Shift=1, Alt=2, Ctrl=4, Super=8).
54        modifiers: u8,
55    },
56    /// The pointer moved (no button state change).
57    Move {
58        point: LayoutPoint,
59        /// Modifier bitmask (Shift=1, Alt=2, Ctrl=4, Super=8).
60        modifiers: u8,
61    },
62    /// A scroll (mouse wheel or trackpad) gesture.
63    Scroll {
64        point: LayoutPoint,
65        /// Scroll delta in layout units (positive = scroll down / right).
66        delta: LayoutPoint,
67        /// Modifier bitmask (Shift=1, Alt=2, Ctrl=4, Super=8).
68        modifiers: u8,
69    },
70}
71
72/// Platform-independent key code for keyboard events.
73///
74/// Named keys map directly to their function. Printable characters use
75/// `Char(char)`.
76///
77/// # Example
78///
79/// ```rust,ignore
80/// let event = InputEvent::Keyboard(KeyEvent::Down {
81///     key_code: KeyCode::Enter,
82///     modifiers: 0,
83/// });
84/// ```
85#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
86pub enum KeyCode {
87    Space,
88    Enter,
89    Escape,
90    Backspace,
91    Delete,
92    Tab,
93    Left,
94    Right,
95    Up,
96    Down,
97    Home,
98    End,
99    PageUp,
100    PageDown,
101    /// A printable character.
102    Char(char),
103}
104
105/// Shift modifier bit.
106pub const MOD_SHIFT: u8 = 1;
107/// Alt/Option modifier bit.
108pub const MOD_ALT: u8 = 2;
109/// Control modifier bit.
110pub const MOD_CTRL: u8 = 4;
111/// Super/Meta/Command modifier bit.
112pub const MOD_SUPER: u8 = 8;
113
114/// A keyboard key press or release event.
115///
116/// The `modifiers` field is a bitmask: bit 0 = Shift, bit 1 = Alt,
117/// bit 2 = Ctrl, bit 3 = Super/Meta.
118#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
119pub enum KeyEvent {
120    /// A key was pressed.
121    Down {
122        key_code: KeyCode,
123        /// Modifier bitmask (Shift=1, Alt=2, Ctrl=4, Super=8).
124        modifiers: u8,
125    },
126    /// A key was released.
127    Up { key_code: KeyCode, modifiers: u8 },
128}
129
130/// Application lifecycle events.
131#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
132pub enum LifecycleEvent {
133    /// The application has finished initialisation.
134    Init,
135    /// The application returned to the foreground.
136    Resume,
137    /// The application moved to the background.
138    Pause,
139    /// The application is about to terminate.
140    Terminate,
141    /// The viewport was resized.
142    Resize { size: LayoutSize },
143}
144
145/// High-level gesture events recognised by the platform.
146#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
147pub enum GestureEvent {
148    /// A single tap (pointer down + up within threshold).
149    Tap { point: LayoutPoint },
150    /// Two taps in quick succession.
151    DoubleTap { point: LayoutPoint },
152    /// A pan/drag gesture began.
153    PanStart { point: LayoutPoint },
154    /// A pan/drag gesture updated.
155    PanUpdate {
156        point: LayoutPoint,
157        delta: LayoutPoint,
158    },
159    /// A pan/drag gesture ended.
160    PanEnd { point: LayoutPoint },
161    /// The pointer was held down for longer than the long-press threshold.
162    LongPress { point: LayoutPoint },
163}
164
165/// The top-level input event type consumed by
166/// [`Runtime::handle_input`](crate::Runtime::handle_input).
167///
168/// Platform shells convert native OS events into `InputEvent` values.
169#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
170pub enum InputEvent {
171    /// Mouse, touch, or stylus events.
172    Pointer(PointerEvent),
173    /// Keyboard key events.
174    Keyboard(KeyEvent),
175    /// Input Method Editor (IME) events for CJK and composed text.
176    Ime(ImeEvent),
177    /// High-level gesture events.
178    Gesture(GestureEvent),
179    /// Application lifecycle transitions.
180    Lifecycle(LifecycleEvent),
181}
182
183/// Input Method Editor events for composed text input (CJK, emoji, etc.).
184#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
185pub enum ImeEvent {
186    /// The IME is composing text (shown as a preview before the user confirms).
187    Preedit { text: String },
188    /// The user confirmed the composed text.
189    Commit { text: String },
190}