tfix3-f3: DialogHost::open_serial: each open of a dialog has its own number while it stays open

This commit is contained in:
aiongg
2026-11-15 17:01:32 +00:00
parent 55fd41e76c
commit a6ff11d8b2
2 changed files with 33 additions and 0 deletions
+16
View File
@@ -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<Box<dyn AnyStackDialog>>,
/// 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<u64> {
self.position_by_id(id)?;
self.serials.iter().find(|(open, _)| *open == id).map(|(_, serial)| *serial)
}
fn open_with_parent(&mut self, dialog: Box<dyn AnyStackDialog>, parent: Option<DialogId>) -> 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
+17
View File
@@ -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");
}