Skip to main content

cranpose_render_common/
graph_scene.rs

1use std::cell::{Cell, RefCell};
2use std::cmp::Reverse;
3use std::collections::{HashMap, HashSet};
4use std::rc::Rc;
5
6use cranpose_core::{MemoryApplier, NodeId};
7use cranpose_foundation::{PointerEvent, PointerEventKind};
8use cranpose_ui::{LayoutNode, ModifierNodeSlices, SubcomposeLayoutNode};
9use cranpose_ui_graphics::{Point, Rect, RoundedCornerShape};
10
11use crate::graph::{ProjectiveTransform, RenderGraph};
12use crate::{HitTestTarget, RenderScene};
13
14pub struct RenderDiagnostics {
15    reported_warnings: RefCell<HashSet<&'static str>>,
16    live_modifier_slice_lookup_miss_count: Cell<usize>,
17}
18
19impl RenderDiagnostics {
20    pub fn new() -> Self {
21        Self {
22            reported_warnings: RefCell::new(HashSet::new()),
23            live_modifier_slice_lookup_miss_count: Cell::new(0),
24        }
25    }
26
27    pub fn claim_warning_once(&self, key: &'static str) -> bool {
28        self.reported_warnings.borrow_mut().insert(key)
29    }
30
31    pub fn record_live_modifier_slice_lookup_miss(&self) {
32        self.live_modifier_slice_lookup_miss_count.set(
33            self.live_modifier_slice_lookup_miss_count
34                .get()
35                .saturating_add(1),
36        );
37    }
38
39    pub fn live_modifier_slice_lookup_miss_count(&self) -> usize {
40        self.live_modifier_slice_lookup_miss_count.get()
41    }
42}
43
44impl Default for RenderDiagnostics {
45    fn default() -> Self {
46        Self::new()
47    }
48}
49
50#[derive(Clone)]
51pub enum ClickAction {
52    Simple(Rc<RefCell<dyn FnMut()>>),
53    WithPoint(Rc<dyn Fn(Point)>),
54}
55
56impl ClickAction {
57    fn invoke(&self, local_position: Point) {
58        match self {
59            ClickAction::Simple(handler) => (handler.borrow_mut())(),
60            ClickAction::WithPoint(handler) => handler(local_position),
61        }
62    }
63}
64
65#[derive(Clone, Copy, Debug, PartialEq)]
66pub struct HitClip {
67    pub quad: [[f32; 2]; 4],
68    pub bounds: Rect,
69}
70
71#[derive(Clone)]
72pub struct HitGeometry {
73    pub rect: Rect,
74    pub quad: [[f32; 2]; 4],
75    pub local_bounds: Rect,
76    pub world_to_local: ProjectiveTransform,
77    pub hit_clip_bounds: Option<Rect>,
78    pub hit_clips: Vec<HitClip>,
79}
80
81#[derive(Clone)]
82pub struct HitRegion {
83    pub node_id: NodeId,
84    pub capture_path: Vec<NodeId>,
85    pub rect: Rect,
86    pub quad: [[f32; 2]; 4],
87    pub local_bounds: Rect,
88    pub world_to_local: ProjectiveTransform,
89    pub shape: Option<RoundedCornerShape>,
90    pub click_actions: Vec<ClickAction>,
91    pub pointer_inputs: Vec<Rc<dyn Fn(PointerEvent)>>,
92    pub z_index: usize,
93    pub hit_clip_bounds: Option<Rect>,
94    pub hit_clips: Vec<HitClip>,
95    diagnostics: Rc<RenderDiagnostics>,
96}
97
98struct HitRegionInit {
99    node_id: NodeId,
100    capture_path: Vec<NodeId>,
101    geometry: HitGeometry,
102    shape: Option<RoundedCornerShape>,
103    click_actions: Vec<ClickAction>,
104    pointer_inputs: Vec<Rc<dyn Fn(PointerEvent)>>,
105    z_index: usize,
106    diagnostics: Rc<RenderDiagnostics>,
107}
108
109impl HitRegion {
110    fn with_diagnostics(init: HitRegionInit) -> Self {
111        let HitRegionInit {
112            node_id,
113            capture_path,
114            geometry,
115            shape,
116            click_actions,
117            pointer_inputs,
118            z_index,
119            diagnostics,
120        } = init;
121        let HitGeometry {
122            rect,
123            quad,
124            local_bounds,
125            world_to_local,
126            hit_clip_bounds,
127            hit_clips,
128        } = geometry;
129        Self {
130            node_id,
131            capture_path,
132            rect,
133            quad,
134            local_bounds,
135            world_to_local,
136            shape,
137            click_actions,
138            pointer_inputs,
139            z_index,
140            hit_clip_bounds,
141            hit_clips,
142            diagnostics,
143        }
144    }
145
146    fn contains(&self, x: f32, y: f32) -> bool {
147        if !self.rect.contains(x, y) {
148            return false;
149        }
150
151        if let Some(clip_bounds) = self.hit_clip_bounds {
152            if !clip_bounds.contains(x, y) {
153                return false;
154            }
155        }
156
157        let point = Point { x, y };
158        if !point_in_quad(point, self.quad) {
159            return false;
160        }
161
162        for clip in &self.hit_clips {
163            if !point_in_quad(point, clip.quad) {
164                return false;
165            }
166        }
167
168        let local_point = self.world_to_local.map_point(point);
169        if let Some(shape) = self.shape {
170            point_in_rounded_rect(local_point, self.local_bounds, shape)
171        } else {
172            self.local_bounds.contains(local_point.x, local_point.y)
173        }
174    }
175
176    fn localize_event(&self, event: &PointerEvent) -> (PointerEvent, Point) {
177        let local = self.world_to_local.map_point(event.global_position);
178        let local_position = Point {
179            x: local.x - self.local_bounds.x,
180            y: local.y - self.local_bounds.y,
181        };
182        (
183            event.copy_with_local_position(local_position),
184            local_position,
185        )
186    }
187
188    fn dispatch_pointer_inputs(
189        pointer_inputs: &[Rc<dyn Fn(PointerEvent)>],
190        local_event: &PointerEvent,
191    ) {
192        for handler in pointer_inputs {
193            if local_event.is_consumed() && !is_terminal_pointer_event(local_event.kind) {
194                break;
195            }
196            handler(local_event.clone());
197        }
198    }
199
200    fn dispatch_click_actions(&self, local_position: Point) {
201        for action in &self.click_actions {
202            action.invoke(local_position);
203        }
204    }
205
206    fn dispatch_modifier_slices(&self, modifier_slices: &ModifierNodeSlices, event: PointerEvent) {
207        if should_skip_consumed_event(&event) {
208            return;
209        }
210
211        let (local_event, local_position) = self.localize_event(&event);
212        Self::dispatch_pointer_inputs(modifier_slices.pointer_inputs(), &local_event);
213
214        if event.kind == PointerEventKind::Down && !local_event.is_consumed() {
215            for handler in modifier_slices.click_handlers() {
216                handler(local_position);
217            }
218        }
219    }
220
221    fn dispatch_cached_handlers(&self, event: PointerEvent) {
222        if should_skip_consumed_event(&event) {
223            return;
224        }
225
226        let (local_event, local_position) = self.localize_event(&event);
227        Self::dispatch_pointer_inputs(&self.pointer_inputs, &local_event);
228
229        if event.kind == PointerEventKind::Down && !local_event.is_consumed() {
230            self.dispatch_click_actions(local_position);
231        }
232    }
233
234    fn live_modifier_slices(&self, applier: &mut MemoryApplier) -> Option<Rc<ModifierNodeSlices>> {
235        if let Ok(modifier_slices) =
236            applier.with_node::<LayoutNode, _>(self.node_id, |node| node.modifier_slices_snapshot())
237        {
238            return Some(modifier_slices);
239        }
240
241        applier
242            .with_node::<SubcomposeLayoutNode, _>(self.node_id, |node| {
243                node.modifier_slices_snapshot()
244            })
245            .ok()
246    }
247}
248
249fn is_terminal_pointer_event(kind: PointerEventKind) -> bool {
250    matches!(kind, PointerEventKind::Up | PointerEventKind::Cancel)
251}
252
253fn should_skip_consumed_event(event: &PointerEvent) -> bool {
254    event.is_consumed() && !is_terminal_pointer_event(event.kind)
255}
256
257impl HitTestTarget for HitRegion {
258    fn node_id(&self) -> NodeId {
259        self.node_id
260    }
261
262    fn capture_path(&self) -> Vec<NodeId> {
263        self.capture_path.clone()
264    }
265
266    fn dispatch(&self, event: PointerEvent) {
267        self.dispatch_cached_handlers(event);
268    }
269
270    fn dispatch_with_applier(&self, applier: &mut MemoryApplier, event: PointerEvent) {
271        if let Some(modifier_slices) = self.live_modifier_slices(applier) {
272            self.dispatch_modifier_slices(modifier_slices.as_ref(), event);
273            return;
274        }
275
276        self.diagnostics.record_live_modifier_slice_lookup_miss();
277        self.dispatch_cached_handlers(event);
278    }
279}
280
281pub struct Scene {
282    pub graph: Option<RenderGraph>,
283    pub hits: Vec<HitRegion>,
284    pub next_hit_z: usize,
285    pub node_index: HashMap<NodeId, usize>,
286    diagnostics: Rc<RenderDiagnostics>,
287}
288
289impl Scene {
290    pub fn new() -> Self {
291        Self {
292            graph: None,
293            hits: Vec::new(),
294            next_hit_z: 0,
295            node_index: HashMap::new(),
296            diagnostics: Rc::new(RenderDiagnostics::new()),
297        }
298    }
299
300    pub fn diagnostics(&self) -> &RenderDiagnostics {
301        self.diagnostics.as_ref()
302    }
303
304    pub fn push_hit(
305        &mut self,
306        node_id: NodeId,
307        capture_path: Vec<NodeId>,
308        geometry: HitGeometry,
309        shape: Option<RoundedCornerShape>,
310        click_actions: Vec<ClickAction>,
311        pointer_inputs: Vec<Rc<dyn Fn(PointerEvent)>>,
312    ) {
313        if click_actions.is_empty() && pointer_inputs.is_empty() {
314            return;
315        }
316
317        let z_index = self.next_hit_z;
318        self.next_hit_z += 1;
319        let hit_index = self.hits.len();
320        self.hits.push(HitRegion::with_diagnostics(HitRegionInit {
321            node_id,
322            capture_path,
323            geometry,
324            shape,
325            click_actions,
326            pointer_inputs,
327            z_index,
328            diagnostics: Rc::clone(&self.diagnostics),
329        }));
330        self.node_index.insert(node_id, hit_index);
331    }
332
333    pub fn replace_graph(&mut self, graph: RenderGraph) {
334        self.graph = Some(graph);
335    }
336}
337
338impl Default for Scene {
339    fn default() -> Self {
340        Self::new()
341    }
342}
343
344impl RenderScene for Scene {
345    type HitTarget = HitRegion;
346
347    fn clear(&mut self) {
348        self.graph = None;
349        self.hits.clear();
350        self.node_index.clear();
351        self.next_hit_z = 0;
352    }
353
354    fn hit_test(&self, x: f32, y: f32) -> Vec<Self::HitTarget> {
355        let mut hit_indices: Vec<usize> = self
356            .hits
357            .iter()
358            .enumerate()
359            .filter_map(|(index, hit)| hit.contains(x, y).then_some(index))
360            .collect();
361
362        hit_indices.sort_by_key(|&index| Reverse(self.hits[index].z_index));
363        hit_indices
364            .into_iter()
365            .map(|index| self.hits[index].clone())
366            .collect()
367    }
368
369    fn find_target(&self, node_id: NodeId) -> Option<Self::HitTarget> {
370        self.node_index
371            .get(&node_id)
372            .and_then(|&index| self.hits.get(index))
373            .cloned()
374    }
375}
376
377fn point_in_rounded_rect(point: Point, rect: Rect, shape: RoundedCornerShape) -> bool {
378    if !rect.contains(point.x, point.y) {
379        return false;
380    }
381
382    let local_x = point.x - rect.x;
383    let local_y = point.y - rect.y;
384    let radii = shape.resolve(rect.width, rect.height);
385    let tl = radii.top_left;
386    let tr = radii.top_right;
387    let bl = radii.bottom_left;
388    let br = radii.bottom_right;
389
390    if local_x < tl && local_y < tl {
391        let dx = tl - local_x;
392        let dy = tl - local_y;
393        return dx * dx + dy * dy <= tl * tl;
394    }
395
396    if local_x > rect.width - tr && local_y < tr {
397        let dx = local_x - (rect.width - tr);
398        let dy = tr - local_y;
399        return dx * dx + dy * dy <= tr * tr;
400    }
401
402    if local_x < bl && local_y > rect.height - bl {
403        let dx = bl - local_x;
404        let dy = local_y - (rect.height - bl);
405        return dx * dx + dy * dy <= bl * bl;
406    }
407
408    if local_x > rect.width - br && local_y > rect.height - br {
409        let dx = local_x - (rect.width - br);
410        let dy = local_y - (rect.height - br);
411        return dx * dx + dy * dy <= br * br;
412    }
413
414    true
415}
416
417fn point_in_quad(point: Point, quad: [[f32; 2]; 4]) -> bool {
418    point_in_triangle(point, quad[0], quad[1], quad[3])
419        || point_in_triangle(point, quad[0], quad[3], quad[2])
420}
421
422fn point_in_triangle(point: Point, a: [f32; 2], b: [f32; 2], c: [f32; 2]) -> bool {
423    let d1 = triangle_sign(point, a, b);
424    let d2 = triangle_sign(point, b, c);
425    let d3 = triangle_sign(point, c, a);
426    let has_negative = d1 < -f32::EPSILON || d2 < -f32::EPSILON || d3 < -f32::EPSILON;
427    let has_positive = d1 > f32::EPSILON || d2 > f32::EPSILON || d3 > f32::EPSILON;
428    !(has_negative && has_positive)
429}
430
431fn triangle_sign(point: Point, a: [f32; 2], b: [f32; 2]) -> f32 {
432    (point.x - b[0]) * (a[1] - b[1]) - (a[0] - b[0]) * (point.y - b[1])
433}
434
435#[cfg(test)]
436mod tests {
437    use super::*;
438    use std::cell::Cell;
439
440    fn rect_to_quad(rect: Rect) -> [[f32; 2]; 4] {
441        [
442            [rect.x, rect.y],
443            [rect.x + rect.width, rect.y],
444            [rect.x, rect.y + rect.height],
445            [rect.x + rect.width, rect.y + rect.height],
446        ]
447    }
448
449    fn translated_world_to_local(rect: Rect) -> ProjectiveTransform {
450        ProjectiveTransform::translation(-rect.x, -rect.y)
451    }
452
453    fn local_bounds_for_rect(rect: Rect) -> Rect {
454        Rect {
455            x: 0.0,
456            y: 0.0,
457            width: rect.width,
458            height: rect.height,
459        }
460    }
461
462    fn hit_geometry_for_rect(rect: Rect) -> HitGeometry {
463        HitGeometry {
464            rect,
465            quad: rect_to_quad(rect),
466            local_bounds: local_bounds_for_rect(rect),
467            world_to_local: translated_world_to_local(rect),
468            hit_clip_bounds: None,
469            hit_clips: Vec::new(),
470        }
471    }
472
473    fn test_diagnostics() -> Rc<RenderDiagnostics> {
474        Rc::new(RenderDiagnostics::new())
475    }
476
477    fn make_handler(counter: Rc<Cell<u32>>, consume: bool) -> Rc<dyn Fn(PointerEvent)> {
478        Rc::new(move |event: PointerEvent| {
479            counter.set(counter.get() + 1);
480            if consume {
481                event.consume();
482            }
483        })
484    }
485
486    #[test]
487    fn hit_test_respects_hit_clip() {
488        let mut scene = Scene::new();
489        let rect = Rect {
490            x: 0.0,
491            y: 0.0,
492            width: 100.0,
493            height: 100.0,
494        };
495        let clip = Rect {
496            x: 0.0,
497            y: 0.0,
498            width: 40.0,
499            height: 40.0,
500        };
501        scene.push_hit(
502            1,
503            vec![1],
504            HitGeometry {
505                hit_clip_bounds: Some(clip),
506                hit_clips: vec![HitClip {
507                    quad: rect_to_quad(clip),
508                    bounds: clip,
509                }],
510                ..hit_geometry_for_rect(rect)
511            },
512            None,
513            Vec::new(),
514            vec![Rc::new(|_event: PointerEvent| {})],
515        );
516
517        assert!(scene.hit_test(60.0, 20.0).is_empty());
518        assert_eq!(scene.hit_test(20.0, 20.0).len(), 1);
519    }
520
521    #[test]
522    fn hit_test_sorts_by_z_without_duplicating_hit_storage() {
523        let mut scene = Scene::new();
524        let rect = Rect {
525            x: 0.0,
526            y: 0.0,
527            width: 50.0,
528            height: 50.0,
529        };
530
531        scene.push_hit(
532            1,
533            vec![1],
534            hit_geometry_for_rect(rect),
535            None,
536            Vec::new(),
537            vec![Rc::new(|_event: PointerEvent| {})],
538        );
539        scene.push_hit(
540            2,
541            vec![2],
542            hit_geometry_for_rect(rect),
543            None,
544            Vec::new(),
545            vec![Rc::new(|_event: PointerEvent| {})],
546        );
547
548        assert_eq!(scene.node_index.get(&1), Some(&0));
549        assert_eq!(scene.node_index.get(&2), Some(&1));
550
551        let hits = scene.hit_test(10.0, 10.0);
552        assert_eq!(
553            hits.iter().map(|hit| hit.node_id).collect::<Vec<_>>(),
554            vec![2, 1]
555        );
556        assert_eq!(scene.find_target(1).map(|hit| hit.node_id), Some(1));
557        assert_eq!(scene.find_target(2).map(|hit| hit.node_id), Some(2));
558    }
559
560    #[test]
561    fn hit_test_rejects_points_in_rounded_corner_cutout() {
562        let mut scene = Scene::new();
563        let rect = Rect {
564            x: 0.0,
565            y: 0.0,
566            width: 40.0,
567            height: 40.0,
568        };
569        scene.push_hit(
570            1,
571            vec![1],
572            hit_geometry_for_rect(rect),
573            Some(RoundedCornerShape::uniform(20.0)),
574            Vec::new(),
575            vec![Rc::new(|_event: PointerEvent| {})],
576        );
577
578        assert!(scene.hit_test(1.0, 1.0).is_empty());
579        assert_eq!(scene.hit_test(20.0, 20.0).len(), 1);
580    }
581
582    #[test]
583    fn render_diagnostics_claim_each_warning_key_once() {
584        let diagnostics = RenderDiagnostics::new();
585
586        assert!(diagnostics.claim_warning_once("pixels.effect-fallback"));
587        assert!(!diagnostics.claim_warning_once("pixels.effect-fallback"));
588        assert!(diagnostics.claim_warning_once("pixels.blend-fallback"));
589    }
590
591    #[test]
592    fn dispatch_stops_after_event_consumed() {
593        let count_first = Rc::new(Cell::new(0));
594        let count_second = Rc::new(Cell::new(0));
595
596        let hit = HitRegion::with_diagnostics(HitRegionInit {
597            node_id: 1,
598            capture_path: vec![1],
599            geometry: hit_geometry_for_rect(Rect {
600                x: 0.0,
601                y: 0.0,
602                width: 50.0,
603                height: 50.0,
604            }),
605            shape: None,
606            click_actions: Vec::new(),
607            pointer_inputs: vec![
608                make_handler(count_first.clone(), true),
609                make_handler(count_second.clone(), false),
610            ],
611            z_index: 0,
612            diagnostics: test_diagnostics(),
613        });
614
615        let event = PointerEvent::new(
616            PointerEventKind::Down,
617            Point { x: 10.0, y: 10.0 },
618            Point { x: 10.0, y: 10.0 },
619        );
620        hit.dispatch(event);
621
622        assert_eq!(count_first.get(), 1);
623        assert_eq!(count_second.get(), 0);
624    }
625
626    #[test]
627    fn dispatch_delivers_terminal_events_after_consumption_for_cleanup() {
628        let count_first = Rc::new(Cell::new(0));
629        let count_second = Rc::new(Cell::new(0));
630
631        let hit = HitRegion::with_diagnostics(HitRegionInit {
632            node_id: 1,
633            capture_path: vec![1],
634            geometry: hit_geometry_for_rect(Rect {
635                x: 0.0,
636                y: 0.0,
637                width: 50.0,
638                height: 50.0,
639            }),
640            shape: None,
641            click_actions: Vec::new(),
642            pointer_inputs: vec![
643                make_handler(count_first.clone(), true),
644                make_handler(count_second.clone(), false),
645            ],
646            z_index: 0,
647            diagnostics: test_diagnostics(),
648        });
649
650        for kind in [PointerEventKind::Up, PointerEventKind::Cancel] {
651            let event =
652                PointerEvent::new(kind, Point { x: 10.0, y: 10.0 }, Point { x: 10.0, y: 10.0 });
653            hit.dispatch(event);
654        }
655
656        assert_eq!(count_first.get(), 2);
657        assert_eq!(count_second.get(), 2);
658    }
659
660    #[test]
661    fn dispatch_delivers_terminal_events_to_later_captured_targets_after_consumption() {
662        let child_count = Rc::new(Cell::new(0));
663        let parent_count = Rc::new(Cell::new(0));
664
665        let child_hit = HitRegion::with_diagnostics(HitRegionInit {
666            node_id: 2,
667            capture_path: vec![2, 1],
668            geometry: hit_geometry_for_rect(Rect {
669                x: 8.0,
670                y: 8.0,
671                width: 20.0,
672                height: 20.0,
673            }),
674            shape: None,
675            click_actions: Vec::new(),
676            pointer_inputs: vec![make_handler(child_count.clone(), true)],
677            z_index: 1,
678            diagnostics: test_diagnostics(),
679        });
680        let parent_hit = HitRegion::with_diagnostics(HitRegionInit {
681            node_id: 1,
682            capture_path: vec![1],
683            geometry: hit_geometry_for_rect(Rect {
684                x: 0.0,
685                y: 0.0,
686                width: 50.0,
687                height: 50.0,
688            }),
689            shape: None,
690            click_actions: Vec::new(),
691            pointer_inputs: vec![make_handler(parent_count.clone(), false)],
692            z_index: 0,
693            diagnostics: test_diagnostics(),
694        });
695
696        let event = PointerEvent::new(
697            PointerEventKind::Up,
698            Point { x: 12.0, y: 12.0 },
699            Point { x: 12.0, y: 12.0 },
700        );
701        child_hit.dispatch(event.clone());
702        parent_hit.dispatch(event);
703
704        assert_eq!(child_count.get(), 1);
705        assert_eq!(parent_count.get(), 1);
706    }
707
708    #[test]
709    fn dispatch_triggers_click_action_on_down() {
710        let click_count = Rc::new(Cell::new(0));
711        let click_count_for_handler = Rc::clone(&click_count);
712        let click_action = ClickAction::Simple(Rc::new(RefCell::new(move || {
713            click_count_for_handler.set(click_count_for_handler.get() + 1);
714        })));
715
716        let hit = HitRegion::with_diagnostics(HitRegionInit {
717            node_id: 1,
718            capture_path: vec![1],
719            geometry: hit_geometry_for_rect(Rect {
720                x: 0.0,
721                y: 0.0,
722                width: 50.0,
723                height: 50.0,
724            }),
725            shape: None,
726            click_actions: vec![click_action],
727            pointer_inputs: Vec::new(),
728            z_index: 0,
729            diagnostics: test_diagnostics(),
730        });
731
732        hit.dispatch(PointerEvent::new(
733            PointerEventKind::Down,
734            Point { x: 10.0, y: 10.0 },
735            Point { x: 10.0, y: 10.0 },
736        ));
737        hit.dispatch(PointerEvent::new(
738            PointerEventKind::Move,
739            Point { x: 10.0, y: 10.0 },
740            Point { x: 12.0, y: 12.0 },
741        ));
742
743        assert_eq!(click_count.get(), 1);
744    }
745
746    #[test]
747    fn dispatch_passes_local_position_to_click_action() {
748        let local_positions = Rc::new(RefCell::new(Vec::new()));
749        let local_positions_for_handler = Rc::clone(&local_positions);
750        let click_action = ClickAction::WithPoint(Rc::new(move |point| {
751            local_positions_for_handler.borrow_mut().push(point);
752        }));
753
754        let hit = HitRegion::with_diagnostics(HitRegionInit {
755            node_id: 1,
756            capture_path: vec![1],
757            geometry: hit_geometry_for_rect(Rect {
758                x: 10.0,
759                y: 12.0,
760                width: 50.0,
761                height: 50.0,
762            }),
763            shape: None,
764            click_actions: vec![click_action],
765            pointer_inputs: Vec::new(),
766            z_index: 0,
767            diagnostics: test_diagnostics(),
768        });
769
770        hit.dispatch(PointerEvent::new(
771            PointerEventKind::Down,
772            Point { x: 15.0, y: 17.0 },
773            Point { x: 15.0, y: 17.0 },
774        ));
775
776        assert_eq!(*local_positions.borrow(), vec![Point { x: 5.0, y: 5.0 }]);
777    }
778
779    #[test]
780    fn dispatch_does_not_trigger_click_action_when_consumed() {
781        let click_count = Rc::new(Cell::new(0));
782        let click_count_for_handler = Rc::clone(&click_count);
783        let click_action = ClickAction::Simple(Rc::new(RefCell::new(move || {
784            click_count_for_handler.set(click_count_for_handler.get() + 1);
785        })));
786
787        let hit = HitRegion::with_diagnostics(HitRegionInit {
788            node_id: 1,
789            capture_path: vec![1],
790            geometry: hit_geometry_for_rect(Rect {
791                x: 0.0,
792                y: 0.0,
793                width: 50.0,
794                height: 50.0,
795            }),
796            shape: None,
797            click_actions: vec![click_action],
798            pointer_inputs: vec![Rc::new(|event: PointerEvent| event.consume())],
799            z_index: 0,
800            diagnostics: test_diagnostics(),
801        });
802
803        hit.dispatch(PointerEvent::new(
804            PointerEventKind::Down,
805            Point { x: 10.0, y: 10.0 },
806            Point { x: 10.0, y: 10.0 },
807        ));
808
809        assert_eq!(click_count.get(), 0);
810    }
811
812    #[test]
813    fn hit_test_uses_exact_quad_for_transformed_region() {
814        let mut scene = Scene::new();
815        let rect = Rect {
816            x: 0.0,
817            y: 0.0,
818            width: 40.0,
819            height: 20.0,
820        };
821        let quad = [[10.0, 10.0], [50.0, 10.0], [20.0, 30.0], [60.0, 30.0]];
822        let world_to_local = ProjectiveTransform::from_rect_to_quad(rect, quad)
823            .inverse()
824            .expect("transformed hit region should be invertible");
825        scene.push_hit(
826            1,
827            vec![1],
828            HitGeometry {
829                rect: Rect {
830                    x: 10.0,
831                    y: 10.0,
832                    width: 50.0,
833                    height: 20.0,
834                },
835                quad,
836                local_bounds: rect,
837                world_to_local,
838                hit_clip_bounds: None,
839                hit_clips: Vec::new(),
840            },
841            None,
842            Vec::new(),
843            vec![Rc::new(|_event: PointerEvent| {})],
844        );
845
846        assert!(
847            scene.hit_test(15.0, 28.0).is_empty(),
848            "point inside the quad bounds but outside the transformed quad must not hit"
849        );
850        assert_eq!(scene.hit_test(30.0, 20.0).len(), 1);
851    }
852
853    #[test]
854    fn dispatch_uses_inverse_transform_for_local_position() {
855        let local_positions = Rc::new(RefCell::new(Vec::new()));
856        let local_positions_for_handler = Rc::clone(&local_positions);
857        let click_action = ClickAction::WithPoint(Rc::new(move |point| {
858            local_positions_for_handler.borrow_mut().push(point);
859        }));
860        let local_bounds = Rect {
861            x: 0.0,
862            y: 0.0,
863            width: 20.0,
864            height: 10.0,
865        };
866        let quad = [[20.0, 10.0], [60.0, 10.0], [20.0, 30.0], [60.0, 30.0]];
867        let world_to_local = ProjectiveTransform::from_rect_to_quad(local_bounds, quad)
868            .inverse()
869            .expect("translated quad should be invertible");
870        let hit = HitRegion::with_diagnostics(HitRegionInit {
871            node_id: 1,
872            capture_path: vec![1],
873            geometry: HitGeometry {
874                rect: Rect {
875                    x: 20.0,
876                    y: 10.0,
877                    width: 40.0,
878                    height: 20.0,
879                },
880                quad,
881                local_bounds,
882                world_to_local,
883                hit_clip_bounds: None,
884                hit_clips: Vec::new(),
885            },
886            shape: None,
887            click_actions: vec![click_action],
888            pointer_inputs: Vec::new(),
889            z_index: 0,
890            diagnostics: test_diagnostics(),
891        });
892
893        hit.dispatch(PointerEvent::new(
894            PointerEventKind::Down,
895            Point { x: 25.0, y: 17.0 },
896            Point { x: 25.0, y: 17.0 },
897        ));
898
899        assert_eq!(*local_positions.borrow(), vec![Point { x: 2.5, y: 3.5 }]);
900    }
901
902    #[test]
903    fn dispatch_with_applier_counts_live_modifier_slice_lookup_misses() {
904        let handler_calls = Rc::new(Cell::new(0));
905        let handler_calls_for_handler = Rc::clone(&handler_calls);
906        let diagnostics = test_diagnostics();
907        let hit = HitRegion::with_diagnostics(HitRegionInit {
908            node_id: 42,
909            capture_path: vec![42],
910            geometry: hit_geometry_for_rect(Rect {
911                x: 0.0,
912                y: 0.0,
913                width: 50.0,
914                height: 50.0,
915            }),
916            shape: None,
917            click_actions: Vec::new(),
918            pointer_inputs: vec![Rc::new(move |_event: PointerEvent| {
919                handler_calls_for_handler.set(handler_calls_for_handler.get() + 1);
920            })],
921            z_index: 0,
922            diagnostics: Rc::clone(&diagnostics),
923        });
924        let misses_before = diagnostics.live_modifier_slice_lookup_miss_count();
925        let mut applier = MemoryApplier::new();
926
927        hit.dispatch_with_applier(
928            &mut applier,
929            PointerEvent::new(
930                PointerEventKind::Down,
931                Point { x: 10.0, y: 10.0 },
932                Point { x: 10.0, y: 10.0 },
933            ),
934        );
935
936        assert_eq!(handler_calls.get(), 1);
937        assert_eq!(
938            diagnostics.live_modifier_slice_lookup_miss_count(),
939            misses_before + 1
940        );
941    }
942}