nativeview-layout 0.1.0-rc.1

Shared NodeStore layout layer: StoreLayoutEngine / TextMeasurer traits and the default direct Taffy engine
Documentation
//! Conformance cases of the top-layer pointer-down and close-key policy
//! ([INV:top-layer-policy-conformance]). `nv codegen abi` evaluates every case with the reference
//! ([`top_layer_press`], [`nativeview_abi::is_close_request_key`]) and emits the inputs plus the computed
//! verdicts as `.generated.` vectors that the C++, Swift, Java, Dart and TS host suites run against
//! their ports. Only the inputs are written here; every expected outcome is computed.
//!
//! The `topmost_modal` scene fact is an *input* of every case (by default the one the runtime
//! publishes, [`topmost_modal_index`] over the flags). The `fact …` cases set it apart from the
//! overlay MODAL flags, so a port that derives the modal from the flags instead of reading the
//! fact fails them.
//!
//! The shared tree (generation-0 ids unless noted; overlays hang under their scene parent):
//!
//! ```text
//! 1 root
//! ├─ 2 content ─┬─ 3 anchor button ── 4 label
//! │             └─ 5@1 node reusing the slot of a removed anchor 5@0 (stale)
//! ├─ 10 menu ─┬─ 11 item (submenu anchor) ── 20 submenu ── 21 item (third-level anchor)
//! │           └─ 12 item                                └─ 22 item
//! ├─ 30 third-level menu
//! ├─ 40 dialog ── 41 body ── 42 button (popover anchor) ── 43 popover in the dialog
//! ├─ 50 manual toast ── 51 "Undo"
//! └─ 60 second dialog
//! ```

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

use super::{PressOutcome, ResolvedOverlay, TopLayerPress, top_layer_press, topmost_modal_index};

/// Keys probed against `nativeview_abi::is_close_request_key` (exact, case-sensitive W3C `key` values).
pub const CLOSE_KEY_PROBES: &[&str] = &[
    "Escape", "GoBack", "Esc", "escape", "ESCAPE", "Back", "BrowserBack", "Enter", " Escape", "Escape ", "",
];

/// One pointer-down case: the scene tree, the open overlays (back to front) and the press.
#[derive(Debug, Clone)]
pub struct PressCase {
    pub name: &'static str,
    /// `(node, parent)` pairs; `NodeId::NULL` parents a root.
    pub tree: &'static [(NodeId, NodeId)],
    pub overlays: Vec<ResolvedOverlay>,
    /// The `topmost_modal` scene fact (`NULL`: none), authoritative over the MODAL flags.
    pub topmost_modal: NodeId,
    /// Viewport press point.
    pub x: f32,
    pub y: f32,
    /// The host's hit node (`NodeId::NULL`: none).
    pub hit: NodeId,
}

/// The reference verdict of one [`PressCase`].
#[derive(Debug, Clone, PartialEq)]
pub struct PressVerdict {
    pub press: TopLayerPress,
    /// `LightDismiss`: the backmost overlay to close ("hide popovers until" it); else `NULL`.
    pub until: NodeId,
    /// Every overlay to close, front to back, duplicate-free.
    pub dismissed: Vec<NodeId>,
}

impl PressCase {
    /// Inclusive containment over [`Self::tree`] (bounded by its size; unknown nodes contain nothing).
    pub fn is_within(&self, node: NodeId, ancestor: NodeId) -> bool {
        let mut cur = node;
        for _ in 0..=self.tree.len() {
            if cur.is_null() {
                return false;
            }
            if cur == ancestor {
                return true;
            }
            match self.tree.iter().find(|&&(n, _)| n == cur) {
                Some(&(_, parent)) => cur = parent,
                None => return false,
            }
        }
        false
    }

    /// The fact the runtime would publish for [`Self::overlays`] (the MODAL flags' topmost).
    pub fn published_topmost_modal(&self) -> NodeId {
        topmost_modal_index(&self.overlays).map_or(NodeId::NULL, |i| self.overlays[i].node)
    }

    /// Runs the reference over this case.
    pub fn evaluate(&self) -> PressVerdict {
        let hit = (!self.hit.is_null()).then_some(self.hit);
        let press = top_layer_press(
            &self.overlays,
            (!self.topmost_modal.is_null()).then_some(self.topmost_modal),
            hit,
            |n, a| self.is_within(n, a),
            |ro| ro.box_contains(self.x, self.y),
        );
        let until = match press.outcome {
            PressOutcome::LightDismiss => self.overlays[press.dismiss_from].node,
            _ => NodeId::NULL,
        };
        PressVerdict {
            press,
            until,
            dismissed: press.light_dismissed(&self.overlays).collect(),
        }
    }
}

const fn n(index: u32) -> NodeId {
    NodeId::new(index, 0)
}

const NULL: NodeId = NodeId::NULL;
const STALE_ANCHOR: NodeId = NodeId::new(5, 0);
const REUSED_SLOT: NodeId = NodeId::new(5, 1);

const TREE: &[(NodeId, NodeId)] = &[
    (n(1), NULL),
    (n(2), n(1)),
    (n(3), n(2)),
    (n(4), n(3)),
    (REUSED_SLOT, n(2)),
    (n(10), n(1)),
    (n(11), n(10)),
    (n(12), n(10)),
    (n(20), n(11)),
    (n(21), n(20)),
    (n(22), n(20)),
    (n(30), n(1)),
    (n(40), n(1)),
    (n(41), n(40)),
    (n(42), n(41)),
    (n(43), n(42)),
    (n(50), n(1)),
    (n(51), n(50)),
    (n(60), n(1)),
];

const MODAL: u8 = 1;
const SCRIM: u8 = 1 << 1;
const LIGHT: u8 = 1 << 2;
const ESCAPE: u8 = 1 << 3;

/// Overlay `node` with box `(x, y, w, h)`, `flags` and `anchor`.
fn ov(node: u32, (x, y, width, height): (f32, f32, f32, f32), flags: u8, anchor: NodeId) -> ResolvedOverlay {
    let is_modal = flags & MODAL != 0;
    ResolvedOverlay {
        node: n(node),
        origin_x: x,
        origin_y: y,
        width,
        height,
        placement: OverlayPlacement::Bottom,
        layer: if is_modal { OverlayLayer::Modal } else { OverlayLayer::Popover },
        is_modal,
        has_backdrop_scrim: flags & SCRIM != 0,
        light_dismiss: flags & LIGHT != 0,
        close_on_escape: flags & ESCAPE != 0,
        backdrop_color: ColorRgba::BLACK,
        anchor,
    }
}

// Boxes: the anchor button sits at (0..40, 0..20); the overlays at y 100..200.
fn menu() -> ResolvedOverlay {
    ov(10, (0.0, 100.0, 100.0, 100.0), LIGHT | ESCAPE, n(3))
}
fn submenu() -> ResolvedOverlay {
    ov(20, (100.0, 100.0, 100.0, 100.0), LIGHT | ESCAPE, n(11))
}
fn third() -> ResolvedOverlay {
    ov(30, (200.0, 100.0, 100.0, 100.0), LIGHT | ESCAPE, n(21))
}
fn toast() -> ResolvedOverlay {
    ov(50, (300.0, 100.0, 100.0, 100.0), 0, NULL)
}
fn dialog(flags: u8) -> ResolvedOverlay {
    ov(40, (400.0, 100.0, 200.0, 200.0), MODAL | SCRIM | flags, NULL)
}
fn dialog_popover() -> ResolvedOverlay {
    ov(43, (450.0, 150.0, 50.0, 50.0), LIGHT | ESCAPE, n(42))
}
fn popover(anchor: NodeId) -> ResolvedOverlay {
    ov(10, (0.0, 100.0, 100.0, 100.0), LIGHT | ESCAPE, anchor)
}

/// Every conformance case (see the module docs for the tree).
pub fn press_cases() -> Vec<PressCase> {
    // `fact_case` sets the `topmost_modal` fact apart from the flags; `case` publishes the runtime's.
    let fact_case = |name, overlays: Vec<ResolvedOverlay>, topmost_modal, (x, y): (f32, f32), hit| PressCase {
        name,
        tree: TREE,
        overlays,
        topmost_modal,
        x,
        y,
        hit,
    };
    let case = |name, overlays: Vec<ResolvedOverlay>, at: (f32, f32), hit: NodeId| {
        let mut c = fact_case(name, overlays, NULL, at, hit);
        c.topmost_modal = c.published_topmost_modal();
        c
    };
    let second_modal = || ov(60, (700.0, 100.0, 100.0, 100.0), MODAL | SCRIM, NULL);
    let out = (900.0, 900.0);
    vec![
        fact_case("fact names no modal: a flagged modal is no barrier", vec![dialog(ESCAPE)], NULL, out, n(2)),
        fact_case(
            "fact names the lower of two flagged modals: a press in it is inside",
            vec![dialog(ESCAPE), second_modal()],
            n(40),
            (500.0, 250.0),
            n(41),
        ),
        fact_case("fact names an unflagged overlay: it is the barrier", vec![menu(), toast()], n(50), out, n(2)),
        fact_case(
            "fact names no open overlay: no modal",
            vec![menu(), dialog(ESCAPE)],
            n(60),
            (50.0, 150.0),
            n(12),
        ),
        case("empty stack", vec![], (10.0, 10.0), n(3)),
        case("empty stack, no hit", vec![], out, NULL),
        case("inside the popover box, no hit node", vec![popover(n(3))], (50.0, 150.0), NULL),
        case("on the far edge of the popover box (inclusive)", vec![popover(n(3))], (100.0, 200.0), NULL),
        case("hit within the popover beyond its box", vec![menu()], out, n(12)),
        case("outside the popover", vec![popover(n(3))], out, n(2)),
        case("on the popover anchor", vec![popover(n(3))], (10.0, 10.0), n(3)),
        case("on a child of the popover anchor", vec![popover(n(3))], (10.0, 10.0), n(4)),
        case("beside the anchor", vec![popover(n(3))], (10.0, 10.0), n(2)),
        case("unanchored popover: no anchor exclusion", vec![popover(NULL)], (10.0, 10.0), n(3)),
        case("stale anchor: the reused slot is no anchor", vec![popover(STALE_ANCHOR)], (10.0, 10.0), REUSED_SLOT),
        case("non-finite press without a hit dismisses", vec![popover(n(3))], (f32::NAN, 150.0), NULL),
        case(
            "empty overlay box never contains",
            vec![ov(10, (0.0, 100.0, 0.0, 0.0), LIGHT, NULL)],
            (0.0, 100.0),
            NULL,
        ),
        case("nested menus: outside both closes both", vec![menu(), submenu()], out, n(2)),
        case("nested menus: inside the menu closes the submenu", vec![menu(), submenu()], (50.0, 150.0), n(12)),
        case("nested menus: on the submenu anchor item", vec![menu(), submenu()], (50.0, 150.0), n(11)),
        case("nested menus: inside the submenu", vec![menu(), submenu()], (150.0, 150.0), n(22)),
        case("nested menus: on the menu anchor closes the submenu", vec![menu(), submenu()], (10.0, 10.0), n(3)),
        case("three levels: inside the first closes two", vec![menu(), submenu(), third()], (50.0, 150.0), n(12)),
        case("three levels: outside closes all", vec![menu(), submenu(), third()], out, NULL),
        case("manual toast between: skipped and stays", vec![menu(), toast(), submenu()], out, n(2)),
        case("press inside a manual toast above a menu closes the menu", vec![menu(), toast()], (350.0, 150.0), n(51)),
        case("press inside a manual toast alone", vec![toast()], (350.0, 150.0), n(51)),
        case("manual popover alone: outside", vec![toast()], out, n(2)),
        case(
            "close-on-escape without light dismiss: outside",
            vec![ov(10, (0.0, 100.0, 100.0, 100.0), ESCAPE, n(3))],
            out,
            n(2),
        ),
        case(
            "scrim without modal, light dismiss",
            vec![ov(10, (0.0, 100.0, 100.0, 100.0), SCRIM | LIGHT, NULL)],
            out,
            n(2),
        ),
        case("scrim without modal, manual", vec![ov(50, (300.0, 100.0, 100.0, 100.0), SCRIM, NULL)], out, n(2)),
        case("modal blocks", vec![dialog(ESCAPE)], out, NULL),
        case("light-dismiss modal: the backdrop closes it", vec![dialog(LIGHT | ESCAPE)], out, NULL),
        case("non-escapable modal", vec![dialog(0)], out, n(2)),
        case("inside the modal", vec![dialog(ESCAPE)], (500.0, 250.0), n(41)),
        case("popover in modal: the modal body closes it", vec![dialog(ESCAPE), dialog_popover()], (420.0, 120.0), n(41)),
        case("popover in modal: outside closes it, blocked", vec![dialog(ESCAPE), dialog_popover()], out, NULL),
        case(
            "popover in a light-dismiss modal: the backdrop closes both",
            vec![dialog(LIGHT | ESCAPE), dialog_popover()],
            out,
            NULL,
        ),
        case("popover in modal: on its active anchor", vec![dialog(ESCAPE), dialog_popover()], (460.0, 120.0), n(42)),
        case("popover in modal: inside it", vec![dialog(ESCAPE), dialog_popover()], (470.0, 170.0), n(43)),
        case(
            "anchor under the modal is inert: its popover closes, blocked",
            vec![dialog(ESCAPE), popover(n(3))],
            (10.0, 10.0),
            n(3),
        ),
        case("overlays below the modal are inert", vec![menu(), dialog(ESCAPE)], out, n(2)),
        case("inside an overlay below the modal: blocked", vec![menu(), dialog(ESCAPE)], (50.0, 150.0), n(12)),
        case("two modals: a press in the lower one is blocked", vec![dialog(ESCAPE), second_modal()], (500.0, 250.0), n(41)),
        case(
            "manual toast above a modal: a press on it is no barrier",
            vec![dialog(ESCAPE), toast()],
            (350.0, 150.0),
            n(51),
        ),
        case(
            "manual toast above a light-dismiss modal: a press on it closes the modal",
            vec![dialog(LIGHT | ESCAPE), toast()],
            (350.0, 150.0),
            n(51),
        ),
    ]
}

#[cfg(test)]
mod tests;