Skip to main content

nativeview_tables/tables/
overlay.rs

1//! `Overlay` sparse table (`nv.overlay`) plus shared anchor geometry enums
2//! (`OverlayLayer`, `Side`, `Alignment`, `OverlayPlacement`, `OverlayFlags`).
3
4use nativeview_store::{word_flags, NvTable, Rgba, Str, WordEnum};
5
6/// Default modal backdrop scrim (`rgba(15, 23, 42, 140)`), matching the Web `::backdrop` fallback.
7pub const DEFAULT_OVERLAY_BACKDROP: Rgba = Rgba::new(15, 23, 42, 140);
8
9/// Semantic top-layer tier of a floating [`Overlay`].
10#[repr(u8)]
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, WordEnum)]
12pub enum OverlayLayer {
13    #[default]
14    Popover = 0,
15    Modal = 1,
16    Tooltip = 2,
17    Menu = 3,
18}
19
20/// Per-node top-layer state resolved once per commit by `nativeview_layout::TopLayerIndex` (dense,
21/// `#[repr(u8)]` so the whole column can be published zero-copy as `*const u8`).
22#[repr(u8)]
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
24pub enum TopLayerState {
25    /// Not an overlay root, or an open overlay laid out inline (`display: contents`, or beneath a
26    /// `display: none` / hidden-input / closed-overlay / paragraph ancestor).
27    #[default]
28    None = 0,
29    /// Effectively-open overlay root hoisted into the top layer (laid out against the viewport,
30    /// painted above the document in document order).
31    Hoisted = 1,
32    /// Closed overlay root: its subtree stays alive but hidden.
33    Hidden = 2,
34}
35
36// The `top_layer_states` scene fact publishes this column as-is: discriminants are the ABI values.
37const _: () = {
38    assert!(TopLayerState::None as u8 == nativeview_abi::NV_TOP_LAYER_STATE_NONE);
39    assert!(TopLayerState::Hoisted as u8 == nativeview_abi::NV_TOP_LAYER_STATE_HOISTED);
40    assert!(TopLayerState::Hidden as u8 == nativeview_abi::NV_TOP_LAYER_STATE_HIDDEN);
41    assert!(size_of::<TopLayerState>() == 1);
42};
43
44impl From<OverlayLayer> for nativeview_abi::NvOverlayLayer {
45    #[inline(always)]
46    fn from(layer: OverlayLayer) -> Self {
47        Self(layer as u8)
48    }
49}
50
51// The published `NvOverlayLayer` values are exactly this enum's discriminants.
52const _: () = {
53    assert!(OverlayLayer::Popover as u8 == nativeview_abi::NvOverlayLayer::POPOVER.raw());
54    assert!(OverlayLayer::Modal as u8 == nativeview_abi::NvOverlayLayer::MODAL.raw());
55    assert!(OverlayLayer::Tooltip as u8 == nativeview_abi::NvOverlayLayer::TOOLTIP.raw());
56    assert!(OverlayLayer::Menu as u8 == nativeview_abi::NvOverlayLayer::MENU.raw());
57    assert!(OverlayLayer::Menu as u8 == nativeview_abi::NvOverlayLayer::MAX.raw());
58};
59
60/// Primary side relative to an anchor rectangle.
61#[repr(u8)]
62#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, WordEnum)]
63pub enum Side {
64    Top = 0,
65    #[default]
66    Bottom = 1,
67    Left = 2,
68    Right = 3,
69}
70
71impl Side {
72    #[inline]
73    pub const fn opposite(self) -> Self {
74        match self {
75            Self::Top => Self::Bottom,
76            Self::Bottom => Self::Top,
77            Self::Left => Self::Right,
78            Self::Right => Self::Left,
79        }
80    }
81
82    #[inline]
83    pub const fn is_vertical(self) -> bool {
84        matches!(self, Self::Top | Self::Bottom)
85    }
86}
87
88/// Cross-axis alignment relative to an anchor rectangle.
89#[repr(u8)]
90#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, WordEnum)]
91pub enum Alignment {
92    Start = 0,
93    #[default]
94    Center = 1,
95    End = 2,
96}
97
98impl Alignment {
99    #[inline]
100    pub const fn opposite(self) -> Self {
101        match self {
102            Self::Start => Self::End,
103            Self::Center => Self::Center,
104            Self::End => Self::Start,
105        }
106    }
107}
108
109/// All 12 anchored overlay placements (4 sides × 3 cross-axis alignments).
110#[repr(u8)]
111#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, WordEnum)]
112pub enum OverlayPlacement {
113    Top = 0,
114    TopStart = 1,
115    TopEnd = 2,
116    #[default]
117    Bottom = 3,
118    BottomStart = 4,
119    BottomEnd = 5,
120    Left = 6,
121    LeftStart = 7,
122    LeftEnd = 8,
123    Right = 9,
124    RightStart = 10,
125    RightEnd = 11,
126}
127
128impl OverlayPlacement {
129    #[inline]
130    pub const fn side(self) -> Side {
131        [Side::Top, Side::Bottom, Side::Left, Side::Right][(self as usize) / 3]
132    }
133
134    #[inline]
135    pub const fn alignment(self) -> Alignment {
136        [Alignment::Center, Alignment::Start, Alignment::End][(self as usize) % 3]
137    }
138
139    #[inline]
140    pub const fn from_side_align(side: Side, align: Alignment) -> Self {
141        const TABLE: [[OverlayPlacement; 3]; 4] = [
142            [OverlayPlacement::TopStart, OverlayPlacement::Top, OverlayPlacement::TopEnd],
143            [OverlayPlacement::BottomStart, OverlayPlacement::Bottom, OverlayPlacement::BottomEnd],
144            [OverlayPlacement::LeftStart, OverlayPlacement::Left, OverlayPlacement::LeftEnd],
145            [OverlayPlacement::RightStart, OverlayPlacement::Right, OverlayPlacement::RightEnd],
146        ];
147        TABLE[side as usize][align as usize]
148    }
149
150    /// Flips the primary side (`Bottom -> Top`, `RightStart -> LeftStart`) while keeping alignment.
151    #[inline]
152    pub const fn opposite(self) -> Self {
153        Self::from_side_align(self.side().opposite(), self.alignment())
154    }
155
156    /// Flips cross-axis alignment (`Start <-> End`) while keeping the primary side.
157    #[inline]
158    pub const fn flip_alignment(self) -> Self {
159        Self::from_side_align(self.side(), self.alignment().opposite())
160    }
161}
162
163impl core::fmt::Display for OverlayPlacement {
164    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
165        let s = match self {
166            Self::Top => "top",
167            Self::TopStart => "top-start",
168            Self::TopEnd => "top-end",
169            Self::Bottom => "bottom",
170            Self::BottomStart => "bottom-start",
171            Self::BottomEnd => "bottom-end",
172            Self::Left => "left",
173            Self::LeftStart => "left-start",
174            Self::LeftEnd => "left-end",
175            Self::Right => "right",
176            Self::RightStart => "right-start",
177            Self::RightEnd => "right-end",
178        };
179        f.write_str(s)
180    }
181}
182
183word_flags! {
184    /// Floating overlay behavioral flags packed into 8 bits.
185    pub struct OverlayFlags(u8) {
186        const OPEN = 1 << 0;
187        const FLIP = 1 << 1;
188        const SHIFT = 1 << 2;
189        const AUTO_SIZE = 1 << 3;
190        const BACKDROP_SCRIM = 1 << 4;
191        /// Non-modal, non-light-dismiss top-layer region (`popover="manual"` on Web; e.g. `ToastViewport`).
192        const MANUAL = 1 << 5;
193        /// Outside/backdrop press emits `Custom { name: "close" }` (independent of `BACKDROP_SCRIM`).
194        const LIGHT_DISMISS = 1 << 6;
195        /// An unconsumed close request (`Escape` / `GoBack` key down) closes this overlay. A modal
196        /// without it still owns the request, so nothing beneath it closes (`closedby="none"`).
197        const CLOSE_ON_ESCAPE = 1 << 7;
198    }
199}
200
201impl OverlayFlags {
202    /// Whether an outside press closes an overlay on `layer` with these flags: `LIGHT_DISMISS`, or
203    /// implied by the `Popover` / `Menu` layers, never for `MANUAL`. The one resolution every
204    /// host (layout scene, Web manual-popover light dismiss) reads.
205    #[inline]
206    pub const fn light_dismiss_on(self, layer: OverlayLayer) -> bool {
207        !self.contains(Self::MANUAL)
208            && (self.contains(Self::LIGHT_DISMISS)
209                || matches!(layer, OverlayLayer::Popover | OverlayLayer::Menu))
210    }
211
212    /// Whether an unconsumed close request closes this overlay (never for `MANUAL`).
213    #[inline]
214    pub const fn closes_on_escape(self) -> bool {
215        self.contains(Self::CLOSE_ON_ESCAPE) && !self.contains(Self::MANUAL)
216    }
217}
218
219/// Cross-platform floating overlay table (`nv.overlay`) driving top-layer promotion,
220/// anchor-relative positioning, collision flipping/shifting, and modal backdrop scrims.
221#[derive(NvTable)]
222#[nv_table(name = "nv.overlay", storage = "sparse", flags = "publish")]
223pub struct Overlay {
224    #[effect(realize, layout, paint)]
225    pub layer: OverlayLayer,
226    /// Anchor node `DebugId::id` (`""` = unanchored / viewport-centered modal).
227    #[effect(layout, paint)]
228    pub anchor_id: Str,
229    #[effect(layout, paint)]
230    pub placement: OverlayPlacement,
231    #[effect(layout, paint)]
232    #[default(8.0)]
233    pub offset_px: f32,
234    #[effect(layout, paint)]
235    #[default(8.0)]
236    pub viewport_padding_px: f32,
237    #[effect(realize, layout, paint)]
238    pub options: OverlayFlags,
239    /// Backdrop scrim color painted behind the overlay when it has a scrim (theme `scrim` token).
240    #[effect(paint)]
241    #[default(DEFAULT_OVERLAY_BACKDROP)]
242    pub backdrop_color: Rgba,
243}
244
245const _: () = assert!(Overlay::DESC.words() == 7);