From a6ff11d8b240d9ee5676cb3ef3ac7845fca284d9 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sun, 15 Nov 2026 17:01:32 +0000 Subject: [PATCH] tfix3-f3: DialogHost::open_serial: each open of a dialog has its own number while it stays open --- crates/ui-panels/src/dialog/host.rs | 16 ++++++++++++++++ crates/ui-panels/tests/it/dialogs.rs | 17 +++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/crates/ui-panels/src/dialog/host.rs b/crates/ui-panels/src/dialog/host.rs index b55313495..37b177993 100644 --- a/crates/ui-panels/src/dialog/host.rs +++ b/crates/ui-panels/src/dialog/host.rs @@ -299,6 +299,10 @@ pub struct DialogHost { /// Dialogs another of their type replaced, waiting for the pass's `run` to run their /// [`StackDialog::cancelled`] (`open` has no `Context`). replaced: Vec>, + /// The number the last open got ([`open_serial`](DialogHost::open_serial)). + opens: u64, + /// The number of each dialog's last open, by id; read only while the dialog is open. + serials: Vec<(DialogId, u64)>, } /// One open dialog as automation sees it ([`DialogHost::open_dialogs`]). @@ -584,6 +588,14 @@ impl DialogHost { self.results.push(StoredResult { type_id, id: dialog.id(), instance, key: dialog.key(), result }); } + /// Which open of the dialog `id` this is: a number distinct for every open on this host, the same while the + /// dialog stays open. A dialog's id is the same at every open of its key; this tells one open from the next (a + /// preview cached for a cancelled open is not the reopened dialog's). `None` when `id` is not open. + pub fn open_serial(&self, id: DialogId) -> Option { + self.position_by_id(id)?; + self.serials.iter().find(|(open, _)| *open == id).map(|(_, serial)| *serial) + } + fn open_with_parent(&mut self, dialog: Box, parent: Option) -> DialogId { let (type_id, instance) = (dialog.result_type(), dialog.instance()); self.results.retain(|stored| stored.type_id != type_id || stored.instance != instance); @@ -595,6 +607,10 @@ impl DialogHost { self.cancel_orphans(None); } let id = dialog.id(); + self.opens += 1; + let open = &self.open; + self.serials.retain(|(serial_of, _)| *serial_of != id && open.iter().any(|dialog| dialog.id() == *serial_of)); + self.serials.push((id, self.opens)); self.stack.push(id, parent); self.open.push(dialog); id diff --git a/crates/ui-panels/tests/it/dialogs.rs b/crates/ui-panels/tests/it/dialogs.rs index cab8da726..ad32a5fba 100644 --- a/crates/ui-panels/tests/it/dialogs.rs +++ b/crates/ui-panels/tests/it/dialogs.rs @@ -269,3 +269,20 @@ fn cmd_d_answers_dont_save_on_macos() { } } } + +/// Each open of a dialog has its own serial, kept while it stays open, though its id is the same at every open: a +/// reopened dialog is told apart from the one cancelled before it. +#[trial] +fn each_open_of_a_dialog_has_its_own_serial() { + let mut frames = Frames::new(); + let mut host = DialogHost::new(); + let id = host.open(Probe::<1>::new("picker")); + let first = host.open_serial(id).expect("an open dialog has a serial"); + run(&mut host, &mut frames); + assert_eq!(host.open_serial(id), Some(first), "the serial stays while the dialog is open"); + assert!(host.cancel_by_id(&frames.ctx, id)); + assert_eq!(host.open_serial(id), None, "a closed dialog has none"); + let again = host.open(Probe::<1>::new("picker")); + assert_eq!(again, id, "the reopened dialog has the same id"); + assert_ne!(host.open_serial(id), Some(first), "the reopened dialog has a serial of its own"); +}