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 },
47 /// A button was released at the given point.
48 Up {
49 point: LayoutPoint,
50 button: PointerButton,
51 },
52 /// The pointer moved (no button state change).
53 Move {
54 point: LayoutPoint,
55 },
56 /// A scroll (mouse wheel or trackpad) gesture.
57 Scroll {
58 point: LayoutPoint,
59 /// Scroll delta in layout units (positive = scroll down / right).
60 delta: LayoutPoint,
61 },
62}
63
64/// Platform-independent key code for keyboard events.
65///
66/// Named keys map directly to their function. Printable characters use
67/// `Char(char)`.
68///
69/// # Example
70///
71/// ```rust,ignore
72/// let event = InputEvent::Keyboard(KeyEvent::Down {
73/// key_code: KeyCode::Enter,
74/// modifiers: 0,
75/// });
76/// ```
77#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
78pub enum KeyCode {
79 Space,
80 Enter,
81 Escape,
82 Backspace,
83 Tab,
84 Left,
85 Right,
86 Up,
87 Down,
88 Home,
89 End,
90 /// A printable character.
91 Char(char),
92}
93
94/// A keyboard key press or release event.
95///
96/// The `modifiers` field is a bitmask: bit 0 = Shift, bit 1 = Ctrl,
97/// bit 2 = Alt, bit 3 = Super/Meta.
98#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
99pub enum KeyEvent {
100 /// A key was pressed.
101 Down {
102 key_code: KeyCode,
103 /// Modifier bitmask (Shift=1, Ctrl=2, Alt=4, Super=8).
104 modifiers: u8,
105 },
106 /// A key was released.
107 Up {
108 key_code: KeyCode,
109 modifiers: u8,
110 },
111}
112
113/// Application lifecycle events.
114#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
115pub enum LifecycleEvent {
116 /// The application has finished initialisation.
117 Init,
118 /// The application returned to the foreground.
119 Resume,
120 /// The application moved to the background.
121 Pause,
122 /// The application is about to terminate.
123 Terminate,
124 /// The viewport was resized.
125 Resize { size: LayoutSize },
126}
127
128/// High-level gesture events recognised by the platform.
129#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
130pub enum GestureEvent {
131 /// A single tap (pointer down + up within threshold).
132 Tap { point: LayoutPoint },
133 /// Two taps in quick succession.
134 DoubleTap { point: LayoutPoint },
135 /// A pan/drag gesture began.
136 PanStart { point: LayoutPoint },
137 /// A pan/drag gesture updated.
138 PanUpdate { point: LayoutPoint, delta: LayoutPoint },
139 /// A pan/drag gesture ended.
140 PanEnd { point: LayoutPoint },
141 /// The pointer was held down for longer than the long-press threshold.
142 LongPress { point: LayoutPoint },
143}
144
145/// The top-level input event type consumed by
146/// [`Runtime::handle_input`](crate::Runtime::handle_input).
147///
148/// Platform shells convert native OS events into `InputEvent` values.
149#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
150pub enum InputEvent {
151 /// Mouse, touch, or stylus events.
152 Pointer(PointerEvent),
153 /// Keyboard key events.
154 Keyboard(KeyEvent),
155 /// Input Method Editor (IME) events for CJK and composed text.
156 Ime(ImeEvent),
157 /// High-level gesture events.
158 Gesture(GestureEvent),
159 /// Application lifecycle transitions.
160 Lifecycle(LifecycleEvent),
161}
162
163/// Input Method Editor events for composed text input (CJK, emoji, etc.).
164#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
165pub enum ImeEvent {
166 /// The IME is composing text (shown as a preview before the user confirms).
167 Preedit { text: String },
168 /// The user confirmed the composed text.
169 Commit { text: String },
170}