nativeview-layout 0.1.0-rc.1

Shared NodeStore layout layer: StoreLayoutEngine / TextMeasurer traits and the default direct Taffy engine
Documentation
//! Scroll content extents: the union of a scroll container's in-flow box children (flattening
//! `display: contents`) and its virtualized totals.

use nativeview_engine::LayoutRect;
use nativeview_store::{NodeId, NodeStore};
use nativeview_tables::tables::{Display, Scroll};

use crate::MAX_TREE_DEPTH;

/// Computes `(content_width, content_height)` for a scroll container node.
/// `display_of` is the cascaded `display` (`@media` / `@container` / state rules), so children a
/// rule hides or turns into `display: contents` are skipped or flattened exactly like layout.
pub fn compute_scroll_content_size(
    store: &NodeStore,
    layout_of: impl Fn(NodeId) -> Option<LayoutRect>,
    display_of: impl Fn(NodeId) -> Display,
    node_id: NodeId,
) -> Option<(f32, f32)> {
    let rect = layout_of(node_id)?;
    let mut extent = Extent {
        origin_x: rect.x,
        origin_y: rect.y,
        max_w: rect.width,
        max_h: rect.height,
    };
    if let Some(scroll) = store.try_row::<Scroll>(node_id) {
        if let Some(vw) = scroll.virtual_total_width().filter(|v| v.is_finite()) {
            extent.max_w = extent.max_w.max(vw);
        }
        if let Some(vh) = scroll.virtual_total_height().filter(|v| v.is_finite()) {
            extent.max_h = extent.max_h.max(vh);
        }
    }
    extent.visit_box_children(store, &layout_of, &display_of, node_id, 0);
    Some((extent.max_w.max(0.0), extent.max_h.max(0.0)))
}

/// Running union of box children, relative to the scroll container's origin.
struct Extent {
    origin_x: f32,
    origin_y: f32,
    max_w: f32,
    max_h: f32,
}

impl Extent {
    fn visit_box_children(
        &mut self,
        store: &NodeStore,
        layout_of: &impl Fn(NodeId) -> Option<LayoutRect>,
        display_of: &impl Fn(NodeId) -> Display,
        parent: NodeId,
        depth: usize,
    ) {
        if depth > MAX_TREE_DEPTH {
            return;
        }
        for &cid in store.children(parent) {
            if !store.is_live(cid) {
                continue;
            }
            match display_of(cid) {
                Display::None => {}
                Display::Contents => {
                    self.visit_box_children(store, layout_of, display_of, cid, depth + 1);
                }
                _ => {
                    if let Some(cr) = layout_of(cid) {
                        self.max_w = self.max_w.max((cr.x - self.origin_x) + cr.width);
                        self.max_h = self.max_h.max((cr.y - self.origin_y) + cr.height);
                    }
                }
            }
        }
    }
}

const SCROLLBAR_THICKNESS: f32 = 6.0;
const SCROLLBAR_MARGIN: f32 = 3.0;
const SCROLLBAR_MIN_THUMB: f32 = 20.0;

#[inline]
pub fn intersect_rects(a: LayoutRect, b: LayoutRect) -> Option<LayoutRect> {
    let x0 = a.x.max(b.x);
    let y0 = a.y.max(b.y);
    let x1 = (a.x + a.width).min(b.x + b.width);
    let y1 = (a.y + a.height).min(b.y + b.height);
    (x1 > x0 && y1 > y0).then_some(LayoutRect {
        x: x0,
        y: y0,
        width: x1 - x0,
        height: y1 - y0,
    })
}

#[inline]
pub fn intersects_clip(clip: LayoutRect, r: LayoutRect, pad: f32) -> bool {
    let x0 = r.x - pad;
    let y0 = r.y - pad;
    let x1 = r.x + r.width + pad;
    let y1 = r.y + r.height + pad;
    x1 > clip.x && x0 < clip.x + clip.width && y1 > clip.y && y0 < clip.y + clip.height
}

#[inline]
fn scrollbar_thumb_axis(
    origin: f32,
    view: f32,
    content: f32,
    scroll: f32,
    overscroll: f32,
    corner_gap: f32,
) -> (f32, f32) {
    let track = (view - SCROLLBAR_MARGIN * 2.0 - corner_gap).max(1.0);
    let base = ((view / content) * track).clamp(SCROLLBAR_MIN_THUMB.min(track), track);
    if overscroll.abs() > 0.25 {
        let len = (base * (view / (view + overscroll.abs() * 1.5)))
            .clamp((SCROLLBAR_THICKNESS * 2.0).min(base), base);
        let pos = origin + SCROLLBAR_MARGIN + if overscroll < 0.0 { 0.0 } else { track - len };
        (pos, len)
    } else {
        let ratio = (scroll / (content - view).max(1.0)).clamp(0.0, 1.0);
        (origin + SCROLLBAR_MARGIN + ratio * (track - base), base)
    }
}

/// Computes overlay vertical & horizontal scrollbar thumb geometry for a `Scroll` container and
/// yields each visible `(thumb_rect, corner_radius)` to `emit`.
#[inline]
pub fn for_each_scrollbar_thumb_geom(
    rect: LayoutRect,
    (sx, sy): (f32, f32),
    (cw, ch): (f32, f32),
    (overscroll_x, overscroll_y): (f32, f32),
    mut emit: impl FnMut(LayoutRect, f32),
) {
    let (has_v, has_h) = (ch > rect.height + 0.5, cw > rect.width + 0.5);
    if !has_v && !has_h {
        return;
    }
    let corner_gap = if has_v && has_h {
        SCROLLBAR_THICKNESS + SCROLLBAR_MARGIN
    } else {
        0.0
    };
    let min_cross = SCROLLBAR_THICKNESS + SCROLLBAR_MARGIN * 2.0;
    let min_main = SCROLLBAR_MARGIN * 2.0 + corner_gap + 4.0;
    if has_v && rect.height > min_main && rect.width > min_cross {
        let (y, height) = scrollbar_thumb_axis(rect.y, rect.height, ch, sy, overscroll_y, corner_gap);
        emit(
            LayoutRect {
                x: rect.x + rect.width - SCROLLBAR_THICKNESS - SCROLLBAR_MARGIN,
                y,
                width: SCROLLBAR_THICKNESS,
                height,
            },
            SCROLLBAR_THICKNESS * 0.5,
        );
    }
    if has_h && rect.width > min_main && rect.height > min_cross {
        let (x, width) = scrollbar_thumb_axis(rect.x, rect.width, cw, sx, overscroll_x, corner_gap);
        emit(
            LayoutRect {
                x,
                y: rect.y + rect.height - SCROLLBAR_THICKNESS - SCROLLBAR_MARGIN,
                width,
                height: SCROLLBAR_THICKNESS,
            },
            SCROLLBAR_THICKNESS * 0.5,
        );
    }
}