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
266 let mods = self.modifiers;
268 let mut fired_any = false;
269 for hit in hits.iter().rev() {
270 let eff = hit.bounds.intersect(hit.clip);
271 let was_in = prev_pos.is_some_and(|(ox, oy)| eff.contains(ox, oy));
272 let is_in = eff.contains(px, py);
273 if was_in && !is_in {
274 if let Some(on_hover) = hit.on_hover() {
275 let ev = DragEvent {
276 x: px,
277 y: py,
278 local_x: (px - hit.bounds.x).clamp(0, hit.bounds.w),
279 local_y: (py - hit.bounds.y).clamp(0, hit.bounds.h),
280 width: hit.bounds.w.max(1),
281 height: hit.bounds.h.max(1),
282 delta_x: dx,
283 delta_y: dy,
284 };
285 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
286 let mut acx = self
287 .make_action_scope(owner, (px, py), ev)
288 .with_modifiers(mods);
289 on_hover(false, &mut acx);
290 fired_any = true;
291 }
292 }
293 }
294 for hit in hits.iter().rev() {
295 let eff = hit.bounds.intersect(hit.clip);
296 let was_in = prev_pos.is_some_and(|(ox, oy)| eff.contains(ox, oy));
297 let is_in = eff.contains(px, py);
298 if is_in {
299 let ev = DragEvent {
300 x: px,
301 y: py,
302 local_x: (px - hit.bounds.x).clamp(0, hit.bounds.w),
303 local_y: (py - hit.bounds.y).clamp(0, hit.bounds.h),
304 width: hit.bounds.w.max(1),
305 height: hit.bounds.h.max(1),
306 delta_x: dx,
307 delta_y: dy,
308 };
309 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
310 if !was_in {
311 if let Some(on_hover) = hit.on_hover() {
312 let mut acx = self
313 .make_action_scope(owner, (px, py), ev)
314 .with_modifiers(mods);
315 on_hover(true, &mut acx);
316 fired_any = true;
317 }
318 }
319 if let Some(on_move) = hit.on_pointer_move() {
320 let mut acx = self
321 .make_action_scope(owner, (px, py), ev)
322 .with_modifiers(mods);
323 on_move(&mut acx);
324 fired_any = true;
325 }
326 if hit.occlude {
327 break;
328 }
329 }
330 }
331 if fired_any {
332 self.flush_async_after_event();
333 self.interaction_dirty = true;
334 return true;
335 }
336 return false;
337 }
338 if dx == 0 && dy == 0 {
339 return false;
340 }
341
342 let (sx, sy) = (self.press_start_x, self.press_start_y);
343 let slop = self.gesture_config.drag_slop_px;
344 if (px - sx).abs() > slop || (py - sy).abs() > slop {
345 self.press_hold_ms = 0;
346 self.repeat_accum_ms = 0;
347 self.long_press_fired = true;
348 }
349
350 for hit in hits.iter().rev() {
352 let effective = hit.bounds.intersect(hit.clip);
353 let matches_pressed = self.has_pressed_hit && hit.id == Some(self.pressed_hit_id);
354 if matches_pressed || effective.contains(sx, sy) {
355 if let Some(on_drag) = hit.on_drag() {
356 let drag_ev = DragEvent {
357 x: px,
358 y: py,
359 local_x: (px - hit.bounds.x).clamp(0, hit.bounds.w),
360 local_y: (py - hit.bounds.y).clamp(0, hit.bounds.h),
361 width: hit.bounds.w.max(1),
362 height: hit.bounds.h.max(1),
363 delta_x: dx,
364 delta_y: dy,
365 };
366 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
367 let mods = self.modifiers;
368 {
369 let mut acx = self
370 .make_action_scope(owner, (px, py), drag_ev)
371 .with_modifiers(mods);
372 on_drag(&mut acx);
373 }
374 self.flush_async_after_event();
375 self.interaction_dirty = true;
376 return true;
377 }
378 if hit.occlude && effective.contains(sx, sy) {
379 break;
380 }
381 }
382 }
383
384 if !self.allows_drag_scroll() {
387 return false;
388 }
389 let mut scroll_to_apply: Option<((u32, i16), bool)> = None;
390 for hit in hits.iter().rev() {
391 let effective = hit.bounds.intersect(hit.clip);
392 if effective.contains(sx, sy) {
393 if let Some(target) = hit.scroll_target {
394 scroll_to_apply = Some((target, hit.scroll_horizontal));
395 break;
396 }
397 if hit.occlude {
398 break;
399 }
400 }
401 }
402
403 if let Some(((scroll_id, max_s), is_horiz)) = scroll_to_apply {
404 let delta = if is_horiz { dx } else { dy };
405 if delta != 0 {
406 let delta_scroll = -delta;
407 self.last_drag_vy = delta_scroll;
408 self.motion.set_scroll_inertia(scroll_id, 0, max_s);
409 let cur = self.motion.scroll_offset(scroll_id);
410 let next = cur.saturating_add(delta_scroll).clamp(0, max_s.max(0));
411 self.motion.set_scroll_offset(scroll_id, next);
412 self.interaction_dirty = true;
413 return true;
414 }
415 }
416 false
417 }
418
419 #[inline(never)]
421 pub fn dispatch_pointer_leave(&mut self) -> bool {
422 let Some((prev_x, prev_y)) = self.interaction.pointer_pos.take() else {
423 return false;
424 };
425 let Some(root_id) = self.root_pod else {
426 return false;
427 };
428 self.ensure_hits_available();
429 let hits = self.cached_hit_regions();
430 let was_hovered = hits.iter().any(|h| {
431 h.has_hover_bg && h.bounds.intersect(h.clip).contains(prev_x, prev_y)
432 });
433 let mods = self.modifiers;
434 let mut fired_leave = false;
435 for hit in hits.iter().rev() {
436 if hit.bounds.intersect(hit.clip).contains(prev_x, prev_y) {
437 if let Some(on_hover) = hit.on_hover() {
438 let ev = DragEvent::default();
439 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
440 let mut acx = self
441 .make_action_scope(owner, (prev_x, prev_y), ev)
442 .with_modifiers(mods);
443 on_hover(false, &mut acx);
444 fired_leave = true;
445 }
446 }
447 }
448 if fired_leave {
449 self.flush_async_after_event();
450 }
451 if was_hovered || fired_leave {
452 self.interaction_dirty = true;
453 }
454 was_hovered || fired_leave
455 }
456
457 #[inline(never)]
459 pub fn dispatch_pointer_up(&mut self, px: i16, py: i16) -> bool {
460 self.ensure_hits_available();
461 self.interaction.pointer_pos = Some((px, py));
462 let was_pressed = self.interaction.is_pressed;
463 let was_long_press_handled = self.long_press_handled;
464 let expected_id = if self.has_pressed_hit {
465 Some(self.pressed_hit_id)
466 } else {
467 None
468 };
469 self.has_pressed_hit = false;
470 let was_drag_fired = self.drag_fired_on_down;
471 self.drag_fired_on_down = false;
472 self.interaction.is_pressed = false;
473 self.press_hold_ms = 0;
474 self.repeat_accum_ms = 0;
475 self.long_press_fired = false;
476 self.long_press_handled = false;
477 self.interaction_dirty = true;
478
479 if !was_pressed {
480 return false;
481 }
482
483 let (sx, sy) = (self.press_start_x, self.press_start_y);
484 let dx = px - sx;
485 let dy = py - sy;
486 let slop = self.gesture_config.drag_slop_px;
487 let moved_far = dx.abs() > slop || dy.abs() > slop;
488
489 if !moved_far {
490 if was_long_press_handled {
491 return true;
492 }
493 return self.trigger_click_internal(px, py, true, expected_id, !was_drag_fired);
494 }
495
496 if self.allows_drag_scroll() {
498 let swipe_th = self.gesture_config.swipe_threshold_px;
499 let edge_w = self.gesture_config.edge_back_width_px;
500 if dx.abs() >= swipe_th || dy.abs() >= swipe_th {
501 let dir = if dx.abs() >= dy.abs() {
502 if dx > 0 {
503 SwipeDir::Right
504 } else {
505 SwipeDir::Left
506 }
507 } else if dy > 0 {
508 SwipeDir::Down
509 } else {
510 SwipeDir::Up
511 };
512 if self.dispatch_swipe(sx, sy, dir) {
513 self.last_drag_vy = 0;
514 return true;
515 }
516 if matches!(self.active_pointer_kind, PointerKind::Touch)
518 && sx <= self.viewport.x + edge_w
519 && dx >= edge_w
520 && dx.abs() > dy.abs() * 2
521 {
522 if self.dispatch_navigate_back() {
523 self.last_drag_vy = 0;
524 return true;
525 }
526 }
527 }
528
529 if self.last_drag_vy.abs() > 1 {
531 let hits = self.cached_hit_regions();
532 let mut scroll_target: Option<(u32, i16)> = None;
533 for hit in hits.iter().rev() {
534 if hit.bounds.intersect(hit.clip).contains(sx, sy) {
535 if let Some(t) = hit.scroll_target {
536 scroll_target = Some(t);
537 break;
538 }
539 if hit.occlude {
540 break;
541 }
542 }
543 }
544 if let Some((scroll_id, max_s)) = scroll_target {
545 self.motion
546 .set_scroll_inertia(scroll_id, self.last_drag_vy, max_s);
547 self.last_drag_vy = 0;
548 return true;
549 }
550 }
551 }
552 self.last_drag_vy = 0;
553 false
554 }
555
556 #[inline(never)]
558 pub fn dispatch_swipe(&mut self, x: i16, y: i16, dir: SwipeDir) -> bool {
559 let Some(root_id) = self.root_pod else {
560 return false;
561 };
562 self.ensure_hits_available();
563 let hits = self.cached_hit_regions();
564 let mut any_handled = false;
565 for hit in hits.iter().rev() {
566 if hit.bounds.intersect(hit.clip).contains(x, y) {
567 if let Some(cb) = hit.on_swipe() {
568 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
569 let stopped = {
570 let mut action = self.make_action_scope(owner, (x, y), DragEvent::default());
571 cb(dir, &mut action);
572 action.is_propagation_stopped()
573 };
574 self.interaction_dirty = true;
575 any_handled = true;
576 if stopped {
577 break;
578 }
579 }
580 if hit.occlude {
581 break;
582 }
583 }
584 }
585 if any_handled {
586 self.flush_async_after_event();
587 }
588 any_handled
589 }
590
591
592 #[inline(never)]
593 pub(super) fn dispatch_scroll_axis_at(&mut self, px: i16, py: i16, delta: i16, prefer_horiz: bool) -> bool {
594 if self.root_pod.is_none() {
595 return false;
596 }
597 self.ensure_hits_available();
598 let mut preferred: crate::arena::SmallVec<(u32, i16), 8> = crate::arena::SmallVec::new();
600 let mut fallback: crate::arena::SmallVec<(u32, i16), 8> = crate::arena::SmallVec::new();
601 for hit in self.cached_hit_regions().iter().rev() {
602 if hit.bounds.intersect(hit.clip).contains(px, py) {
603 if let Some(t) = hit.scroll_target {
604 if hit.scroll_horizontal == prefer_horiz {
605 let _ = preferred.push(t);
606 } else if !prefer_horiz {
607 let _ = fallback.push(t);
608 }
609 }
610 if hit.occlude {
611 break;
612 }
613 }
614 }
615 let candidates = if !preferred.is_empty() {
616 preferred.as_slice()
617 } else {
618 fallback.as_slice()
619 };
620 if candidates.is_empty() {
621 return false;
622 }
623 for &(list_id, max_s) in candidates {
624 self.motion.set_scroll_inertia(list_id, 0, max_s);
625 let cur = self.motion.scroll_offset(list_id);
626 let next = cur.saturating_add(delta).clamp(0, max_s.max(0));
627 if next != cur {
628 self.motion.set_scroll_offset(list_id, next);
629 self.interaction_dirty = true;
630 return true;
631 }
632 }
633 true
635 }
636
637 pub fn dispatch_click(&mut self, px: i16, py: i16) -> bool {
639 self.interaction.pointer_pos = Some((px, py));
640 let hit = self.trigger_click_internal(px, py, false, None, true);
641 if hit {
642 self.interaction_dirty = true;
643 }
644 hit
645 }
646
647 #[inline(never)]
648 pub(super) fn trigger_click_internal(
649 &mut self,
650 px: i16,
651 py: i16,
652 require_pressed_match: bool,
653 expected_id: Option<u32>,
654 allow_drag_fallback: bool,
655 ) -> bool {
656 let Some(root_id) = self.root_pod else {
657 return false;
658 };
659 self.ensure_hits_available();
660 self.interaction.focus_visible = false;
661 let hits = self.cached_hit_regions();
662 for hit in hits.iter().rev() {
663 let effective = hit.bounds.intersect(hit.clip);
664 if effective.contains(px, py) {
665 if let Some(fidx) = hit.focus_index {
666 self.interaction.focused_index = Some(fidx);
667 self.interaction_dirty = true;
668 }
669 if let Some(on_click) = hit.on_click() {
670 if require_pressed_match && hit.id != expected_id {
671 return false;
672 }
673 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
674 {
675 let mut acx =
676 self.make_action_scope(owner, (px, py), DragEvent::default());
677 on_click(&mut acx);
678 }
679 self.flush_async_after_event();
680 return true;
681 }
682 if let Some(on_drag) = hit.on_drag() {
683 if require_pressed_match && hit.id != expected_id {
684 return false;
685 }
686 if !allow_drag_fallback {
687 return true;
688 }
689 let drag_ev = DragEvent {
690 x: px,
691 y: py,
692 local_x: (px - hit.bounds.x).clamp(0, hit.bounds.w),
693 local_y: (py - hit.bounds.y).clamp(0, hit.bounds.h),
694 width: hit.bounds.w.max(1),
695 height: hit.bounds.h.max(1),
696 delta_x: 0,
697 delta_y: 0,
698 };
699 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
700 let mods = self.modifiers;
701 {
702 let mut acx = self
703 .make_action_scope(owner, (px, py), drag_ev)
704 .with_modifiers(mods);
705 on_drag(&mut acx);
706 }
707 self.flush_async_after_event();
708 return true;
709 }
710 if hit.occlude {
711 return false;
712 }
713 }
714 }
715 false
716 }
717
718 #[inline(never)]
720 pub(super) fn trigger_hold_action(&mut self, px: i16, py: i16, is_long_press: bool) -> bool {
721 let Some(root_id) = self.root_pod else {
722 return false;
723 };
724 self.ensure_hits_available();
725 let hits = self.cached_hit_regions();
726 for hit in hits.iter().rev() {
727 let effective = hit.bounds.intersect(hit.clip);
728 if effective.contains(px, py) {
729 if is_long_press {
730 if let Some(on_long_press) = hit.on_long_press() {
731 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
732 {
733 let mut acx =
734 self.make_action_scope(owner, (px, py), DragEvent::default());
735 on_long_press(&mut acx);
736 }
737 self.flush_async_after_event();
738 self.interaction_dirty = true;
739 return true;
740 }
741 } else if hit.auto_repeat {
742 if let Some(on_click) = hit.on_click() {
743 let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
744 {
745 let mut acx =
746 self.make_action_scope(owner, (px, py), DragEvent::default());
747 on_click(&mut acx);
748 }
749 self.flush_async_after_event();
750 self.interaction_dirty = true;
751 return true;
752 }
753 }
754 if hit.occlude {
755 return false;
756 }
757 }
758 }
759 false
760 }
761
762 pub fn scroll(&mut self, list_id: u32, delta_y: i16) {
764 let cur = self.motion.scroll_offset(list_id);
765 let next = cur.saturating_add(delta_y);
766 if next != cur {
767 self.motion.set_scroll_offset(list_id, next);
768 self.interaction_dirty = true;
769 }
770 }
771}