Skip to main content

nativeview_layout/
leaf_measure.rs

1//! Generic intrinsic leaf-measurement extension point ([`LeafMeasureHook`]) for capability tables
2//! (e.g. `TextInput`, `Toggle`, `Gauge`, `Video`, `WebView`, `Camera`, or other capability modules).
3//!
4//! Hooks are stored in a [`SlotRegistry`](nativeview_engine::ext::slot_registry::SlotRegistry) on the
5//! layout engine: an empty registry is 32 B inline and exits in 1 branch with zero heap
6//! allocations, while populated registries dispatch in O(1) via the node's `TableSet` bitmask.
7
8use nativeview_store::{NodeId, NodeStore};
9
10pub use taffy::geometry::Size;
11pub use taffy::style::AvailableSpace;
12
13use crate::{ResolvedTextStyle, TextMeasurer};
14
15/// Compact, zero-allocation context passed to [`LeafMeasureHook::measure`] during Taffy leaf
16/// measurement.
17pub struct LeafMeasureContext<'a> {
18    /// Live `NodeStore` for reading the leaf's own word tables and strings.
19    pub store: &'a NodeStore,
20    /// The leaf node being measured.
21    pub node: NodeId,
22    /// Definite border-box dimensions already known by Taffy (if any).
23    pub known_dimensions: Size<Option<f32>>,
24    /// Available space from the parent container on each axis.
25    pub available_space: Size<AvailableSpace>,
26    /// Active text measurer installed on the layout engine (for `TextInput`, `Gauge`, etc.).
27    pub text_measurer: &'a dyn TextMeasurer,
28    /// Inherited resolved typography for [`Self::node`].
29    pub resolved_text: &'a ResolvedTextStyle,
30    /// Resolved explicit CSS `aspect-ratio` (`width / height`, finite and `> 0.0`), if authored.
31    pub aspect_ratio: Option<f32>,
32}
33
34/// Intrinsic content-box measurement hook registered per [`nativeview_store::TableSlot`] on
35/// [`crate::StoreLayoutEngine::register_measure_hook`].
36///
37/// Returning `Some(size)` supplies the leaf's content-box dimensions in logical points (non-finite
38/// or negative components clamp to `0.0`; explicit CSS `aspect-ratio` combines with a single known
39/// axis matching standard replaced-element sizing); returning `None` falls through to the next
40/// matching hook or default `aspect-ratio` / `Size::ZERO` sizing.
41pub trait LeafMeasureHook: Send + Sync {
42    /// Measures a leaf node's intrinsic content size `(width, height)` in logical points.
43    fn measure(&self, ctx: &LeafMeasureContext<'_>) -> Option<Size<f32>>;
44}