1use crate::window::ThemeMode;
4use std::path::PathBuf;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub enum MouseButton {
9 Left,
10 Right,
11 Middle,
12 Other(u16),
13}
14
15impl MouseButton {
16 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#[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#[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, }
103
104#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106pub enum TouchPhase {
107 Started,
108 Moved,
109 Ended,
110 Cancelled,
111}
112
113#[derive(Debug, Clone, PartialEq)]
115pub enum PlatformEvent {
116 WindowResized {
118 width: u32,
119 height: u32,
120 },
121 WindowMoved {
122 x: i32,
123 y: i32,
124 },
125 WindowCloseRequested,
126 WindowFocused(bool),
127 WindowMaximized(bool),
129 ScaleFactorChanged {
130 scale_factor: f32,
131 },
132 ThemeChanged(ThemeMode),
133 RedrawRequested,
134 AnimationTick,
136
137 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 KeyDown {
167 key: KeyCode,
168 modifiers: Modifiers,
169 },
170 KeyUp {
171 key: KeyCode,
172 modifiers: Modifiers,
173 },
174 ReceivedCharacter(char),
175
176 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 Touch {
189 touch_id: u64,
190 phase: TouchPhase,
191 x: f32,
192 y: f32,
193 },
194
195 FileDrop(Vec<PathBuf>),
197
198 TrayIconClicked,
200 TrayMenuItemClicked(u64),
201
202 SingleInstanceTriggered(Vec<String>),
204}
205
206pub 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
242pub 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}