Skip to main content

repose_ui/
hit_testing.rs

1use std::cell::{Cell, RefCell};
2use std::collections::hash_map::DefaultHasher;
3use std::collections::{HashMap, HashSet};
4use std::hash::{Hash, Hasher};
5use std::sync::Arc;
6
7use repose_core::{
8    ClipOp, Frame, HitRegion, ImeAction, KeyboardActions, Rect, Sp, TextInputConfig, Transform,
9    Vec2,
10};
11
12use crate::textfield::TextFieldMetrics;
13
14#[derive(Clone)]
15pub(crate) struct HitClip {
16    world_to_local: [f64; 9],
17    rect: Rect,
18    op: ClipOp,
19}
20
21#[derive(Clone)]
22struct HitClipLink {
23    clip: HitClip,
24    parent: Option<Arc<HitClipLink>>,
25}
26
27#[derive(Clone, Default)]
28struct HitClipChain {
29    head: Option<Arc<HitClipLink>>,
30}
31
32impl HitClipChain {
33    fn iter(&self) -> HitClipIter<'_> {
34        HitClipIter {
35            next: self.head.as_deref(),
36        }
37    }
38}
39
40struct HitClipIter<'a> {
41    next: Option<&'a HitClipLink>,
42}
43
44impl<'a> Iterator for HitClipIter<'a> {
45    type Item = &'a HitClip;
46
47    fn next(&mut self) -> Option<Self::Item> {
48        let link = self.next?;
49        self.next = link.parent.as_deref();
50        Some(&link.clip)
51    }
52}
53
54#[derive(Clone)]
55pub(crate) struct HitContext {
56    local_to_world: [f64; 9],
57    world_to_local: [f64; 9],
58    clips: HitClipChain,
59    layer_depth: u32,
60    valid: bool,
61    cull_safe: bool,
62    defer_safe: bool,
63}
64
65impl Default for HitContext {
66    fn default() -> Self {
67        Self::root()
68    }
69}
70
71impl HitContext {
72    pub(crate) fn root() -> Self {
73        Self {
74            local_to_world: identity_matrix(),
75            world_to_local: identity_matrix(),
76            clips: HitClipChain::default(),
77            layer_depth: 0,
78            valid: true,
79            cull_safe: true,
80            defer_safe: true,
81        }
82    }
83
84    pub(crate) fn with_transform(&self, transform: Transform) -> Self {
85        let local = transform.projective_matrix();
86        let local_to_world =
87            Transform::compose_projective(&self.local_to_world.map(|v| v as f32), &local)
88                .map(|v| v as f64);
89        let Some(world_to_local) = invert_matrix(local_to_world) else {
90            return Self {
91                local_to_world,
92                world_to_local: self.world_to_local,
93                clips: self.clips.clone(),
94                layer_depth: self.layer_depth,
95                valid: false,
96                cull_safe: false,
97                defer_safe: false,
98            };
99        };
100        Self {
101            local_to_world,
102            world_to_local,
103            clips: self.clips.clone(),
104            layer_depth: self.layer_depth,
105            valid: self.valid,
106            cull_safe: self.cull_safe && !transform.has_perspective(),
107            defer_safe: self.defer_safe && matrix_is_identity(local),
108        }
109    }
110
111    pub(crate) fn can_defer(&self) -> bool {
112        self.valid && self.defer_safe
113    }
114
115    pub(crate) fn with_layer(&self) -> Self {
116        Self {
117            local_to_world: self.local_to_world,
118            world_to_local: self.world_to_local,
119            clips: self.clips.clone(),
120            layer_depth: self.layer_depth.saturating_add(1),
121            valid: self.valid,
122            cull_safe: self.cull_safe,
123            defer_safe: false,
124        }
125    }
126
127    pub(crate) fn with_clip(&self, rect: Rect, op: ClipOp) -> Self {
128        let link = Arc::new(HitClipLink {
129            clip: HitClip {
130                world_to_local: self.world_to_local,
131                rect,
132                op,
133            },
134            parent: self.clips.head.clone(),
135        });
136        Self {
137            local_to_world: self.local_to_world,
138            world_to_local: self.world_to_local,
139            clips: HitClipChain { head: Some(link) },
140            layer_depth: self.layer_depth,
141            valid: self.valid,
142            cull_safe: self.cull_safe,
143            defer_safe: false,
144        }
145    }
146
147    pub(crate) fn project_rect(&self, rect: Rect) -> Rect {
148        project_rect(self.local_to_world, rect)
149    }
150
151    pub(crate) fn local_to_world(&self) -> [f64; 9] {
152        self.local_to_world
153    }
154
155    pub(crate) fn to_local(&self, point: Vec2) -> Vec2 {
156        apply_matrix(self.world_to_local, point)
157    }
158
159    pub(crate) fn intersects(&self, rect: Rect, world: Rect) -> bool {
160        if !self.valid || !self.cull_safe {
161            return true;
162        }
163        intersects_rect(self.project_rect(rect), world)
164            && self
165                .clips
166                .iter()
167                .filter(|clip| clip.op == ClipOp::Intersect)
168                .all(|clip| {
169                    invert_matrix(clip.world_to_local)
170                        .map(|clip_to_world| {
171                            intersects_rect(project_rect(clip_to_world, clip.rect), world)
172                        })
173                        .unwrap_or(false)
174                })
175    }
176
177    pub(crate) fn cache_key(&self) -> u64 {
178        let mut hasher = DefaultHasher::new();
179        for value in self.local_to_world {
180            value.to_bits().hash(&mut hasher);
181        }
182        for value in self.world_to_local {
183            value.to_bits().hash(&mut hasher);
184        }
185        let mut clip_count = 0usize;
186        for clip in self.clips.iter() {
187            clip_count += 1;
188            for value in clip.world_to_local {
189                value.to_bits().hash(&mut hasher);
190            }
191            clip.rect.x.to_bits().hash(&mut hasher);
192            clip.rect.y.to_bits().hash(&mut hasher);
193            clip.rect.w.to_bits().hash(&mut hasher);
194            clip.rect.h.to_bits().hash(&mut hasher);
195            match clip.op {
196                ClipOp::Intersect => 0u8.hash(&mut hasher),
197                ClipOp::Difference => 1u8.hash(&mut hasher),
198            }
199        }
200        clip_count.hash(&mut hasher);
201        self.layer_depth.hash(&mut hasher);
202        self.valid.hash(&mut hasher);
203        self.cull_safe.hash(&mut hasher);
204        self.defer_safe.hash(&mut hasher);
205        hasher.finish()
206    }
207}
208
209#[derive(Clone)]
210pub(crate) struct HitRegionMetadata {
211    local_rect: Rect,
212    world_rect: Rect,
213    local_to_world: [f64; 9],
214    world_to_local: [f64; 9],
215    clips: HitClipChain,
216    textfield_metrics: Option<TextFieldMetrics>,
217    keyboard_actions: Option<KeyboardActions>,
218    textfield_transform_id: Option<u64>,
219}
220
221#[derive(Clone)]
222pub struct HitRegionSnapshot {
223    hit: HitRegion,
224    metadata: Option<Arc<HitRegionMetadata>>,
225}
226
227impl HitRegionSnapshot {
228    pub fn new(hit: &HitRegion) -> Self {
229        Self {
230            hit: hit.clone(),
231            metadata: metadata_for(hit),
232        }
233    }
234
235    pub fn id(&self) -> u64 {
236        self.hit.id
237    }
238
239    pub fn hit(&self) -> &HitRegion {
240        &self.hit
241    }
242
243    pub fn contains(&self, position: Vec2) -> bool {
244        contains(&self.hit, self.metadata.as_deref(), position)
245    }
246
247    pub fn to_local(&self, position: Vec2) -> Vec2 {
248        to_local(&self.hit, self.metadata.as_deref(), position)
249    }
250
251    pub fn pointer_coordinates(&self, position: Vec2) -> (Vec2, Vec2) {
252        pointer_coordinates(&self.hit, self.metadata.as_deref(), position)
253    }
254}
255
256thread_local! {
257    static HIT_METADATA: RefCell<HashMap<u64, (Arc<HitRegionMetadata>, u64)>> =
258        RefCell::new(HashMap::new());
259    static HIT_METADATA_GENERATION: Cell<u64> = const { Cell::new(0) };
260}
261
262pub(crate) fn begin_hit_metadata_frame() {
263    HIT_METADATA_GENERATION.with(|generation| {
264        generation.set(generation.get().wrapping_add(1));
265    });
266}
267
268pub(crate) fn prune_hit_metadata() {
269    let generation = HIT_METADATA_GENERATION.with(Cell::get);
270    HIT_METADATA.with(|metadata| {
271        metadata
272            .borrow_mut()
273            .retain(|_, (_, stamp)| *stamp == generation);
274    });
275}
276
277pub(crate) fn register_hit(
278    hit: &mut HitRegion,
279    context: &HitContext,
280    text_input: Option<&TextInputConfig>,
281) {
282    let local_rect = hit.rect;
283    let mut metadata = HitRegionMetadata {
284        local_rect,
285        world_rect: Rect::default(),
286        local_to_world: context.local_to_world,
287        world_to_local: context.world_to_local,
288        clips: context.clips.clone(),
289        textfield_metrics: text_input.map(TextFieldMetrics::from_config),
290        keyboard_actions: text_input.and_then(|input| input.keyboard_actions.clone()),
291        textfield_transform_id: text_input
292            .and_then(|input| input.visual_transformation.as_ref())
293            .and_then(crate::textfield::textfield_transform_id),
294    };
295    metadata.world_rect = metadata.compute_world_rect();
296    hit.rect = metadata.world_rect;
297    let generation = HIT_METADATA_GENERATION.with(Cell::get);
298    HIT_METADATA.with(|all| {
299        all.borrow_mut()
300            .insert(hit.id, (Arc::new(metadata), generation));
301    });
302}
303
304fn with_metadata<R>(hit: &HitRegion, f: impl FnOnce(&HitRegionMetadata) -> R) -> Option<R> {
305    HIT_METADATA.with(|all| {
306        let all = all.borrow();
307        let (metadata, _) = all.get(&hit.id)?;
308        (metadata.world_rect == hit.rect).then(|| f(metadata))
309    })
310}
311
312pub(crate) fn metadata_for(hit: &HitRegion) -> Option<Arc<HitRegionMetadata>> {
313    HIT_METADATA.with(|all| {
314        let all = all.borrow();
315        let (metadata, _) = all.get(&hit.id)?;
316        (metadata.world_rect == hit.rect).then(|| Arc::clone(metadata))
317    })
318}
319
320pub(crate) fn restore_metadata(id: u64, metadata: Arc<HitRegionMetadata>) {
321    let generation = HIT_METADATA_GENERATION.with(Cell::get);
322    HIT_METADATA.with(|all| {
323        all.borrow_mut().insert(id, (metadata, generation));
324    });
325}
326
327pub fn hit_test_frame(frame: &Frame, position: Vec2) -> Option<&HitRegion> {
328    frame_hit_order(frame, position, true)
329        .first()
330        .copied()
331        .and_then(|index| frame.hit_regions.get(index))
332}
333
334pub fn hit_test_enabled_frame(frame: &Frame, position: Vec2) -> Option<&HitRegion> {
335    frame_hit_order(frame, position, false)
336        .first()
337        .copied()
338        .and_then(|index| frame.hit_regions.get(index))
339}
340
341pub fn hit_test_frame_path(frame: &Frame, position: Vec2) -> Vec<u64> {
342    hit_test_path(frame, position, false)
343}
344
345pub fn hit_test_frame_regions(frame: &Frame, position: Vec2) -> Vec<u64> {
346    frame_hit_order(frame, position, false)
347        .into_iter()
348        .map(|index| frame.hit_regions[index].id)
349        .collect()
350}
351
352pub fn hit_region_contains(hit: &HitRegion, position: Vec2) -> bool {
353    with_metadata(hit, |metadata| contains(hit, Some(metadata), position))
354        .unwrap_or_else(|| hit.rect.contains(position))
355}
356
357pub fn hit_region_to_local(hit: &HitRegion, position: Vec2) -> Vec2 {
358    with_metadata(hit, |metadata| to_local(hit, Some(metadata), position)).unwrap_or(position)
359}
360
361pub fn hit_region_pointer_coordinates(hit: &HitRegion, position: Vec2) -> (Vec2, Vec2) {
362    with_metadata(hit, |metadata| {
363        pointer_coordinates(hit, Some(metadata), position)
364    })
365    .unwrap_or_else(|| {
366        let origin = Vec2 {
367            x: hit.rect.x,
368            y: hit.rect.y,
369        };
370        (origin, position - origin)
371    })
372}
373
374pub fn hit_region_local_rect(hit: &HitRegion) -> Rect {
375    with_metadata(hit, |metadata| metadata.local_rect).unwrap_or(hit.rect)
376}
377
378pub fn hit_region_transformed_origin(hit: &HitRegion) -> Vec2 {
379    with_metadata(hit, |metadata| {
380        apply_matrix(
381            metadata.local_to_world,
382            Vec2 {
383                x: metadata.local_rect.x,
384                y: metadata.local_rect.y,
385            },
386        )
387    })
388    .unwrap_or(Vec2 {
389        x: hit.rect.x,
390        y: hit.rect.y,
391    })
392}
393
394pub fn hit_region_world_rect(hit: &HitRegion) -> Rect {
395    with_metadata(hit, |metadata| metadata.world_rect).unwrap_or(hit.rect)
396}
397
398pub fn dispatch_keyboard_action(hit: &HitRegion, action: ImeAction, default: &dyn Fn()) -> bool {
399    let callback = with_metadata(hit, |metadata| {
400        metadata.keyboard_actions.as_ref().and_then(|actions| {
401            let callback = match action {
402                ImeAction::Done => &actions.on_done,
403                ImeAction::Go => &actions.on_go,
404                ImeAction::Next => &actions.on_next,
405                ImeAction::Previous => &actions.on_previous,
406                ImeAction::Search => &actions.on_search,
407                ImeAction::Send => &actions.on_send,
408                ImeAction::Unspecified | ImeAction::None | ImeAction::Default => return None,
409            };
410            callback.clone()
411        })
412    });
413    let Some(callback) = callback else {
414        default();
415        return true;
416    };
417    if let Some(callback) = callback {
418        let scope = DefaultKeyboardActionScope { action, default };
419        callback(&scope);
420    } else {
421        default();
422    }
423    true
424}
425
426pub(crate) fn textfield_metrics(hit: &HitRegion) -> TextFieldMetrics {
427    with_metadata(hit, |metadata| metadata.textfield_metrics.clone())
428        .flatten()
429        .unwrap_or_else(|| {
430            let mut metrics = TextFieldMetrics::default();
431            if hit.tf_font_size != Sp::ZERO {
432                metrics.font_px = hit.tf_font_size.to_px().0;
433                metrics.line_height_px = metrics.font_px;
434            }
435            metrics
436        })
437}
438
439pub(crate) fn textfield_transform_id(hit: &HitRegion) -> Option<u64> {
440    with_metadata(hit, |metadata| metadata.textfield_transform_id).flatten()
441}
442
443fn contains(hit: &HitRegion, metadata: Option<&HitRegionMetadata>, position: Vec2) -> bool {
444    let Some(metadata) = metadata else {
445        return hit.rect.contains(position);
446    };
447    let local = apply_matrix(metadata.world_to_local, position);
448    metadata.local_rect.contains(local)
449        && metadata
450            .clips
451            .iter()
452            .all(|clip| clip_allows(clip, position))
453}
454
455fn to_local(_hit: &HitRegion, metadata: Option<&HitRegionMetadata>, position: Vec2) -> Vec2 {
456    metadata.map_or(position, |metadata| {
457        apply_matrix(metadata.world_to_local, position)
458    })
459}
460
461fn pointer_coordinates(
462    hit: &HitRegion,
463    metadata: Option<&HitRegionMetadata>,
464    position: Vec2,
465) -> (Vec2, Vec2) {
466    let Some(metadata) = metadata else {
467        let origin = Vec2 {
468            x: hit.rect.x,
469            y: hit.rect.y,
470        };
471        return (origin, position - origin);
472    };
473    let local = apply_matrix(metadata.world_to_local, position);
474    let local_origin = Vec2 {
475        x: metadata.local_rect.x,
476        y: metadata.local_rect.y,
477    };
478    let local_position = local - local_origin;
479    let origin = apply_matrix(metadata.local_to_world, local_origin);
480    (origin, local_position)
481}
482
483fn hit_test_path(frame: &Frame, position: Vec2, include_disabled: bool) -> Vec<u64> {
484    let order = frame_hit_order(frame, position, include_disabled);
485    let Some(&top) = order.first() else {
486        return Vec::new();
487    };
488    let exact: HashSet<u64> = order
489        .iter()
490        .map(|index| frame.hit_regions[*index].id)
491        .collect();
492    let by_id: HashMap<u64, &HitRegion> =
493        frame.hit_regions.iter().map(|hit| (hit.id, hit)).collect();
494    let mut path = Vec::new();
495    let mut current = Some(frame.hit_regions[top].id);
496    let mut seen = HashSet::new();
497    while let Some(id) = current {
498        if !seen.insert(id) {
499            break;
500        }
501        let Some(hit) = by_id.get(&id) else {
502            break;
503        };
504        if exact.contains(&id) {
505            path.push(id);
506        }
507        current = hit.parent;
508    }
509    path
510}
511
512fn frame_hit_order(frame: &Frame, position: Vec2, include_disabled: bool) -> Vec<usize> {
513    let mut order = HIT_METADATA.with(|all| {
514        let all = all.borrow();
515        frame
516            .hit_regions
517            .iter()
518            .enumerate()
519            .filter(|(_, hit)| include_disabled || !hit.disabled)
520            .filter(|(_, hit)| {
521                all.get(&hit.id)
522                    .filter(|(metadata, _)| metadata.world_rect == hit.rect)
523                    .map(|(metadata, _)| contains(hit, Some(metadata), position))
524                    .unwrap_or_else(|| hit.rect.contains(position))
525            })
526            .map(|(index, _)| index)
527            .collect::<Vec<_>>()
528    });
529    order.sort_unstable_by(|left, right| {
530        let left_hit = &frame.hit_regions[*left];
531        let right_hit = &frame.hit_regions[*right];
532        right_hit
533            .z_index
534            .partial_cmp(&left_hit.z_index)
535            .unwrap_or(std::cmp::Ordering::Equal)
536            .then_with(|| right.cmp(left))
537    });
538    order
539}
540
541struct DefaultKeyboardActionScope<'a> {
542    action: ImeAction,
543    default: &'a dyn Fn(),
544}
545
546impl repose_core::KeyboardActionScope for DefaultKeyboardActionScope<'_> {
547    fn default_keyboard_action(&self, action: ImeAction) {
548        if action == self.action {
549            (self.default)();
550        }
551    }
552}
553
554impl HitRegionMetadata {
555    fn compute_world_rect(&self) -> Rect {
556        let mut world = project_rect(self.local_to_world, self.local_rect);
557        for clip in self
558            .clips
559            .iter()
560            .filter(|clip| clip.op == ClipOp::Intersect)
561        {
562            let Some(clip_to_world) = invert_matrix(clip.world_to_local) else {
563                continue;
564            };
565            let projected = project_rect(clip_to_world, clip.rect);
566            world = intersect_rect(world, projected).unwrap_or(Rect {
567                x: world.x,
568                y: world.y,
569                w: 0.0,
570                h: 0.0,
571            });
572            if world.w <= 0.0 || world.h <= 0.0 {
573                break;
574            }
575        }
576        world
577    }
578}
579
580fn intersects_rect(a: Rect, b: Rect) -> bool {
581    let x = a.x.max(b.x);
582    let y = a.y.max(b.y);
583    let right = (a.x + a.w).min(b.x + b.w);
584    let bottom = (a.y + a.h).min(b.y + b.h);
585    right > x && bottom > y
586}
587
588fn intersect_rect(a: Rect, b: Rect) -> Option<Rect> {
589    let x = a.x.max(b.x);
590    let y = a.y.max(b.y);
591    let right = (a.x + a.w).min(b.x + b.w);
592    let bottom = (a.y + a.h).min(b.y + b.h);
593    (right > x && bottom > y).then_some(Rect {
594        x,
595        y,
596        w: right - x,
597        h: bottom - y,
598    })
599}
600
601fn clip_allows(clip: &HitClip, position: Vec2) -> bool {
602    let local = apply_matrix(clip.world_to_local, position);
603    if clip.op == ClipOp::Difference {
604        !clip.rect.contains(local)
605    } else {
606        clip.rect.contains(local)
607    }
608}
609
610fn project_rect(matrix: [f64; 9], rect: Rect) -> Rect {
611    let corners = [
612        Vec2 {
613            x: rect.x,
614            y: rect.y,
615        },
616        Vec2 {
617            x: rect.x + rect.w,
618            y: rect.y,
619        },
620        Vec2 {
621            x: rect.x + rect.w,
622            y: rect.y + rect.h,
623        },
624        Vec2 {
625            x: rect.x,
626            y: rect.y + rect.h,
627        },
628    ];
629    let mut min_x = f64::MAX;
630    let mut min_y = f64::MAX;
631    let mut max_x = f64::MIN;
632    let mut max_y = f64::MIN;
633    for corner in corners {
634        let point = apply_matrix(matrix, corner);
635        min_x = min_x.min(point.x as f64);
636        min_y = min_y.min(point.y as f64);
637        max_x = max_x.max(point.x as f64);
638        max_y = max_y.max(point.y as f64);
639    }
640    Rect {
641        x: min_x as f32,
642        y: min_y as f32,
643        w: (max_x - min_x) as f32,
644        h: (max_y - min_y) as f32,
645    }
646}
647
648fn identity_matrix() -> [f64; 9] {
649    [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
650}
651
652fn matrix_is_identity(matrix: [f32; 9]) -> bool {
653    matrix
654        .iter()
655        .zip([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0])
656        .all(|(left, right)| *left == right)
657}
658
659fn apply_matrix(matrix: [f64; 9], point: Vec2) -> Vec2 {
660    let x = point.x as f64;
661    let y = point.y as f64;
662    let w = matrix[6] * x + matrix[7] * y + matrix[8];
663    let w = if w.abs() < 1e-9 {
664        if w < 0.0 { -1e-9 } else { 1e-9 }
665    } else {
666        w
667    };
668    Vec2 {
669        x: ((matrix[0] * x + matrix[1] * y + matrix[2]) / w) as f32,
670        y: ((matrix[3] * x + matrix[4] * y + matrix[5]) / w) as f32,
671    }
672}
673
674fn invert_matrix(matrix: [f64; 9]) -> Option<[f64; 9]> {
675    let a = matrix[0];
676    let b = matrix[1];
677    let c = matrix[2];
678    let d = matrix[3];
679    let e = matrix[4];
680    let f = matrix[5];
681    let g = matrix[6];
682    let h = matrix[7];
683    let i = matrix[8];
684    let c00 = e * i - f * h;
685    let c01 = -(d * i - f * g);
686    let c02 = d * h - e * g;
687    let det = a * c00 + b * c01 + c * c02;
688    if !det.is_finite() || det.abs() < 1e-12 {
689        return None;
690    }
691    let inv = 1.0 / det;
692    Some([
693        c00 * inv,
694        -(b * i - c * h) * inv,
695        (b * f - c * e) * inv,
696        c01 * inv,
697        (a * i - c * g) * inv,
698        -(a * f - c * d) * inv,
699        c02 * inv,
700        -(a * h - b * g) * inv,
701        (a * e - b * d) * inv,
702    ])
703}
704
705#[cfg(test)]
706mod tests {
707    use super::*;
708
709    #[test]
710    fn pointer_coordinates_preserve_global_position_with_transform() {
711        let mut hit = HitRegion {
712            id: 9_876_543,
713            rect: Rect {
714                x: 10.0,
715                y: 20.0,
716                w: 100.0,
717                h: 50.0,
718            },
719            ..Default::default()
720        };
721        let context = HitContext::root().with_transform(Transform::translate(30.0, 40.0));
722        register_hit(&mut hit, &context, None);
723
724        let global = Vec2 { x: 50.0, y: 70.0 };
725        let (origin, local) = hit_region_pointer_coordinates(&hit, global);
726        assert_eq!(origin, Vec2 { x: 40.0, y: 60.0 });
727        assert_eq!(origin + local, global);
728
729        let mut event = crate::PointerEvent::new(
730            crate::PointerId(1),
731            crate::PointerKind::Mouse,
732            crate::PointerEventKind::Move,
733            global,
734            1.0,
735            crate::Modifiers::default(),
736        );
737        event.origin = origin;
738        event.position = local;
739        assert_eq!(event.position_in_window(), global);
740
741        let mut scaled = HitRegion {
742            id: 9_876_544,
743            rect: Rect {
744                x: 10.0,
745                y: 20.0,
746                w: 100.0,
747                h: 50.0,
748            },
749            ..Default::default()
750        };
751        let scaled_context = HitContext::root().with_transform(Transform {
752            scale_x: 2.0,
753            scale_y: 2.0,
754            ..Transform::identity()
755        });
756        register_hit(&mut scaled, &scaled_context, None);
757        let (origin, local) = hit_region_pointer_coordinates(&scaled, global);
758        let mut event = crate::PointerEvent::new(
759            crate::PointerId(2),
760            crate::PointerKind::Mouse,
761            crate::PointerEventKind::Move,
762            global,
763            1.0,
764            crate::Modifiers::default(),
765        );
766        event.origin = origin;
767        event.position = local;
768        assert_eq!(event.position_in_window(), global);
769    }
770}