nativeview-layout 0.1.0-rc.1

Shared NodeStore layout layer: StoreLayoutEngine / TextMeasurer traits and the default direct Taffy engine
Documentation
//! [`DisplayEnv`]: the committed inputs of the shared [`StyleCascade`] that a layout engine
//! holds itself (its `@media` env, committed per-node `@container` envs, live pseudo-state), so
//! [`crate::TopLayerIndex`] decides hoisting with exactly what layout, paint, hit-testing, focus
//! and accessibility resolve. `Copy`, no closures, zero allocations.

use nativeview_engine::InteractionTracker;
use nativeview_store::{NodeId, NodeStore, RowPatchView};
use nativeview_tables::container::ContainerEnv;
use nativeview_tables::media::{ConditionalTier, MediaEnv};
use nativeview_tables::semantics::SemanticStyleOracle;
use nativeview_tables::tables::Display;

use crate::scene::{EffectiveBox, ResponsivePatches, StyleCascade};
use crate::taffy_store::container::scope::{GenSlots, committed_container_env};
use crate::taffy_store::responsive::{is_node_show_on_hidden, visit_node_responsive_patches};

/// Committed cascade inputs. `Default` resolves the static rows only (engines without
/// responsive support, store-only callers).
#[derive(Clone, Copy, Default)]
pub struct DisplayEnv<'a> {
    /// `@media` / `show_on` environment; `None` ignores `Responsive` rows.
    pub media: Option<&'a MediaEnv>,
    /// Committed per-node `@container` envs of the last pass (engine-internal).
    pub(crate) container_envs: Option<&'a GenSlots<ContainerEnv>>,
    /// Live pseudo-state for `StateRules` patches.
    pub interaction: Option<&'a InteractionTracker>,
}

impl<'a> DisplayEnv<'a> {
    /// Static rows plus live pseudo-state (engines without `@media` / `@container` support).
    #[inline]
    pub fn with_interaction(interaction: Option<&'a InteractionTracker>) -> Self {
        Self {
            interaction,
            ..Self::default()
        }
    }

    #[inline]
    fn cascade(&self) -> StyleCascade<'_, Self> {
        StyleCascade {
            responsive: self,
            interaction: self.interaction,
            inert: None,
        }
    }

    /// `id`'s [`EffectiveBox`] under these inputs.
    #[inline]
    pub fn effective_box(&self, store: &NodeStore, id: NodeId) -> EffectiveBox {
        self.cascade().effective_box(store, id)
    }

    #[inline]
    pub fn display(&self, store: &NodeStore, id: NodeId) -> Display {
        self.effective_box(store, id).display
    }

    /// Dead or effectively `display: none`.
    #[inline]
    pub fn is_display_none(&self, store: &NodeStore, id: NodeId) -> bool {
        self.cascade().is_display_none(store, id)
    }

    /// Non-root, live and effectively `display: contents`.
    #[inline]
    pub fn is_display_contents(&self, store: &NodeStore, id: NodeId) -> bool {
        self.cascade().is_display_contents(store, id)
    }
}

impl ResponsivePatches for DisplayEnv<'_> {
    #[inline]
    fn visit_responsive_patches<'a>(
        &self,
        store: &'a NodeStore,
        id: NodeId,
        f: &mut dyn FnMut(ConditionalTier, RowPatchView<'a>),
    ) {
        let Some(media) = self.media else { return };
        let cenv = self
            .container_envs
            .map_or(ContainerEnv::NONE, |envs| committed_container_env(envs, id));
        visit_node_responsive_patches(store, id, media, cenv, f);
    }

    #[inline]
    fn is_show_on_hidden(&self, store: &NodeStore, id: NodeId) -> bool {
        self.media
            .is_some_and(|media| is_node_show_on_hidden(store, id, media))
    }
}

impl SemanticStyleOracle for DisplayEnv<'_> {
    #[inline]
    fn is_display_none(&self, store: &NodeStore, id: NodeId) -> bool {
        DisplayEnv::is_display_none(self, store, id)
    }

    #[inline]
    fn is_display_contents(&self, store: &NodeStore, id: NodeId) -> bool {
        DisplayEnv::is_display_contents(self, store, id)
    }
}