From f29cce450d72166477c898fb1857bcbc8eca0a7a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 22:49:30 -0800 Subject: [PATCH] Find and Replace: every set lists action, and a click or reopening resets it - A `ui.dialog.set` on the dialog that gives no `action` answers with the dialog as `ui.inspect` lists it, `action` included, instead of the framework's listing without it. - A `fields` list of more than 256 items is refused before `action` is taken out of it, so `action` counts toward the framework's limit. - A click on Find Next, Replace, Replace & Find or Replace All puts `action` back to find, as `ui.dialog.confirm` does. - A test covers these and the reset when the dialog opens again. --- crates/ui-egui/src/dialogs/find_replace.rs | 23 +++++++++----- .../ui-egui/src/dialogs/find_replace/tests.rs | 31 +++++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/dialogs/find_replace.rs b/crates/ui-egui/src/dialogs/find_replace.rs index cb987773ff..52dae1b1a5 100644 --- a/crates/ui-egui/src/dialogs/find_replace.rs +++ b/crates/ui-egui/src/dialogs/find_replace.rs @@ -341,6 +341,8 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { FindRequest::ReplaceAll { .. } => replace_all(app, &form), _ => return None, }; + // A click presses a button as `ui.dialog.confirm` does: `action` goes back to find. + app.ui.find_action = None; Some(ran.unwrap_or_else(|e| said(&e))) }); report(app, &form, message); @@ -378,6 +380,11 @@ fn take_action(params: &mut Value) -> Result, String> { } let mut only_action = false; if let Some(list) = o.get_mut("fields").and_then(Value::as_array_mut) { + // The framework's limit counts `action` with the other items. + let most = craft::automation::dialogs::MAX_DIALOG_FIELDS; + if list.len() > most { + return Err(format!("`fields` holds {} items; at most {most}", list.len())); + } let is_action = |item: &Value| item.get("field").and_then(Value::as_str) == Some("action"); if let Some(last) = list.iter().rev().find(|item| is_action(item)) { given = Some(last.get("value").cloned().unwrap_or(Value::Null)); @@ -417,10 +424,11 @@ pub(crate) fn show_action(app: &VectorcraftApp, dialogs: &mut Value) { } /// `ui.dialog.set` and `ui.dialog.confirm` (`method`) on the open Find and Replace dialog, when -/// `params.dialog` names it: a set takes the `action` field, and `ui.dialog.confirm`, or a set with +/// `params.dialog` names it: a set takes the `action` field and answers with the dialog as +/// `ui.inspect` lists it, `action` included, and `ui.dialog.confirm`, or a set with /// `action: "confirm"`, presses the button it names (`find` when none is set) and leaves `action` at -/// `find` again. Every other request is the host's, as is a set that gives no `action` field and -/// does not confirm: `None`. +/// `find` again. Every other request is the host's, as is a set with `action: "cancel"` that gives +/// no `action` field: `None`. pub(crate) fn dialog_method(app: &mut VectorcraftApp, ctx: &egui::Context, method: &str, params: &Value) -> Option> { if !matches!(method, "ui.dialog.set" | "ui.dialog.confirm") { return None; @@ -452,14 +460,13 @@ pub(crate) fn dialog_method(app: &mut VectorcraftApp, ctx: &egui::Context, metho Some(Some("cancel")) => false, Some(_) => return Some(Err("`action` must be confirm or cancel".into())), }; - if given.is_none() && !confirm { - return None; - } if let Err(e) = craft::automation::protocol::Params(¶ms).only(None, keys) { return Some(Err(e)); } - // The other fields are set first, all or none, as the host sets and checks them. - let sets = ["field", "value", "fields"].iter().any(|k| params.get(k).is_some()); + // The other fields are set first, all or none, as the host sets and checks them. A set that + // gives no `action` and does not confirm goes to the host's setter whole, which refuses one + // with nothing to set. + let sets = ["field", "value", "fields"].iter().any(|k| params.get(k).is_some()) || (given.is_none() && !confirm); if sets && let Some(Err(e)) = dialogs.with(|host| craft::automation::dialogs::dialog_method(host, ctx, "ui.dialog.set", ¶ms)) { return Some(Err(e)); } diff --git a/crates/ui-egui/src/dialogs/find_replace/tests.rs b/crates/ui-egui/src/dialogs/find_replace/tests.rs index 81e550c77a..9c7e5f122d 100644 --- a/crates/ui-egui/src/dialogs/find_replace/tests.rs +++ b/crates/ui-egui/src/dialogs/find_replace/tests.rs @@ -198,6 +198,37 @@ fn an_action_that_names_no_button_is_refused() { assert!(is_open(&p)); } +/// A set of other fields answers with `action` listed. A click on a button, like a confirm, puts +/// `action` back to find, and so does opening the dialog again. A `fields` list of more than 256 +/// items is refused, `action` counted with the rest. +#[test] +fn action_is_listed_and_goes_back_to_find() { + let (app, _) = app_with(&["cat"]); + let mut p = Panel::new(app, draw); + choose(&p); + p.frame(); + set_action(&p, "replace"); + let shown = control(&p, "ui.dialog.set", json!({"dialog": "top", "field": "find", "value": "cat"})).unwrap(); + assert_eq!((shown["fields"]["find"].clone(), shown["fields"]["action"].clone()), (json!("cat"), json!("replace"))); + assert!(control(&p, "ui.dialog.set", json!({"dialog": "top"})).is_err(), "a set with nothing to set"); + + let mut fields = vec![json!({"field": "find", "value": "cat"}); 256]; + fields.push(json!({"field": "action", "value": "replaceAll"})); + let r = control(&p, "ui.dialog.set", json!({"dialog": "top", "fields": fields})); + assert_eq!(r, Err("`fields` holds 257 items; at most 256".to_string())); + assert_eq!(listed_action(&p), json!("replace")); + + p.click("Find Next"); + assert_eq!(listed_action(&p), json!("find"), "a click"); + + set_action(&p, "replace"); + control(&p, "ui.dialog.cancel", json!({"dialog": "top"})).unwrap(); + assert!(!is_open(&p)); + choose(&p); + p.frame(); + assert_eq!(listed_action(&p), json!("find"), "opening again"); +} + #[test] fn a_cached_match_cannot_replace_text_restored_by_revert() { let (mut app, ids) = app_with(&["Needle Needle"]);