nativeview-layout 0.1.0-rc.1

Shared NodeStore layout layer: StoreLayoutEngine / TextMeasurer traits and the default direct Taffy engine
Documentation
//! Value-type layout/visual snapshots and visibility helpers over `NodeStore`.

use nativeview_abi::ColorRgba;
use nativeview_tables::snapshots::{LayoutSnapshot, VisualSnapshot};
use nativeview_tables::tables::{Layout as NvLayout, Typography, box_visual};
use nativeview_store::{Length, NodeId, NodeStore};

/// Extracts a value-type [`LayoutSnapshot`] from `store` for `id`.
pub fn layout_snapshot_of(store: &NodeStore, id: NodeId) -> LayoutSnapshot {
    let Some(l) = store.try_row::<NvLayout>(id) else {
        return LayoutSnapshot::default();
    };
    let px = |len: Length| if len.is_points() { len.value() } else { 0.0 };
    LayoutSnapshot {
        width: l.width().to_dimension(),
        height: l.height().to_dimension(),
        min_width: l.min_width().to_dimension(),
        min_height: l.min_height().to_dimension(),
        max_width: l.max_width().to_dimension(),
        max_height: l.max_height().to_dimension(),
        flex_direction: l.flex_direction(),
        justify_content: l.justify_content().into(),
        align_items: l.align_items().into(),
        align_self: l.align_self(),
        flex_grow: l.flex_grow(),
        flex_shrink: l.flex_shrink(),
        gap: px(l.row_gap()).max(px(l.column_gap())),
        padding: nativeview_abi::EdgeInsets {
            top: px(l.padding_top()),
            right: px(l.padding_right()),
            bottom: px(l.padding_bottom()),
            left: px(l.padding_left()),
        },
        margin: nativeview_abi::EdgeInsets {
            top: px(l.margin_top()),
            right: px(l.margin_right()),
            bottom: px(l.margin_bottom()),
            left: px(l.margin_left()),
        },
    }
}

/// Extracts a value-type [`VisualSnapshot`] from `store` for `id`.
pub fn visual_snapshot_of(store: &NodeStore, id: NodeId) -> VisualSnapshot {
    let mut snap = VisualSnapshot::default();
    if let Some(v) = box_visual(store, id) {
        snap.background = ColorRgba::from(v.background());
        snap.border_color = ColorRgba::from(v.border_color());
        snap.border_width = v.border_width();
        snap.border_radius = v.border_radius();
        snap.opacity = v.opacity();
        snap.z_index = v.z_index().map(nativeview_abi::ZIndex);
    }
    if let Some(t) = store.try_row::<Typography>(id) {
        snap.text_color = ColorRgba::from(t.color());
        snap.font_size = t.font_size();
    }
    snap
}