diff --git a/crates/automation/src/dialogs.rs b/crates/automation/src/dialogs.rs index 6ed8c7847..3bc5baf3b 100644 --- a/crates/automation/src/dialogs.rs +++ b/crates/automation/src/dialogs.rs @@ -23,6 +23,12 @@ pub fn inspect(host: &DialogHost) -> Value { .collect() } +/// Whether `params.dialog` names an open dialog of `host`, so an app that lists dialogs of its own beside the host's +/// knows which of them a `ui.dialog.*` request is for. +pub fn names_open_dialog(host: &DialogHost, params: &Value) -> bool { + dialog(host, Params(params)).is_ok() +} + /// Runs a `ui.dialog.*` method on `host`; `None` when `method` is not one of them. /// /// - `ui.dialog.set {dialog, field, value}` sets one field ([`DialogHost::set_json_field`]). diff --git a/crates/automation/tests/it/dialogs.rs b/crates/automation/tests/it/dialogs.rs index f1cbf77e6..5a78ef1b7 100644 --- a/crates/automation/tests/it/dialogs.rs +++ b/crates/automation/tests/it/dialogs.rs @@ -1,6 +1,6 @@ //! `ui.dialog.*` on typed dialogs through one generic path. -use craft_automation::dialogs::{dialog_method, inspect}; +use craft_automation::dialogs::{dialog_method, inspect, names_open_dialog}; use craft_ui::contract::{Bind, SaveKey}; use craft_ui::dialog::{DialogHost, DialogResult, PromptChoice, PromptKind, StandardPrompt}; use craft_ui::patterns::form_dialogs::{DialogFields, HostedParamDialog, Param, ParamDialog, ParamKind}; @@ -50,6 +50,9 @@ fn dialog_methods_read_set_and_answer_typed_dialogs() { // can't be applied, so the default answer is refused too. The id may also come as a number. host.open(StandardPrompt::confirm_delete(["Layer 1"], 1)); let delete: u64 = inspect(&host)[0]["id"].as_str().and_then(|id| id.parse().ok()).expect("a decimal id"); + // An app with dialogs of its own beside the host's learns which ones a request names. + assert!(names_open_dialog(&host, &json!({"dialog": delete})) && names_open_dialog(&host, &json!({"dialog": delete.to_string()}))); + assert!(!names_open_dialog(&host, &json!({"dialog": 7})) && !names_open_dialog(&host, &json!({"dialog": "shell"}))); assert!(call(&mut host, &ctx, "ui.dialog.set", json!({"dialog": delete, "field": "choice", "value": "alternate"})).is_err()); assert!(host.cancel::(&ctx)); host.take::();