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};
pub const CLOSE_KEY_PROBES: &[&str] = &[
"Escape", "GoBack", "Esc", "escape", "ESCAPE", "Back", "BrowserBack", "Enter", " Escape", "Escape ", "",
];
#[derive(Debug, Clone)]
pub struct PressCase {
pub name: &'static str,
pub tree: &'static [(NodeId, NodeId)],
pub overlays: Vec<ResolvedOverlay>,
pub topmost_modal: NodeId,
pub x: f32,
pub y: f32,
pub hit: NodeId,
}
#[derive(Debug, Clone, PartialEq)]
pub struct PressVerdict {
pub press: TopLayerPress,
pub until: NodeId,
pub dismissed: Vec<NodeId>,
}
impl PressCase {
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
}
pub fn published_topmost_modal(&self) -> NodeId {
topmost_modal_index(&self.overlays).map_or(NodeId::NULL, |i| self.overlays[i].node)
}
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;
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,
}
}
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)
}
pub fn press_cases() -> Vec<PressCase> {
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;