Skip to main content

nativeview_layout/scene/light/
mod.rs

1//! Primary pointer-DOWN light dismiss over the open top layer: the one rule every host applies
2//! ([`hide_popovers_until`]; skia-shell through [`top_layer_press`], the Web overlay runtime's
3//! emulation for overlays the browser does not dismiss itself, and the C++ / Swift / Java / Dart /
4//! TS ports, which run the `codegen abi` conformance vectors of this rule
5//! [INV:top-layer-policy-conformance]).
6//!
7//! HTML "hide popovers until": walking the open overlays front to back (reverse paint order), only
8//! `light_dismiss` overlays and modals take part (HTML's auto / hint popovers and modal dialogs);
9//! every other overlay (manual toasts, plain hints) is skipped: a press inside it stops nothing
10//! and it never closes, but the press still lands inside an open overlay (`Inside` when nothing
11//! closes, and never blocked by a modal below it). The walk stops at the first overlay taking
12//! part that contains the press (its hit
13//! node lies within it, or the point lies inside its box), at the first `light_dismiss` overlay
14//! whose *active* anchor the press lands on (the anchor toggles that overlay itself; an anchor
15//! made inert by a modal above it is no anchor), and at the first modal (the barrier: nothing
16//! below it sees the press). Every `light_dismiss` overlay the walk passed is outside the press,
17//! so the backmost of them — and every `light_dismiss` overlay above it — closes in one batch: a
18//! press outside a menu and its submenu closes both, a press inside the menu closes only the
19//! submenu, a press on a manual toast above a menu closes the menu. A `light_dismiss` modal the
20//! press is outside of closes with them.
21//!
22//! Anchors are matched by node containment of the hit node (as the DOM's `anchor.contains(target)`),
23//! never by the anchor's box: a box test claims presses on content painted over the anchor.
24//!
25//! The host contract around the walk (every host follows it):
26//! - **Modal**: only the `topmost_modal` scene fact is the modal of the walk. The overlay MODAL
27//!   flags are never scanned for it; the fact is authoritative.
28//! - **Hit node**: the *deepest painted* node at the press point (the node whose box is topmost
29//!   in paint order), never the topmost *interactive* node: an interactive ancestor can lie
30//!   outside the overlay that paints the pressed content (a popover hangs under its anchor). One
31//!   hit test per press feeds this walk, press focus and the pointer-down dispatch.
32//! - **After the walk**: the host sends the [`TopLayerPress::light_dismissed`] closes first, then
33//!   continues the press normally (hit test, press focus, `PointerDown`) unless
34//!   [`TopLayerPress::modal_barrier`]: a light-dismissing press is not swallowed (HTML: the click
35//!   that closes a popover still reaches what it lands on).
36
37use nativeview_store::NodeId;
38
39use super::ResolvedOverlay;
40
41/// What a primary pointer DOWN does to the open top layer.
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub enum PressOutcome {
44    /// No overlay claims the press: normal hit-testing.
45    None,
46    /// Inside an open overlay (or on an active overlay anchor): normal hit-testing.
47    Inside,
48    /// Close [`TopLayerPress::dismissed`] (non-bubbling `close`, front to back, one batch).
49    LightDismiss,
50    /// Outside a modal without `light_dismiss`: the press is swallowed.
51    BlockedByModal,
52}
53
54/// The decision of [`top_layer_press`] over one `overlays` slice.
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56pub struct TopLayerPress {
57    pub outcome: PressOutcome,
58    /// [`PressOutcome::LightDismiss`]: paint index of the backmost overlay to close.
59    pub dismiss_from: usize,
60    /// The press must not reach anything below (a modal stopped the walk).
61    pub modal_barrier: bool,
62}
63
64impl TopLayerPress {
65    /// The overlays to close, front to back (children before the overlays they opened from): every
66    /// `light_dismiss` overlay of the decided `overlays` from [`Self::dismiss_from`] up.
67    pub fn light_dismissed<'a>(&self, overlays: &'a [ResolvedOverlay]) -> impl Iterator<Item = NodeId> + 'a {
68        let from = if self.outcome == PressOutcome::LightDismiss {
69            self.dismiss_from
70        } else {
71            overlays.len()
72        };
73        overlays
74            .get(from..)
75            .unwrap_or_default()
76            .iter()
77            .rev()
78            .filter(|ro| ro.light_dismiss)
79            .map(|ro| ro.node)
80    }
81}
82
83/// One open overlay as the walk sees a press (see the module docs).
84#[derive(Debug, Clone, Copy, PartialEq, Eq)]
85pub struct PressedOverlay {
86    /// The press lies inside it (hit node within it, or the point inside its box).
87    pub inside: bool,
88    /// The press lands on its active anchor.
89    pub on_anchor: bool,
90    pub light_dismiss: bool,
91    pub modal: bool,
92}
93
94/// The "hide popovers until" walk over `overlays` (back-to-front paint order; see the module
95/// docs). The views are produced lazily: the walk evaluates only the overlays it reaches, front
96/// to back, and reads `on_anchor` only of a non-modal `light_dismiss` overlay.
97pub fn hide_popovers_until(
98    overlays: impl DoubleEndedIterator<Item = PressedOverlay> + ExactSizeIterator,
99) -> TopLayerPress {
100    let len = overlays.len();
101    let mut dismiss_from = None;
102    let mut modal_barrier = false;
103    let mut inside = false;
104    for (i, ov) in overlays.enumerate().rev() {
105        if !(ov.light_dismiss || ov.modal) {
106            inside |= ov.inside;
107            continue;
108        }
109        if ov.inside {
110            inside = true;
111            break;
112        }
113        if ov.modal {
114            // A press inside an overlay above the modal (a manual toast) is in the active layer.
115            modal_barrier = !inside;
116            if ov.light_dismiss {
117                dismiss_from = Some(i);
118            }
119            break;
120        }
121        if ov.on_anchor {
122            inside = true;
123            break;
124        }
125        dismiss_from = Some(i);
126    }
127    let outcome = match (dismiss_from, modal_barrier, inside) {
128        (Some(_), _, _) => PressOutcome::LightDismiss,
129        (None, true, _) => PressOutcome::BlockedByModal,
130        (None, false, true) => PressOutcome::Inside,
131        (None, false, false) => PressOutcome::None,
132    };
133    TopLayerPress {
134        outcome,
135        dismiss_from: dismiss_from.unwrap_or(len),
136        modal_barrier,
137    }
138}
139
140/// Decides a primary pointer DOWN against the open `overlays` (back-to-front paint order; see the
141/// module docs). `topmost_modal` is the `topmost_modal` scene fact (`None`, or a node that is not
142/// open: no modal), `hit` the deepest painted node at the press, `is_within(node, ancestor)`
143/// inclusive tree containment, and `contains(overlay)` the (transform-aware) point-in-overlay-box
144/// test. Allocation-free; O(open overlays) plus O(depth) containment walks.
145pub fn top_layer_press(
146    overlays: &[ResolvedOverlay],
147    topmost_modal: Option<NodeId>,
148    hit: Option<NodeId>,
149    is_within: impl Fn(NodeId, NodeId) -> bool,
150    contains: impl Fn(&ResolvedOverlay) -> bool,
151) -> TopLayerPress {
152    let modal = topmost_modal.and_then(|m| overlays.iter().rposition(|ro| ro.node == m));
153    let within = |node: NodeId| !node.is_null() && hit.is_some_and(|h| is_within(h, node));
154    // The hit takes input: no modal, or it lies within the topmost modal or an overlay above it.
155    let hit_active = || modal.is_none_or(|m| overlays[m..].iter().any(|ro| within(ro.node)));
156    hide_popovers_until(overlays.iter().enumerate().map(|(i, ro)| {
157        let is_modal = modal == Some(i);
158        PressedOverlay {
159            inside: within(ro.node) || contains(ro),
160            on_anchor: ro.light_dismiss && !is_modal && within(ro.anchor) && hit_active(),
161            light_dismiss: ro.light_dismiss,
162            modal: is_modal,
163        }
164    }))
165}
166
167#[cfg(test)]
168mod tests;