1use crate::pod::{DragEvent, ImePreedit, InputEvent, KeyCode, SwipeDir};
4use crate::solver::Region;
5
6use super::VeloxEngine;
7
8impl VeloxEngine {
9 #[inline(never)]
11 pub fn dispatch_event(&mut self, event: InputEvent) -> bool {
12 match event {
13 InputEvent::PointerDown { x, y } => self.dispatch_pointer_down(x, y),
14 InputEvent::PointerMove { x, y } => self.dispatch_pointer_move(x, y),
15 InputEvent::PointerUp { x, y } => self.dispatch_pointer_up(x, y),
16 InputEvent::PointerSecondaryDown { x, y } => {
17 self.dispatch_pointer_secondary_down(x, y)
18 }
19 InputEvent::PointerSecondaryUp { .. } => false,
20 InputEvent::PointerLeave => self.dispatch_pointer_leave(),
21 InputEvent::PointerCancel => {
22 self.interaction.is_pressed = false;
23 self.has_pressed_hit = false;
24 self.drag_fired_on_down = false;
25 self.press_hold_ms = 0;
26 self.repeat_accum_ms = 0;
27 self.long_press_fired = false;
28 self.long_press_handled = false;
29 self.last_drag_vy = 0;
30 self.interaction_dirty = true;
31 true
32 }
33 InputEvent::PointerKindChanged(kind) => {
34 self.active_pointer_kind = kind;
35 false
36 }
37 InputEvent::Scroll { x, y, delta_y } => self.dispatch_scroll_axis_at(x, y, delta_y, false),
38 InputEvent::ScrollX { x, y, delta_x } => self.dispatch_scroll_axis_at(x, y, delta_x, true),
39 InputEvent::KeyDown(key) => self.dispatch_key(key),
40 InputEvent::KeyUp(_) => false,
41 InputEvent::ModifiersChanged(mods) => {
42 self.modifiers = mods;
43 false
44 }
45 InputEvent::Backspace => self.dispatch_key(KeyCode::Backspace),
46 InputEvent::Delete => self.dispatch_key(KeyCode::Delete),
47 InputEvent::TextCommit(ch) => self.dispatch_key(KeyCode::Char(ch)),
48 InputEvent::ImeEnabled => {
49 self.ime_enabled = true;
50 false
51 }
52 InputEvent::ImeDisabled => {
53 self.ime_enabled = false;
54 if !self.ime_preedit.is_empty() {
55 self.ime_preedit = ImePreedit::empty();
56 self.frame_scope.set_ime_preedit(ImePreedit::empty());
57 self.mark_focused_or_root_dirty();
58 true
59 } else {
60 false
61 }
62 }
63 InputEvent::ImePreedit(preedit) => self.apply_ime_preedit(preedit),
64 InputEvent::NavigateBack => self.dispatch_navigate_back(),
65 InputEvent::SafeAreaChanged(insets) => {
66 self.set_safe_area(insets);
67 true
68 }
69 InputEvent::KeyboardInsetChanged(height) => {
70 self.set_keyboard_inset(height);
71 true
72 }
73 InputEvent::Pinch {
74 delta_scale_x1000, ..
75 } => {
76 self.last_pinch_delta_x1000 = delta_scale_x1000;
77 self.interaction_dirty = true;
78 true
79 }
80 InputEvent::Rotate { delta_deg10, .. } => {
81 self.last_rotate_deg10 = delta_deg10;
82 self.interaction_dirty = true;
83 true
84 }
85 InputEvent::ScrollPixel { x, y, dx, dy } => {
86 let mut handled = false;
87 if dy != 0 {
88 handled |= self.dispatch_scroll_axis_at(x, y, dy, false);
89 }
90 if dx != 0 {
91 handled |= self.dispatch_scroll_axis_at(x, y, dx, true);
92 }
93 handled
94 }
95 InputEvent::FileHovered { x, y } => {
96 self.file_drop_hover = Some((x, y));
97 self.interaction_dirty = true;
98 true
99 }
100 InputEvent::FileDropped { .. } => {
101 self.file_drop_hover = None;
102 self.interaction_dirty = true;
103 true
104 }
105 InputEvent::FileHoverCancelled => {
106 self.file_drop_hover = None;
107 self.interaction_dirty = true;
108 true
109 }
110 InputEvent::SystemThemeChanged { dark } => {
111 if self.system_theme_dark != dark {
112 self.system_theme_dark = dark;
113 self.interaction_dirty = true;
114 true
115 } else {
116 false
117 }
118 }
119 }
120 }
121
122 fn mark_focused_or_root_dirty(&mut self) {
123 self.interaction_dirty = true;
124 if let Some(root_id) = self.root_pod {
125 let focused_idx = self.interaction.focused_index;
126 let owner = self
127 .cached_hit_regions()
128 .iter()
129 .rev()
130 .find(|h| focused_idx.is_some() && h.focus_index == focused_idx)
131 .map(|h| Self::resolve_hit_pod(root_id, h.owner_pod))
132 .unwrap_or(root_id);
133 self.pool.mark_dirty(owner);
134 }
135 }
136
137 fn apply_ime_preedit(&mut self, preedit: ImePreedit) -> bool {
138 if self.ime_preedit == preedit {
139 return false;
140 }
141 self.ime_preedit = preedit;
142 self.frame_scope.set_ime_preedit(preedit);
143 self.mark_focused_or_root_dirty();
144 true
145 }
146
147 #[inline]
149 pub fn ime_preedit(&self) -> &str {
150 self.ime_preedit.as_str()
151 }
152
153 #[inline(always)]
155 pub const fn ime_preedit_state(&self) -> ImePreedit {
156 self.ime_preedit
157 }
158
159 #[inline(always)]
161 pub const fn ime_enabled(&self) -> bool {
162 self.ime_enabled
163 }
164
165 #[inline]
167 pub fn dispatch_ime_preedit(&mut self, text: &str, cursor: Option<(usize, usize)>) -> bool {
168 let preedit = ImePreedit::from_str(text, cursor);
169 self.apply_ime_preedit(preedit)
170 }
171
172 pub fn dispatch_ime_commit(&mut self, text: &str) -> bool {
174 let mut handled = false;
175 if !self.ime_preedit.is_empty() {
176 self.ime_preedit = ImePreedit::empty();
177 self.frame_scope.set_ime_preedit(ImePreedit::empty());
178 self.mark_focused_or_root_dirty();
179 handled = true;
180 }
181 for ch in text.chars() {
182 if self.dispatch_key(KeyCode::Char(ch)) {
183 handled = true;
184 }
185 }
186 handled
187 }
188
189 pub fn focused_bounds(&self) -> Option<Region> {
192 let hits = self.cached_hit_regions();
193 if let Some(fidx) = self.interaction.focused_index {
194 if let Some(hit) = hits.iter().rev().find(|h| h.focus_index == Some(fidx)) {
195 return Some(hit.bounds);
196 }
197 }
198 hits.iter()
200 .rev()
201 .find(|h| h.on_key_input().is_some())
202 .map(|h| h.bounds)
203 }
204
205 pub fn has_focused_text_input(&self) -> bool {
207 let hits = self.cached_hit_regions();
208 let focused_idx = self.interaction.focused_index;
209 hits.iter().rev().any(|h| {
210 h.on_key_input().is_some()
211 && (focused_idx.is_none() || h.focus_index == focused_idx)
212 })
213 }
214
215 #[inline(never)]
218 pub fn dispatch_navigate_back(&mut self) -> bool {
219 let Some(root_id) = self.root_pod else {
220 return false;
221 };
222 self.ensure_hits_available();
223 let hits = self.cached_hit_regions();
224 for hit in hits.iter().rev() {
225 if let Some(cb) = hit.on_navigate_back() {
226 let cx = hit.bounds.x + hit.bounds.w / 2;
227 let cy = hit.bounds.y + hit.bounds.h / 2;
228 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
229 {
230 let mut acx = self.make_action_scope(owner, (cx, cy), DragEvent::default());
231 cb(&mut acx);
232 }
233 self.flush_async_after_event();
234 self.interaction_dirty = true;
235 return true;
236 }
237 }
238 let cx = self.viewport.x + self.viewport.w / 2;
239 let cy = self.viewport.y + self.viewport.h / 2;
240 self.dispatch_swipe(cx, cy, SwipeDir::Right)
241 }
242
243
244 #[inline(never)]
246 pub fn dispatch_key(&mut self, key: KeyCode) -> bool {
247 let Some(root_id) = self.root_pod else {
248 return false;
249 };
250 self.ensure_hits_available();
251 let hits = self.cached_hit_regions();
252
253 if matches!(key, KeyCode::Escape) {
255 for hit in hits.iter().rev() {
256 if let Some(cb) = hit.on_navigate_back() {
257 let cx_px = hit.bounds.x + (hit.bounds.w >> 1);
258 let cy_py = hit.bounds.y + (hit.bounds.h >> 1);
259 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
260 {
261 let mut acx =
262 self.make_action_scope(owner, (cx_px, cy_py), DragEvent::default());
263 cb(&mut acx);
264 }
265 self.flush_async_after_event();
266 self.interaction_dirty = true;
267 return true;
268 }
269 }
270 }
271
272 let is_nav_key = matches!(
275 key,
276 KeyCode::Up
277 | KeyCode::Down
278 | KeyCode::Left
279 | KeyCode::Right
280 | KeyCode::Home
281 | KeyCode::End
282 | KeyCode::Enter
283 | KeyCode::Space
284 );
285 if is_nav_key {
286 let focused_idx = self.interaction.focused_index;
287 if let Some(fidx) = focused_idx {
289 for hit in hits.iter().rev() {
290 if hit.focus_index == Some(fidx) {
291 if let Some(on_nav) = hit.on_key_nav() {
292 let cx_px = hit.bounds.x + (hit.bounds.w >> 1);
293 let cy_py = hit.bounds.y + (hit.bounds.h >> 1);
294 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
295 {
296 let mut acx = self.make_action_scope(
297 owner,
298 (cx_px, cy_py),
299 DragEvent::default(),
300 );
301 on_nav(key, &mut acx);
302 }
303 self.flush_async_after_event();
304 self.interaction_dirty = true;
305 return true;
306 }
307 }
308 }
309 }
310 for hit in hits.iter().rev() {
312 if hit.occlude {
313 if let Some(on_nav) = hit.on_key_nav() {
314 let cx_px = hit.bounds.x + (hit.bounds.w >> 1);
315 let cy_py = hit.bounds.y + (hit.bounds.h >> 1);
316 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
317 {
318 let mut acx =
319 self.make_action_scope(owner, (cx_px, cy_py), DragEvent::default());
320 on_nav(key, &mut acx);
321 }
322 self.flush_async_after_event();
323 self.interaction_dirty = true;
324 return true;
325 }
326 break;
327 }
328 }
329 }
330
331 let is_edit_key = matches!(
333 key,
334 KeyCode::Char(_)
335 | KeyCode::Space
336 | KeyCode::Backspace
337 | KeyCode::Delete
338 | KeyCode::Enter
339 | KeyCode::Tab
340 | KeyCode::BackTab
341 | KeyCode::Up
342 | KeyCode::Down
343 | KeyCode::Left
344 | KeyCode::Right
345 | KeyCode::Home
346 | KeyCode::End
347 | KeyCode::PageUp
348 | KeyCode::PageDown
349 | KeyCode::Escape
350 );
351 if is_edit_key {
352 let focused_idx = self.interaction.focused_index;
353 let require_explicit_focus = matches!(
355 key,
356 KeyCode::Tab | KeyCode::BackTab | KeyCode::Space | KeyCode::Escape
357 );
358 let cur_mods = self.modifiers;
359 for hit in hits.iter().rev() {
360 let matches_focus = if require_explicit_focus {
361 focused_idx.is_some() && hit.focus_index == focused_idx
362 } else {
363 focused_idx.is_none() || hit.focus_index == focused_idx
364 };
365 if matches_focus {
366 if let Some(on_key_in) = hit.on_key_input() {
367 let cx_px = hit.bounds.x + (hit.bounds.w >> 1);
368 let cy_py = hit.bounds.y + (hit.bounds.h >> 1);
369 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
370 {
371 let mut acx = self
372 .make_action_scope(owner, (cx_px, cy_py), DragEvent::default())
373 .with_modifiers(cur_mods);
374 on_key_in(key, &mut acx);
375 }
376 self.flush_async_after_event();
377 self.interaction_dirty = true;
378 return true;
379 }
380 }
381 }
382 if matches!(
383 key,
384 KeyCode::Char(_)
385 | KeyCode::Backspace
386 | KeyCode::Delete
387 | KeyCode::Home
388 | KeyCode::End
389 | KeyCode::PageUp
390 | KeyCode::PageDown
391 ) {
392 return false;
393 }
394 }
395
396 let explicit_max = hits
397 .iter()
398 .filter_map(|h| h.focus_index)
399 .max()
400 .map(|m| m + 1)
401 .unwrap_or(0);
402 let clickable_count = hits.iter().filter(|h| h.on_click().is_some()).count() as u16;
403 let max_focus = if explicit_max > 0 {
404 explicit_max
405 } else {
406 clickable_count
407 };
408
409 match key {
410 KeyCode::Tab | KeyCode::Down | KeyCode::Right => {
411 if max_focus == 0 {
412 return false;
413 }
414 let next = match self.interaction.focused_index {
415 Some(cur) => (cur + 1) % max_focus,
416 None => 0,
417 };
418 self.interaction.focused_index = Some(next);
419 self.interaction.focus_visible = true;
420 self.interaction_dirty = true;
421 return true;
422 }
423 KeyCode::BackTab | KeyCode::Up | KeyCode::Left => {
424 if max_focus == 0 {
425 return false;
426 }
427 let prev = match self.interaction.focused_index {
428 Some(cur) => (cur + max_focus - 1) % max_focus,
429 None => max_focus - 1,
430 };
431 self.interaction.focused_index = Some(prev);
432 self.interaction.focus_visible = true;
433 self.interaction_dirty = true;
434 return true;
435 }
436 KeyCode::Enter | KeyCode::Space => {
437 if max_focus == 0 {
438 return false;
439 }
440 let fidx = match self.interaction.focused_index {
441 Some(idx) => idx,
442 None => {
443 self.interaction.focused_index = Some(0);
444 self.interaction.focus_visible = true;
445 self.interaction_dirty = true;
446 0
447 }
448 };
449 if explicit_max > 0 {
450 for hit in hits.iter().rev() {
451 if hit.focus_index == Some(fidx) {
452 if let Some(on_click) = hit.on_click() {
453 let cx_px = hit.bounds.x + (hit.bounds.w >> 1);
454 let cy_py = hit.bounds.y + (hit.bounds.h >> 1);
455 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
456 {
457 let mut acx = self.make_action_scope(
458 owner,
459 (cx_px, cy_py),
460 DragEvent::default(),
461 );
462 on_click(&mut acx);
463 }
464 self.flush_async_after_event();
465 self.interaction_dirty = true;
466 return true;
467 }
468 }
469 }
470 } else if let Some(hit) = hits
471 .iter()
472 .filter(|h| h.on_click().is_some())
473 .nth(fidx as usize)
474 {
475 if let Some(on_click) = hit.on_click() {
476 let cx_px = hit.bounds.x + (hit.bounds.w >> 1);
477 let cy_py = hit.bounds.y + (hit.bounds.h >> 1);
478 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
479 {
480 let mut acx =
481 self.make_action_scope(owner, (cx_px, cy_py), DragEvent::default());
482 on_click(&mut acx);
483 }
484 self.flush_async_after_event();
485 self.interaction_dirty = true;
486 return true;
487 }
488 }
489 }
490 KeyCode::Escape => {
491 if self.interaction.focused_index.is_some() || self.interaction.focus_visible {
492 self.interaction.focused_index = None;
493 self.interaction.focus_visible = false;
494 self.interaction_dirty = true;
495 return true;
496 }
497 }
498 KeyCode::Char(_)
499 | KeyCode::Backspace
500 | KeyCode::Delete
501 | KeyCode::Home
502 | KeyCode::End
503 | KeyCode::PageUp
504 | KeyCode::PageDown => {}
505 }
506 false
507 }
508
509 pub fn dispatch_key_up(&mut self, _key: KeyCode) -> bool {
511 false
512 }
513
514}