1use std::rc::Rc;
2
3use cranpose_core::NodeId;
4use cranpose_ui_graphics::{Rect, layer_transform::layer_surface};
5use smallvec::SmallVec;
6
7use crate::{
8 graph::{HitTestNode, ProjectiveTransform, quad_bounds},
9 graph_scene::{
10 HitClip, HitGeometry, HitTargetSpec, HitTargets, Scene, SurfaceGeometry, SurfaceTarget,
11 },
12 primitive_emit::resolve_clip,
13 scene_pick::{ScenePick, SurfaceHolds},
14 scene_store::LayerRef,
15};
16
17pub trait HitGraphSink {
19 fn push_hit(
22 &mut self,
23 node_id: NodeId,
24 capture_path: &[NodeId],
25 geometry: HitGeometry<'_>,
26 hit: &HitTestNode,
27 );
28
29 fn surface_holds(&self) -> Option<&SurfaceHolds> {
33 None
34 }
35}
36
37impl HitGraphSink for Scene {
38 fn push_hit(
39 &mut self,
40 node_id: NodeId,
41 capture_path: &[NodeId],
42 geometry: HitGeometry<'_>,
43 hit: &HitTestNode,
44 ) {
45 Self::push_hit(self, node_id, capture_path, geometry, hit_spec(hit));
46 }
47
48 fn surface_holds(&self) -> Option<&SurfaceHolds> {
49 Some(&self.holds)
50 }
51}
52
53impl HitGraphSink for HitTargets<'_> {
54 fn push_hit(
55 &mut self,
56 node_id: NodeId,
57 capture_path: &[NodeId],
58 geometry: HitGeometry<'_>,
59 hit: &HitTestNode,
60 ) {
61 self.push(node_id, capture_path, geometry, hit_spec(hit));
62 }
63
64 fn surface_holds(&self) -> Option<&SurfaceHolds> {
65 Some(self.holds)
66 }
67}
68
69fn hit_spec(hit: &HitTestNode) -> HitTargetSpec<'_> {
70 HitTargetSpec {
71 shape: None,
72 handlers: &hit.handlers,
73 }
74}
75
76pub fn collect_hits_from_graph<S: HitGraphSink>(
81 layer: LayerRef<'_>,
82 parent_transform: ProjectiveTransform,
83 sink: &mut S,
84 parent_hit_clip: Option<Rect>,
85) {
86 if !layer.has_hit_targets() {
87 return;
88 }
89 let holds = sink.surface_holds().cloned();
90 let mut walk = HitWalk {
91 sink,
92 holds,
93 hit_clips: Vec::new(),
94 pointer_input_ancestors: Vec::new(),
95 capture_path: SmallVec::new(),
96 surface: None,
97 };
98 walk.visit(
99 layer,
100 parent_transform,
101 parent_hit_clip,
102 &ParentSpace::default(),
103 );
104}
105
106struct HitWalk<'s, S> {
107 sink: &'s mut S,
108 holds: Option<SurfaceHolds>,
109 hit_clips: Vec<HitClip>,
110 pointer_input_ancestors: Vec<NodeId>,
111 capture_path: SmallVec<[NodeId; 8]>,
112 surface: Option<SurfaceWalk>,
113}
114
115struct SurfaceWalk {
116 target: Rc<SurfaceTarget>,
117 clips: Vec<HitClip>,
118}
119
120#[derive(Default)]
121struct ParentSpace {
122 scene: Option<Rc<ScenePick>>,
123 scene_clips: (Option<Rect>, usize),
124 content: Option<ProjectiveTransform>,
125 content_clip: Option<Rect>,
126 behind_doorway: bool,
127 unseen: bool,
128}
129
130impl<S: HitGraphSink> HitWalk<'_, S> {
131 fn visit(
132 &mut self,
133 layer: LayerRef<'_>,
134 parent_transform: ProjectiveTransform,
135 parent_hit_clip: Option<Rect>,
136 space: &ParentSpace,
137 ) {
138 if !layer.has_hit_targets() {
139 return;
140 }
141 let Some((doorway, unseen)) = Self::seen(layer, space) else {
143 return;
144 };
145 let transform = layer.transform_to_parent().then(parent_transform);
146 let node_rect = layer.node_rect();
149 let transformed_quad = hit_quad(&transform, node_rect);
150 let transformed_rect = quad_bounds(transformed_quad);
151
152 let in_scene = space.scene.is_some();
156 if !in_scene && (transformed_rect.width <= 0.0 || transformed_rect.height <= 0.0) {
157 return;
158 }
159 let world_to_local = match transform.inverse() {
160 Some(inverse) => inverse,
161 None if in_scene => ProjectiveTransform::identity(),
162 None => return,
163 };
164
165 let (entered, content, content_clip) = self.enter_content(layer, (space, doorway));
166 let behind_doorway = space.behind_doorway || doorway;
167 let Some((hit_clip_bounds, pushed_clip)) =
168 self.push_window_clip(layer, &transform, (parent_hit_clip, in_scene))
169 else {
170 self.leave(entered, content_clip.pushed);
171 return;
172 };
173
174 if let (Some(node_id), Some(hit), false) = (
175 layer.node_id(),
176 layer.hit_test(),
177 self.hides_targets(unseen, behind_doorway),
178 ) {
179 let geometry = PlacedNode {
180 rect: transformed_rect,
181 quad: transformed_quad,
182 local_bounds: node_rect,
183 world_to_local,
184 hit_clip_bounds,
185 content,
186 content_clip: content_clip.bounds,
187 };
188 self.push_node(node_id, hit, geometry);
189 }
190
191 let pointer_input_ancestor = layer
192 .hit_test()
193 .filter(|hit| !hit.handlers.pointer_inputs().is_empty())
194 .and_then(|_| layer.node_id());
195 if let Some(node_id) = pointer_input_ancestor {
196 self.pointer_input_ancestors.push(node_id);
197 }
198
199 let children_space = ParentSpace {
200 content,
201 content_clip: content_clip.bounds,
202 behind_doorway,
203 unseen,
204 ..self.children_scene(layer, (transform, hit_clip_bounds), space)
205 };
206 for child_layer in layer.children() {
207 self.visit(child_layer, transform, hit_clip_bounds, &children_space);
208 }
209
210 if pointer_input_ancestor.is_some() {
211 let _ = self.pointer_input_ancestors.pop();
212 }
213
214 if pushed_clip {
215 let _ = self.hit_clips.pop();
216 }
217 self.leave(entered, content_clip.pushed);
218 }
219
220 fn push_node(&mut self, node_id: NodeId, hit: &HitTestNode, node: PlacedNode) {
221 self.capture_path.clear();
222 self.capture_path.push(node_id);
223 self.capture_path
224 .extend(self.pointer_input_ancestors.iter().rev().copied());
225 let content_to_local = node.content.and_then(ProjectiveTransform::inverse);
226 let surface =
227 self.surface
228 .as_ref()
229 .zip(content_to_local)
230 .map(|(surface, content_to_local)| SurfaceGeometry {
231 target: &surface.target,
232 content_to_local,
233 clip_bounds: node.content_clip,
234 clips: &surface.clips,
235 });
236 self.sink.push_hit(
237 node_id,
238 &self.capture_path,
239 HitGeometry {
240 rect: node.rect,
241 quad: node.quad,
242 local_bounds: node.local_bounds,
243 world_to_local: node.world_to_local,
244 hit_clip_bounds: node.hit_clip_bounds,
245 hit_clips: &self.hit_clips,
246 surface,
247 },
248 hit,
249 );
250 }
251
252 fn children_scene(
253 &self,
254 layer: LayerRef<'_>,
255 (transform, hit_clip_bounds): (ProjectiveTransform, Option<Rect>),
256 space: &ParentSpace,
257 ) -> ParentSpace {
258 let through_portal = space
259 .scene
260 .as_ref()
261 .is_some_and(|scene| scene.holds_scene(layer));
262 let shown = match (layer.wraps(), through_portal) {
263 (None, false) => ScenePick::of(layer, transform, self.holds.as_ref()).map(Rc::new),
264 _ => None,
265 };
266 let (scene, scene_clips) = match shown {
267 Some(pick) => (Some(pick), (hit_clip_bounds, self.hit_clips.len())),
268 None => (space.scene.clone(), space.scene_clips),
269 };
270 ParentSpace {
271 scene,
272 scene_clips,
273 ..ParentSpace::default()
274 }
275 }
276
277 fn enter_surface(
278 &mut self,
279 layer: LayerRef<'_>,
280 space: &ParentSpace,
281 ) -> Option<Option<SurfaceWalk>> {
282 let pick = space.scene.as_ref()?;
283 let index = pick.index_of(layer)?;
284 let (outer_clip_bounds, outer_clip_count) = space.scene_clips;
285 let target = Rc::new(SurfaceTarget {
286 pick: Rc::clone(pick),
287 index,
288 outer_clip_bounds,
289 outer_clips: self.hit_clips[..outer_clip_count.min(self.hit_clips.len())].to_vec(),
290 });
291 Some(self.surface.replace(SurfaceWalk {
292 target,
293 clips: Vec::new(),
294 }))
295 }
296
297 fn doorway(layer: LayerRef<'_>, space: &ParentSpace) -> Option<bool> {
298 let Some(pick) = space.scene.as_ref().filter(|pick| pick.holds_scene(layer)) else {
299 return Some(false);
300 };
301 pick.sees_scene(layer).then_some(true)
302 }
303
304 fn seen(layer: LayerRef<'_>, space: &ParentSpace) -> Option<(bool, bool)> {
305 let doorway = Self::doorway(layer, space)?;
306 let unseen = !doorway && (space.unseen || Self::unseen_surface(layer, space));
307 Some((doorway, unseen))
308 }
309
310 fn hides_targets(&self, unseen: bool, behind_doorway: bool) -> bool {
311 unseen || (behind_doorway && self.surface.is_none())
312 }
313
314 fn unseen_surface(layer: LayerRef<'_>, space: &ParentSpace) -> bool {
315 space.scene.as_ref().is_some_and(|pick| {
316 layer_surface(layer.graphics_layer()).is_some() && pick.index_of(layer).is_none()
317 })
318 }
319
320 fn enter_content(
321 &mut self,
322 layer: LayerRef<'_>,
323 (space, doorway): (&ParentSpace, bool),
324 ) -> (
325 Option<Option<SurfaceWalk>>,
326 Option<ProjectiveTransform>,
327 ContentClip,
328 ) {
329 if doorway {
330 let unclipped = ContentClip {
331 bounds: None,
332 pushed: false,
333 };
334 return (Some(self.surface.take()), None, unclipped);
335 }
336 let entered = self.enter_surface(layer, space);
337 let (content, above) = match entered {
340 Some(_) => (Some(ProjectiveTransform::identity()), None),
341 None => (
342 space
343 .content
344 .map(|parent| layer.transform_to_parent().then(parent)),
345 space.content_clip,
346 ),
347 };
348 let content_clip = self.push_content_clip(layer, content, above);
349 (entered, content, content_clip)
350 }
351
352 fn push_window_clip(
353 &mut self,
354 layer: LayerRef<'_>,
355 transform: &ProjectiveTransform,
356 (parent_hit_clip, in_scene): (Option<Rect>, bool),
357 ) -> Option<(Option<Rect>, bool)> {
358 let Some(local_clip) = layer.clip_rect().filter(|_| !in_scene) else {
359 return Some((parent_hit_clip, false));
360 };
361 let quad = hit_quad(transform, local_clip);
362 let bounds = quad_bounds(quad);
363 let resolved = resolve_clip(parent_hit_clip, Some(bounds));
364 if resolved.is_some_and(|clip| clip.is_empty()) {
365 return None;
366 }
367 self.hit_clips.push(HitClip { quad, bounds });
368 Some((resolved, true))
369 }
370
371 fn push_content_clip(
372 &mut self,
373 layer: LayerRef<'_>,
374 content: Option<ProjectiveTransform>,
375 parent_clip: Option<Rect>,
376 ) -> ContentClip {
377 let unclipped = ContentClip {
378 bounds: parent_clip,
379 pushed: false,
380 };
381 let (Some(surface), Some(content)) = (self.surface.as_mut(), content) else {
382 return unclipped;
383 };
384 let Some(local_clip) = layer.clip_rect() else {
385 return unclipped;
386 };
387 let quad = hit_quad(&content, local_clip);
388 let bounds = quad_bounds(quad);
389 surface.clips.push(HitClip { quad, bounds });
390 ContentClip {
391 bounds: resolve_clip(parent_clip, Some(bounds)),
392 pushed: true,
393 }
394 }
395
396 fn leave(&mut self, entered: Option<Option<SurfaceWalk>>, pushed_content_clip: bool) {
397 if pushed_content_clip && let Some(surface) = self.surface.as_mut() {
398 let _ = surface.clips.pop();
399 }
400 if let Some(previous) = entered {
401 self.surface = previous;
402 }
403 }
404}
405
406struct PlacedNode {
407 rect: Rect,
408 quad: [[f32; 2]; 4],
409 local_bounds: Rect,
410 world_to_local: ProjectiveTransform,
411 hit_clip_bounds: Option<Rect>,
412 content: Option<ProjectiveTransform>,
413 content_clip: Option<Rect>,
414}
415
416struct ContentClip {
417 bounds: Option<Rect>,
418 pushed: bool,
419}
420
421fn hit_quad(transform: &ProjectiveTransform, rect: Rect) -> [[f32; 2]; 4] {
422 if transform.rect_in_front(rect) {
423 return transform.map_rect(rect);
424 }
425 let bounds = transform.bounds_for_rect(rect);
426 let (right, bottom) = (bounds.x + bounds.width, bounds.y + bounds.height);
427 [
428 [bounds.x, bounds.y],
429 [right, bounds.y],
430 [bounds.x, bottom],
431 [right, bottom],
432 ]
433}
434
435#[cfg(test)]
436#[path = "tests/hit_graph_tests.rs"]
437mod tests;