1use crate::pod::{CursorShape, DragEvent, PointerKind, SwipeDir};
4
5use super::VeloxEngine;
6
7impl VeloxEngine {
8 pub fn cursor_shape(&self) -> CursorShape {
10 let hits = self.cached_hit_regions();
11 if hits.is_empty() {
12 return CursorShape::Default;
13 }
14
15 if self.interaction.is_pressed && self.has_pressed_hit {
17 if let Some(hit) = hits
18 .iter()
19 .rev()
20 .find(|h| h.id == Some(self.pressed_hit_id))
21 {
22 if hit.on_drag().is_some() {
23 return match hit.cursor {
24 Some(
25 c @ (CursorShape::Pointer
26 | CursorShape::Text
27 | CursorShape::ResizeEW
28 | CursorShape::ResizeNS
29 | CursorShape::ResizeNWSE
30 | CursorShape::ResizeNESW
31 | CursorShape::Move
32 | CursorShape::Crosshair),
33 ) => c,
34 Some(CursorShape::NotAllowed) => CursorShape::NotAllowed,
35 _ => CursorShape::Grabbing,
36 };
37 }
38 }
39 }
40
41 let Some((px, py)) = self.interaction.pointer_pos else {
42 return CursorShape::Default;
43 };
44
45 for hit in hits.iter().rev() {
47 let effective = hit.bounds.intersect(hit.clip);
48 if effective.contains(px, py) {
49 if let Some(explicit) = hit.cursor {
50 if explicit == CursorShape::Grab && self.interaction.is_pressed {
51 return CursorShape::Grabbing;
52 }
53 return explicit;
54 }
55 if hit.on_key_input().is_some() {
56 return CursorShape::Text;
57 }
58 if hit.on_drag().is_some() {
59 return if self.interaction.is_pressed {
60 CursorShape::Grabbing
61 } else {
62 CursorShape::Grab
63 };
64 }
65 if hit.on_click().is_some() || hit.on_long_press().is_some() {
66 return CursorShape::Pointer;
67 }
68 if hit.occlude {
69 return CursorShape::Default;
70 }
71 }
72 }
73 CursorShape::Default
74 }
75
76 #[inline]
78 pub fn dispatch_pointer_down_with_kind(
79 &mut self,
80 px: i16,
81 py: i16,
82 kind: PointerKind,
83 ) -> bool {
84 self.active_pointer_kind = kind;
85 self.dispatch_pointer_down(px, py)
86 }
87
88 #[inline]
90 pub fn dispatch_mouse_down(&mut self, px: i16, py: i16) -> bool {
91 self.dispatch_pointer_down_with_kind(px, py, PointerKind::Mouse)
92 }
93
94 #[inline]
96 pub fn dispatch_touch_down(&mut self, px: i16, py: i16) -> bool {
97 self.dispatch_pointer_down_with_kind(px, py, PointerKind::Touch)
98 }
99
100 #[inline(never)]
102 pub fn dispatch_pointer_down(&mut self, px: i16, py: i16) -> bool {
103 let Some(root_id) = self.root_pod else {
104 return false;
105 };
106 self.ensure_hits_available();
107 self.interaction.pointer_pos = Some((px, py));
108 self.interaction.is_pressed = true;
109 self.interaction.focus_visible = false;
111 self.press_start_x = px;
112 self.press_start_y = py;
113 self.has_pressed_hit = false;
114 self.drag_fired_on_down = false;
115 self.press_hold_ms = 0;
116 self.repeat_accum_ms = 0;
117 self.long_press_fired = false;
118 self.long_press_handled = false;
119 self.last_drag_vy = 0;
120 self.interaction_dirty = true;
121
122 let mut hit_focused: Option<u16> = None;
123 let mut matched_passive = false;
124 let hits = self.cached_hit_regions();
125 for hit in hits.iter().rev() {
126 let effective = hit.bounds.intersect(hit.clip);
127 if effective.contains(px, py) {
128 if hit_focused.is_none() {
129 if let Some(fidx) = hit.focus_index {
130 hit_focused = Some(fidx);
131 self.interaction.focused_index = Some(fidx);
132 }
133 }
134 if self.allows_drag_scroll() {
135 if let Some((scroll_id, max_s)) = hit.scroll_target {
136 self.motion.set_scroll_inertia(scroll_id, 0, max_s);
137 }
138 }
139 if let Some(on_drag) = hit.on_drag() {
140 if let Some(hid) = hit.id {
141 self.pressed_hit_id = hid;
142 self.has_pressed_hit = true;
143 }
144 self.drag_fired_on_down = true;
145 let drag_ev = DragEvent {
146 x: px,
147 y: py,
148 local_x: (px - hit.bounds.x).clamp(0, hit.bounds.w),
149 local_y: (py - hit.bounds.y).clamp(0, hit.bounds.h),
150 width: hit.bounds.w.max(1),
151 height: hit.bounds.h.max(1),
152 delta_x: 0,
153 delta_y: 0,
154 };
155 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
156 let mods = self.modifiers;
157 {
158 let mut acx = self
159 .make_action_scope(owner, (px, py), drag_ev)
160 .with_modifiers(mods);
161 on_drag(&mut acx);
162 }
163 self.flush_async_after_event();
164 return true;
165 }
166 if hit.on_click().is_some() || hit.on_long_press().is_some() {
167 if let Some(hid) = hit.id {
168 self.pressed_hit_id = hid;
169 self.has_pressed_hit = true;
170 }
171 return true;
172 }
173 if hit.active_bg_or_focus() {
174 matched_passive = true;
175 }
176 if hit.occlude {
177 if hit_focused.is_none() {
178 self.interaction.focused_index = None;
179 }
180 return true;
181 }
182 }
183 }
184 if hit_focused.is_none() {
186 self.interaction.focused_index = None;
187 }
188 matched_passive
189 }
190
191 #[inline(never)]
193 pub fn dispatch_pointer_secondary_down(&mut self, px: i16, py: i16) -> bool {
194 let Some(root_id) = self.root_pod else {
195 return false;
196 };
197 self.ensure_hits_available();
198 self.interaction.pointer_pos = Some((px, py));
199 self.interaction.focus_visible = false;
200 self.interaction_dirty = true;
201
202 let hits = self.cached_hit_regions();
203 for hit in hits.iter().rev() {
204 let effective = hit.bounds.intersect(hit.clip);
205 if effective.contains(px, py) {
206 if let Some(fidx) = hit.focus_index {
207 self.interaction.focused_index = Some(fidx);
208 }
209 if let Some(on_secondary) = hit.on_secondary_click() {
210 let drag_ev = DragEvent {
211 x: px,
212 y: py,
213 local_x: (px - hit.bounds.x).clamp(0, hit.bounds.w),
214 local_y: (py - hit.bounds.y).clamp(0, hit.bounds.h),
215 width: hit.bounds.w.max(1),
216 height: hit.bounds.h.max(1),
217 delta_x: 0,
218 delta_y: 0,
219 };
220 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
221 let mods = self.modifiers;
222 {
223 let mut acx = self
224 .make_action_scope(owner, (px, py), drag_ev)
225 .with_modifiers(mods);
226 on_secondary(&mut acx);
227 }
228 self.flush_async_after_event();
229 return true;
230 }
231 if hit.occlude {
232 return true;
233 }
234 }
235 }
236 false
237 }
238
239 #[inline(never)]
241 pub fn dispatch_pointer_move(&mut self, px: i16, py: i16) -> bool {
242 let Some(root_id) = self.root_pod else {
243 return false;
244 };
245 self.ensure_hits_available();
246 let prev_pos = self.interaction.pointer_pos;
247 let (prev_x, prev_y) = prev_pos.unwrap_or((px, py));
248 let dx = px.saturating_sub(prev_x);
249 let dy = py.saturating_sub(prev_y);
250 self.interaction.pointer_pos = Some((px, py));
251
252 let hits = self.cached_hit_regions();
253 if !self.interaction.is_pressed {
254 let hover_changed = hits.iter().any(|h| {
255 if !h.has_hover_bg {
256 return false;
257 }
258 let eff = h.bounds.intersect(h.clip);
259 let was_in = prev_pos.is_some_and(|(ox, oy)| eff.contains(ox, oy));
260 was_in != eff.contains(px, py)
261 });
262 if hover_changed {
263 self.interaction_dirty = true;
264 }
265 return false;
266 }
267 if dx == 0 && dy == 0 {
268 return false;
269 }
270
271 let (sx, sy) = (self.press_start_x, self.press_start_y);
272 let slop = self.gesture_config.drag_slop_px;
273 if (px - sx).abs() > slop || (py - sy).abs() > slop {
274 self.press_hold_ms = 0;
275 self.repeat_accum_ms = 0;
276 self.long_press_fired = true;
277 }
278
279 for hit in hits.iter().rev() {
281 let effective = hit.bounds.intersect(hit.clip);
282 let matches_pressed = self.has_pressed_hit && hit.id == Some(self.pressed_hit_id);
283 if matches_pressed || effective.contains(sx, sy) {
284 if let Some(on_drag) = hit.on_drag() {
285 let drag_ev = DragEvent {
286 x: px,
287 y: py,
288 local_x: (px - hit.bounds.x).clamp(0, hit.bounds.w),
289 local_y: (py - hit.bounds.y).clamp(0, hit.bounds.h),
290 width: hit.bounds.w.max(1),
291 height: hit.bounds.h.max(1),
292 delta_x: dx,
293 delta_y: dy,
294 };
295 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
296 let mods = self.modifiers;
297 {
298 let mut acx = self
299 .make_action_scope(owner, (px, py), drag_ev)
300 .with_modifiers(mods);
301 on_drag(&mut acx);
302 }
303 self.flush_async_after_event();
304 self.interaction_dirty = true;
305 return true;
306 }
307 if hit.occlude && effective.contains(sx, sy) {
308 break;
309 }
310 }
311 }
312
313 if !self.allows_drag_scroll() {
316 return false;
317 }
318 let mut scroll_to_apply: Option<((u32, i16), bool)> = None;
319 for hit in hits.iter().rev() {
320 let effective = hit.bounds.intersect(hit.clip);
321 if effective.contains(sx, sy) {
322 if let Some(target) = hit.scroll_target {
323 scroll_to_apply = Some((target, hit.scroll_horizontal));
324 break;
325 }
326 if hit.occlude {
327 break;
328 }
329 }
330 }
331
332 if let Some(((scroll_id, max_s), is_horiz)) = scroll_to_apply {
333 let delta = if is_horiz { dx } else { dy };
334 if delta != 0 {
335 let delta_scroll = -delta;
336 self.last_drag_vy = delta_scroll;
337 self.motion.set_scroll_inertia(scroll_id, 0, max_s);
338 let cur = self.motion.scroll_offset(scroll_id);
339 let next = cur.saturating_add(delta_scroll).clamp(0, max_s.max(0));
340 self.motion.set_scroll_offset(scroll_id, next);
341 self.interaction_dirty = true;
342 return true;
343 }
344 }
345 false
346 }
347
348 #[inline(never)]
350 pub fn dispatch_pointer_leave(&mut self) -> bool {
351 let Some((prev_x, prev_y)) = self.interaction.pointer_pos.take() else {
352 return false;
353 };
354 if self.root_pod.is_none() {
355 return false;
356 }
357 self.ensure_hits_available();
358 let hits = self.cached_hit_regions();
359 let was_hovered = hits.iter().any(|h| {
360 h.has_hover_bg && h.bounds.intersect(h.clip).contains(prev_x, prev_y)
361 });
362 if was_hovered {
363 self.interaction_dirty = true;
364 }
365 was_hovered
366 }
367
368 #[inline(never)]
370 pub fn dispatch_pointer_up(&mut self, px: i16, py: i16) -> bool {
371 self.ensure_hits_available();
372 self.interaction.pointer_pos = Some((px, py));
373 let was_pressed = self.interaction.is_pressed;
374 let was_long_press_handled = self.long_press_handled;
375 let expected_id = if self.has_pressed_hit {
376 Some(self.pressed_hit_id)
377 } else {
378 None
379 };
380 self.has_pressed_hit = false;
381 let was_drag_fired = self.drag_fired_on_down;
382 self.drag_fired_on_down = false;
383 self.interaction.is_pressed = false;
384 self.press_hold_ms = 0;
385 self.repeat_accum_ms = 0;
386 self.long_press_fired = false;
387 self.long_press_handled = false;
388 self.interaction_dirty = true;
389
390 if !was_pressed {
391 return false;
392 }
393
394 let (sx, sy) = (self.press_start_x, self.press_start_y);
395 let dx = px - sx;
396 let dy = py - sy;
397 let slop = self.gesture_config.drag_slop_px;
398 let moved_far = dx.abs() > slop || dy.abs() > slop;
399
400 if !moved_far {
401 if was_long_press_handled {
402 return true;
403 }
404 return self.trigger_click_internal(px, py, true, expected_id, !was_drag_fired);
405 }
406
407 if self.allows_drag_scroll() {
409 let swipe_th = self.gesture_config.swipe_threshold_px;
410 let edge_w = self.gesture_config.edge_back_width_px;
411 if dx.abs() >= swipe_th || dy.abs() >= swipe_th {
412 let dir = if dx.abs() >= dy.abs() {
413 if dx > 0 {
414 SwipeDir::Right
415 } else {
416 SwipeDir::Left
417 }
418 } else if dy > 0 {
419 SwipeDir::Down
420 } else {
421 SwipeDir::Up
422 };
423 if self.dispatch_swipe(sx, sy, dir) {
424 self.last_drag_vy = 0;
425 return true;
426 }
427 if matches!(self.active_pointer_kind, PointerKind::Touch)
429 && sx <= self.viewport.x + edge_w
430 && dx >= edge_w
431 && dx.abs() > dy.abs() * 2
432 {
433 if self.dispatch_navigate_back() {
434 self.last_drag_vy = 0;
435 return true;
436 }
437 }
438 }
439
440 if self.last_drag_vy.abs() > 1 {
442 let hits = self.cached_hit_regions();
443 let mut scroll_target: Option<(u32, i16)> = None;
444 for hit in hits.iter().rev() {
445 if hit.bounds.intersect(hit.clip).contains(sx, sy) {
446 if let Some(t) = hit.scroll_target {
447 scroll_target = Some(t);
448 break;
449 }
450 if hit.occlude {
451 break;
452 }
453 }
454 }
455 if let Some((scroll_id, max_s)) = scroll_target {
456 self.motion
457 .set_scroll_inertia(scroll_id, self.last_drag_vy, max_s);
458 self.last_drag_vy = 0;
459 return true;
460 }
461 }
462 }
463 self.last_drag_vy = 0;
464 false
465 }
466
467 #[inline(never)]
469 pub fn dispatch_swipe(&mut self, x: i16, y: i16, dir: SwipeDir) -> bool {
470 let Some(root_id) = self.root_pod else {
471 return false;
472 };
473 self.ensure_hits_available();
474 let hits = self.cached_hit_regions();
475 let mut any_handled = false;
476 for hit in hits.iter().rev() {
477 if hit.bounds.intersect(hit.clip).contains(x, y) {
478 if let Some(cb) = hit.on_swipe() {
479 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
480 let stopped = {
481 let mut action = self.make_action_scope(owner, (x, y), DragEvent::default());
482 cb(dir, &mut action);
483 action.is_propagation_stopped()
484 };
485 self.interaction_dirty = true;
486 any_handled = true;
487 if stopped {
488 break;
489 }
490 }
491 if hit.occlude {
492 break;
493 }
494 }
495 }
496 if any_handled {
497 self.flush_async_after_event();
498 }
499 any_handled
500 }
501
502
503 #[inline(never)]
504 pub(super) fn dispatch_scroll_axis_at(&mut self, px: i16, py: i16, delta: i16, prefer_horiz: bool) -> bool {
505 if self.root_pod.is_none() {
506 return false;
507 }
508 self.ensure_hits_available();
509 let hits = self.cached_hit_regions();
510 let mut preferred: Option<(u32, i16)> = None;
511 let mut fallback: Option<(u32, i16)> = None;
512 for hit in hits.iter().rev() {
513 if hit.bounds.intersect(hit.clip).contains(px, py) {
514 if let Some(t) = hit.scroll_target {
515 if hit.scroll_horizontal == prefer_horiz {
516 preferred = Some(t);
517 break;
518 }
519 if !prefer_horiz && fallback.is_none() {
520 fallback = Some(t);
521 }
522 }
523 if hit.occlude {
524 break;
525 }
526 }
527 }
528 if let Some((list_id, max_s)) = preferred.or(fallback) {
529 self.motion.set_scroll_inertia(list_id, 0, max_s);
530 let cur = self.motion.scroll_offset(list_id);
531 let next = cur.saturating_add(delta).clamp(0, max_s.max(0));
532 if next != cur {
533 self.motion.set_scroll_offset(list_id, next);
534 self.interaction_dirty = true;
535 }
536 return true;
537 }
538 false
539 }
540
541 pub fn dispatch_click(&mut self, px: i16, py: i16) -> bool {
543 self.interaction.pointer_pos = Some((px, py));
544 let hit = self.trigger_click_internal(px, py, false, None, true);
545 if hit {
546 self.interaction_dirty = true;
547 }
548 hit
549 }
550
551 #[inline(never)]
552 pub(super) fn trigger_click_internal(
553 &mut self,
554 px: i16,
555 py: i16,
556 require_pressed_match: bool,
557 expected_id: Option<u32>,
558 allow_drag_fallback: bool,
559 ) -> bool {
560 let Some(root_id) = self.root_pod else {
561 return false;
562 };
563 self.ensure_hits_available();
564 self.interaction.focus_visible = false;
565 let hits = self.cached_hit_regions();
566 for hit in hits.iter().rev() {
567 let effective = hit.bounds.intersect(hit.clip);
568 if effective.contains(px, py) {
569 if let Some(fidx) = hit.focus_index {
570 self.interaction.focused_index = Some(fidx);
571 self.interaction_dirty = true;
572 }
573 if let Some(on_click) = hit.on_click() {
574 if require_pressed_match && hit.id != expected_id {
575 return false;
576 }
577 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
578 {
579 let mut acx =
580 self.make_action_scope(owner, (px, py), DragEvent::default());
581 on_click(&mut acx);
582 }
583 self.flush_async_after_event();
584 return true;
585 }
586 if let Some(on_drag) = hit.on_drag() {
587 if require_pressed_match && hit.id != expected_id {
588 return false;
589 }
590 if !allow_drag_fallback {
591 return true;
592 }
593 let drag_ev = DragEvent {
594 x: px,
595 y: py,
596 local_x: (px - hit.bounds.x).clamp(0, hit.bounds.w),
597 local_y: (py - hit.bounds.y).clamp(0, hit.bounds.h),
598 width: hit.bounds.w.max(1),
599 height: hit.bounds.h.max(1),
600 delta_x: 0,
601 delta_y: 0,
602 };
603 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
604 let mods = self.modifiers;
605 {
606 let mut acx = self
607 .make_action_scope(owner, (px, py), drag_ev)
608 .with_modifiers(mods);
609 on_drag(&mut acx);
610 }
611 self.flush_async_after_event();
612 return true;
613 }
614 if hit.occlude {
615 return false;
616 }
617 }
618 }
619 false
620 }
621
622 #[inline(never)]
624 pub(super) fn trigger_hold_action(&mut self, px: i16, py: i16, is_long_press: bool) -> bool {
625 let Some(root_id) = self.root_pod else {
626 return false;
627 };
628 self.ensure_hits_available();
629 let hits = self.cached_hit_regions();
630 for hit in hits.iter().rev() {
631 let effective = hit.bounds.intersect(hit.clip);
632 if effective.contains(px, py) {
633 if is_long_press {
634 if let Some(on_long_press) = hit.on_long_press() {
635 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
636 {
637 let mut acx =
638 self.make_action_scope(owner, (px, py), DragEvent::default());
639 on_long_press(&mut acx);
640 }
641 self.flush_async_after_event();
642 self.interaction_dirty = true;
643 return true;
644 }
645 } else if hit.auto_repeat {
646 if let Some(on_click) = hit.on_click() {
647 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
648 {
649 let mut acx =
650 self.make_action_scope(owner, (px, py), DragEvent::default());
651 on_click(&mut acx);
652 }
653 self.flush_async_after_event();
654 self.interaction_dirty = true;
655 return true;
656 }
657 }
658 if hit.occlude {
659 return false;
660 }
661 }
662 }
663 false
664 }
665
666 pub fn scroll(&mut self, list_id: u32, delta_y: i16) {
668 let cur = self.motion.scroll_offset(list_id);
669 let next = cur.saturating_add(delta_y);
670 if next != cur {
671 self.motion.set_scroll_offset(list_id, next);
672 self.interaction_dirty = true;
673 }
674 }
675}