1use super::*;
4
5#[derive(Clone, Copy, Debug)]
6pub(crate) struct HitClipStep {
7 pub clip: Option<strata_layout::Rect>,
8 pub is_abs_cb: bool,
9 pub is_fixed_cb: bool,
10}
11
12impl AppRuntime {
13 #[inline]
15 pub fn unproject_point(layout: &LayoutNode, px: f32, py: f32) -> (f32, f32) {
16 if layout.style.has_transform() {
17 let mat = layout.style.compute_transform_matrix(
18 layout.geometry.border_box.width,
19 layout.geometry.border_box.height,
20 );
21 if let Some(inv) = mat.inverse() {
22 let lx = px - layout.geometry.border_box.x;
23 let ly = py - layout.geometry.border_box.y;
24 let (un_lx, un_ly) = inv.transform_point(lx, ly);
25 (
26 layout.geometry.border_box.x + un_lx,
27 layout.geometry.border_box.y + un_ly,
28 )
29 } else {
30 (px, py)
31 }
32 } else {
33 (px, py)
34 }
35 }
36
37 #[inline]
40 pub(crate) fn hit_clip_step_for(layout: &LayoutNode) -> HitClipStep {
41 let is_fixed_cb = layout.style.establishes_fixed_containing_block();
42 let is_abs_cb =
43 layout.style.position != strata_style::types::Position::Static || is_fixed_cb;
44 let clips = layout.style.overflow_x != strata_style::types::Overflow::Visible
45 || layout.style.overflow_y != strata_style::types::Overflow::Visible;
46 HitClipStep {
47 clip: if clips {
48 Some(layout.geometry.border_box)
49 } else {
50 None
51 },
52 is_abs_cb,
53 is_fixed_cb,
54 }
55 }
56
57 #[inline]
58 pub(crate) fn effective_hit_clip_steps<'a>(
59 node: &LayoutNode,
60 steps: &'a [HitClipStep],
61 ) -> &'a [HitClipStep] {
62 match node.style.position {
63 strata_style::types::Position::Fixed => {
64 if let Some(last_idx) = steps.iter().rposition(|s| s.is_fixed_cb) {
65 &steps[..=last_idx]
66 } else {
67 &[]
68 }
69 }
70 strata_style::types::Position::Absolute => {
71 if let Some(last_idx) = steps.iter().rposition(|s| s.is_abs_cb) {
72 &steps[..=last_idx]
73 } else {
74 &[]
75 }
76 }
77 _ => steps,
78 }
79 }
80
81 #[inline]
82 pub(crate) fn hit_clip_steps_allow(steps: &[HitClipStep], px: f32, py: f32) -> bool {
83 steps
84 .iter()
85 .all(|s| s.clip.is_none_or(|r| r.contains_point(px, py)))
86 }
87
88 #[inline]
90 pub fn establishes_stacking_context(node: &LayoutNode, is_root: bool) -> bool {
91 node.establishes_stacking_context_in(None, is_root)
92 }
93
94 #[inline]
96 pub fn establishes_stacking_context_in(
97 node: &LayoutNode,
98 parent_display: Option<strata_style::types::Display>,
99 is_root: bool,
100 ) -> bool {
101 node.establishes_stacking_context_in(parent_display, is_root)
102 }
103
104 pub(crate) fn paired_blueprint_children<'a>(
107 node_bp: &'a NodeBlueprint,
108 node_layout: &'a LayoutNode,
109 ) -> Vec<(usize, &'a NodeBlueprint, &'a LayoutNode)> {
110 let mut flat_children = Vec::new();
111 match node_bp {
112 NodeBlueprint::Element { children, .. } | NodeBlueprint::Fragment(children) => {
113 for c in children {
114 Self::flatten_blueprints(c, &mut flat_children);
115 }
116 }
117 _ => {}
118 }
119
120 let mut out = Vec::with_capacity(node_layout.children.len());
121 for (idx, child_layout) in node_layout.children.iter().enumerate() {
122 let resolved_bp = if child_layout.entity_id.is_valid() {
123 if let Some(&cand) = flat_children.get(idx) {
124 if cand.entity_id() == Some(child_layout.entity_id) {
125 Some(cand)
126 } else {
127 node_bp
128 .find_by_entity_id(child_layout.entity_id)
129 .or(Some(cand))
130 }
131 } else {
132 node_bp.find_by_entity_id(child_layout.entity_id)
133 }
134 } else {
135 flat_children.get(idx).copied()
136 };
137
138 if let Some(child_bp) = resolved_bp {
139 out.push((idx, child_bp, child_layout));
140 }
141 }
142 out
143 }
144
145 fn collect_top_layer_portals<'a>(
146 bp: &'a NodeBlueprint,
147 layout: &'a LayoutNode,
148 out: &mut Vec<(&'a NodeBlueprint, &'a LayoutNode)>,
149 depth: usize,
150 ) {
151 if depth >= 256 {
152 return;
153 }
154 for (_idx, child_bp, child_layout) in Self::paired_blueprint_children(bp, layout) {
155 if child_layout.style.display == strata_style::types::Display::None {
156 continue;
157 }
158 if child_layout.is_top_layer {
159 out.push((child_bp, child_layout));
160 }
161 if !child_layout.children.is_empty() {
162 Self::collect_top_layer_portals(child_bp, child_layout, out, depth + 1);
163 }
164 }
165 }
166
167 fn collect_hoisted_stacking_participants<'a>(
168 bp: &'a NodeBlueprint,
169 layout: &'a LayoutNode,
170 steps: &[HitClipStep],
171 negative_z: &mut Vec<(&'a NodeBlueprint, &'a LayoutNode, Vec<HitClipStep>)>,
172 auto_or_zero_z: &mut Vec<(&'a NodeBlueprint, &'a LayoutNode, bool, Vec<HitClipStep>)>,
173 positive_z: &mut Vec<(&'a NodeBlueprint, &'a LayoutNode, Vec<HitClipStep>)>,
174 depth: usize,
175 ) {
176 if depth >= 256 {
177 return;
178 }
179 let parent_display = Some(layout.style.display);
180 for (_idx, child_bp, child_layout) in Self::paired_blueprint_children(bp, layout) {
181 if child_layout.style.display == strata_style::types::Display::None
182 || child_layout.is_top_layer
183 {
184 continue;
185 }
186 if Self::establishes_stacking_context_in(child_layout, parent_display, false) {
187 let eff_steps = Self::effective_hit_clip_steps(child_layout, steps).to_vec();
188 let z = child_layout.style.z_index.unwrap_or(0);
189 if z < 0 {
190 negative_z.push((child_bp, child_layout, eff_steps));
191 } else if z > 0 {
192 positive_z.push((child_bp, child_layout, eff_steps));
193 } else {
194 auto_or_zero_z.push((child_bp, child_layout, true, eff_steps));
195 }
196 } else {
197 let eff_for_child = Self::effective_hit_clip_steps(child_layout, steps);
198 if child_layout.style.position != strata_style::types::Position::Static {
199 auto_or_zero_z.push((child_bp, child_layout, false, eff_for_child.to_vec()));
200 }
201 let mut child_steps = Vec::with_capacity(eff_for_child.len() + 1);
202 child_steps.extend_from_slice(eff_for_child);
203 let step = Self::hit_clip_step_for(child_layout);
204 if step.clip.is_some() || step.is_abs_cb || step.is_fixed_cb {
205 child_steps.push(step);
206 }
207 Self::collect_hoisted_stacking_participants(
208 child_bp,
209 child_layout,
210 &child_steps,
211 negative_z,
212 auto_or_zero_z,
213 positive_z,
214 depth + 1,
215 );
216 }
217 }
218 }
219
220 fn hit_test_direct_in_flow_children<'a, T, F>(
221 bp: &'a NodeBlueprint,
222 layout: &'a LayoutNode,
223 px: f32,
224 py: f32,
225 depth: usize,
226 test_self: &mut F,
227 ) -> Option<T>
228 where
229 F: FnMut(&'a NodeBlueprint, &'a LayoutNode, f32, f32) -> Option<T>,
230 {
231 let paired = Self::paired_blueprint_children(bp, layout);
232 if paired.is_empty() {
233 return None;
234 }
235
236 let parent_display = Some(layout.style.display);
237 let is_in_flow_non_sc = |c: &LayoutNode| {
238 c.style.display != strata_style::types::Display::None
239 && !c.is_top_layer
240 && !Self::establishes_stacking_context_in(c, parent_display, false)
241 && c.style.position == strata_style::types::Position::Static
242 };
243 let is_inline_level = |c: &LayoutNode| {
244 matches!(
245 c.style.display,
246 strata_style::types::Display::Inline
247 | strata_style::types::Display::InlineBlock
248 | strata_style::types::Display::InlineFlex
249 | strata_style::types::Display::InlineGrid
250 )
251 };
252
253 for &(_idx, child_bp, child_layout) in paired.iter().rev() {
255 if is_in_flow_non_sc(child_layout)
256 && child_layout.style.float == strata_style::types::Float::None
257 && is_inline_level(child_layout)
258 {
259 if let Some(hit) = Self::hit_test_in_flow_non_hoisted(
260 child_bp,
261 child_layout,
262 px,
263 py,
264 &[],
265 depth + 1,
266 test_self,
267 ) {
268 return Some(hit);
269 }
270 }
271 }
272
273 for &(_idx, child_bp, child_layout) in paired.iter().rev() {
275 if is_in_flow_non_sc(child_layout)
276 && child_layout.style.float != strata_style::types::Float::None
277 {
278 if let Some(hit) = Self::hit_test_in_flow_non_hoisted(
279 child_bp,
280 child_layout,
281 px,
282 py,
283 &[],
284 depth + 1,
285 test_self,
286 ) {
287 return Some(hit);
288 }
289 }
290 }
291
292 for &(_idx, child_bp, child_layout) in paired.iter().rev() {
294 if is_in_flow_non_sc(child_layout)
295 && child_layout.style.float == strata_style::types::Float::None
296 && !is_inline_level(child_layout)
297 {
298 if let Some(hit) = Self::hit_test_in_flow_non_hoisted(
299 child_bp,
300 child_layout,
301 px,
302 py,
303 &[],
304 depth + 1,
305 test_self,
306 ) {
307 return Some(hit);
308 }
309 }
310 }
311
312 None
313 }
314
315 fn hit_test_in_flow_non_hoisted<'a, T, F>(
316 bp: &'a NodeBlueprint,
317 layout: &'a LayoutNode,
318 px: f32,
319 py: f32,
320 inherited_steps: &[HitClipStep],
321 depth: usize,
322 test_self: &mut F,
323 ) -> Option<T>
324 where
325 F: FnMut(&'a NodeBlueprint, &'a LayoutNode, f32, f32) -> Option<T>,
326 {
327 if depth >= 256 || layout.style.display == strata_style::types::Display::None {
328 return None;
329 }
330 let eff_inherited = Self::effective_hit_clip_steps(layout, inherited_steps);
331 if !Self::hit_clip_steps_allow(eff_inherited, px, py) {
332 return None;
333 }
334 if (layout.style.overflow_x != strata_style::types::Overflow::Visible
335 || layout.style.overflow_y != strata_style::types::Overflow::Visible)
336 && !layout.geometry.border_box.contains_point(px, py)
337 {
338 return None;
339 }
340 if let Some(hit) =
341 Self::hit_test_direct_in_flow_children(bp, layout, px, py, depth, test_self)
342 {
343 return Some(hit);
344 }
345 test_self(bp, layout, px, py)
346 }
347
348 fn hit_test_stacking_context_with<'a, T, F>(
349 bp: &'a NodeBlueprint,
350 layout: &'a LayoutNode,
351 px: f32,
352 py: f32,
353 inherited_steps: &[HitClipStep],
354 depth: usize,
355 test_self: &mut F,
356 ) -> Option<T>
357 where
358 F: FnMut(&'a NodeBlueprint, &'a LayoutNode, f32, f32) -> Option<T>,
359 {
360 if depth >= 256 || layout.style.display == strata_style::types::Display::None {
361 return None;
362 }
363
364 if depth == 0 {
365 let mut top_layer_portals = Vec::new();
366 Self::collect_top_layer_portals(bp, layout, &mut top_layer_portals, 0);
367 for &(p_bp, p_layout) in top_layer_portals.iter().rev() {
368 if let Some(hit) =
369 Self::hit_test_stacking_context_with(p_bp, p_layout, px, py, &[], 1, test_self)
370 {
371 return Some(hit);
372 }
373 }
374 }
375
376 let eff_inherited = Self::effective_hit_clip_steps(layout, inherited_steps);
377 let inherited_allows = Self::hit_clip_steps_allow(eff_inherited, px, py);
378
379 let (px, py) = Self::unproject_point(layout, px, py);
380
381 let own_clips = layout.style.overflow_x != strata_style::types::Overflow::Visible
382 || layout.style.overflow_y != strata_style::types::Overflow::Visible;
383 let own_allows = !own_clips || layout.geometry.border_box.contains_point(px, py);
384
385 let mut base_steps = Vec::with_capacity(eff_inherited.len() + 1);
386 base_steps.extend_from_slice(eff_inherited);
387 let own_step = Self::hit_clip_step_for(layout);
388 if own_step.clip.is_some() || own_step.is_abs_cb || own_step.is_fixed_cb {
389 base_steps.push(own_step);
390 }
391
392 let mut negative_z = Vec::new();
393 let mut auto_or_zero_z = Vec::new();
394 let mut positive_z = Vec::new();
395 Self::collect_hoisted_stacking_participants(
396 bp,
397 layout,
398 &base_steps,
399 &mut negative_z,
400 &mut auto_or_zero_z,
401 &mut positive_z,
402 depth + 1,
403 );
404
405 negative_z.sort_by_key(|(_, l, _)| l.style.z_index.unwrap_or(0));
406 positive_z.sort_by_key(|(_, l, _)| l.style.z_index.unwrap_or(0));
407
408 for (c_bp, c_layout, c_steps) in positive_z.iter().rev() {
410 if let Some(hit) = Self::hit_test_stacking_context_with(
411 c_bp,
412 c_layout,
413 px,
414 py,
415 c_steps,
416 depth + 1,
417 test_self,
418 ) {
419 return Some(hit);
420 }
421 }
422
423 for (c_bp, c_layout, is_sc, c_steps) in auto_or_zero_z.iter().rev() {
425 let hit = if *is_sc {
426 Self::hit_test_stacking_context_with(
427 c_bp,
428 c_layout,
429 px,
430 py,
431 c_steps,
432 depth + 1,
433 test_self,
434 )
435 } else {
436 Self::hit_test_in_flow_non_hoisted(
437 c_bp,
438 c_layout,
439 px,
440 py,
441 c_steps,
442 depth + 1,
443 test_self,
444 )
445 };
446 if hit.is_some() {
447 return hit;
448 }
449 }
450
451 if inherited_allows && own_allows {
453 if let Some(hit) =
454 Self::hit_test_direct_in_flow_children(bp, layout, px, py, depth, test_self)
455 {
456 return Some(hit);
457 }
458 }
459
460 for (c_bp, c_layout, c_steps) in negative_z.iter().rev() {
462 if let Some(hit) = Self::hit_test_stacking_context_with(
463 c_bp,
464 c_layout,
465 px,
466 py,
467 c_steps,
468 depth + 1,
469 test_self,
470 ) {
471 return Some(hit);
472 }
473 }
474
475 if inherited_allows {
477 test_self(bp, layout, px, py)
478 } else {
479 None
480 }
481 }
482
483 pub fn hit_test_event_with_box_and_entity<'a>(
486 blueprint: &'a NodeBlueprint,
487 layout: &'a LayoutNode,
488 px: f32,
489 py: f32,
490 event_type: strata_dom::EventType,
491 ) -> Option<(
492 std::rc::Rc<dyn Fn(strata_dom::Event)>,
493 &'a strata_layout::Rect,
494 strata_primitives::EntityId,
495 )> {
496 Self::hit_test_stacking_context_with(
497 blueprint,
498 layout,
499 px,
500 py,
501 &[],
502 0,
503 &mut |bp, node_layout, lx, ly| {
504 if matches!(
505 node_layout.style.visibility,
506 strata_style::types::Visibility::Hidden
507 ) || matches!(
508 node_layout.style.pointer_events,
509 strata_style::types::PointerEvents::None
510 ) {
511 return None;
512 }
513 if let NodeBlueprint::Element { attributes, .. } = bp {
514 if node_layout.geometry.border_box.contains_point(lx, ly) {
515 for attr in attributes {
516 if let strata_dom::Attribute::EventListener { event: ev, handler } =
517 attr
518 {
519 if *ev == event_type {
520 return Some((
521 handler.clone(),
522 &node_layout.geometry.border_box,
523 node_layout.entity_id,
524 ));
525 }
526 }
527 }
528 }
529 }
530 None
531 },
532 )
533 }
534
535 pub fn hit_test_event_with_box<'a>(
538 blueprint: &'a NodeBlueprint,
539 layout: &'a LayoutNode,
540 px: f32,
541 py: f32,
542 event_type: strata_dom::EventType,
543 ) -> Option<(
544 std::rc::Rc<dyn Fn(strata_dom::Event)>,
545 &'a strata_layout::Rect,
546 )> {
547 Self::hit_test_event_with_box_and_entity(blueprint, layout, px, py, event_type)
548 .map(|(h, r, _)| (h, r))
549 }
550
551 pub fn find_event_at_or_above_path<'a>(
554 blueprint: &'a NodeBlueprint,
555 layout: &'a LayoutNode,
556 path: &[usize],
557 event_type: strata_dom::EventType,
558 ) -> Option<(
559 std::rc::Rc<dyn Fn(strata_dom::Event)>,
560 &'a strata_layout::Rect,
561 strata_primitives::EntityId,
562 )> {
563 for len in (0..=path.len()).rev() {
564 let prefix = &path[..len];
565 let Some(bp) = Self::find_blueprint_at_path(blueprint, prefix) else {
566 continue;
567 };
568 let mut node_layout = layout;
569 let mut valid = true;
570 for &idx in prefix {
571 if let Some(child) = node_layout.children.get(idx) {
572 node_layout = child;
573 } else {
574 valid = false;
575 break;
576 }
577 }
578 if !valid {
579 continue;
580 }
581 if let NodeBlueprint::Element { attributes, .. } = bp {
582 for attr in attributes {
583 if let strata_dom::Attribute::EventListener { event: ev, handler } = attr {
584 if *ev == event_type {
585 return Some((
586 handler.clone(),
587 &node_layout.geometry.border_box,
588 node_layout.entity_id,
589 ));
590 }
591 }
592 }
593 }
594 }
595 None
596 }
597
598 pub fn hit_test_event(
601 blueprint: &NodeBlueprint,
602 layout: &LayoutNode,
603 px: f32,
604 py: f32,
605 event_type: strata_dom::EventType,
606 ) -> Option<std::rc::Rc<dyn Fn(strata_dom::Event)>> {
607 Self::hit_test_event_with_box(blueprint, layout, px, py, event_type).map(|(h, _)| h)
608 }
609
610 pub fn hit_test_click(
612 blueprint: &NodeBlueprint,
613 layout: &LayoutNode,
614 px: f32,
615 py: f32,
616 ) -> Option<std::rc::Rc<dyn Fn(strata_dom::Event)>> {
617 Self::hit_test_event(blueprint, layout, px, py, strata_dom::EventType::Click)
618 }
619
620 pub fn hit_test_cursor(
622 blueprint: &NodeBlueprint,
623 layout: &LayoutNode,
624 px: f32,
625 py: f32,
626 ) -> strata_platform::window::CursorIcon {
627 Self::hit_test_stacking_context_with(
628 blueprint,
629 layout,
630 px,
631 py,
632 &[],
633 0,
634 &mut |bp, node_layout, lx, ly| {
635 if matches!(
636 node_layout.style.visibility,
637 strata_style::types::Visibility::Hidden
638 ) || matches!(
639 node_layout.style.pointer_events,
640 strata_style::types::PointerEvents::None
641 ) {
642 return None;
643 }
644 if let NodeBlueprint::Element {
645 tag,
646 classes,
647 attributes,
648 ..
649 } = bp
650 {
651 if node_layout.geometry.border_box.contains_point(lx, ly) {
652 match node_layout.style.cursor {
653 strata_style::types::Cursor::Pointer => {
654 return Some(strata_platform::window::CursorIcon::Pointer)
655 }
656 strata_style::types::Cursor::Text => {
657 return Some(strata_platform::window::CursorIcon::Text)
658 }
659 strata_style::types::Cursor::Crosshair => {
660 return Some(strata_platform::window::CursorIcon::Crosshair)
661 }
662 strata_style::types::Cursor::Move => {
663 return Some(strata_platform::window::CursorIcon::Move)
664 }
665 strata_style::types::Cursor::NotAllowed => {
666 return Some(strata_platform::window::CursorIcon::NotAllowed)
667 }
668 strata_style::types::Cursor::Wait => {
669 return Some(strata_platform::window::CursorIcon::Wait)
670 }
671 strata_style::types::Cursor::Grab => {
672 return Some(strata_platform::window::CursorIcon::Grab)
673 }
674 strata_style::types::Cursor::Grabbing => {
675 return Some(strata_platform::window::CursorIcon::Grabbing)
676 }
677 strata_style::types::Cursor::ColResize => {
678 return Some(strata_platform::window::CursorIcon::ColResize)
679 }
680 strata_style::types::Cursor::RowResize => {
681 return Some(strata_platform::window::CursorIcon::RowResize)
682 }
683 strata_style::types::Cursor::Help => {
684 return Some(strata_platform::window::CursorIcon::Help)
685 }
686 strata_style::types::Cursor::None => {
687 return Some(strata_platform::window::CursorIcon::None)
688 }
689 strata_style::types::Cursor::Default => {}
690 }
691
692 let has_pointer = matches!(tag, Tag::Button | Tag::A)
693 || classes.iter().any(|c| {
694 c.split_whitespace()
695 .any(|tok| tok == "cursor-pointer" || tok == "pointer")
696 })
697 || attributes.iter().any(|a| {
698 matches!(
699 a,
700 strata_dom::Attribute::EventListener {
701 event: strata_dom::EventType::Click,
702 ..
703 }
704 )
705 });
706
707 if has_pointer {
708 return Some(strata_platform::window::CursorIcon::Pointer);
709 }
710
711 let has_text_cursor = matches!(tag, Tag::Input | Tag::Textarea)
712 || classes.iter().any(|c| {
713 c.split_whitespace()
714 .any(|tok| tok == "cursor-text" || tok == "select-text")
715 });
716
717 if has_text_cursor {
718 return Some(strata_platform::window::CursorIcon::Text);
719 }
720 }
721 }
722 None
723 },
724 )
725 .unwrap_or(strata_platform::window::CursorIcon::Default)
726 }
727
728 pub fn hit_test_property(
731 blueprint: &NodeBlueprint,
732 layout: &LayoutNode,
733 px: f32,
734 py: f32,
735 key: &str,
736 ) -> Option<String> {
737 Self::hit_test_stacking_context_with(
738 blueprint,
739 layout,
740 px,
741 py,
742 &[],
743 0,
744 &mut |bp, node_layout, lx, ly| {
745 if matches!(
746 node_layout.style.visibility,
747 strata_style::types::Visibility::Hidden
748 ) || matches!(
749 node_layout.style.pointer_events,
750 strata_style::types::PointerEvents::None
751 ) {
752 return None;
753 }
754 if let NodeBlueprint::Element { attributes, .. } = bp {
755 if node_layout.geometry.border_box.contains_point(lx, ly) {
756 for attr in attributes {
757 if let strata_dom::Attribute::Property { key: k, value } = attr {
758 if k == key {
759 return Some(value.clone());
760 }
761 }
762 }
763 }
764 }
765 None
766 },
767 )
768 }
769}