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
}
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 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();
if view_w <= 0.0 && view_h <= 0.0 {
return None;
}
let max_x: f32 = (layout_box.get_content_size().get_width() - view_w).max(0.0);
let max_y: f32 = (layout_box.get_content_size().get_height() - 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);
}
}
}