use nativeview_store::NodeId;
use super::ResolvedOverlay;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PressOutcome {
None,
Inside,
LightDismiss,
BlockedByModal,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TopLayerPress {
pub outcome: PressOutcome,
pub dismiss_from: usize,
pub modal_barrier: bool,
}
impl TopLayerPress {
pub fn light_dismissed<'a>(&self, overlays: &'a [ResolvedOverlay]) -> impl Iterator<Item = NodeId> + 'a {
let from = if self.outcome == PressOutcome::LightDismiss {
self.dismiss_from
} else {
overlays.len()
};
overlays
.get(from..)
.unwrap_or_default()
.iter()
.rev()
.filter(|ro| ro.light_dismiss)
.map(|ro| ro.node)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PressedOverlay {
pub inside: bool,
pub on_anchor: bool,
pub light_dismiss: bool,
pub modal: bool,
}
pub fn hide_popovers_until(
overlays: impl DoubleEndedIterator<Item = PressedOverlay> + ExactSizeIterator,
) -> TopLayerPress {
let len = overlays.len();
let mut dismiss_from = None;
let mut modal_barrier = false;
let mut inside = false;
for (i, ov) in overlays.enumerate().rev() {
if !(ov.light_dismiss || ov.modal) {
inside |= ov.inside;
continue;
}
if ov.inside {
inside = true;
break;
}
if ov.modal {
modal_barrier = !inside;
if ov.light_dismiss {
dismiss_from = Some(i);
}
break;
}
if ov.on_anchor {
inside = true;
break;
}
dismiss_from = Some(i);
}
let outcome = match (dismiss_from, modal_barrier, inside) {
(Some(_), _, _) => PressOutcome::LightDismiss,
(None, true, _) => PressOutcome::BlockedByModal,
(None, false, true) => PressOutcome::Inside,
(None, false, false) => PressOutcome::None,
};
TopLayerPress {
outcome,
dismiss_from: dismiss_from.unwrap_or(len),
modal_barrier,
}
}
pub fn top_layer_press(
overlays: &[ResolvedOverlay],
topmost_modal: Option<NodeId>,
hit: Option<NodeId>,
is_within: impl Fn(NodeId, NodeId) -> bool,
contains: impl Fn(&ResolvedOverlay) -> bool,
) -> TopLayerPress {
let modal = topmost_modal.and_then(|m| overlays.iter().rposition(|ro| ro.node == m));
let within = |node: NodeId| !node.is_null() && hit.is_some_and(|h| is_within(h, node));
let hit_active = || modal.is_none_or(|m| overlays[m..].iter().any(|ro| within(ro.node)));
hide_popovers_until(overlays.iter().enumerate().map(|(i, ro)| {
let is_modal = modal == Some(i);
PressedOverlay {
inside: within(ro.node) || contains(ro),
on_anchor: ro.light_dismiss && !is_modal && within(ro.anchor) && hit_active(),
light_dismiss: ro.light_dismiss,
modal: is_modal,
}
}))
}
#[cfg(test)]
mod tests;