From e98b39a326e0e76d6ba6e2d53295d73d3cf5dd4c Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 13 Nov 2026 16:15:05 +0000 Subject: [PATCH] DialogHost::sync_beside and automation inspect_stack: app dialogs share the host's stack --- crates/automation/src/dialogs.rs | 26 +++++++++++++++++---- crates/automation/tests/it/dialogs.rs | 33 +++++++++++++++++++++++++-- crates/ui-panels/src/dialog/host.rs | 31 ++++++++++++++++++++++--- 3 files changed, 80 insertions(+), 10 deletions(-) diff --git a/crates/automation/src/dialogs.rs b/crates/automation/src/dialogs.rs index 3bc5baf3b..9957ca07d 100644 --- a/crates/automation/src/dialogs.rs +++ b/crates/automation/src/dialogs.rs @@ -1,7 +1,7 @@ //! `ui.dialog.set`, `ui.dialog.confirm` and `ui.dialog.cancel` on an app's [`DialogHost`], for every dialog type. //! //! The app's method table hands these methods to [`dialog_method`] and lists [`inspect`] under `ui.inspect`'s -//! `dialogs`. A dialog is named by the `id` `inspect` lists; its fields are its +//! `dialogs`, or [`inspect_stack`] when it shows dialogs of its own beside the host's. A dialog is named by the `id` `inspect` lists; its fields are its //! [`StackDialog::json_fields`](craft_ui::dialog::StackDialog::json_fields). Opening a dialog (`ui.dialog.open`) and //! running the command its answer is for stay the app's: an answer waits for the app's `DialogHost::take` as a //! click's would. @@ -15,10 +15,26 @@ use crate::protocol::Params; /// The open dialogs, the top one last: `{id, parent, key, fields}`, `null` for a missing parent, key or fields. Ids /// are 64-bit hashes, so they are listed as decimal strings, which every JSON reader keeps exact. pub fn inspect(host: &DialogHost) -> Value { - host.open_dialogs() - .into_iter() - .map(|dialog| { - json!({"id": dialog.id.value().to_string(), "parent": dialog.parent.map(|id| id.value().to_string()), "key": dialog.key, "fields": dialog.fields}) + inspect_stack(host, |_| None) +} + +/// The whole stack, the top dialog last: the host's dialogs as [`inspect`] lists them, and each dialog the app shows +/// beside them ([`DialogHost::sync_beside`]) as `beside` describes it (`None` leaves it out). +pub fn inspect_stack(host: &DialogHost, mut beside: impl FnMut(DialogId) -> Option) -> Value { + let mut hosted = host.open_dialogs(); + host.stack() + .ids() + .filter_map(|id| match hosted.iter().position(|dialog| dialog.id == id) { + Some(at) => { + let dialog = hosted.swap_remove(at); + Some(json!({ + "id": dialog.id.value().to_string(), + "parent": dialog.parent.map(|id| id.value().to_string()), + "key": dialog.key, + "fields": dialog.fields, + })) + } + None => beside(id), }) .collect() } diff --git a/crates/automation/tests/it/dialogs.rs b/crates/automation/tests/it/dialogs.rs index 5a78ef1b7..7ab90f0dd 100644 --- a/crates/automation/tests/it/dialogs.rs +++ b/crates/automation/tests/it/dialogs.rs @@ -1,8 +1,8 @@ //! `ui.dialog.*` on typed dialogs through one generic path. -use craft_automation::dialogs::{dialog_method, inspect, names_open_dialog}; +use craft_automation::dialogs::{dialog_method, inspect, inspect_stack, names_open_dialog}; use craft_ui::contract::{Bind, SaveKey}; -use craft_ui::dialog::{DialogHost, DialogResult, PromptChoice, PromptKind, StandardPrompt}; +use craft_ui::dialog::{DialogHost, DialogResult, PromptChoice, PromptKind, StandardPrompt, dialog_id}; use craft_ui::patterns::form_dialogs::{DialogFields, HostedParamDialog, Param, ParamDialog, ParamKind}; use craft_ui::testing::trial; use egui::Context; @@ -116,3 +116,32 @@ fn param_dialog_automation_follows_its_rows() { let Some(DialogResult::Ok(reopened)) = host.take::() else { panic!("the reopened dialog answers OK") }; assert_eq!((reopened.fields.number("radius"), reopened.fields.text("method")), (Some(7.0), Some("zoom"))); } + +/// Dialogs an app shows itself share the host's stack: listed in one opening order between the hosted ones, never +/// answered by the host's methods, and closing the hosted dialogs opened over them when they leave. +#[trial] +fn beside_dialogs_share_the_hosts_stack() { + let ctx = Context::default(); + let mut host = DialogHost::new(); + let (first, second) = (dialog_id(("app", 1)), dialog_id(("app", 2))); + let own = |id| Some(json!({"id": if id == first { "first" } else { "second" }})); + host.sync_beside(&ctx, [(first, None)]); + let prompt = host.open(StandardPrompt::new(PromptKind::TrashMask)); + host.sync_beside(&ctx, [(first, None), (second, None)]); + let listed = inspect_stack(&host, own); + assert_eq!((listed[0]["id"].clone(), listed[2]["id"].clone()), (json!("first"), json!("second"))); + assert_eq!(listed[1]["id"], prompt.value().to_string()); + assert!(host.stack().is_top(second)); + assert!(!names_open_dialog(&host, &json!({"dialog": first.value()}))); + assert!(call(&mut host, &ctx, "ui.dialog.cancel", json!({"dialog": second.value()})).is_err()); + + host.sync_beside(&ctx, [(first, None)]); + assert!(host.stack().is_top(prompt), "an app dialog that closed leaves the stack"); + assert!(host.cancel_by_id(&ctx, prompt)); + host.take::(); + let child = host.open_child(StandardPrompt::confirm_delete(["Layer 1"], 1), first); + assert!(host.stack().is_top(child)); + host.sync_beside(&ctx, []); + assert!(matches!(host.take::(), Some(DialogResult::Cancel)), "a child closes with its app dialog"); + assert!(host.stack().is_empty() && host.is_empty()); +} diff --git a/crates/ui-panels/src/dialog/host.rs b/crates/ui-panels/src/dialog/host.rs index de67396d8..fef475ec0 100644 --- a/crates/ui-panels/src/dialog/host.rs +++ b/crates/ui-panels/src/dialog/host.rs @@ -228,6 +228,8 @@ impl AnyStackDialog for D { /// one of them; the per-type calls reach the most recently opened `D`. /// [`open_unless_open`](DialogHost::open_unless_open) refuses while any `D` is open and leaves /// it alone (PhotoCraft's `confirmationPending`). +/// - Dialogs the app still shows itself share the stack: [`sync_beside`](DialogHost::sync_beside) +/// puts them in the one opening order, so the top dialog and the startup queue count them too. /// - Automation: [`get`](DialogHost::get) and [`get_mut`](DialogHost::get_mut) reach the open /// dialog's value for code that knows its type. A script that doesn't goes by id: /// [`open_dialogs`](DialogHost::open_dialogs) lists every open dialog with its @@ -280,13 +282,36 @@ impl DialogHost { Self::default() } - /// The routing stack, for a [`Dialog`](super::Dialog) the app shows itself beside the host's - /// (`Dialog::new(..).stack(dialogs.stack())`). Push its id with [`DialogStack::push`] and - /// remove it on the closing outcome, as before. + /// The routing stack: every open dialog in opening order, the host's and the ones the app + /// shows beside them ([`sync_beside`](DialogHost::sync_beside)). The top one takes the input. pub fn stack(&self) -> &DialogStack { &self.stack } + /// Puts the dialogs the app shows itself beside the host's on the stack: `beside` lists every + /// one open now, in opening order, with its parent. One not on the stack yet goes on top; one + /// on the stack that `beside` no longer lists leaves it, and the host's dialogs opened as its + /// children close with `Cancel`. A dialog on the stack that the host doesn't hold is a beside + /// dialog. Call it before anything reads the stack in a pass (the app's own dialogs, a + /// control request), so the top, the startup queue and automation see one order. + pub fn sync_beside(&mut self, ctx: &Context, beside: impl IntoIterator)>) { + let beside: Vec<_> = beside.into_iter().collect(); + let closed: Vec = self + .stack + .ids() + .filter(|id| !self.open.iter().any(|dialog| dialog.id() == *id) && !beside.iter().any(|(listed, _)| listed == id)) + .collect(); + for id in &closed { + self.stack.remove_tree(*id); + } + for (id, parent) in beside { + self.stack.push(id, parent); + } + if !closed.is_empty() { + self.cancel_orphans(Some(ctx)); + } + } + /// Opens `dialog` on top. Its id goes on the stack now; the dialog shows from the next /// [`run`](DialogHost::run). Returns the id. pub fn open(&mut self, dialog: D) -> DialogId {