diff --git a/crates/ui-panels/src/dialog/host.rs b/crates/ui-panels/src/dialog/host.rs index 5c1208e163..f24d494fc2 100644 --- a/crates/ui-panels/src/dialog/host.rs +++ b/crates/ui-panels/src/dialog/host.rs @@ -129,6 +129,13 @@ pub trait StackDialog: Any + Send { /// dialog's own Cancel button does this itself before it returns. Does nothing by default. fn cancelled(&mut self, _ctx: &Context) {} + /// The requests the dialog raised for its app since the last call (a list edit that takes effect at once, say), + /// each taken once. The host keeps what is undelivered when the dialog closes, however it closes + /// ([`DialogHost::take_actions`]). Empty by default. + fn take_actions(&mut self) -> Vec { + Vec::new() + } + /// Shows `color` live in the dialog's colour row `key` while a child picker edits it (`None` ends the preview). /// Hosted dialogs with colour rows override it; does nothing by default. fn preview_color(&mut self, key: &str, color: Option) { @@ -269,6 +276,8 @@ trait AnyStackDialog: Send { fn cancelled(&self) -> Box; /// [`StackDialog::cancelled`]. fn release(&mut self, ctx: &Context); + /// [`StackDialog::take_actions`]. + fn take_actions(&mut self) -> Vec; fn preview_color(&mut self, key: &str, color: Option); fn take_color_request(&mut self) -> Option; fn answer_color(&mut self, key: &str, color: craft_theme::Srgba); @@ -332,6 +341,10 @@ impl AnyStackDialog for D { StackDialog::cancelled(self, ctx); } + fn take_actions(&mut self) -> Vec { + StackDialog::take_actions(self) + } + fn cancelled(&self) -> Box { Box::new(DialogResult::::Cancel) } @@ -422,6 +435,8 @@ pub struct DialogHost { /// Dialogs [`close_by_id`](DialogHost::close_by_id) asked to close, closed by the next `run` /// (which has the `Context` a close needs). closing: Vec, + /// Actions of dialogs that closed before the app took them, by dialog id, in the order raised. + kept_actions: Vec<(DialogId, Vec)>, } /// One open dialog as automation sees it ([`DialogHost::open_dialogs`]). @@ -541,9 +556,9 @@ impl DialogHost { continue; }; self.remove_tree(id); - let dialog = self.open.remove(at); + let mut dialog = self.open.remove(at); crate::runtime::overlay::end_stack_dialog(&ctx, id); - self.store_result(dialog.as_ref(), result); + self.store_result(&mut *dialog, result); self.cancel_orphans(Some(&ctx)); } if self.open_next_queued() { @@ -632,9 +647,9 @@ impl DialogHost { let Some(result) = self.open[at].confirm_erased(ctx) else { return false }; let id = self.open[at].id(); self.remove_tree(id); - let dialog = self.open.remove(at); + let mut dialog = self.open.remove(at); crate::runtime::overlay::end_stack_dialog(ctx, id); - self.store_result(dialog.as_ref(), result); + self.store_result(&mut *dialog, result); self.cancel_orphans(Some(ctx)); true } @@ -680,7 +695,8 @@ impl DialogHost { let mut dialog = self.open.remove(at); crate::runtime::overlay::end_stack_dialog(ctx, id); dialog.release(ctx); - self.store_result(dialog.as_ref(), dialog.cancelled()); + let result = dialog.cancelled(); + self.store_result(&mut *dialog, result); self.cancel_orphans(Some(ctx)); true } @@ -793,7 +809,33 @@ impl DialogHost { self.open.iter().position(|dialog| dialog.id() == id) } - fn store_result(&mut self, dialog: &dyn AnyStackDialog, result: Box) { + /// The actions the app has not taken yet, of the open dialog `id` names and of earlier dialogs of that id that + /// closed, oldest first, each once. A dialog's actions survive its close (finish, confirm, cancel, a replacing open + /// or its parent closing), so the app drains them before it settles the dialog's result. + pub fn take_actions(&mut self, id: DialogId) -> Vec { + let mut actions = Vec::new(); + self.kept_actions.retain_mut(|(kept, list)| { + if *kept == id { + actions.append(list); + } + *kept != id + }); + if let Some(dialog) = self.open.iter_mut().find(|dialog| dialog.id() == id) { + actions.extend(dialog.take_actions()); + } + actions + } + + /// Moves what a dialog leaving the host still holds for the app into `kept_actions`. + fn keep_actions(&mut self, dialog: &mut dyn AnyStackDialog) { + let actions = dialog.take_actions(); + if !actions.is_empty() { + self.kept_actions.push((dialog.id(), actions)); + } + } + + fn store_result(&mut self, dialog: &mut dyn AnyStackDialog, result: Box) { + self.keep_actions(dialog); let (type_id, instance) = (dialog.result_type(), dialog.instance()); self.results.retain(|stored| stored.type_id != type_id || stored.instance != instance); self.results.push(StoredResult { type_id, id: dialog.id(), instance, key: dialog.key(), result }); @@ -812,8 +854,9 @@ impl DialogHost { self.results.retain(|stored| stored.type_id != type_id || stored.instance != instance); if let Some(at) = self.open.iter().position(|open| open.result_type() == type_id && open.instance() == instance) { self.remove_tree(self.open[at].id()); - let old = self.open.remove(at); - self.store_result(old.as_ref(), old.cancelled()); + let mut old = self.open.remove(at); + let result = old.cancelled(); + self.store_result(&mut *old, result); self.replaced.push(old); self.cancel_orphans(None); } @@ -833,11 +876,13 @@ impl DialogHost { let (kept, orphans): (Vec<_>, Vec<_>) = std::mem::take(&mut self.open).into_iter().partition(|dialog| self.stack.contains(dialog.id())); self.open = kept; for mut dialog in orphans { - self.store_result(dialog.as_ref(), dialog.cancelled()); + let result = dialog.cancelled(); + self.store_result(&mut *dialog, result); match ctx { Some(ctx) => { crate::runtime::overlay::end_stack_dialog(ctx, dialog.id()); dialog.release(ctx); + self.keep_actions(&mut *dialog); } None => self.replaced.push(dialog), } @@ -849,6 +894,7 @@ impl DialogHost { for mut dialog in std::mem::take(&mut self.replaced) { crate::runtime::overlay::end_stack_dialog(ctx, dialog.id()); dialog.release(ctx); + self.keep_actions(&mut *dialog); } } diff --git a/crates/ui-panels/tests/it/dialogs.rs b/crates/ui-panels/tests/it/dialogs.rs index 453dc081e4..c9196d966a 100644 --- a/crates/ui-panels/tests/it/dialogs.rs +++ b/crates/ui-panels/tests/it/dialogs.rs @@ -105,6 +105,55 @@ fn a_parent_with_an_open_child_refuses_confirm() { assert_eq!(host.take::>(), Some(DialogResult::Ok(1))); } +/// A dialog holding requests for its app, which never finishes by itself. +struct Emitter { + actions: Vec, +} + +impl StackDialog for Emitter { + type Result = (); + + fn id(&self) -> DialogId { + dialog_id("emitter") + } + + fn show(&mut self, _: &mut Ui, _: &DialogStack) -> Option> { + None + } + + fn confirm(&mut self, _: &egui::Context) -> Option<()> { + Some(()) + } + + fn take_actions(&mut self) -> Vec { + std::mem::take(&mut self.actions) + } +} + +/// A request a dialog raised and the app has not taken reaches the app once, whether the dialog confirmed, was +/// cancelled or was replaced by another of its type. +#[trial] +fn a_dialogs_undrained_actions_survive_every_way_it_closes() { + let mut frames = Frames::new(); + let mut host = DialogHost::new(); + let id = dialog_id("emitter"); + for way in 0..3 { + let raised = vec![json!({ "add": way })]; + host.open(Emitter { actions: raised.clone() }); + run(&mut host, &mut frames); + match way { + 0 => assert!(host.confirm::(&frames.ctx)), + 1 => assert!(host.cancel::(&frames.ctx)), + _ => { + host.open(Emitter { actions: Vec::new() }); + assert!(host.cancel::(&frames.ctx)); + } + } + assert_eq!(host.take_actions(id), raised, "way {way}"); + assert!(host.take_actions(id).is_empty(), "each action is delivered once"); + } +} + /// A dialog that asks for a `Probe<3>` child in the show after `ask` is set, and finishes with `finish` once set. struct Asker { ask: bool, diff --git a/crates/ui-patterns/src/patterns/preferences/model.rs b/crates/ui-patterns/src/patterns/preferences/model.rs index a32e3dd58a..098a8ac302 100644 --- a/crates/ui-patterns/src/patterns/preferences/model.rs +++ b/crates/ui-patterns/src/patterns/preferences/model.rs @@ -43,26 +43,27 @@ pub struct PageSpec<'a> { pub keywords: &'a str, /// The host draws the whole page ([`PreferencesModel::custom_page`]). pub custom: bool, + /// Whether the page offers Reset Section. + pub resettable: bool, } impl<'a> PageSpec<'a> { /// Whether the page offers Reset Section (default true). - // P3(dialogs-w17): owed by dialogs-w17 (q222). Until it lands every page stays resettable, the behaviour before - // the option existed. - pub fn resettable(self, resettable: bool) -> Self { - let _ = resettable; + #[must_use] + pub fn resettable(mut self, resettable: bool) -> Self { + self.resettable = resettable; self } /// A plain page of one untitled group. pub fn new(id: &'a str, title: &'a str, rows: Vec>) -> Self { - Self { id, title, groups: vec![GroupSpec::new(None, rows)], footnote: None, keywords: "", custom: false } + Self { id, title, groups: vec![GroupSpec::new(None, rows)], footnote: None, keywords: "", custom: false, resettable: true } } /// A page the host draws ([`PreferencesModel::custom_page`]). Its [`group`](PageSpec::group)s follow the host's /// part, drawn and searched as a plain page's groups are. pub fn custom(id: &'a str, title: &'a str) -> Self { - Self { id, title, groups: Vec::new(), footnote: None, keywords: "", custom: true } + Self { id, title, groups: Vec::new(), footnote: None, keywords: "", custom: true, resettable: true } } /// Adds `group` under a custom page's own part, where the search sees its rows. diff --git a/crates/ui-patterns/src/patterns/preferences/page.rs b/crates/ui-patterns/src/patterns/preferences/page.rs index f3371fc6db..bb73732ae2 100644 --- a/crates/ui-patterns/src/patterns/preferences/page.rs +++ b/crates/ui-patterns/src/patterns/preferences/page.rs @@ -184,6 +184,7 @@ struct ShownPage { id: String, title: Label, custom: bool, + resettable: bool, footnote: Option