use super::*;
#[must_use]
pub(crate) fn is_overlay_scroll_barrier(node: &SceneNode) -> bool {
matches!(node.get_kind(), NodeKind::Portal)
|| node.style_ref().layout_ref().get_position() == Position::Fixed
}
#[must_use]
pub(crate) fn node_scroll_metrics(node: &SceneNode) -> ScrollAxisMetrics {
let is_scroll_box: bool = node.style_ref().layout_ref().get_overflow() == Overflow::Scroll;
if !is_scroll_box {
return ScrollAxisMetrics::default();
}
let layout_box: LayoutBox = node.get_layout_box();
let view_w: f32 = layout_box.get_content_bounds().get_width().max(0.0);
let view_h: f32 = layout_box.get_content_bounds().get_height().max(0.0);
let max_x: f32 = (layout_box.get_content_size().get_width() - view_w).max(0.0);
let cur_x: f32 = layout_box.get_scroll_offset().get_x().clamp(0.0, max_x);
if let Some((vy, total_h)) = node.virtual_scroll_metrics() {
let max_y: f32 = (total_h - view_h).max(0.0);
let cur_y: f32 = vy.clamp(0.0, max_y);
ScrollAxisMetrics::new(true, true, cur_x, max_x, cur_y, max_y, total_h)
} else {
let max_y: f32 = (layout_box.get_content_size().get_height() - view_h).max(0.0);
let cur_y: f32 = layout_box.get_scroll_offset().get_y().clamp(0.0, max_y);
ScrollAxisMetrics::new(true, false, cur_x, max_x, cur_y, max_y, 0.0)
}
}
#[must_use]
pub(crate) fn axis_can_scroll(cur: f32, max_scroll: f32, delta: f32) -> bool {
if max_scroll <= SCROLL_OVERFLOW_EPS || delta.abs() <= SCROLL_DELTA_EPS {
return false;
}
let clamped: f32 = cur.clamp(0.0, max_scroll);
if delta < 0.0 {
clamped > SCROLL_BOUNDARY_EPS
} else {
clamped < max_scroll - SCROLL_BOUNDARY_EPS
}
}
#[must_use]
pub(crate) fn consume_scroll_axis(cur: f32, max_scroll: f32, delta: f32) -> (f32, f32, f32) {
if max_scroll <= SCROLL_OVERFLOW_EPS || delta.abs() <= SCROLL_DELTA_EPS {
return (cur.clamp(0.0, max_scroll), 0.0, delta);
}
let clamped_cur: f32 = cur.clamp(0.0, max_scroll);
let next: f32 = (clamped_cur + delta).clamp(0.0, max_scroll);
let consumed: f32 = next - clamped_cur;
if consumed.abs() <= SCROLL_DELTA_EPS {
(clamped_cur, 0.0, delta)
} else {
(next, consumed, delta - consumed)
}
}
#[must_use]
pub(crate) fn node_blocks_scroll_chaining(
node: &SceneNode,
metrics: ScrollAxisMetrics,
rem_dx: f32,
rem_dy: f32,
) -> bool {
if is_overlay_scroll_barrier(node) {
return true;
}
if !node.style_ref().paint_ref().get_stop_scroll_propagation() {
return false;
}
if !metrics.get_is_scroll_box() {
return true;
}
let blocks_x: bool = axis_can_scroll(metrics.get_cur_x(), metrics.get_max_x(), rem_dx);
let blocks_y: bool = axis_can_scroll(metrics.get_cur_y(), metrics.get_max_y(), rem_dy);
blocks_x || blocks_y
}
#[must_use]
pub(crate) fn find_scroll_chain_target(
tree: &SceneTree,
start: Option<NodeId>,
delta: Point,
) -> Option<NodeId> {
let dx: f32 = delta.get_x();
let dy: f32 = delta.get_y();
let mut cursor: Option<NodeId> = start;
let mut first_scroll_box: Option<NodeId> = None;
while let Some(aid) = cursor {
let Some(node) = tree.get_node(aid) else {
break;
};
let metrics: ScrollAxisMetrics = node_scroll_metrics(node);
if metrics.get_is_scroll_box() {
if first_scroll_box.is_none() {
first_scroll_box = Some(aid);
}
let can_x: bool = axis_can_scroll(metrics.get_cur_x(), metrics.get_max_x(), dx);
let can_y: bool = axis_can_scroll(metrics.get_cur_y(), metrics.get_max_y(), dy);
if can_x || can_y {
return Some(aid);
}
}
if node_blocks_scroll_chaining(node, metrics, dx, dy) {
break;
}
cursor = node.parent_id();
}
if let Some(container_id) = first_scroll_box.or(start) {
let allow_descendant_fallback: bool = tree.child_count(container_id) <= 1
|| tree
.get_node(container_id)
.is_some_and(|scene_node: &SceneNode| {
matches!(
scene_node.style_ref().get_layout().get_flex_direction(),
FlexDirection::Row | FlexDirection::RowReverse
)
});
if !allow_descendant_fallback {
return None;
}
let (allow_x, allow_y): (bool, bool) = match first_scroll_box
.and_then(|id: NodeId| tree.get_node(id))
.map(node_scroll_metrics)
{
Some(matrix_elems) => (
dx.abs() > SCROLL_DELTA_EPS && matrix_elems.get_max_x() <= SCROLL_OVERFLOW_EPS,
dy.abs() > SCROLL_DELTA_EPS && matrix_elems.get_max_y() <= SCROLL_OVERFLOW_EPS,
),
None => (dx.abs() > SCROLL_DELTA_EPS, dy.abs() > SCROLL_DELTA_EPS),
};
if !allow_x && !allow_y {
return None;
}
for child_id in tree.children_iter(container_id) {
if !tree.is_node_visible(child_id) {
continue;
}
if let Some(child) = tree.get_node(child_id) {
if child.style_ref().paint_ref().get_pointer_events() == PointerEvents::None
|| is_overlay_scroll_barrier(child)
{
continue;
}
let cmetrics: ScrollAxisMetrics = node_scroll_metrics(child);
if cmetrics.get_is_scroll_box()
&& ((allow_x
&& axis_can_scroll(cmetrics.get_cur_x(), cmetrics.get_max_x(), dx))
|| (allow_y
&& axis_can_scroll(cmetrics.get_cur_y(), cmetrics.get_max_y(), dy)))
{
return Some(child_id);
}
for grand_id in tree.children_iter(child_id) {
if !tree.is_node_visible(grand_id) {
continue;
}
if let Some(grand) = tree.get_node(grand_id) {
if grand.style_ref().paint_ref().get_pointer_events() == PointerEvents::None
|| is_overlay_scroll_barrier(grand)
{
continue;
}
let gmetrics: ScrollAxisMetrics = node_scroll_metrics(grand);
if gmetrics.get_is_scroll_box()
&& ((allow_x
&& axis_can_scroll(gmetrics.get_cur_x(), gmetrics.get_max_x(), dx))
|| (allow_y
&& axis_can_scroll(
gmetrics.get_cur_y(),
gmetrics.get_max_y(),
dy,
)))
{
return Some(grand_id);
}
}
}
}
}
}
None
}
#[must_use]
fn find_scroll_anchor_node(
tree: &SceneTree,
scroll_id: NodeId,
view_top: f32,
view_bottom: f32,
) -> Option<NodeId> {
let mut container_id: NodeId = scroll_id;
for _ in 0..SCROLL_ANCHOR_MAX_WRAPPER_DEPTH {
let mut visible_flow_child: Option<NodeId> = None;
let mut flow_count: usize = 0;
for child_id in tree.children_iter(container_id) {
let Some(child_node) = tree.get_node(child_id) else {
continue;
};
let child_layout: &LayoutStyle = child_node.layout_ref();
if child_layout.get_display() == Display::None
|| child_layout.get_position() != Position::Relative
{
continue;
}
flow_count += 1;
visible_flow_child = Some(child_id);
if flow_count > 1 {
break;
}
}
if flow_count == 1
&& let Some(only_child) = visible_flow_child
&& let Some(only_node) = tree.get_node(only_child)
&& only_node.layout_ref().get_overflow() != Overflow::Scroll
&& only_node.layout_ref().get_flex_direction() == FlexDirection::Column
&& tree.has_children(only_child)
{
container_id = only_child;
} else {
break;
}
}
let container_node: &SceneNode = tree.get_node(container_id)?;
if container_node.layout_ref().get_flex_direction() != FlexDirection::Column
|| container_node.layout_ref().get_justify_content() != JustifyContent::Start
{
return None;
}
let dirty_layout_base: u16 = DIRTY_LAYOUT | DIRTY_CHILDREN;
let dirty_layout_mask: u16 = dirty_layout_base | DIRTY_SUBTREE_LAYOUT;
let mut fallback_anchor: Option<NodeId> = None;
for child_id in tree.children_iter(container_id) {
let Some(child_node) = tree.get_node(child_id) else {
continue;
};
let child_layout: &LayoutStyle = child_node.layout_ref();
if child_layout.get_display() == Display::None
|| child_layout.get_position() != Position::Relative
{
continue;
}
let bounds: Rect = child_node.layout_box_ref().get_bounds();
if bounds.get_height() <= 0.0 || bounds.bottom() <= view_top + SCROLL_ANCHOR_DELTA_EPS {
continue;
}
if bounds.get_y() >= view_bottom {
break;
}
if (child_node.get_dirty().get_bits() & dirty_layout_mask) == 0 {
return Some(child_id);
}
if fallback_anchor.is_none() && bounds.get_y() >= view_top - SCROLL_ANCHOR_DELTA_EPS {
fallback_anchor = Some(child_id);
}
}
fallback_anchor
}
pub(crate) fn capture_scroll_anchors(tree: &SceneTree, out: &mut Vec<ScrollAnchorSnapshot>) {
out.clear();
let subtree_mask: u16 = DIRTY_SUBTREE_LAYOUT | DIRTY_CHILDREN;
for &candidate in tree.scroll_nodes().iter() {
let Some(node) = tree.get_node(candidate) else {
continue;
};
if !node.get_alive()
|| !tree.is_node_visible(candidate)
|| node.layout_ref().get_overflow() != Overflow::Scroll
|| node.virtual_scroll_metrics().is_some()
|| (node.get_dirty().get_bits() & subtree_mask) == 0
{
continue;
}
let layout_box: &LayoutBox = node.layout_box_ref();
let cur_scroll_y: f32 = layout_box.get_scroll_offset().get_y();
if cur_scroll_y <= SCROLL_ANCHOR_MIN_OFFSET_DP {
continue;
}
let view_rect: Rect = layout_box.get_content_bounds();
let view_h: f32 = view_rect.get_height();
let old_content_h: f32 = layout_box.get_content_size().get_height();
if view_h <= 1.0 || old_content_h <= view_h + SCROLL_OVERFLOW_EPS {
continue;
}
let view_top: f32 = view_rect.get_y() + cur_scroll_y;
let view_bottom: f32 = view_top + view_h;
if let Some(anchor_id) = find_scroll_anchor_node(tree, candidate, view_top, view_bottom)
&& let Some(anchor_node) = tree.get_node(anchor_id)
{
let old_anchor_rel_y: f32 =
anchor_node.layout_box_ref().get_bounds().get_y() - view_rect.get_y();
out.push(ScrollAnchorSnapshot::new(
candidate,
anchor_id,
old_anchor_rel_y,
));
}
}
}
pub(crate) fn apply_scroll_anchors(tree: &mut SceneTree, anchors: &[ScrollAnchorSnapshot]) -> bool {
let mut adjusted_any: bool = false;
for snap in anchors {
let scroll_id: NodeId = snap.get_scroll_id();
let anchor_id: NodeId = snap.get_anchor_id();
if !tree.is_node_visible(scroll_id) || !tree.is_node_visible(anchor_id) {
continue;
}
let next_offset_opt: Option<Point> =
match (tree.get_node(scroll_id), tree.get_node(anchor_id)) {
(Some(scroll_node), Some(anchor_node)) => {
let layout_box: &LayoutBox = scroll_node.layout_box_ref();
let view_rect: Rect = layout_box.get_content_bounds();
let view_h: f32 = view_rect.get_height();
let new_content_h: f32 = layout_box.get_content_size().get_height();
if view_h <= 1.0 {
None
} else {
let new_anchor_rel_y: f32 =
anchor_node.layout_box_ref().get_bounds().get_y() - view_rect.get_y();
let delta_y: f32 = new_anchor_rel_y - snap.get_old_anchor_rel_y();
if delta_y.abs() <= SCROLL_ANCHOR_DELTA_EPS {
None
} else {
let max_y: f32 = (new_content_h - view_h).max(0.0);
let cur: Point = layout_box.get_scroll_offset();
let next_y: f32 = (cur.get_y() + delta_y).clamp(0.0, max_y);
if (next_y - cur.get_y()).abs() > SCROLL_DELTA_EPS {
Some(Point::new(cur.get_x(), next_y))
} else {
None
}
}
}
}
_ => None,
};
if let Some(next_offset) = next_offset_opt {
tree.set_scroll_offset(scroll_id, next_offset);
adjusted_any = true;
}
}
adjusted_any
}
pub(crate) fn clamp_tree_scroll_offsets(tree: &mut SceneTree) -> bool {
let mut clamped_any: bool = false;
let scroll_count: usize = tree.scroll_nodes().len();
for idx in 0..scroll_count {
let candidate: NodeId = tree.scroll_nodes()[idx];
if !tree.is_node_visible(candidate) {
continue;
}
let has_children: bool = tree.has_children(candidate);
let clamp_target: Option<Point> = tree.get_node(candidate).and_then(|node: &SceneNode| {
if !node.get_alive()
|| node.layout_ref().get_overflow() != Overflow::Scroll
|| node.virtual_scroll_metrics().is_some()
{
return None;
}
let layout_box: &LayoutBox = node.layout_box_ref();
let view_w: f32 = layout_box.get_content_bounds().get_width();
let view_h: f32 = layout_box.get_content_bounds().get_height();
let content_w: f32 = layout_box.get_content_size().get_width();
let content_h: f32 = layout_box.get_content_size().get_height();
if (view_w <= 0.0 && view_h <= 0.0)
|| (has_children && content_w <= 0.0 && content_h <= 0.0)
{
return None;
}
let max_x: f32 = (content_w - view_w).max(0.0);
let max_y: f32 = (content_h - view_h).max(0.0);
let cur: Point = layout_box.get_scroll_offset();
let clamped: Point =
Point::new(cur.get_x().clamp(0.0, max_x), cur.get_y().clamp(0.0, max_y));
if (clamped.get_x() - cur.get_x()).abs() > SCROLL_DELTA_EPS
|| (clamped.get_y() - cur.get_y()).abs() > SCROLL_DELTA_EPS
{
Some(clamped)
} else {
None
}
});
if let Some(clamped) = clamp_target {
tree.set_scroll_offset(candidate, clamped);
clamped_any = true;
}
}
clamped_any
}