use crate::{
core::NodeId,
layout::{layout_kind::LayoutKind, layout_result::LayoutResult},
node::Node,
};
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct StickyReach {
pub(crate) top: f32,
pub(crate) bottom: f32,
pub(crate) left: f32,
pub(crate) right: f32,
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct Over {
pub(crate) origin: (f32, f32),
pub(crate) target: (f32, f32, f32, f32),
}
pub(crate) fn sticky_reach(
content: &Node,
layout: &LayoutResult,
except: Option<NodeId>,
over: Option<Over>,
) -> StickyReach {
fn contains(node: &Node, target: NodeId) -> bool {
node.node_id() == target || node.children().iter().any(|child| contains(child, target))
}
fn walk(
node: &Node,
layout: &LayoutResult,
origin: (f32, f32),
except: Option<NodeId>,
target: Option<(f32, f32, f32, f32)>,
reach: &mut StickyReach,
) {
for (child, child_layout) in node.children().iter().zip(&layout.children) {
let result = &child_layout.result;
let at = (origin.0 + child_layout.offset.x, origin.1 + child_layout.offset.y);
if let Some(sticky) = child.sticky()
&& !except.is_some_and(|target| contains(child, target))
{
let (width, height) = (result.size.width, result.size.height);
let across = target.is_none_or(|(x, _, w, _)| at.0 < x + w && at.0 + width > x);
let along = target.is_none_or(|(_, y, _, h)| at.1 < y + h && at.1 + height > y);
if let Some(inset) = sticky.top.filter(|_| across) {
reach.top = reach.top.max(inset + height);
}
if let Some(inset) = sticky.bottom.filter(|_| across) {
reach.bottom = reach.bottom.max(inset + height);
}
if let Some(inset) = sticky.left.filter(|_| along) {
reach.left = reach.left.max(inset + width);
}
if let Some(inset) = sticky.right.filter(|_| along) {
reach.right = reach.right.max(inset + width);
}
}
if result.holds.sticky && !matches!(child.layout_kind(), LayoutKind::ScrollModifier { .. }) {
walk(child, result, at, except, target, reach);
}
}
}
let mut reach = StickyReach::default();
let (origin, target) = match over {
Some(over) => (over.origin, Some(over.target)),
None => ((0.0, 0.0), None),
};
walk(content, layout, origin, except, target, &mut reach);
reach
}