nativeview-layout 0.1.0-rc.1

Shared NodeStore layout layer: StoreLayoutEngine / TextMeasurer traits and the default direct Taffy engine
Documentation
//! One open top-layer overlay as the scene resolved it ([`ResolvedOverlay`]), and the stack-level
//! rules every host shares over the open overlays (back-to-front paint order): the topmost modal
//! and the close-request owner the runtime publishes as scene facts, the overlay box test of the
//! pointer-down policy ([`super::top_layer_press`]), and the published ABI item.

use nativeview_abi::{ColorRgba, NvOverlayFlags, NvOverlayItem};
use nativeview_store::NodeId;
use nativeview_tables::tables::{OverlayLayer, OverlayPlacement};

/// Reconciled viewport-space geometry and layer metadata for one open top-layer `Overlay`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ResolvedOverlay {
    pub node: NodeId,
    /// Resolved top-layer origin in logical viewport coordinates.
    pub origin_x: f32,
    pub origin_y: f32,
    /// Effective width after optional `AUTO_SIZE` viewport clamping.
    pub width: f32,
    /// Effective height after optional `AUTO_SIZE` viewport clamping.
    pub height: f32,
    pub placement: OverlayPlacement,
    pub layer: OverlayLayer,
    pub is_modal: bool,
    pub has_backdrop_scrim: bool,
    pub light_dismiss: bool,
    /// Closes on an unconsumed close request (`Overlay::options` `CLOSE_ON_ESCAPE`).
    pub close_on_escape: bool,
    /// `Overlay::backdrop_color`; painted only when `has_backdrop_scrim`.
    pub backdrop_color: ColorRgba,
    /// Live node resolved from `Overlay::anchor_id` (`NodeId::NULL` when unanchored or
    /// unresolved). A press on it never light-dismisses the overlay.
    pub anchor: NodeId,
}

impl ResolvedOverlay {
    /// The scrim color painted behind this overlay, or `None` without a backdrop scrim.
    #[inline]
    pub const fn backdrop(&self) -> Option<ColorRgba> {
        if self.has_backdrop_scrim {
            Some(self.backdrop_color)
        } else {
            None
        }
    }

    /// Inclusive containment of the (untransformed, overlay-space) point `(x, y)` in this
    /// overlay's box. An empty box and a non-finite point never contain: the box test every host
    /// port of the pointer-down policy applies (C++ / Swift `contains`, Java / Dart / TS).
    #[inline]
    pub fn box_contains(&self, x: f32, y: f32) -> bool {
        self.width > 0.0
            && self.height > 0.0
            && x.is_finite()
            && y.is_finite()
            && x >= self.origin_x
            && x <= self.origin_x + self.width
            && y >= self.origin_y
            && y <= self.origin_y + self.height
    }

    /// The `NvOverlayItem` the runtime publishes for this overlay (`NvSceneView::overlays`).
    pub fn overlay_item(&self) -> NvOverlayItem {
        let mut flags = NvOverlayFlags::NONE;
        flags.set(NvOverlayFlags::MODAL, self.is_modal);
        flags.set(NvOverlayFlags::BACKDROP_SCRIM, self.has_backdrop_scrim);
        flags.set(NvOverlayFlags::LIGHT_DISMISS, self.light_dismiss);
        flags.set(NvOverlayFlags::CLOSE_ON_ESCAPE, self.close_on_escape);
        NvOverlayItem {
            node_id: self.node,
            x: self.origin_x,
            y: self.origin_y,
            width: self.width,
            height: self.height,
            flags,
            layer: self.layer.into(),
            _pad: 0,
            backdrop_color: self.backdrop_color.to_u32(),
            anchor_node_id: self.anchor,
        }
    }
}

/// Paint index of the topmost (last painted) modal of `overlays`: the `topmost_modal` scene fact.
/// Content outside it and the overlays painted above it is inert.
#[inline]
pub fn topmost_modal_index(overlays: &[ResolvedOverlay]) -> Option<usize> {
    overlays.iter().rposition(|ro| ro.is_modal)
}

/// The overlay an unconsumed close request closes: the `close_target` scene fact
/// ([INV:close-request-single-path]). Front to back, the first overlay that is modal or
/// `close_on_escape` owns the request; it closes only when it is `close_on_escape`, so a
/// non-escapable modal swallows the request (HTML close-watcher semantics).
#[inline]
pub fn close_request_owner(overlays: &[ResolvedOverlay]) -> Option<NodeId> {
    let owner = overlays.iter().rev().find(|ro| ro.is_modal || ro.close_on_escape)?;
    owner.close_on_escape.then_some(owner.node)
}