Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modals: Add UiKind::Modal, and consume escape-key properly #5414

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions crates/egui/src/containers/modal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
Area, Color32, Context, Frame, Id, InnerResponse, Order, Response, Sense, Ui, UiBuilder,
Area, Color32, Context, Frame, Id, InnerResponse, Order, Response, Sense, Ui, UiBuilder, UiKind,
};
use emath::{Align2, Vec2};

Expand Down Expand Up @@ -32,6 +32,7 @@ impl Modal {
/// - order: foreground
pub fn default_area(id: Id) -> Area {
Area::new(id)
.kind(UiKind::Modal)
.sense(Sense::hover())
.anchor(Align2::CENTER_CENTER, Vec2::ZERO)
.order(Order::Foreground)
Expand Down Expand Up @@ -153,8 +154,12 @@ impl<T> ModalResponse<T> {
/// - this is the topmost modal, no popup is open and the escape key was pressed
pub fn should_close(&self) -> bool {
let ctx = &self.response.ctx;
let escape_clicked = ctx.input(|i| i.key_pressed(crate::Key::Escape));

// this is a closure so that `Esc` is consumed only if the modal is topmost
let escape_clicked =
|| ctx.input_mut(|i| i.consume_key(crate::Modifiers::NONE, crate::Key::Escape));

self.backdrop_response.clicked()
|| (self.is_top_modal && !self.any_popup_open && escape_clicked)
|| (self.is_top_modal && !self.any_popup_open && escape_clicked())
}
}
10 changes: 10 additions & 0 deletions crates/egui/src/ui_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub enum UiKind {
/// A bottom [`crate::TopBottomPanel`].
BottomPanel,

/// A modal [`crate::Modal`].
Modal,

/// A [`crate::Frame`].
Frame,

Expand Down Expand Up @@ -82,6 +85,7 @@ impl UiKind {

Self::Window
| Self::Menu
| Self::Modal
| Self::Popup
| Self::Tooltip
| Self::Picker
Expand Down Expand Up @@ -228,6 +232,12 @@ impl UiStack {
self.kind().map_or(false, |kind| kind.is_panel())
}

/// Is this [`crate::Ui`] an [`crate::Area`]?
#[inline]
pub fn is_area_ui(&self) -> bool {
self.kind().map_or(false, |kind| kind.is_area())
}

/// Is this a root [`crate::Ui`], i.e. created with [`crate::Ui::new()`]?
#[inline]
pub fn is_root_ui(&self) -> bool {
Expand Down
Loading