craft-automation: names_open_dialog tells an app which ui.dialog requests are its DialogHost's

This commit is contained in:
aiongg
2026-11-13 03:20:08 +00:00
parent d4c4f57d96
commit 738d034b69
2 changed files with 10 additions and 1 deletions
+6
View File
@@ -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`]).
+4 -1
View File
@@ -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::<StandardPrompt>(&ctx));
host.take::<StandardPrompt>();