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 lbox: LayoutBox = node.get_layout_box();
let view_w: f32 = lbox.get_content_bounds().get_width().max(0.0);
let view_h: f32 = lbox.get_content_bounds().get_height().max(0.0);
let max_x: f32 = (lbox.get_content_size().get_width() - view_w).max(0.0);
let cur_x: f32 = lbox.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 = (lbox.get_content_size().get_height() - view_h).max(0.0);
let cur_y: f32 = lbox.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 =
rem_dx.abs() > SCROLL_DELTA_EPS && metrics.get_max_x() > SCROLL_OVERFLOW_EPS;
let blocks_y: bool =
rem_dy.abs() > SCROLL_DELTA_EPS && metrics.get_max_y() > SCROLL_OVERFLOW_EPS;
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;
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() {
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();
}
None
}
pub(crate) fn clamp_tree_scroll_offsets(tree: &mut SceneTree) {
let scroll_count: usize = tree.scroll_nodes().len();
for idx in 0..scroll_count {
let candidate: NodeId = tree.scroll_nodes()[idx];
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 lbox: &LayoutBox = node.layout_box_ref();
let view_w: f32 = lbox.get_content_bounds().get_width();
let view_h: f32 = lbox.get_content_bounds().get_height();
if view_w <= 0.0 && view_h <= 0.0 {
return None;
}
let max_x: f32 = (lbox.get_content_size().get_width() - view_w).max(0.0);
let max_y: f32 = (lbox.get_content_size().get_height() - view_h).max(0.0);
let cur: Point = lbox.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);
}
}
}