From 1d93a470888a22d8334dca8572671ac2c65eb726 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 22 Nov 2026 19:58:51 -0500 Subject: [PATCH] dialog::set_close_button: an app takes the close x off every dialog's title row (trial) --- crates/ui-panels/src/dialog/frame.rs | 16 +++++++++++++++- crates/ui-panels/src/dialog/mod.rs | 2 +- crates/ui-panels/src/dialog/modal.rs | 2 +- crates/ui-panels/tests/it/dialogs.rs | 18 ++++++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/ui-panels/src/dialog/frame.rs b/crates/ui-panels/src/dialog/frame.rs index 4bfb1d8d2..52689baf1 100644 --- a/crates/ui-panels/src/dialog/frame.rs +++ b/crates/ui-panels/src/dialog/frame.rs @@ -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::(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<'_>) { diff --git a/crates/ui-panels/src/dialog/mod.rs b/crates/ui-panels/src/dialog/mod.rs index 5e7cbde5a..5e7d46722 100644 --- a/crates/ui-panels/src/dialog/mod.rs +++ b/crates/ui-panels/src/dialog/mod.rs @@ -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; diff --git a/crates/ui-panels/src/dialog/modal.rs b/crates/ui-panels/src/dialog/modal.rs index e736592ea..1e3b1c26d 100644 --- a/crates/ui-panels/src/dialog/modal.rs +++ b/crates/ui-panels/src/dialog/modal.rs @@ -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); } diff --git a/crates/ui-panels/tests/it/dialogs.rs b/crates/ui-panels/tests/it/dialogs.rs index 8f743c177..89a0256df 100644 --- a/crates/ui-panels/tests/it/dialogs.rs +++ b/crates/ui-panels/tests/it/dialogs.rs @@ -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::>()); 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"); +}