nativeview-layout 0.1.0-rc.1

Shared NodeStore layout layer: StoreLayoutEngine / TextMeasurer traits and the default direct Taffy engine
Documentation
//! Generic intrinsic leaf-measurement extension point ([`LeafMeasureHook`]) for capability tables
//! (e.g. `TextInput`, `Toggle`, `Gauge`, `Video`, `WebView`, `Camera`, or other capability modules).
//!
//! Hooks are stored in a [`SlotRegistry`](nativeview_engine::ext::slot_registry::SlotRegistry) on the
//! layout engine: an empty registry is 32 B inline and exits in 1 branch with zero heap
//! allocations, while populated registries dispatch in O(1) via the node's `TableSet` bitmask.

use nativeview_store::{NodeId, NodeStore};

pub use taffy::geometry::Size;
pub use taffy::style::AvailableSpace;

use crate::{ResolvedTextStyle, TextMeasurer};

/// Compact, zero-allocation context passed to [`LeafMeasureHook::measure`] during Taffy leaf
/// measurement.
pub struct LeafMeasureContext<'a> {
    /// Live `NodeStore` for reading the leaf's own word tables and strings.
    pub store: &'a NodeStore,
    /// The leaf node being measured.
    pub node: NodeId,
    /// Definite border-box dimensions already known by Taffy (if any).
    pub known_dimensions: Size<Option<f32>>,
    /// Available space from the parent container on each axis.
    pub available_space: Size<AvailableSpace>,
    /// Active text measurer installed on the layout engine (for `TextInput`, `Gauge`, etc.).
    pub text_measurer: &'a dyn TextMeasurer,
    /// Inherited resolved typography for [`Self::node`].
    pub resolved_text: &'a ResolvedTextStyle,
    /// Resolved explicit CSS `aspect-ratio` (`width / height`, finite and `> 0.0`), if authored.
    pub aspect_ratio: Option<f32>,
}

/// Intrinsic content-box measurement hook registered per [`nativeview_store::TableSlot`] on
/// [`crate::StoreLayoutEngine::register_measure_hook`].
///
/// Returning `Some(size)` supplies the leaf's content-box dimensions in logical points (non-finite
/// or negative components clamp to `0.0`; explicit CSS `aspect-ratio` combines with a single known
/// axis matching standard replaced-element sizing); returning `None` falls through to the next
/// matching hook or default `aspect-ratio` / `Size::ZERO` sizing.
pub trait LeafMeasureHook: Send + Sync {
    /// Measures a leaf node's intrinsic content size `(width, height)` in logical points.
    fn measure(&self, ctx: &LeafMeasureContext<'_>) -> Option<Size<f32>>;
}