DialogHost::open_beside and close_beside: app dialogs take their stack place when they open and close

This commit is contained in:
aiongg
2026-11-13 18:15:44 +00:00
parent e98b39a326
commit a9ed41b60f
3 changed files with 48 additions and 36 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ pub fn inspect(host: &DialogHost) -> Value {
}
/// 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).
/// beside them ([`DialogHost::open_beside`]) as `beside` describes it (`None` leaves it out).
pub fn inspect_stack(host: &DialogHost, mut beside: impl FnMut(DialogId) -> Option<Value>) -> Value {
let mut hosted = host.open_dialogs();
host.stack()
+31 -11
View File
@@ -2,11 +2,13 @@
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, dialog_id};
use craft_ui::dialog::{DialogHost, DialogResult, PromptChoice, PromptKind, StandardPrompt, StartupStage, dialog_id};
use craft_ui::patterns::form_dialogs::{DialogFields, HostedParamDialog, Param, ParamDialog, ParamKind};
use craft_ui::testing::trial;
use craft_ui::testing::{Fixture, trial};
use egui::Context;
use serde_json::{Value, json};
use std::cell::RefCell;
use std::rc::Rc;
fn call(host: &mut DialogHost, ctx: &Context, method: &str, params: Value) -> Result<Value, String> {
dialog_method(host, ctx, method, &params).expect("a ui.dialog method")
@@ -117,17 +119,18 @@ fn param_dialog_automation_follows_its_rows() {
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.
/// Dialogs an app shows itself take their place on the host's stack when they open: listed in one opening order
/// between the hosted ones, never answered by the host's methods, and closing the dialogs opened over them, the
/// host's with `Cancel`, when they close.
#[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 (first, second, third) = (dialog_id(("app", 1)), dialog_id(("app", 2)), dialog_id(("app", 3)));
let own = |id| Some(json!({"id": if id == first { "first" } else { "second" }}));
host.sync_beside(&ctx, [(first, None)]);
host.open_beside(first, None);
let prompt = host.open(StandardPrompt::new(PromptKind::TrashMask));
host.sync_beside(&ctx, [(first, None), (second, None)]);
host.open_beside(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());
@@ -135,13 +138,30 @@ fn beside_dialogs_share_the_hosts_stack() {
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)]);
host.close_beside(second);
assert!(host.stack().is_top(prompt), "an app dialog that closed leaves the stack");
assert!(host.cancel_by_id(&ctx, prompt));
host.take::<StandardPrompt>();
let child = host.open_child(StandardPrompt::confirm_delete(["Layer 1"], 1), first);
assert!(host.stack().is_top(child));
host.sync_beside(&ctx, []);
host.open_beside(third, Some(child));
assert!(host.stack().is_top(third));
host.close_beside(first);
assert!(matches!(host.take::<StandardPrompt>(), Some(DialogResult::Cancel)), "a child closes with its app dialog");
assert!(host.stack().is_empty() && host.is_empty());
assert!(host.stack().is_empty() && host.is_empty(), "the app dialog over the child leaves with it");
}
/// A startup modal waits while a dialog the app shows itself is open, and opens once it closes.
#[trial]
fn the_startup_queue_waits_for_beside_dialogs() {
let host = Rc::new(RefCell::new(DialogHost::new()));
let own = dialog_id(("app", 1));
host.borrow_mut().open_beside(own, None);
host.borrow_mut().enqueue(StartupStage::Warning, StandardPrompt::new(PromptKind::TrashMask));
let shown = Rc::clone(&host);
let mut fixture = Fixture::new(move |ui| shown.borrow_mut().run(ui));
fixture.idle(2);
assert!(!host.borrow().is_open::<StandardPrompt>(), "the startup modal waits for the app's dialog");
host.borrow_mut().close_beside(own);
fixture.idle(2);
assert!(host.borrow().is_open::<StandardPrompt>(), "the startup modal opens once the app's dialog closed");
}
+16 -24
View File
@@ -228,8 +228,9 @@ impl<D: StackDialog> 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.
/// - Dialogs the app still shows itself share the stack: [`open_beside`](DialogHost::open_beside) and
/// [`close_beside`](DialogHost::close_beside), called where they open and close, give them their place 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
@@ -283,32 +284,23 @@ impl DialogHost {
}
/// 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.
/// shows beside them ([`open_beside`](DialogHost::open_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<Item = (DialogId, Option<DialogId>)>) {
let beside: Vec<_> = beside.into_iter().collect();
let closed: Vec<DialogId> = 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));
/// Puts a dialog the app shows itself beside the host's on the stack when it opens: on top, over `parent` for a
/// child. From then on the top dialog, the startup queue and automation count it as one of the stack's.
pub fn open_beside(&mut self, id: DialogId, parent: Option<DialogId>) {
self.stack.push(id, parent);
}
/// Takes the dialog the app showed beside the host's off the stack when it closes, with every dialog opened over
/// it: the host's among them close with `Cancel`.
pub fn close_beside(&mut self, id: DialogId) {
if self.stack.contains(id) {
self.stack.remove_tree(id);
self.cancel_orphans(None);
}
}