euv-core 0.48.3

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Returns `true` when `node` forms a Tier 0 top-layer overlay barrier (`Portal` or `Position::Fixed`).
///
/// # Arguments
///
/// - `&SceneNode` - Candidate scene node.
///
/// # Returns
///
/// - `bool` - Whether scroll events must not pass behind `node`.
#[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
}

/// Computes normalized scroll bounds and current offsets for `node` across standard and virtualized containers.
///
/// # Arguments
///
/// - `&SceneNode` - Target scene node.
///
/// # Returns
///
/// - `ScrollAxisMetrics` - Scrollability extents and current offsets for both axes.
#[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)
    }
}

/// Returns `true` when an axis has scrollable overflow and remaining travel in the direction of `delta`.
///
/// # Arguments
///
/// - `f32` - Current scroll offset along the axis.
/// - `f32` - Maximum scrollable offset along the axis.
/// - `f32` - Incoming scroll delta along the axis (negative scrolls toward `0.0`, positive toward `max_scroll`).
///
/// # Returns
///
/// - `bool` - Whether this axis can consume at least part of `delta`.
#[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
    }
}

/// Applies `delta` along one scroll axis, clamping cleanly at `[0.0, max_scroll]` and returning any unconsumed overscroll delta.
///
/// # Arguments
///
/// - `f32` - Current scroll offset along the axis.
/// - `f32` - Maximum scrollable offset along the axis.
/// - `f32` - Incoming scroll delta along the axis.
///
/// # Returns
///
/// - `(f32, f32, f32)` - Tuple `(next_offset, consumed_delta, remaining_delta)`.
#[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)
    }
}

/// Determines whether `node` intentionally terminates scroll chaining for the remaining `(rem_dx, rem_dy)` delta.
///
/// # Arguments
///
/// - `&SceneNode` - Current node along the ancestor chain.
/// - `ScrollAxisMetrics` - Precomputed scroll metrics for `node`.
/// - `f32` - Remaining horizontal scroll delta.
/// - `f32` - Remaining vertical scroll delta.
///
/// # Returns
///
/// - `bool` - `true` when scroll propagation must stop at `node`.
#[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
}

/// Finds the innermost ancestor starting at `start` that can scroll in `delta`'s direction without passing an overlay or scroll-lock barrier.
///
/// # Arguments
///
/// - `&SceneTree` - Retained scene tree.
/// - `Option<NodeId>` - Starting hit node handle.
/// - `Point` - Desired scroll delta `(dx, dy)`.
///
/// # Returns
///
/// - `Option<NodeId>` - First scrollable ancestor with remaining extent in `delta`'s direction.
#[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
}

/// Re-clamps all live non-virtual scroll container offsets into their latest laid-out `[0, max_scroll]` extents after a viewport resize.
///
/// # Arguments
///
/// - `&mut SceneTree` - Mutable retained scene tree after layout.
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);
        }
    }
}