use nativeview_abi::{ColorRgba, NvOverlayFlags, NvOverlayItem};
use nativeview_store::NodeId;
use nativeview_tables::tables::{OverlayLayer, OverlayPlacement};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ResolvedOverlay {
pub node: NodeId,
pub origin_x: f32,
pub origin_y: f32,
pub width: f32,
pub height: f32,
pub placement: OverlayPlacement,
pub layer: OverlayLayer,
pub is_modal: bool,
pub has_backdrop_scrim: bool,
pub light_dismiss: bool,
pub close_on_escape: bool,
pub backdrop_color: ColorRgba,
pub anchor: NodeId,
}
impl ResolvedOverlay {
#[inline]
pub const fn backdrop(&self) -> Option<ColorRgba> {
if self.has_backdrop_scrim {
Some(self.backdrop_color)
} else {
None
}
}
#[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
}
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,
}
}
}
#[inline]
pub fn topmost_modal_index(overlays: &[ResolvedOverlay]) -> Option<usize> {
overlays.iter().rposition(|ro| ro.is_modal)
}
#[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)
}