Skip to main content

strata_platform/
events.rs

1//! Platform Event Model: Mouse, Keyboard, Touch, Window State, and File Drag-and-Drop.
2
3use crate::window::ThemeMode;
4use std::path::PathBuf;
5
6/// Mouse button identifiers.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub enum MouseButton {
9    Left,
10    Right,
11    Middle,
12    Other(u16),
13}
14
15impl MouseButton {
16    /// Returns the standard W3C DOM button index: 0 for Left, 1 for Middle, 2 for Right, or custom.
17    pub fn to_button_index(&self) -> i16 {
18        match self {
19            MouseButton::Left => 0,
20            MouseButton::Middle => 1,
21            MouseButton::Right => 2,
22            MouseButton::Other(b) => *b as i16,
23        }
24    }
25}
26
27/// Keyboard key codes.
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
29pub enum KeyCode {
30    KeyA,
31    KeyB,
32    KeyC,
33    KeyD,
34    KeyE,
35    KeyF,
36    KeyG,
37    KeyH,
38    KeyI,
39    KeyJ,
40    KeyK,
41    KeyL,
42    KeyM,
43    KeyN,
44    KeyO,
45    KeyP,
46    KeyQ,
47    KeyR,
48    KeyS,
49    KeyT,
50    KeyU,
51    KeyV,
52    KeyW,
53    KeyX,
54    KeyY,
55    KeyZ,
56    Digit0,
57    Digit1,
58    Digit2,
59    Digit3,
60    Digit4,
61    Digit5,
62    Digit6,
63    Digit7,
64    Digit8,
65    Digit9,
66    Escape,
67    Enter,
68    Tab,
69    Space,
70    Backspace,
71    Delete,
72    ArrowUp,
73    ArrowDown,
74    ArrowLeft,
75    ArrowRight,
76    Home,
77    End,
78    PageUp,
79    PageDown,
80    F1,
81    F2,
82    F3,
83    F4,
84    F5,
85    F6,
86    F7,
87    F8,
88    F9,
89    F10,
90    F11,
91    F12,
92    Unknown,
93}
94
95/// Keyboard modifier keys active during an event.
96#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
97pub struct Modifiers {
98    pub shift: bool,
99    pub ctrl: bool,
100    pub alt: bool,
101    pub meta: bool, // Command on macOS, Windows key on Windows
102}
103
104/// Touch phase for mobile and touchscreens.
105#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106pub enum TouchPhase {
107    Started,
108    Moved,
109    Ended,
110    Cancelled,
111}
112
113/// Native platform event emitted to the Strata event loop.
114#[derive(Debug, Clone, PartialEq)]
115pub enum PlatformEvent {
116    // Window lifecycle & geometry
117    WindowResized {
118        width: u32,
119        height: u32,
120    },
121    WindowMoved {
122        x: i32,
123        y: i32,
124    },
125    WindowCloseRequested,
126    WindowFocused(bool),
127    /// Emitted when the window enters or leaves the maximized state.
128    WindowMaximized(bool),
129    ScaleFactorChanged {
130        scale_factor: f32,
131    },
132    ThemeChanged(ThemeMode),
133    RedrawRequested,
134    /// Periodic VSync display frame animation tick for physics and inertial kinetic scrolling.
135    AnimationTick,
136
137    // Cursor / Mouse
138    MouseMove {
139        x: f32,
140        y: f32,
141    },
142    MouseDown {
143        button: MouseButton,
144        x: f32,
145        y: f32,
146    },
147    MouseUp {
148        button: MouseButton,
149        x: f32,
150        y: f32,
151    },
152    DoubleClick {
153        button: MouseButton,
154        x: f32,
155        y: f32,
156    },
157    Scroll {
158        delta_x: f32,
159        delta_y: f32,
160        is_momentum: bool,
161    },
162    CursorEntered,
163    CursorLeft,
164
165    // Keyboard
166    KeyDown {
167        key: KeyCode,
168        modifiers: Modifiers,
169    },
170    KeyUp {
171        key: KeyCode,
172        modifiers: Modifiers,
173    },
174    ReceivedCharacter(char),
175
176    // Input Method Editor (IME) for internationalized / CJK text composition
177    ImePreedit {
178        text: String,
179        cursor: Option<(usize, usize)>,
180    },
181    ImeCommit(String),
182    ImeDeleteSurrounding {
183        offset: i32,
184        n_chars: u32,
185    },
186
187    // Multi-touch
188    Touch {
189        touch_id: u64,
190        phase: TouchPhase,
191        x: f32,
192        y: f32,
193    },
194
195    // File Drag-and-Drop
196    FileDrop(Vec<PathBuf>),
197
198    // Desktop System Tray
199    TrayIconClicked,
200    TrayMenuItemClicked(u64),
201
202    // Single-Instance IPC
203    SingleInstanceTriggered(Vec<String>),
204}
205
206/// Decodes a `file://` URI (or raw local filesystem path) with RFC 3986 percent-encoding into a `PathBuf`.
207pub fn decode_file_uri_to_path(uri: &str) -> Option<PathBuf> {
208    let trimmed = uri.trim();
209    if trimmed.is_empty() || trimmed.starts_with('#') {
210        return None;
211    }
212    let raw_path = if let Some(rest) = trimmed.strip_prefix("file://localhost") {
213        rest
214    } else if let Some(rest) = trimmed.strip_prefix("file://") {
215        rest
216    } else {
217        trimmed
218    };
219    if raw_path.is_empty() {
220        return None;
221    }
222    let bytes = raw_path.as_bytes();
223    let mut decoded = Vec::with_capacity(bytes.len());
224    let mut i = 0;
225    while i < bytes.len() {
226        if bytes[i] == b'%' && i + 2 < bytes.len() {
227            let h1 = (bytes[i + 1] as char).to_digit(16);
228            let h2 = (bytes[i + 2] as char).to_digit(16);
229            if let (Some(d1), Some(d2)) = (h1, h2) {
230                decoded.push(((d1 << 4) | d2) as u8);
231                i += 3;
232                continue;
233            }
234        }
235        decoded.push(bytes[i]);
236        i += 1;
237    }
238    let s = String::from_utf8_lossy(&decoded).into_owned();
239    Some(PathBuf::from(s))
240}
241
242/// Parses an RFC 2483 `text/uri-list` payload into a list of local `PathBuf`s.
243pub fn parse_dropped_uri_list_to_paths(payload: &str) -> Vec<PathBuf> {
244    payload
245        .lines()
246        .filter_map(decode_file_uri_to_path)
247        .collect()
248}