Skip to main content

velox_engine/engine/
pointer.rs

1//! 指针交互、拖拽/长按/自动连发/滑动手势状态机与滚轮事件派发。
2
3use crate::pod::{CursorShape, DragEvent, PointerKind, SwipeDir};
4
5use super::VeloxEngine;
6
7impl VeloxEngine {
8    /// 根据当前指针位置、按下/拖拽态与最顶层命中热区解析实时鼠标光标形状(严格遵守 `occlude` 遮罩阻断)。
9    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        // 1. 正在按住拖拽某个 `on_drag` 热区(如 Slider / ResizableSplit)时锁定拖拽光标,避免指针短暂滑出边缘闪烁
16        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        // 2. 逆序命中测试(后绘制/高 Z-Order 优先),优先采用显式 `.cursor(...)`,否则按语义自动推断
46        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    /// 以显式指针设备来源([`PointerKind::Mouse`] / [`PointerKind::Touch`] / [`PointerKind::Pen`])派发指针按下事件。
77    #[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    /// 派发桌面鼠标左键按下事件(标记为 [`PointerKind::Mouse`],禁用空白区域按住拖动画布滚动与边缘右滑返回)。
89    #[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    /// 派发触控屏手指按下事件(标记为 [`PointerKind::Touch`],启用跟手拖拽滚动、惯性滚动与边缘右滑返回)。
95    #[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    /// 派发指针/手指按下事件(激活 `active_bg`、捕获焦点与拖拽起点,直接复用缓存 `HitRegion` 切片避免重复整树排版)。
101    #[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        // 指针点击不激活键盘可见焦点环(`:focus-visible`),仅文本输入控件在聚焦时展示柔和输入边框
110        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        // 点击空白非可聚焦区域时自然失焦,避免残留过时的聚焦状态
185        if hit_focused.is_none() {
186            self.interaction.focused_index = None;
187        }
188        matched_passive
189    }
190
191    /// 派发鼠标右键(次键)按下事件(命中注册了 `.on_secondary_click(...)` 的节点并触发回调)。
192    #[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    /// 派发指针移动/拖拽事件(更新 `hover_bg`、触发 `on_drag` 滑块连续拖动或滚动容器跟手滑动,复用帧缓存热区)。
240    #[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            // 派发 `on_hover`(先离开后进入)与 `on_pointer_move`(用于图表等悬停交互追踪,零堆分配)
267            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        // 1. 优先路由给按下起点命中的 on_drag 组件(如 Slider)
351        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        // 2. 非触摸鼠标/手写笔默认禁止在空白可滚动容器上按住拖动页面(滚动走滚轮或显式 Scrollbar/on_drag);
385        //    触摸屏(PointerKind::Touch)或显式开启 `gesture_config.mouse_drag_scroll` 时执行跟手滚动并记录瞬时速度。
386        if !self.allows_drag_scroll() {
387            return false;
388        }
389        let mut scroll_to_apply: Option<((u32, i16), bool, i16)> = 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                    let dim = if hit.scroll_horizontal {
395                        effective.w.max(64)
396                    } else {
397                        effective.h.max(64)
398                    };
399                    scroll_to_apply = Some((target, hit.scroll_horizontal, dim));
400                    break;
401                }
402                if hit.occlude {
403                    break;
404                }
405            }
406        }
407
408        if let Some(((scroll_id, max_s), is_horiz, dim)) = scroll_to_apply {
409            let delta = if is_horiz { dx } else { dy };
410            if delta != 0 {
411                let delta_scroll = -delta;
412                self.last_drag_vy = delta_scroll;
413                self.motion
414                    .apply_scroll_drag(scroll_id, delta_scroll, max_s, dim);
415                self.interaction_dirty = true;
416                return true;
417            }
418        }
419        false
420    }
421
422    /// 派发指针离开表面事件(清除悬停坐标,并在离开 `hover_bg` 热区时标记局部重绘)。
423    #[inline(never)]
424    pub fn dispatch_pointer_leave(&mut self) -> bool {
425        let Some((prev_x, prev_y)) = self.interaction.pointer_pos.take() else {
426            return false;
427        };
428        let Some(root_id) = self.root_pod else {
429            return false;
430        };
431        self.ensure_hits_available();
432        let hits = self.cached_hit_regions();
433        let was_hovered = hits.iter().any(|h| {
434            h.has_hover_bg && h.bounds.intersect(h.clip).contains(prev_x, prev_y)
435        });
436        let mods = self.modifiers;
437        let mut fired_leave = false;
438        for hit in hits.iter().rev() {
439            if hit.bounds.intersect(hit.clip).contains(prev_x, prev_y) {
440                if let Some(on_hover) = hit.on_hover() {
441                    let ev = DragEvent::default();
442                    let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
443                    let mut acx = self
444                        .make_action_scope(owner, (prev_x, prev_y), ev)
445                        .with_modifiers(mods);
446                    on_hover(false, &mut acx);
447                    fired_leave = true;
448                }
449            }
450        }
451        if fired_leave {
452            self.flush_async_after_event();
453        }
454        if was_hovered || fired_leave {
455            self.interaction_dirty = true;
456        }
457        was_hovered || fired_leave
458    }
459
460    /// 派发指针/手指抬起事件(释放 `active_bg`、触发点击或启动惯性滑动衰减)。
461    #[inline(never)]
462    pub fn dispatch_pointer_up(&mut self, px: i16, py: i16) -> bool {
463        self.ensure_hits_available();
464        self.interaction.pointer_pos = Some((px, py));
465        let was_pressed = self.interaction.is_pressed;
466        let was_long_press_handled = self.long_press_handled;
467        let expected_id = if self.has_pressed_hit {
468            Some(self.pressed_hit_id)
469        } else {
470            None
471        };
472        self.has_pressed_hit = false;
473        let was_drag_fired = self.drag_fired_on_down;
474        self.drag_fired_on_down = false;
475        self.interaction.is_pressed = false;
476        self.press_hold_ms = 0;
477        self.repeat_accum_ms = 0;
478        self.long_press_fired = false;
479        self.long_press_handled = false;
480        self.interaction_dirty = true;
481
482        if !was_pressed {
483            return false;
484        }
485
486        let (sx, sy) = (self.press_start_x, self.press_start_y);
487        let dx = px - sx;
488        let dy = py - sy;
489        let slop = self.gesture_config.drag_slop_px;
490        let moved_far = dx.abs() > slop || dy.abs() > slop;
491
492        if !moved_far {
493            if was_long_press_handled {
494                return true;
495            }
496            return self.trigger_click_internal(px, py, true, expected_id, !was_drag_fired);
497        }
498
499        // 若允许触摸/拖拽手势且滑动幅度达到 swipe_threshold_px 以上,检测 4 向滑动手势(SwipeDir)与移动端左边缘右滑返回
500        if self.allows_drag_scroll() {
501            let swipe_th = self.gesture_config.swipe_threshold_px;
502            let edge_w = self.gesture_config.edge_back_width_px;
503            if dx.abs() >= swipe_th || dy.abs() >= swipe_th {
504                let dir = if dx.abs() >= dy.abs() {
505                    if dx > 0 {
506                        SwipeDir::Right
507                    } else {
508                        SwipeDir::Left
509                    }
510                } else if dy > 0 {
511                    SwipeDir::Down
512                } else {
513                    SwipeDir::Up
514                };
515                if self.dispatch_swipe(sx, sy, dir) {
516                    self.last_drag_vy = 0;
517                    return true;
518                }
519                // 移动端左边缘右滑返回手势仅在真实触屏下触发,避免桌面鼠标边缘框选误返回
520                if matches!(self.active_pointer_kind, PointerKind::Touch)
521                    && sx <= self.viewport.x + edge_w
522                    && dx >= edge_w
523                    && dx.abs() > dy.abs() * 2
524                {
525                    if self.dispatch_navigate_back() {
526                        self.last_drag_vy = 0;
527                        return true;
528                    }
529                }
530            }
531
532            // 若刚刚进行了快速拖拽滑动,启动惯性滚动
533            if self.last_drag_vy.abs() > 1 {
534                let hits = self.cached_hit_regions();
535                let mut scroll_target: Option<(u32, i16)> = None;
536                for hit in hits.iter().rev() {
537                    if hit.bounds.intersect(hit.clip).contains(sx, sy) {
538                        if let Some(t) = hit.scroll_target {
539                            scroll_target = Some(t);
540                            break;
541                        }
542                        if hit.occlude {
543                            break;
544                        }
545                    }
546                }
547                if let Some((scroll_id, max_s)) = scroll_target {
548                    self.motion
549                        .release_scroll_drag(scroll_id, self.last_drag_vy, max_s);
550                    self.last_drag_vy = 0;
551                    return true;
552                }
553            }
554        }
555        self.last_drag_vy = 0;
556        false
557    }
558
559    /// 派发指定坐标 `(x, y)` 处的 4 向滑动手势 [`SwipeDir`]。
560    #[inline(never)]
561    pub fn dispatch_swipe(&mut self, x: i16, y: i16, dir: SwipeDir) -> bool {
562        let Some(root_id) = self.root_pod else {
563            return false;
564        };
565        self.ensure_hits_available();
566        let hits = self.cached_hit_regions();
567        let mut any_handled = false;
568        for hit in hits.iter().rev() {
569            if hit.bounds.intersect(hit.clip).contains(x, y) {
570                if let Some(cb) = hit.on_swipe() {
571                    let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
572                    let stopped = {
573                        let mut action = self.make_action_scope(owner, (x, y), DragEvent::default());
574                        cb(dir, &mut action);
575                        action.is_propagation_stopped()
576                    };
577                    self.interaction_dirty = true;
578                    any_handled = true;
579                    if stopped {
580                        break;
581                    }
582                }
583                if hit.occlude {
584                    break;
585                }
586            }
587        }
588        if any_handled {
589            self.flush_async_after_event();
590        }
591        any_handled
592    }
593
594
595    #[inline(never)]
596    pub(super) fn dispatch_scroll_axis_at(&mut self, px: i16, py: i16, delta: i16, prefer_horiz: bool) -> bool {
597        let Some(root_id) = self.root_pod else {
598            return false;
599        };
600        self.ensure_hits_available();
601        // 0. 优先派发自定义 `on_wheel` 回调(如交互画布 Zoom-to-Cursor),避免滚轮被外层页面滚动吞掉
602        let hits = self.cached_hit_regions();
603        let mods = self.modifiers;
604        for hit in hits.iter().rev() {
605            if hit.bounds.intersect(hit.clip).contains(px, py) {
606                if let Some(on_wheel) = hit.on_wheel() {
607                    let ev = crate::pod::WheelEvent {
608                        x: px,
609                        y: py,
610                        local_x: (px - hit.bounds.x).clamp(0, hit.bounds.w),
611                        local_y: (py - hit.bounds.y).clamp(0, hit.bounds.h),
612                        width: hit.bounds.w.max(1),
613                        height: hit.bounds.h.max(1),
614                        delta_x: if prefer_horiz { delta } else { 0 },
615                        delta_y: if prefer_horiz { 0 } else { delta },
616                    };
617                    let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
618                    {
619                        let mut acx = self
620                            .make_action_scope(owner, (px, py), DragEvent::default())
621                            .with_modifiers(mods);
622                        on_wheel(ev, &mut acx);
623                    }
624                    self.flush_async_after_event();
625                    self.interaction_dirty = true;
626                    return true;
627                }
628                if hit.occlude {
629                    break;
630                }
631            }
632        }
633        // 支持嵌套滚动链(Scroll Chaining):先尝试最内层匹配滚动容器,若已触顶/触底无法继续滚动则自动向上冒泡至外层容器
634        let mut preferred: crate::arena::SmallVec<(u32, i16), 8> = crate::arena::SmallVec::new();
635        let mut fallback: crate::arena::SmallVec<(u32, i16), 8> = crate::arena::SmallVec::new();
636        for hit in self.cached_hit_regions().iter().rev() {
637            if hit.bounds.intersect(hit.clip).contains(px, py) {
638                if let Some(t) = hit.scroll_target {
639                    if hit.scroll_horizontal == prefer_horiz {
640                        let _ = preferred.push(t);
641                    } else if !prefer_horiz {
642                        let _ = fallback.push(t);
643                    }
644                }
645                if hit.occlude {
646                    break;
647                }
648            }
649        }
650        let candidates = if !preferred.is_empty() {
651            preferred.as_slice()
652        } else {
653            fallback.as_slice()
654        };
655        if candidates.is_empty() {
656            return false;
657        }
658        for &(list_id, max_s) in candidates {
659            self.motion.set_scroll_inertia(list_id, 0, max_s);
660            let cur = self.motion.scroll_offset(list_id);
661            let next = cur.saturating_add(delta).clamp(0, max_s.max(0));
662            if next != cur {
663                self.motion.set_scroll_offset(list_id, next);
664                self.interaction_dirty = true;
665                return true;
666            }
667        }
668        // 即使均在边界未产生位移,仍返回 true 表示事件已被滚动容器体系消费
669        true
670    }
671
672    /// 派发指定坐标 `(px, py)` 处的双指捏合缩放手势 [`crate::pod::PinchEvent`]。
673    #[inline(never)]
674    pub fn dispatch_pinch_at(&mut self, px: i16, py: i16, delta_scale_x1000: i16) -> bool {
675        self.last_pinch_delta_x1000 = delta_scale_x1000;
676        let Some(root_id) = self.root_pod else {
677            self.interaction_dirty = true;
678            return true;
679        };
680        self.ensure_hits_available();
681        let hits = self.cached_hit_regions();
682        let mods = self.modifiers;
683        let mut handled = false;
684        for hit in hits.iter().rev() {
685            if hit.bounds.intersect(hit.clip).contains(px, py) {
686                if let Some(on_pinch) = hit.on_pinch() {
687                    let ev = crate::pod::PinchEvent {
688                        x: px,
689                        y: py,
690                        local_x: (px - hit.bounds.x).clamp(0, hit.bounds.w),
691                        local_y: (py - hit.bounds.y).clamp(0, hit.bounds.h),
692                        width: hit.bounds.w.max(1),
693                        height: hit.bounds.h.max(1),
694                        delta_scale_x1000,
695                    };
696                    let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
697                    let stopped = {
698                        let mut acx = self
699                            .make_action_scope(owner, (px, py), DragEvent::default())
700                            .with_modifiers(mods);
701                        on_pinch(ev, &mut acx);
702                        acx.is_propagation_stopped()
703                    };
704                    handled = true;
705                    if stopped {
706                        break;
707                    }
708                }
709                if hit.occlude {
710                    break;
711                }
712            }
713        }
714        if handled {
715            self.flush_async_after_event();
716        }
717        self.interaction_dirty = true;
718        true
719    }
720
721    /// 派发屏幕坐标 `(px, py)` 的点击事件。若命中某个可点击元素则执行其回调并返回 `true`。
722    pub fn dispatch_click(&mut self, px: i16, py: i16) -> bool {
723        self.interaction.pointer_pos = Some((px, py));
724        let hit = self.trigger_click_internal(px, py, false, None, true);
725        if hit {
726            self.interaction_dirty = true;
727        }
728        hit
729    }
730
731    #[inline(never)]
732    pub(super) fn trigger_click_internal(
733        &mut self,
734        px: i16,
735        py: i16,
736        require_pressed_match: bool,
737        expected_id: Option<u32>,
738        allow_drag_fallback: bool,
739    ) -> bool {
740        let Some(root_id) = self.root_pod else {
741            return false;
742        };
743        self.ensure_hits_available();
744        self.interaction.focus_visible = false;
745        let hits = self.cached_hit_regions();
746        for hit in hits.iter().rev() {
747            let effective = hit.bounds.intersect(hit.clip);
748            if effective.contains(px, py) {
749                if let Some(fidx) = hit.focus_index {
750                    self.interaction.focused_index = Some(fidx);
751                    self.interaction_dirty = true;
752                }
753                if let Some(on_click) = hit.on_click() {
754                    if require_pressed_match && hit.id != expected_id {
755                        return false;
756                    }
757                    let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
758                    {
759                        let mut acx =
760                            self.make_action_scope(owner, (px, py), DragEvent::default());
761                        on_click(&mut acx);
762                    }
763                    self.flush_async_after_event();
764                    return true;
765                }
766                if let Some(on_drag) = hit.on_drag() {
767                    if require_pressed_match && hit.id != expected_id {
768                        return false;
769                    }
770                    if !allow_drag_fallback {
771                        return true;
772                    }
773                    let drag_ev = DragEvent {
774                        x: px,
775                        y: py,
776                        local_x: (px - hit.bounds.x).clamp(0, hit.bounds.w),
777                        local_y: (py - hit.bounds.y).clamp(0, hit.bounds.h),
778                        width: hit.bounds.w.max(1),
779                        height: hit.bounds.h.max(1),
780                        delta_x: 0,
781                        delta_y: 0,
782                    };
783                    let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
784                    let mods = self.modifiers;
785                    {
786                        let mut acx = self
787                            .make_action_scope(owner, (px, py), drag_ev)
788                            .with_modifiers(mods);
789                        on_drag(&mut acx);
790                    }
791                    self.flush_async_after_event();
792                    return true;
793                }
794                if hit.occlude {
795                    return false;
796                }
797            }
798        }
799        false
800    }
801
802    /// 内部辅助:触发长按回调(`is_long_press == true`)或按住连发回调(`is_long_press == false`)。
803    #[inline(never)]
804    pub(super) fn trigger_hold_action(&mut self, px: i16, py: i16, is_long_press: bool) -> bool {
805        let Some(root_id) = self.root_pod else {
806            return false;
807        };
808        self.ensure_hits_available();
809        let hits = self.cached_hit_regions();
810        for hit in hits.iter().rev() {
811            let effective = hit.bounds.intersect(hit.clip);
812            if effective.contains(px, py) {
813                if is_long_press {
814                    if let Some(on_long_press) = hit.on_long_press() {
815                        let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
816                        {
817                            let mut acx =
818                                self.make_action_scope(owner, (px, py), DragEvent::default());
819                            on_long_press(&mut acx);
820                        }
821                        self.flush_async_after_event();
822                        self.interaction_dirty = true;
823                        return true;
824                    }
825                } else if hit.auto_repeat {
826                    if let Some(on_click) = hit.on_click() {
827                        let owner = Self::resolve_hit_pod(root_id, hit.owner_pod);
828                        {
829                            let mut acx =
830                                self.make_action_scope(owner, (px, py), DragEvent::default());
831                            on_click(&mut acx);
832                        }
833                        self.flush_async_after_event();
834                        self.interaction_dirty = true;
835                        return true;
836                    }
837                }
838                if hit.occlude {
839                    return false;
840                }
841            }
842        }
843        false
844    }
845
846    /// 更新指定列表容器 ID 的垂直滚动偏移量并标记下一帧执行 Pass-4 快径重拍平。
847    pub fn scroll(&mut self, list_id: u32, delta_y: i16) {
848        let cur = self.motion.scroll_offset(list_id);
849        let next = cur.saturating_add(delta_y);
850        if next != cur {
851            self.motion.set_scroll_offset(list_id, next);
852            self.interaction_dirty = true;
853        }
854    }
855}