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 pub(crate) fn resolve_chain_for_path<'a>(
170 blueprint: &'a NodeBlueprint,
171 layout: &'a LayoutNode,
172 path: &[usize],
173 ) -> Vec<(&'a NodeBlueprint, &'a LayoutNode)> {
174 let mut chain = Vec::with_capacity(path.len() + 1);
175 let mut cur_bp = blueprint;
176 let mut cur_layout = layout;
177 chain.push((cur_bp, cur_layout));
178
179 for &idx in path {
180 let paired = Self::paired_blueprint_children(cur_bp, cur_layout);
181 let Some((_, next_bp, next_layout)) =
182 paired.into_iter().find(|(child_idx, _, _)| *child_idx == idx)
183 else {
184 break;
185 };
186 cur_bp = next_bp;
187 cur_layout = next_layout;
188 chain.push((cur_bp, cur_layout));
189 }
190 chain
191 }
192
193 pub(crate) fn is_portal_backdrop_bp(bp: &NodeBlueprint) -> bool {
194 if let NodeBlueprint::Element { attributes, .. } = bp {
195 attributes.iter().any(|a| {
196 matches!(
197 a,
198 strata_dom::Attribute::Property { key, value }
199 if key == "data-portal-backdrop" && value == "true"
200 )
201 })
202 } else {
203 false
204 }
205 }
206
207 pub(crate) fn top_layer_portal_node_is_visual_content(
208 bp: &NodeBlueprint,
209 layout: &LayoutNode,
210 px: f32,
211 py: f32,
212 depth: usize,
213 ) -> bool {
214 if depth >= 256
215 || layout.style.display == strata_style::types::Display::None
216 || layout.style.visibility == strata_style::types::Visibility::Hidden
217 || layout.style.pointer_events == strata_style::types::PointerEvents::None
218 {
219 return false;
220 }
221 if Self::is_portal_backdrop_bp(bp) {
222 return false;
223 }
224 let (px, py) = Self::unproject_point(layout, px, py);
225 let in_box = layout.geometry.border_box.contains_point(px, py);
226 if in_box {
227 for (_idx, child_bp, child_layout) in Self::paired_blueprint_children(bp, layout) {
228 if Self::top_layer_portal_node_is_visual_content(
229 child_bp,
230 child_layout,
231 px,
232 py,
233 depth + 1,
234 ) {
235 return true;
236 }
237 }
238 if depth > 0
239 || layout.style.background_color.is_some_and(|c| c.a > 0)
240 || !layout.style.box_shadow.is_empty()
241 {
242 return true;
243 }
244 } else {
245 for (_idx, child_bp, child_layout) in Self::paired_blueprint_children(bp, layout) {
246 if Self::top_layer_portal_node_is_visual_content(
247 child_bp,
248 child_layout,
249 px,
250 py,
251 depth + 1,
252 ) {
253 return true;
254 }
255 }
256 }
257 false
258 }
259
260 pub(crate) fn point_hits_non_backdrop_top_layer_portal(&self, px: f32, py: f32) -> bool {
261 let mut top_layer_portals = Vec::new();
262 Self::collect_top_layer_portals(
263 &self.root_blueprint,
264 &self.layout_root,
265 &mut top_layer_portals,
266 0,
267 );
268 top_layer_portals.into_iter().rev().any(|(p_bp, p_layout)| {
269 !Self::is_portal_backdrop_bp(p_bp)
270 && Self::top_layer_portal_node_is_visual_content(p_bp, p_layout, px, py, 0)
271 })
272 }
273
274 pub fn hit_test_event_with_box_and_entity<'a>(
278 blueprint: &'a NodeBlueprint,
279 layout: &'a LayoutNode,
280 px: f32,
281 py: f32,
282 event_type: strata_dom::EventType,
283 ) -> Option<(
284 std::rc::Rc<dyn Fn(strata_dom::Event)>,
285 &'a strata_layout::Rect,
286 strata_primitives::EntityId,
287 )> {
288 let visual_path = Self::hit_test_node_path(blueprint, layout, px, py)?;
289 Self::find_event_at_or_above_path(blueprint, layout, &visual_path, event_type)
290 }
291
292 pub fn hit_test_event_with_box<'a>(
295 blueprint: &'a NodeBlueprint,
296 layout: &'a LayoutNode,
297 px: f32,
298 py: f32,
299 event_type: strata_dom::EventType,
300 ) -> Option<(
301 std::rc::Rc<dyn Fn(strata_dom::Event)>,
302 &'a strata_layout::Rect,
303 )> {
304 Self::hit_test_event_with_box_and_entity(blueprint, layout, px, py, event_type)
305 .map(|(h, r, _)| (h, r))
306 }
307
308 pub fn find_event_at_or_above_path<'a>(
311 blueprint: &'a NodeBlueprint,
312 layout: &'a LayoutNode,
313 path: &[usize],
314 event_type: strata_dom::EventType,
315 ) -> Option<(
316 std::rc::Rc<dyn Fn(strata_dom::Event)>,
317 &'a strata_layout::Rect,
318 strata_primitives::EntityId,
319 )> {
320 let chain = Self::resolve_chain_for_path(blueprint, layout, path);
321 for (bp, node_layout) in chain.into_iter().rev() {
322 if let NodeBlueprint::Element { attributes, .. } = bp {
323 for attr in attributes {
324 if let strata_dom::Attribute::EventListener { event: ev, handler } = attr {
325 if *ev == event_type {
326 return Some((
327 handler.clone(),
328 &node_layout.geometry.border_box,
329 node_layout.entity_id,
330 ));
331 }
332 }
333 }
334 }
335 }
336 None
337 }
338
339 pub fn hit_test_event(
342 blueprint: &NodeBlueprint,
343 layout: &LayoutNode,
344 px: f32,
345 py: f32,
346 event_type: strata_dom::EventType,
347 ) -> Option<std::rc::Rc<dyn Fn(strata_dom::Event)>> {
348 Self::hit_test_event_with_box(blueprint, layout, px, py, event_type).map(|(h, _)| h)
349 }
350
351 pub fn hit_test_click(
353 blueprint: &NodeBlueprint,
354 layout: &LayoutNode,
355 px: f32,
356 py: f32,
357 ) -> Option<std::rc::Rc<dyn Fn(strata_dom::Event)>> {
358 Self::hit_test_event(blueprint, layout, px, py, strata_dom::EventType::Click)
359 }
360
361 pub fn hit_test_cursor(
364 blueprint: &NodeBlueprint,
365 layout: &LayoutNode,
366 px: f32,
367 py: f32,
368 ) -> strata_platform::window::CursorIcon {
369 let Some(visual_path) = Self::hit_test_node_path(blueprint, layout, px, py) else {
370 return strata_platform::window::CursorIcon::Default;
371 };
372 let chain = Self::resolve_chain_for_path(blueprint, layout, &visual_path);
373 for (bp, node_layout) in chain.into_iter().rev() {
374 if let NodeBlueprint::Element {
375 tag,
376 classes,
377 attributes,
378 ..
379 } = bp
380 {
381 match node_layout.style.cursor {
382 strata_style::types::Cursor::Pointer => {
383 return strata_platform::window::CursorIcon::Pointer
384 }
385 strata_style::types::Cursor::Text => {
386 return strata_platform::window::CursorIcon::Text
387 }
388 strata_style::types::Cursor::Crosshair => {
389 return strata_platform::window::CursorIcon::Crosshair
390 }
391 strata_style::types::Cursor::Move => {
392 return strata_platform::window::CursorIcon::Move
393 }
394 strata_style::types::Cursor::NotAllowed => {
395 return strata_platform::window::CursorIcon::NotAllowed
396 }
397 strata_style::types::Cursor::Wait => {
398 return strata_platform::window::CursorIcon::Wait
399 }
400 strata_style::types::Cursor::Grab => {
401 return strata_platform::window::CursorIcon::Grab
402 }
403 strata_style::types::Cursor::Grabbing => {
404 return strata_platform::window::CursorIcon::Grabbing
405 }
406 strata_style::types::Cursor::ColResize => {
407 return strata_platform::window::CursorIcon::ColResize
408 }
409 strata_style::types::Cursor::RowResize => {
410 return strata_platform::window::CursorIcon::RowResize
411 }
412 strata_style::types::Cursor::Help => {
413 return strata_platform::window::CursorIcon::Help
414 }
415 strata_style::types::Cursor::None => {
416 return strata_platform::window::CursorIcon::None
417 }
418 strata_style::types::Cursor::Default => {}
419 }
420
421 let has_pointer = matches!(tag, Tag::Button | Tag::A)
422 || classes.iter().any(|c| {
423 c.split_whitespace()
424 .any(|tok| tok == "cursor-pointer" || tok == "pointer")
425 })
426 || attributes.iter().any(|a| {
427 matches!(
428 a,
429 strata_dom::Attribute::EventListener {
430 event: strata_dom::EventType::Click,
431 ..
432 }
433 )
434 });
435
436 if has_pointer {
437 return strata_platform::window::CursorIcon::Pointer;
438 }
439
440 let has_text_cursor = matches!(tag, Tag::Input | Tag::Textarea)
441 || classes.iter().any(|c| {
442 c.split_whitespace()
443 .any(|tok| tok == "cursor-text" || tok == "select-text")
444 });
445
446 if has_text_cursor {
447 return strata_platform::window::CursorIcon::Text;
448 }
449 }
450 }
451 strata_platform::window::CursorIcon::Default
452 }
453
454 pub fn hit_test_property(
457 blueprint: &NodeBlueprint,
458 layout: &LayoutNode,
459 px: f32,
460 py: f32,
461 key: &str,
462 ) -> Option<String> {
463 let visual_path = Self::hit_test_node_path(blueprint, layout, px, py)?;
464 let chain = Self::resolve_chain_for_path(blueprint, layout, &visual_path);
465 for (bp, _) in chain.into_iter().rev() {
466 if let NodeBlueprint::Element { attributes, .. } = bp {
467 for attr in attributes {
468 if let strata_dom::Attribute::Property { key: k, value } = attr {
469 if k == key {
470 return Some(value.clone());
471 }
472 }
473 }
474 }
475 }
476 None
477 }
478}