use super::{Dismissal, PopupState, Popups, MAX_POPUP_DEPTH};
use crate::{
context::{Context, Id},
Rect, Vec2,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Registered {
Opening,
Kept,
Rejected,
}
pub(crate) struct Opening {
pub id: Id,
pub window: Id,
pub anchor: Rect,
pub rect: Rect,
pub return_focus: Option<Id>,
pub key_target: Option<Id>,
}
impl Popups {
pub(crate) fn top(&self) -> Option<&PopupState> {
self.branch.last()
}
pub(crate) fn is_active(&self) -> bool {
!self.branch.is_empty()
}
pub(crate) fn index_of(&self, id: Id) -> Option<usize> {
self.branch.iter().position(|popup| popup.id == id)
}
pub(crate) fn index_of_layer(&self, layer: Id) -> Option<usize> {
self.branch
.iter()
.position(|p| p.id == layer || p.extra.iter().any(|(extra, _)| *extra == layer))
}
pub(crate) fn is_top_layer(&self, layer: Id) -> bool {
self.top()
.is_some_and(|p| p.id == layer || p.extra.iter().any(|(extra, _)| *extra == layer))
}
pub(crate) fn level_at(&self, point: Vec2) -> Option<usize> {
self.branch.iter().rposition(|popup| {
popup.rect.contains(point)
|| popup.extra.iter().any(|(_, rect)| rect.contains(point))
|| (popup.key_target.is_some() && popup.anchor.contains(point))
})
}
}
impl Context {
pub fn close_popup(&mut self) {
if let Some(last) = self.popups.branch.len().checked_sub(1) {
self.dismiss_popups_from(last, true);
}
}
pub fn close_popup_branch(&mut self) {
self.dismiss_popups_from(0, true);
}
pub(crate) fn dismiss_popup(&mut self, restore_focus: bool) {
self.dismiss_popups_from(0, restore_focus);
}
pub(crate) fn close_popup_of_builder(&mut self, id: Id) {
let Some(index) = self.popups.index_of(id) else {
return;
};
for closed in self.dismiss_popups_from(index, true) {
self.popups.dismissed.remove(&closed);
}
}
pub(crate) fn dismiss_popups_from(&mut self, index: usize, restore_focus: bool) -> Vec<Id> {
if index >= self.popups.branch.len() {
return Vec::new();
}
let closed: Vec<PopupState> = self.popups.branch.drain(index..).collect();
self.keys.reset();
for popup in closed.iter().rev() {
if let Some(target) = popup.key_target {
self.take_menu_keys(target);
}
let dismissal = Dismissal {
parent: popup.parent,
frame: self.frame,
};
self.popups.dismissed.insert(popup.id, dismissal);
self.remove_popup_hits(popup.id);
for (layer, _) in &popup.extra {
self.remove_popup_hits(*layer);
}
}
if restore_focus {
let target = self.restore_target(&closed[0]);
self.set_focus(target);
}
self.interaction.keyboard_active = None;
self.stop_auto_scroll();
self.request_repaint();
closed.iter().map(|popup| popup.id).collect()
}
fn restore_target(&self, closed: &PopupState) -> Option<Id> {
let parent = closed
.parent
.and_then(|id| self.popups.index_of(id))
.map(|i| &self.popups.branch[i]);
[closed.return_focus, parent.and_then(|p| p.key_target)]
.into_iter()
.flatten()
.find(|id| self.can_take_focus(*id))
.or(closed.return_focus)
}
fn can_take_focus(&self, id: Id) -> bool {
self.interaction.previous_hits.iter().any(|hit| {
hit.id == id && hit.action.focusable() && !hit.rect.intersect(hit.clip).is_empty()
})
}
pub(crate) fn popup_open(&self, id: Id) -> bool {
self.popups.index_of(id).is_some()
}
pub(crate) fn register_popup(&mut self, opening: Opening) -> Registered {
let parent = self.popups.index_of_layer(opening.window);
let existing = self.popups.index_of(opening.id);
if let (Some(parent), Some(existing)) = (parent, existing) {
if parent >= existing {
self.report_nesting(&opening, "popup built inside itself or a popup it opened");
return Registered::Rejected;
}
}
let expected = parent.map(|i| self.popups.branch[i].id);
let existing = match existing {
Some(i) if self.popups.branch[i].parent != expected => {
self.dismiss_popups_from(i, true);
None
}
other => other,
};
let frame = self.frame;
if let Some(i) = existing {
let popup = &mut self.popups.branch[i];
popup.anchor = opening.anchor;
popup.rect = opening.rect;
popup.key_target = opening.key_target;
popup.last_frame = frame;
return Registered::Kept;
}
let depth = parent.map_or(0, |i| i + 1);
if depth >= MAX_POPUP_DEPTH {
self.report_nesting(&opening, "popups nested too deep; the popup is not opened");
return Registered::Rejected;
}
let owner = parent.map_or(opening.window, |i| self.popups.branch[i].owner);
self.dismiss_popups_from(depth, true);
self.popups.branch.push(PopupState {
id: opening.id,
parent: expected,
owner,
anchor: opening.anchor,
rect: opening.rect,
return_focus: opening.return_focus,
key_target: opening.key_target,
last_frame: frame,
extra: Vec::new(),
});
Registered::Opening
}
fn report_nesting(&mut self, opening: &Opening, message: &'static str) {
self.report(
crate::DiagnosticKind::InvalidUsage,
Some(opening.id),
Some(opening.anchor),
|| message.into(),
);
}
pub(crate) fn retire_popups(&mut self) {
let (frame, windows) = (self.frame, &self.windows);
let built = self
.popups
.branch
.iter()
.take_while(|popup| {
popup.last_frame == frame
&& windows
.get(&popup.owner)
.is_some_and(|w| w.last_frame == frame)
})
.count();
self.dismiss_popups_from(built, true);
}
}