Skip to main content

nativeview_layout/scene/overlay/
resolved.rs

1//! One open top-layer overlay as the scene resolved it ([`ResolvedOverlay`]), and the stack-level
2//! rules every host shares over the open overlays (back-to-front paint order): the topmost modal
3//! and the close-request owner the runtime publishes as scene facts, the overlay box test of the
4//! pointer-down policy ([`super::top_layer_press`]), and the published ABI item.
5
6use nativeview_abi::{ColorRgba, NvOverlayFlags, NvOverlayItem};
7use nativeview_store::NodeId;
8use nativeview_tables::tables::{OverlayLayer, OverlayPlacement};
9
10/// Reconciled viewport-space geometry and layer metadata for one open top-layer `Overlay`.
11#[derive(Debug, Clone, Copy, PartialEq)]
12pub struct ResolvedOverlay {
13    pub node: NodeId,
14    /// Resolved top-layer origin in logical viewport coordinates.
15    pub origin_x: f32,
16    pub origin_y: f32,
17    /// Effective width after optional `AUTO_SIZE` viewport clamping.
18    pub width: f32,
19    /// Effective height after optional `AUTO_SIZE` viewport clamping.
20    pub height: f32,
21    pub placement: OverlayPlacement,
22    pub layer: OverlayLayer,
23    pub is_modal: bool,
24    pub has_backdrop_scrim: bool,
25    pub light_dismiss: bool,
26    /// Closes on an unconsumed close request (`Overlay::options` `CLOSE_ON_ESCAPE`).
27    pub close_on_escape: bool,
28    /// `Overlay::backdrop_color`; painted only when `has_backdrop_scrim`.
29    pub backdrop_color: ColorRgba,
30    /// Live node resolved from `Overlay::anchor_id` (`NodeId::NULL` when unanchored or
31    /// unresolved). A press on it never light-dismisses the overlay.
32    pub anchor: NodeId,
33}
34
35impl ResolvedOverlay {
36    /// The scrim color painted behind this overlay, or `None` without a backdrop scrim.
37    #[inline]
38    pub const fn backdrop(&self) -> Option<ColorRgba> {
39        if self.has_backdrop_scrim {
40            Some(self.backdrop_color)
41        } else {
42            None
43        }
44    }
45
46    /// Inclusive containment of the (untransformed, overlay-space) point `(x, y)` in this
47    /// overlay's box. An empty box and a non-finite point never contain: the box test every host
48    /// port of the pointer-down policy applies (C++ / Swift `contains`, Java / Dart / TS).
49    #[inline]
50    pub fn box_contains(&self, x: f32, y: f32) -> bool {
51        self.width > 0.0
52            && self.height > 0.0
53            && x.is_finite()
54            && y.is_finite()
55            && x >= self.origin_x
56            && x <= self.origin_x + self.width
57            && y >= self.origin_y
58            && y <= self.origin_y + self.height
59    }
60
61    /// The `NvOverlayItem` the runtime publishes for this overlay (`NvSceneView::overlays`).
62    pub fn overlay_item(&self) -> NvOverlayItem {
63        let mut flags = NvOverlayFlags::NONE;
64        flags.set(NvOverlayFlags::MODAL, self.is_modal);
65        flags.set(NvOverlayFlags::BACKDROP_SCRIM, self.has_backdrop_scrim);
66        flags.set(NvOverlayFlags::LIGHT_DISMISS, self.light_dismiss);
67        flags.set(NvOverlayFlags::CLOSE_ON_ESCAPE, self.close_on_escape);
68        NvOverlayItem {
69            node_id: self.node,
70            x: self.origin_x,
71            y: self.origin_y,
72            width: self.width,
73            height: self.height,
74            flags,
75            layer: self.layer.into(),
76            _pad: 0,
77            backdrop_color: self.backdrop_color.to_u32(),
78            anchor_node_id: self.anchor,
79        }
80    }
81}
82
83/// Paint index of the topmost (last painted) modal of `overlays`: the `topmost_modal` scene fact.
84/// Content outside it and the overlays painted above it is inert.
85#[inline]
86pub fn topmost_modal_index(overlays: &[ResolvedOverlay]) -> Option<usize> {
87    overlays.iter().rposition(|ro| ro.is_modal)
88}
89
90/// The overlay an unconsumed close request closes: the `close_target` scene fact
91/// ([INV:close-request-single-path]). Front to back, the first overlay that is modal or
92/// `close_on_escape` owns the request; it closes only when it is `close_on_escape`, so a
93/// non-escapable modal swallows the request (HTML close-watcher semantics).
94#[inline]
95pub fn close_request_owner(overlays: &[ResolvedOverlay]) -> Option<NodeId> {
96    let owner = overlays.iter().rev().find(|ro| ro.is_modal || ro.close_on_escape)?;
97    owner.close_on_escape.then_some(owner.node)
98}