Skip to main content

nativeview_layout/top/
display.rs

1//! [`DisplayEnv`]: the committed inputs of the shared [`StyleCascade`] that a layout engine
2//! holds itself (its `@media` env, committed per-node `@container` envs, live pseudo-state), so
3//! [`crate::TopLayerIndex`] decides hoisting with exactly what layout, paint, hit-testing, focus
4//! and accessibility resolve. `Copy`, no closures, zero allocations.
5
6use nativeview_engine::InteractionTracker;
7use nativeview_store::{NodeId, NodeStore, RowPatchView};
8use nativeview_tables::container::ContainerEnv;
9use nativeview_tables::media::{ConditionalTier, MediaEnv};
10use nativeview_tables::semantics::SemanticStyleOracle;
11use nativeview_tables::tables::Display;
12
13use crate::scene::{EffectiveBox, ResponsivePatches, StyleCascade};
14use crate::taffy_store::container::scope::{GenSlots, committed_container_env};
15use crate::taffy_store::responsive::{is_node_show_on_hidden, visit_node_responsive_patches};
16
17/// Committed cascade inputs. `Default` resolves the static rows only (engines without
18/// responsive support, store-only callers).
19#[derive(Clone, Copy, Default)]
20pub struct DisplayEnv<'a> {
21    /// `@media` / `show_on` environment; `None` ignores `Responsive` rows.
22    pub media: Option<&'a MediaEnv>,
23    /// Committed per-node `@container` envs of the last pass (engine-internal).
24    pub(crate) container_envs: Option<&'a GenSlots<ContainerEnv>>,
25    /// Live pseudo-state for `StateRules` patches.
26    pub interaction: Option<&'a InteractionTracker>,
27}
28
29impl<'a> DisplayEnv<'a> {
30    /// Static rows plus live pseudo-state (engines without `@media` / `@container` support).
31    #[inline]
32    pub fn with_interaction(interaction: Option<&'a InteractionTracker>) -> Self {
33        Self {
34            interaction,
35            ..Self::default()
36        }
37    }
38
39    #[inline]
40    fn cascade(&self) -> StyleCascade<'_, Self> {
41        StyleCascade {
42            responsive: self,
43            interaction: self.interaction,
44            inert: None,
45        }
46    }
47
48    /// `id`'s [`EffectiveBox`] under these inputs.
49    #[inline]
50    pub fn effective_box(&self, store: &NodeStore, id: NodeId) -> EffectiveBox {
51        self.cascade().effective_box(store, id)
52    }
53
54    #[inline]
55    pub fn display(&self, store: &NodeStore, id: NodeId) -> Display {
56        self.effective_box(store, id).display
57    }
58
59    /// Dead or effectively `display: none`.
60    #[inline]
61    pub fn is_display_none(&self, store: &NodeStore, id: NodeId) -> bool {
62        self.cascade().is_display_none(store, id)
63    }
64
65    /// Non-root, live and effectively `display: contents`.
66    #[inline]
67    pub fn is_display_contents(&self, store: &NodeStore, id: NodeId) -> bool {
68        self.cascade().is_display_contents(store, id)
69    }
70}
71
72impl ResponsivePatches for DisplayEnv<'_> {
73    #[inline]
74    fn visit_responsive_patches<'a>(
75        &self,
76        store: &'a NodeStore,
77        id: NodeId,
78        f: &mut dyn FnMut(ConditionalTier, RowPatchView<'a>),
79    ) {
80        let Some(media) = self.media else { return };
81        let cenv = self
82            .container_envs
83            .map_or(ContainerEnv::NONE, |envs| committed_container_env(envs, id));
84        visit_node_responsive_patches(store, id, media, cenv, f);
85    }
86
87    #[inline]
88    fn is_show_on_hidden(&self, store: &NodeStore, id: NodeId) -> bool {
89        self.media
90            .is_some_and(|media| is_node_show_on_hidden(store, id, media))
91    }
92}
93
94impl SemanticStyleOracle for DisplayEnv<'_> {
95    #[inline]
96    fn is_display_none(&self, store: &NodeStore, id: NodeId) -> bool {
97        DisplayEnv::is_display_none(self, store, id)
98    }
99
100    #[inline]
101    fn is_display_contents(&self, store: &NodeStore, id: NodeId) -> bool {
102        DisplayEnv::is_display_contents(self, store, id)
103    }
104}