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    Slash,
93    Comma,
94    Period,
95    Minus,
96    Equals,
97    BracketLeft,
98    BracketRight,
99    Semicolon,
100    Backquote,
101    Unknown,
102}
103
104/// Keyboard modifier keys active during an event.
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
106pub struct Modifiers {
107    pub shift: bool,
108    pub ctrl: bool,
109    pub alt: bool,
110    pub meta: bool, // Command on macOS, Windows key on Windows
111}
112
113/// Touch phase for mobile and touchscreens.
114#[derive(Debug, Clone, Copy, PartialEq, Eq)]
115pub enum TouchPhase {
116    Started,
117    Moved,
118    Ended,
119    Cancelled,
120}
121
122/// Native platform event emitted to the Strata event loop.
123#[derive(Debug, Clone, PartialEq)]
124pub enum PlatformEvent {
125    // Window lifecycle & geometry
126    WindowResized {
127        width: u32,
128        height: u32,
129    },
130    WindowMoved {
131        x: i32,
132        y: i32,
133    },
134    WindowCloseRequested,
135    WindowFocused(bool),
136    /// Emitted when the window enters or leaves the maximized state.
137    WindowMaximized(bool),
138    ScaleFactorChanged {
139        scale_factor: f32,
140    },
141    ThemeChanged(ThemeMode),
142    RedrawRequested,
143    /// Periodic VSync display frame animation tick for physics and inertial kinetic scrolling.
144    AnimationTick,
145
146    // Cursor / Mouse
147    MouseMove {
148        x: f32,
149        y: f32,
150    },
151    MouseDown {
152        button: MouseButton,
153        x: f32,
154        y: f32,
155    },
156    MouseUp {
157        button: MouseButton,
158        x: f32,
159        y: f32,
160    },
161    DoubleClick {
162        button: MouseButton,
163        x: f32,
164        y: f32,
165    },
166    Scroll {
167        delta_x: f32,
168        delta_y: f32,
169        is_momentum: bool,
170    },
171    CursorEntered,
172    CursorLeft,
173
174    // Keyboard
175    KeyDown {
176        key: KeyCode,
177        modifiers: Modifiers,
178    },
179    KeyUp {
180        key: KeyCode,
181        modifiers: Modifiers,
182    },
183    ReceivedCharacter(char),
184
185    // Input Method Editor (IME) for internationalized / CJK text composition
186    ImePreedit {
187        text: String,
188        cursor: Option<(usize, usize)>,
189    },
190    ImeCommit(String),
191    ImeDeleteSurrounding {
192        offset: i32,
193        n_chars: u32,
194    },
195
196    // Multi-touch
197    Touch {
198        touch_id: u64,
199        phase: TouchPhase,
200        x: f32,
201        y: f32,
202    },
203
204    // File Drag-and-Drop
205    FileDrop(Vec<PathBuf>),
206
207    // Desktop System Tray
208    TrayIconClicked,
209    TrayMenuItemClicked(u64),
210
211    // Single-Instance IPC
212    SingleInstanceTriggered(Vec<String>),
213}
214
215/// Decodes a `file://` URI (or raw local filesystem path) with RFC 3986 percent-encoding into a `PathBuf`.
216pub fn decode_file_uri_to_path(uri: &str) -> Option<PathBuf> {
217    let trimmed = uri.trim();
218    if trimmed.is_empty() || trimmed.starts_with('#') {
219        return None;
220    }
221    let raw_path = if let Some(rest) = trimmed.strip_prefix("file://localhost") {
222        rest
223    } else if let Some(rest) = trimmed.strip_prefix("file://") {
224        rest
225    } else {
226        trimmed
227    };
228    if raw_path.is_empty() {
229        return None;
230    }
231    let bytes = raw_path.as_bytes();
232    let mut decoded = Vec::with_capacity(bytes.len());
233    let mut i = 0;
234    while i < bytes.len() {
235        if bytes[i] == b'%' && i + 2 < bytes.len() {
236            let h1 = (bytes[i + 1] as char).to_digit(16);
237            let h2 = (bytes[i + 2] as char).to_digit(16);
238            if let (Some(d1), Some(d2)) = (h1, h2) {
239                decoded.push(((d1 << 4) | d2) as u8);
240                i += 3;
241                continue;
242            }
243        }
244        decoded.push(bytes[i]);
245        i += 1;
246    }
247    let s = String::from_utf8_lossy(&decoded).into_owned();
248    Some(PathBuf::from(s))
249}
250
251/// Parses an RFC 2483 `text/uri-list` payload into a list of local `PathBuf`s.
252pub fn parse_dropped_uri_list_to_paths(payload: &str) -> Vec<PathBuf> {
253    payload
254        .lines()
255        .filter_map(decode_file_uri_to_path)
256        .collect()
257}