use nativeview_abi::NV_PARAGRAPH_FLAG_HAS_INTERACTIVE_RUNS;
use nativeview_engine::LayoutRect;
use nativeview_store::{NodeId, NodeStore};
use nativeview_tables::tables::{Interaction, PointerEvents};
use nativeview_tables::transform::Transform2D;
use crate::{MAX_TREE_DEPTH, StoreLayoutEngine};
use super::super::LaidOutScene;
use super::helpers::{HitFrame, HitStack, paragraph_content_insets, point_in_text_foreground};
use super::super::stacking::{ClipScrollIdx, ScopeIdx, ScopeKind, StackingPlan};
const MAX_INLINE_CLIP_CHAIN: usize = 64;
pub(crate) struct PlanHitWalk<'a, L: StoreLayoutEngine, T, F> {
pub(crate) scene: &'a LaidOutScene<L>,
pub(crate) store: &'a NodeStore,
pub(crate) plan: &'a StackingPlan,
pub(crate) resolve_transform: T,
pub(crate) has_plugin_fg: F,
}
impl<
L: StoreLayoutEngine,
T: Fn(NodeId, Option<Transform2D>) -> Option<Transform2D>,
F: Fn(NodeId) -> bool,
> PlanHitWalk<'_, L, T, F>
{
#[inline]
fn box_rect_and_delta(&self, id: NodeId, is_top_layer: bool, is_sticky: bool) -> Option<(LayoutRect, (f32, f32))> {
let mut rect = self.scene.layout.layout_of(id)?;
if is_top_layer && let Some(ro) = self.scene.overlays.get(id) {
let delta = (ro.origin_x - rect.x, ro.origin_y - rect.y);
rect.width = ro.width;
rect.height = ro.height;
return Some((rect, delta));
}
let delta = if is_sticky {
self.scene.sticky_delta_of_box(self.store, id)
} else {
(0.0, 0.0)
};
Some((rect, delta))
}
fn eval_clip_chain(
&self,
verified_clip: ClipScrollIdx,
verified_offset: (f32, f32),
target_clip: ClipScrollIdx,
point: (f32, f32),
) -> Option<(f32, f32)> {
if target_clip == verified_clip {
return Some(verified_offset);
}
let mut stack_buf = [ClipScrollIdx::NONE; MAX_INLINE_CLIP_CHAIN];
let mut len = 0usize;
let mut overflow: Option<Vec<ClipScrollIdx>> = None;
let mut cur = target_clip;
let mut reached_verified = cur == verified_clip;
while cur.is_some() && !reached_verified {
if len < MAX_INLINE_CLIP_CHAIN {
stack_buf[len] = cur;
len += 1;
} else {
let v = overflow.get_or_insert_with(|| stack_buf.to_vec());
v.push(cur);
}
cur = self.plan.clips[cur.index()].parent;
if cur == verified_clip {
reached_verified = true;
}
}
let mut offset = if reached_verified {
verified_offset
} else {
(0.0, 0.0)
};
let chain: &[ClipScrollIdx] = match overflow.as_deref() {
Some(v) => v,
None => &stack_buf[..len],
};
for &cs_idx in chain.iter().rev() {
let cs = self.plan.clips[cs_idx.index()];
let (rect, delta) =
self.box_rect_and_delta(cs.owner, cs.is_top_layer(), cs.is_sticky())?;
let wx = rect.x + offset.0 + delta.0;
let wy = rect.y + offset.1 + delta.1;
if cs.clips()
&& cs.owner != NodeId::ROOT
&& (rect.width <= 0.0
|| rect.height <= 0.0
|| point.0 < wx
|| point.0 > wx + rect.width
|| point.1 < wy
|| point.1 > wy + rect.height)
{
return None;
}
let (sx, sy) = if cs.has_scroll() {
self.scene.compositor_scroll_shift(self.store, cs.owner)
} else {
(0.0, 0.0)
};
offset = (offset.0 + delta.0 - sx, offset.1 + delta.1 - sy);
}
Some(offset)
}
#[inline]
fn point_in_node_box(
&self,
id: NodeId,
is_top_layer: bool,
is_sticky: bool,
point: (f32, f32),
offset: (f32, f32),
) -> bool {
let Some((rect, delta)) = self.box_rect_and_delta(id, is_top_layer, is_sticky) else {
return false;
};
let wx = rect.x + offset.0 + delta.0;
let wy = rect.y + offset.1 + delta.1;
rect.width > 0.0
&& rect.height > 0.0
&& point.0 >= wx
&& point.0 <= wx + rect.width
&& point.1 >= wy
&& point.1 <= wy + rect.height
}
fn make_frame(
&self,
scope_idx: ScopeIdx,
point: (f32, f32),
verified_clip: ClipScrollIdx,
verified_offset: (f32, f32),
depth: u16,
) -> Option<HitFrame> {
if depth as usize > MAX_TREE_DEPTH {
return None;
}
let scope = *self.plan.scopes.get(scope_idx.index())?;
let entry_clip = scope.min_descendant_clip;
let entry_offset = self.eval_clip_chain(verified_clip, verified_offset, entry_clip, point)?;
let (scope_point, own_offset) = if scope.has_transform {
let (rect, delta) =
self.box_rect_and_delta(scope.node_id, scope.is_top_layer(), scope.is_sticky())?;
let wx = rect.x + entry_offset.0 + delta.0;
let wy = rect.y + entry_offset.1 + delta.1;
let eff_tf = (self.resolve_transform)(
scope.node_id,
self.scene.effective_transform(self.store, scope.node_id),
);
let mapped = match eff_tf {
Some(tf) if !tf.is_identity() => tf.inverse_map_surface_point(
point.0, point.1, wx, wy, rect.width, rect.height,
)?,
_ => point,
};
(mapped, Some(entry_offset))
} else {
(point, (scope.clip_scroll == entry_clip).then_some(entry_offset))
};
let items_start = scope.items_start;
let items_len = scope.items_len.get();
let neg_end = scope.neg_end.min(items_len);
let stop_at = if scope.kind == ScopeKind::RealStackingContext {
items_start
} else {
items_start + neg_end
};
Some(HitFrame {
scope_idx,
cursor: items_start + items_len,
stop_at,
entry_clip,
entry_offset,
scope_point,
own_offset,
depth,
checked_fg: false,
})
}
#[inline]
fn resolve_paragraph_hit(
&self,
id: NodeId,
is_top_layer: bool,
is_sticky: bool,
point: (f32, f32),
offset: (f32, f32),
) -> NodeId {
if !self.store.is_inline_context_root(id) {
return id;
}
let Some(paras) = self.scene.paragraphs() else {
return id;
};
let Some(para) = paras.paragraph_view(self.store, id) else {
return id;
};
if (para.flags & NV_PARAGRAPH_FLAG_HAS_INTERACTIVE_RUNS) == 0 {
return id;
}
let Some((rect, delta)) = self.box_rect_and_delta(id, is_top_layer, is_sticky) else {
return id;
};
let wx = rect.x + offset.0 + delta.0;
let wy = rect.y + offset.1 + delta.1;
let (cl, ct, cr, _) = paragraph_content_insets(self.store, id, rect.width);
let lx = point.0 - wx - cl;
let ly = point.1 - wy - ct;
let cw = (rect.width - cl - cr).max(0.0);
self.scene
.text_measurer()
.hit_test_paragraph(¶, cw, lx, ly)
.filter(|&run_id| !self.pointer_events_none(run_id))
.unwrap_or(id)
}
#[inline]
fn pointer_events_none(&self, id: NodeId) -> bool {
let mut cur = Some(id);
for _ in 0..MAX_TREE_DEPTH {
let Some(nid) = cur else {
break;
};
if let Some(pe) = self
.store
.try_row::<Interaction>(nid)
.and_then(|i| i.pointer_events())
{
return pe == PointerEvents::None;
}
cur = self.store.parent(nid).filter(|&p| p != nid);
}
false
}
fn point_in_step7_foreground(
&self,
scope: super::super::stacking::StackingScope,
frame: &HitFrame,
) -> Option<(f32, f32)> {
let id = scope.node_id;
let has_full_fg = (self.has_plugin_fg)(id)
|| self
.store
.try_row::<Interaction>(id)
.is_some_and(|i| i.captures_text_keys());
let off = frame.own_offset.or_else(|| {
self.eval_clip_chain(frame.entry_clip, frame.entry_offset, scope.clip_scroll, frame.scope_point)
})?;
let (rect, delta) = self.box_rect_and_delta(id, scope.is_top_layer(), scope.is_sticky())?;
let wx = rect.x + off.0 + delta.0;
let wy = rect.y + off.1 + delta.1;
let pt = frame.scope_point;
if rect.width <= 0.0 || rect.height <= 0.0 || pt.0 < wx || pt.0 > wx + rect.width || pt.1 < wy || pt.1 > wy + rect.height {
return None;
}
if has_full_fg
|| point_in_text_foreground(self.store, &*self.scene.text_measurer, id, wx, wy, rect, pt)
{
return Some(off);
}
None
}
pub(crate) fn walk_scope(&self, start_scope: ScopeIdx, start_point: (f32, f32)) -> Option<NodeId> {
let root_frame =
self.make_frame(start_scope, start_point, ClipScrollIdx::NONE, (0.0, 0.0), 0)?;
let mut stack = HitStack::new();
stack.push(root_frame);
while let Some(top) = stack.top_mut() {
let scope = self.plan.scopes[top.scope_idx.index()];
let neg_end = scope.neg_end.min(scope.items_len.get());
if !top.checked_fg
&& scope.kind == ScopeKind::RealStackingContext
&& neg_end > 0
&& top.cursor == scope.items_start + neg_end
{
top.checked_fg = true;
if !scope.hidden
&& !self.pointer_events_none(scope.node_id)
&& let Some(off) = self.point_in_step7_foreground(scope, top)
{
return Some(self.resolve_paragraph_hit(
scope.node_id, scope.is_top_layer(), scope.is_sticky(), top.scope_point, off,
));
}
}
if top.cursor > top.stop_at {
top.cursor -= 1;
let item = self.plan.ordered_items[top.cursor as usize];
let (pt, ec, eo, nd) =
(top.scope_point, top.entry_clip, top.entry_offset, top.depth.saturating_add(1));
if item.child_scope.is_some() {
if let Some(cf) = self.make_frame(item.child_scope, pt, ec, eo, nd) {
stack.push(cf);
}
} else if !item.hidden
&& !self.pointer_events_none(item.node_id)
&& let Some(offset) = self.eval_clip_chain(ec, eo, item.clip_scroll, pt)
&& self.point_in_node_box(item.node_id, false, false, pt, offset)
{
return Some(self.resolve_paragraph_hit(item.node_id, false, false, pt, offset));
}
} else if let Some(done) = stack.pop() {
let scope = self.plan.scopes[done.scope_idx.index()];
if !scope.hidden && !self.pointer_events_none(scope.node_id) {
let own_off = done.own_offset.or_else(|| {
self.eval_clip_chain(done.entry_clip, done.entry_offset, scope.clip_scroll, done.scope_point)
});
if let Some(off) = own_off
&& self.point_in_node_box(
scope.node_id, scope.is_top_layer(), scope.is_sticky(), done.scope_point, off,
)
{
return Some(self.resolve_paragraph_hit(
scope.node_id, scope.is_top_layer(), scope.is_sticky(), done.scope_point, off,
));
}
}
}
}
None
}
}