dialog::set_close_button: an app takes the close x off every dialog's title row (trial)

This commit is contained in:
Dylan M. Taylor
2026-11-22 19:58:51 -05:00
parent fa918b1090
commit 1d93a47088
4 changed files with 35 additions and 3 deletions
+15 -1
View File
@@ -233,6 +233,20 @@ pub(crate) fn footer_in(
}
}
/// Whether dialogs show the close x in their title row (true until an app turns it off).
pub fn close_button(ctx: &egui::Context) -> bool {
ctx.data(|data| data.get_temp::<CloseButton>(egui::Id::NULL)).is_none_or(|held| held.0)
}
/// Turns the close x of every dialog's title row on or off, as an app's look decides; Escape and the footer buttons
/// still close a dialog. Call it every pass or once at start.
pub fn set_close_button(ctx: &egui::Context, on: bool) {
ctx.data_mut(|data| data.insert_temp(egui::Id::NULL, CloseButton(on)));
}
#[derive(Clone, Copy)]
struct CloseButton(bool);
/// The header actions of a shell: the close action when it has one.
#[doc(hidden)]
pub fn close_action(closable: bool) -> &'static [HeaderAction] {
@@ -328,7 +342,7 @@ impl Component for DialogFrame<'_> {
impl Container for DialogFrame<'_> {
fn header(&mut self, ui: &mut Ui, cx: &mut BodyCx<'_>) {
header(ui, cx, close_action(self.closable), None, None);
header(ui, cx, close_action(self.closable && close_button(ui.ctx())), None, None);
}
fn footer(&mut self, ui: &mut Ui, cx: &mut BodyCx<'_>) {
+1 -1
View File
@@ -24,7 +24,7 @@ mod words;
pub use alert::{Alert, AlertOutput, HostedAlert};
pub use blocking::{BlockingOverlay, BlockingOverlayStyle};
pub use frame::{DialogFrame, DialogFrameStyle, DialogOutput, FooterCx, FooterHooks};
pub use frame::{DialogFrame, DialogFrameStyle, DialogOutput, FooterCx, FooterHooks, close_button, set_close_button};
pub use held::{HeldAnswer, HeldOk};
#[doc(hidden)]
pub use host::dialog_probe;
+1 -1
View File
@@ -493,7 +493,7 @@ impl Container for Dialog<'_> {
if self.holds_outer_gesture() {
outer::keep(ui.ctx(), cx);
}
frame::header(ui, cx, frame::close_action(self.closable), self.title_note.as_ref(), self.title_icon.as_ref());
frame::header(ui, cx, frame::close_action(self.closable && frame::close_button(ui.ctx())), self.title_note.as_ref(), self.title_icon.as_ref());
self.keep_window_draggable(ui);
}
+18
View File
@@ -474,3 +474,21 @@ fn a_footer_cancel_is_a_ghost_button_beside_the_accent_answer() {
assert!(seen.iter().any(|style| style.variant == Variant::GHOST), "Cancel is a ghost button: {:?}", seen.iter().map(|s| s.variant).collect::<Vec<_>>());
assert!(!seen.iter().any(|style| style.variant == Variant::SECONDARY), "no outlined button in the footer");
}
/// A dialog's title row carries a close x; an app whose dialogs end with their buttons (production's) turns it off
/// once with `dialog::set_close_button`, and no dialog of the app shows it.
#[trial]
fn an_app_can_take_the_close_x_off_every_dialog() {
let off = Rc::new(Cell::new(false));
let flag = Rc::clone(&off);
let mut fixture = Fixture::new(move |ui| {
craft_ui::dialog::set_close_button(ui.ctx(), !flag.get());
let _ = Dialog::new("Plain").buttons([FooterButton::new("OK", DialogRole::Accept)]).show(ui, |_, _| {});
});
fixture.idle(4);
assert!(fixture.by_label("Close").is_some(), "the close x is there by default");
off.set(true);
fixture.idle(4);
assert!(fixture.by_label("Close").is_none(), "the close x is gone once the app turned it off");
assert!(fixture.by_label("OK").is_some(), "the dialog still has its buttons");
}