From 284ce3e3d47bdef0b76665e9dbe696842fa8a9e4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 21:19:20 -0800 Subject: [PATCH 1/5] Find and Replace: ui.dialog.confirm presses the button the action field names Since the dialog moved to the craft UI framework, `ui.dialog.confirm` on Find and Replace was refused ("the dialog refuses OK"), and `ui.inspect` listed no `action` field, although the params text and docs/mcp.md describe both. - `ui.inspect` lists the open dialog's `action`, `find` until a set changes it. - `ui.dialog.set` takes `action` (as `field`/`value` or in `fields`): find, replace, replaceFind, replaceAll or done. Any other value is an error, and nothing in the request is set. - `ui.dialog.confirm`, or a set with `action: "confirm"`, presses that button as a click does and puts `action` back to find. Replace and Replace & Find find a match when Find Next has reached none; done closes the dialog. A set whose fields are refused presses nothing. --- crates/ui-egui/src/control.rs | 5 + crates/ui-egui/src/dialogs/find_replace.rs | 148 ++++++++++++++++++ .../ui-egui/src/dialogs/find_replace/tests.rs | 101 ++++++++++++ crates/ui-egui/src/state.rs | 5 + 4 files changed, 259 insertions(+) diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index bfe9f1991c..4bbfe819ff 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -153,6 +153,7 @@ pub fn inspect(app: &VectorcraftApp, ctx: &egui::Context) -> Value { } let mut dialogs = app.dialogs().map_or_else(|| json!([]), |dialogs| dialogs.with(|host| craft::automation::dialogs::inspect(host))); crate::dialogs::javascript_files::show_selected(app, &mut dialogs); + crate::dialogs::find_replace::show_action(app, &mut dialogs); // The Simplify or Smooth slider over the canvas, listed first: `"top"` names a hosted dialog open with it. if let (Some(open), Some(strip)) = (dialogs.as_array_mut(), crate::dialogs::simplify_slider::inspected(app)) { open.insert(0, strip); @@ -1463,6 +1464,10 @@ fn dispatch(app: &mut VectorcraftApp, ctx: &egui::Context, method: &str, p: &Val if let Some(r) = crate::dialogs::script_window::dialog_method(app, method, ¶ms) { return wrap(r); } + // Find and Replace presses the button its `action` field names and stays open. + if let Some(r) = crate::dialogs::find_replace::dialog_method(app, ctx, method, ¶ms) { + return wrap(r); + } // `ui.dialog.set {action}` answers the dialog after setting its fields: the app's own key, which the // framework's method does not take. let action = if method == "ui.dialog.set" { params.as_object_mut().and_then(|o| o.remove("action")) } else { None }; diff --git a/crates/ui-egui/src/dialogs/find_replace.rs b/crates/ui-egui/src/dialogs/find_replace.rs index 78a24f8868..cb987773ff 100644 --- a/crates/ui-egui/src/dialogs/find_replace.rs +++ b/crates/ui-egui/src/dialogs/find_replace.rs @@ -17,6 +17,8 @@ //! `replace` text and `backwards`, the place it reached in `from` (`{id, start, end}`), the drawing's //! UID and Revert counter in `doc`, and the match in `found` (`{index, count}`). Changing the //! loaded drawing, Find text, or switches restarts the search. [`pump`] runs the requests each frame. +//! Automation presses the buttons through the dialog's `action` field and `ui.dialog.confirm` +//! ([`dialog_method`]). use craft::text::find::FindOptions; use craft::text::ui::find::{FindCheck, FindReplaceDialog, FindRequest, FindTab, ReplaceButtons}; @@ -109,6 +111,7 @@ pub fn open(app: &mut VectorcraftApp, params: &Map) -> Result) { let (matched, match_message) = refresh_match(app, &form.query); + let Some(dialogs) = app.dialogs() else { return }; dialogs.with(|host| { if let Some(d) = host.get_mut::() { if let Some(message) = message.or(match_message) { @@ -351,5 +361,143 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { }); } +/// The values the `action` field takes: the button `ui.dialog.confirm` presses. +const ACTIONS: [&str; 5] = ["find", "replace", "replaceFind", "replaceAll", "done"]; + +/// The `action` field a `ui.dialog.set` gives, as `field` and `value` or as an item of `fields`, taken +/// out of `params`: `Ok(None)` when it gives none, an error when it is not one of [`ACTIONS`]. +fn take_action(params: &mut Value) -> Result, String> { + let Some(o) = params.as_object_mut() else { return Ok(None) }; + let mut given = None; + if o.get("field").and_then(Value::as_str) == Some("action") { + if o.contains_key("fields") { + return Err("`fields` takes the place of `field` and `value`".into()); + } + o.remove("field"); + given = Some(o.remove("value").unwrap_or(Value::Null)); + } + let mut only_action = false; + if let Some(list) = o.get_mut("fields").and_then(Value::as_array_mut) { + 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)); + } + let before = list.len(); + list.retain(|item| !is_action(item)); + only_action = list.is_empty() && before > 0; + } + // A list that held only `action` sets nothing else. + if only_action { + o.remove("fields"); + } + match given { + None => Ok(None), + Some(Value::String(a)) if ACTIONS.contains(&a.as_str()) => Ok(Some(a)), + Some(_) => Err("`action` must be find, replace, replaceFind, replaceAll or done".into()), + } +} + +/// The open dialog as `ui.inspect` lists it, `{id, parent, key, fields}` with its `action`, or null. +fn listed(app: &VectorcraftApp) -> Value { + let Some(dialogs) = app.dialogs() else { return Value::Null }; + let mut open = dialogs.with(|host| craft::automation::dialogs::inspect(host)); + show_action(app, &mut open); + let id = dialogs.with(|host| host.get::().map(|d| d.id().value().to_string())); + open.as_array().and_then(|open| open.iter().find(|d| id.as_deref().is_some_and(|id| d["id"] == id)).cloned()).unwrap_or(Value::Null) +} + +/// Adds the `action` field to the open Find and Replace dialog in `ui.inspect`'s `dialogs`. +pub(crate) fn show_action(app: &VectorcraftApp, dialogs: &mut Value) { + let Some(id) = app.dialogs().and_then(|d| d.with(|host| host.get::().map(|d| d.id().value().to_string()))) else { return }; + let action = app.ui.find_action.clone().unwrap_or_else(|| "find".into()); + let shown = dialogs.as_array_mut().and_then(|open| open.iter_mut().find(|d| d["id"] == id.as_str())); + if let Some(fields) = shown.and_then(|d| d.get_mut("fields")).and_then(Value::as_object_mut) { + fields.insert("action".into(), json!(action)); + } +} + +/// `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 +/// `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`. +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; + } + let dialogs = app.dialogs()?; + let ours = dialogs.with(|host| { + let id = host.get::().map(StackDialog::id); + id.is_some() && craft::automation::dialogs::dialog_id(host, params).ok() == id + }); + if !ours { + return None; + } + let keys: &[&str] = if method == "ui.dialog.set" { &["dialog", "field", "value", "fields", "action"] } else { &["dialog"] }; + let mut params = params.clone(); + let answer = if method == "ui.dialog.set" { params.as_object_mut().and_then(|o| o.remove("action")) } else { Some(json!("confirm")) }; + let given = if method == "ui.dialog.set" { + match take_action(&mut params) { + Ok(given) => given, + Err(e) => return Some(Err(e)), + } + } else { + None + }; + let confirm = match answer.as_ref().map(Value::as_str) { + None => false, + Some(Some("confirm")) => true, + // Cancel closes it as the host closes any dialog. + Some(Some("cancel")) if given.is_none() => return None, + 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()); + if sets && let Some(Err(e)) = dialogs.with(|host| craft::automation::dialogs::dialog_method(host, ctx, "ui.dialog.set", ¶ms)) { + return Some(Err(e)); + } + if let Some(given) = given { + app.ui.find_action = Some(given); + } + if answer.as_ref().and_then(Value::as_str) == Some("cancel") { + dialogs.with(|host| host.cancel::(ctx)); + return Some(Ok(Value::Null)); + } + Some(if confirm { press(app, ctx) } else { Ok(listed(app)) }) +} + +/// Presses the button the `action` field names, as a click on it does, and leaves `action` at +/// `find`. Replace and Replace & Find find a match when Find Next has reached none with the Find +/// text and the switches as they are now. Done closes the dialog → the dialog as listed, or null +/// once closed; a refusal is the dialog's message and the error. +fn press(app: &mut VectorcraftApp, ctx: &egui::Context) -> Result { + let action = app.ui.find_action.take().unwrap_or_else(|| "find".into()); + let dialogs = app.dialogs().ok_or("no window")?; + let form = dialogs.with(|host| host.get::().map(form)).ok_or("no hosted dialog is open")?; + if action == "done" { + dialogs.with(|host| host.cancel::(ctx)); + return Ok(Value::Null); + } + let found = app.ui.last_find.as_ref().is_some_and(|last| current_match(app, last, &form.query).is_some()); + let ran = match action.as_str() { + "replace" | "replaceFind" if found => replace(app, &form, action == "replaceFind"), + "replaceAll" => replace_all(app, &form), + _ => find(app, &form), + }; + let message = match &ran { + Ok(m) => Some(m.clone()), + Err(e) => Some(said(e)), + }; + report(app, &form, message); + ran.map(|_| listed(app)) +} + #[cfg(test)] mod tests; diff --git a/crates/ui-egui/src/dialogs/find_replace/tests.rs b/crates/ui-egui/src/dialogs/find_replace/tests.rs index e90efe3f8a..81e550c77a 100644 --- a/crates/ui-egui/src/dialogs/find_replace/tests.rs +++ b/crates/ui-egui/src/dialogs/find_replace/tests.rs @@ -97,6 +97,107 @@ fn selected(p: &Panel) -> Vec { p.app().session.active().unwrap().selection.objects.iter().map(|id| id.0).collect() } +/// A control request answered at once → its result, or its error. +fn control(p: &Panel, method: &str, params: Value) -> Result { + let (req, _) = craft::automation::ControlRequest::new(method, params); + let craft::automation::Reply::Done(r) = crate::control::handle(&mut p.app(), p.ctx(), &req) else { panic!("{method} answered later") }; + match r.get("error") { + Some(e) if !e.is_null() => Err(e.as_str().map_or_else(|| e.to_string(), str::to_string)), + _ => Ok(r["result"].clone()), + } +} + +/// The open Find and Replace dialog's `action`, as `ui.inspect` lists it. +fn listed_action(p: &Panel) -> Value { + let r = control(p, "ui.inspect", json!({})).unwrap(); + let open = r["dialogs"].as_array().cloned().unwrap_or_default(); + open.last().map(|d| d["fields"]["action"].clone()).unwrap_or(Value::Null) +} + +fn set_action(p: &Panel, action: &str) { + control(p, "ui.dialog.set", json!({"dialog": "top", "field": "action", "value": action})).unwrap(); +} + +/// `ui.inspect` lists the dialog's `action`, `find` until a set changes it. `ui.dialog.confirm` +/// presses the button it names, as a click does, keeps the dialog open and puts `action` back to +/// find. Replace and Replace & Find find a match when Find Next reached none; done closes it. +#[test] +fn confirm_presses_the_button_the_action_names() { + let (app, ids) = app_with(&["cat cat cat"]); + let mut p = Panel::new(app, draw); + choose(&p); + p.frame(); + assert_eq!(listed_action(&p), json!("find")); + let fields = json!([{"field": "find", "value": "cat"}, {"field": "replace", "value": "dog"}]); + control(&p, "ui.dialog.set", json!({"dialog": "top", "fields": fields})).unwrap(); + let before = undo_steps(&p); + control(&p, "ui.dialog.confirm", json!({})).unwrap(); + assert_eq!(matched(&p), Some((ids[0], 0, 3)), "find runs without an action set"); + assert!(message(&p).starts_with("1 / 3"), "{}", message(&p)); + + set_action(&p, "replace"); + assert_eq!(listed_action(&p), json!("replace")); + let shown = control(&p, "ui.dialog.confirm", json!({})).unwrap(); + assert_eq!(plain(&p, ids[0]), "dog cat cat", "replace changes the match found"); + assert_eq!((undo_steps(&p), listed_action(&p)), (before + 1, json!("find"))); + assert_eq!(shown["fields"]["action"], json!("find")); + + // Replace & Find with no match found finds one and changes nothing. + set_action(&p, "replaceFind"); + control(&p, "ui.dialog.confirm", json!({})).unwrap(); + assert_eq!((plain(&p, ids[0]), matched(&p)), ("dog cat cat".to_string(), Some((ids[0], 4, 7)))); + set_action(&p, "replaceFind"); + control(&p, "ui.dialog.confirm", json!({})).unwrap(); + assert_eq!((plain(&p, ids[0]), matched(&p)), ("dog dog cat".to_string(), Some((ids[0], 8, 11)))); + + control(&p, "ui.dialog.set", json!({"dialog": "top", "field": "action", "value": "replaceAll", "action": "confirm"})).unwrap(); + assert_eq!((plain(&p, ids[0]), message(&p)), ("dog dog dog".to_string(), "Matches replaced: 1".to_string())); + assert_eq!(undo_steps(&p), before + 3); + + // A set and confirm in one request closes the dialog with done. + let done = json!([{"field": "action", "value": "done"}]); + let closed = control(&p, "ui.dialog.set", json!({"dialog": "top", "fields": done, "action": "confirm"})).unwrap(); + assert_eq!(closed, Value::Null); + assert!(!is_open(&p)); +} + +/// An `action` that names no button is refused when it is set, as are the fields set with it, and +/// `action` stays as it was. A set and confirm whose set is refused presses no button. +#[test] +fn an_action_that_names_no_button_is_refused() { + let (app, ids) = app_with(&["cat"]); + let mut p = Panel::new(app, draw); + choose(&p); + p.frame(); + let refusal = "`action` must be find, replace, replaceFind, replaceAll or done"; + for v in [json!("jump"), json!(3), json!(null), json!("Replace")] { + let r = control(&p, "ui.dialog.set", json!({"dialog": "top", "field": "action", "value": v})); + assert_eq!(r, Err(refusal.to_string()), "{v}"); + } + let fields = json!([{"field": "replace", "value": "dog"}, {"field": "action", "value": "skip"}]); + let r = control(&p, "ui.dialog.set", json!({"dialog": "top", "fields": fields})); + assert_eq!(r, Err(refusal.to_string())); + assert_eq!((field(&p, "replace"), listed_action(&p)), (json!(""), json!("find"))); + // A field the dialog refuses leaves `action` as it was. + let fields = json!([{"field": "matchCase", "value": "yes"}, {"field": "action", "value": "replaceAll"}]); + let r = control(&p, "ui.dialog.set", json!({"dialog": "top", "fields": fields})); + assert!(r.is_err(), "{r:?}"); + assert_eq!(listed_action(&p), json!("find")); + // A request with a key the method does not take is refused. + assert!(control(&p, "ui.dialog.confirm", json!({"dialog": "top", "action": "replace"})).is_err()); + // A set the host refuses presses nothing, even with `action: "confirm"`. + let fields = json!([{"field": "find", "value": "cat"}, {"field": "replace", "value": "dog"}, {"field": "action", "value": "replaceAll"}]); + control(&p, "ui.dialog.set", json!({"dialog": "top", "fields": fields})).unwrap(); + for bad in [json!({"fields": {}}), json!({"value": 1}), json!({"field": "nope", "value": 1})] { + let mut request = bad.clone(); + request["dialog"] = json!("top"); + request["action"] = json!("confirm"); + assert!(control(&p, "ui.dialog.set", request).is_err(), "{bad}"); + assert_eq!((plain(&p, ids[0]), listed_action(&p)), ("cat".to_string(), json!("replaceAll")), "{bad}"); + } + assert!(is_open(&p)); +} + #[test] fn a_cached_match_cannot_replace_text_restored_by_revert() { let (mut app, ids) = app_with(&["Needle Needle"]); diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 990d245bc3..19ef45dbbc 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -732,6 +732,10 @@ pub struct UiState { /// Edit › Find Next goes on from while the dialog is closed. Not saved. #[serde(skip)] pub last_find: Option, + /// The open Find and Replace dialog's `action` field, the button `ui.dialog.confirm` presses + /// ([`crate::dialogs::find_replace::dialog_method`]); `None` is `find`. Not saved. + #[serde(skip)] + pub find_action: Option, /// The notes of the last open that gave any, or of the last save or export, with the status /// text that names the first of them: hovering that text lists them when there are several. /// Not saved. @@ -1530,6 +1534,7 @@ impl Default for UiState { status_sets: 0, replace_font_warning: None, last_find: None, + find_action: None, status_notes: None, startup_notes: Vec::new(), status_kept: None, From f29cce450d72166477c898fb1857bcbc8eca0a7a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 22:49:30 -0800 Subject: [PATCH 2/5] 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"]); From b6bca116e41f7c87a31fa1498798779f9c989dfd Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 3 Dec 2026 01:38:36 -0800 Subject: [PATCH 3/5] Find and Replace: a confirm with no Find text presses nothing With an empty Find text the dialog's buttons other than Done run nothing when clicked. ui.dialog.confirm now refuses such a press with "the Find text is empty" and leaves the text, the undo steps, the dialog's message and the action field as they were, instead of running edit.findNext or edit.findReplace and writing their parameter error into the message. ui.dialog.set now refuses `fields` given together with `field` or `value`, as the dialog host does, also when the list holds only `action`. --- crates/ui-egui/src/dialogs/find_replace.rs | 18 +++++--- .../ui-egui/src/dialogs/find_replace/tests.rs | 41 +++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/dialogs/find_replace.rs b/crates/ui-egui/src/dialogs/find_replace.rs index 52dae1b1a5..0cc695feaa 100644 --- a/crates/ui-egui/src/dialogs/find_replace.rs +++ b/crates/ui-egui/src/dialogs/find_replace.rs @@ -370,11 +370,12 @@ const ACTIONS: [&str; 5] = ["find", "replace", "replaceFind", "replaceAll", "don /// out of `params`: `Ok(None)` when it gives none, an error when it is not one of [`ACTIONS`]. fn take_action(params: &mut Value) -> Result, String> { let Some(o) = params.as_object_mut() else { return Ok(None) }; + // The host's setter refuses the same mix. + if o.contains_key("fields") && (o.contains_key("field") || o.contains_key("value")) { + return Err("`fields` takes the place of `field` and `value`".into()); + } let mut given = None; if o.get("field").and_then(Value::as_str) == Some("action") { - if o.contains_key("fields") { - return Err("`fields` takes the place of `field` and `value`".into()); - } o.remove("field"); given = Some(o.remove("value").unwrap_or(Value::Null)); } @@ -483,15 +484,22 @@ pub(crate) fn dialog_method(app: &mut VectorcraftApp, ctx: &egui::Context, metho /// Presses the button the `action` field names, as a click on it does, and leaves `action` at /// `find`. Replace and Replace & Find find a match when Find Next has reached none with the Find /// text and the switches as they are now. Done closes the dialog → the dialog as listed, or null -/// once closed; a refusal is the dialog's message and the error. +/// once closed; a refusal is the dialog's message and the error. With an empty Find text every +/// button but Done runs nothing, as a click does: the press is an error that leaves the message and +/// `action` as they were. fn press(app: &mut VectorcraftApp, ctx: &egui::Context) -> Result { - let action = app.ui.find_action.take().unwrap_or_else(|| "find".into()); + let action = app.ui.find_action.clone().unwrap_or_else(|| "find".into()); let dialogs = app.dialogs().ok_or("no window")?; let form = dialogs.with(|host| host.get::().map(form)).ok_or("no hosted dialog is open")?; if action == "done" { + app.ui.find_action = None; dialogs.with(|host| host.cancel::(ctx)); return Ok(Value::Null); } + if form.query["find"].as_str().is_none_or(str::is_empty) { + return Err("the Find text is empty".into()); + } + app.ui.find_action = None; let found = app.ui.last_find.as_ref().is_some_and(|last| current_match(app, last, &form.query).is_some()); let ran = match action.as_str() { "replace" | "replaceFind" if found => replace(app, &form, action == "replaceFind"), diff --git a/crates/ui-egui/src/dialogs/find_replace/tests.rs b/crates/ui-egui/src/dialogs/find_replace/tests.rs index 9c7e5f122d..8fc19c094e 100644 --- a/crates/ui-egui/src/dialogs/find_replace/tests.rs +++ b/crates/ui-egui/src/dialogs/find_replace/tests.rs @@ -198,6 +198,47 @@ fn an_action_that_names_no_button_is_refused() { assert!(is_open(&p)); } +/// With an empty Find text a confirm presses nothing, as the dialog's buttons run nothing: it is +/// refused, and the text, the undo steps, the message and `action` stay as they were. Done still +/// closes the dialog. +#[test] +fn a_confirm_with_no_find_text_presses_nothing() { + let (app, ids) = app_with(&["cat cat"]); + let mut p = Panel::new(app, draw); + choose(&p); + p.frame(); + control(&p, "ui.dialog.set", json!({"dialog": "top", "field": "replace", "value": "dog"})).unwrap(); + assert_eq!(field(&p, "find"), json!("")); + let (before, said) = (undo_steps(&p), message(&p)); + for action in ["find", "replace", "replaceFind", "replaceAll"] { + set_action(&p, action); + let r = control(&p, "ui.dialog.confirm", json!({})); + assert_eq!(r, Err("the Find text is empty".to_string()), "{action}"); + assert_eq!((plain(&p, ids[0]), undo_steps(&p), message(&p)), ("cat cat".to_string(), before, said.clone()), "{action}"); + assert_eq!(listed_action(&p), json!(action)); + } + set_action(&p, "done"); + assert_eq!(control(&p, "ui.dialog.confirm", json!({})), Ok(Value::Null)); + assert!(!is_open(&p)); +} + +/// `fields` given with `field` or `value` is refused, as the host refuses it, even when the list +/// holds only `action`; nothing is set. +#[test] +fn fields_with_field_or_value_is_refused() { + let (app, _) = app_with(&["cat"]); + let mut p = Panel::new(app, draw); + choose(&p); + p.frame(); + let refusal = Err("`fields` takes the place of `field` and `value`".to_string()); + let only_action = json!([{"field": "action", "value": "replace"}]); + let r = control(&p, "ui.dialog.set", json!({"dialog": "top", "field": "find", "value": "cat", "fields": only_action})); + assert_eq!(r, refusal); + let r = control(&p, "ui.dialog.set", json!({"dialog": "top", "value": "cat", "fields": only_action})); + assert_eq!(r, refusal); + assert_eq!((field(&p, "find"), listed_action(&p)), (json!(""), json!("find"))); +} + /// 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. From 787eafd7de0d5dd13ddcb07d6645ca951962f44f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 3 Dec 2026 02:33:14 -0800 Subject: [PATCH 4/5] Find and Replace: a confirm presses only a button the open tab shows ui.dialog.confirm pressed the button `action` names whatever tab the dialog showed. With `tab` set to find, a confirm of replaceAll replaced every match although that tab shows only Find Next and Replace more, and on the Go To tab a confirm of find ran a search although that tab shows no Find Next. A confirm now presses only a button the open tab shows: Find Next on the Find and Replace tabs, Replace, Replace & Find and Replace All on the Replace tab, and Done on every tab. Any other press is refused with "the tab has no button for `action` " and leaves the document, the dialog's message and `action` as they were. --- crates/ui-egui/src/dialogs/find_replace.rs | 27 ++++++++++++--- .../ui-egui/src/dialogs/find_replace/tests.rs | 34 +++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/dialogs/find_replace.rs b/crates/ui-egui/src/dialogs/find_replace.rs index 0cc695feaa..fc62033bf9 100644 --- a/crates/ui-egui/src/dialogs/find_replace.rs +++ b/crates/ui-egui/src/dialogs/find_replace.rs @@ -481,16 +481,35 @@ pub(crate) fn dialog_method(app: &mut VectorcraftApp, ctx: &egui::Context, metho Some(if confirm { press(app, ctx) } else { Ok(listed(app)) }) } +/// Whether the dialog's `tab` (its `tab` field: `find`, `replace` or `goTo`) shows the button +/// `action` presses, enabled. Every tab shows Done. The Find tab shows Find Next, and the Replace tab +/// shows Find Next, Replace, Replace & Find and Replace All. The Go To tab shows no Find Next: its +/// first button is Go To, which stays disabled while the dialog has no go-to kinds. +fn shown_on(tab: &str, action: &str) -> bool { + match action { + "done" => true, + "find" => matches!(tab, "find" | "replace"), + _ => tab == "replace", + } +} + /// Presses the button the `action` field names, as a click on it does, and leaves `action` at /// `find`. Replace and Replace & Find find a match when Find Next has reached none with the Find /// text and the switches as they are now. Done closes the dialog → the dialog as listed, or null -/// once closed; a refusal is the dialog's message and the error. With an empty Find text every -/// button but Done runs nothing, as a click does: the press is an error that leaves the message and -/// `action` as they were. +/// once closed; a refusal is the dialog's message and the error. A button the open tab does not +/// show ([`shown_on`]) and, with an empty Find text, every button but Done run nothing, as a click +/// cannot press them: the press is an error that leaves the document, the message and `action` as +/// they were. fn press(app: &mut VectorcraftApp, ctx: &egui::Context) -> Result { let action = app.ui.find_action.clone().unwrap_or_else(|| "find".into()); let dialogs = app.dialogs().ok_or("no window")?; - let form = dialogs.with(|host| host.get::().map(form)).ok_or("no hosted dialog is open")?; + let (form, tab) = dialogs + .with(|host| host.get::().map(|d| (form(d), d.json_fields().and_then(|f| f["tab"].as_str().map(str::to_string))))) + .ok_or("no hosted dialog is open")?; + let tab = tab.unwrap_or_default(); + if !shown_on(&tab, &action) { + return Err(format!("the {tab} tab has no button for `action` {action}")); + } if action == "done" { app.ui.find_action = None; dialogs.with(|host| host.cancel::(ctx)); diff --git a/crates/ui-egui/src/dialogs/find_replace/tests.rs b/crates/ui-egui/src/dialogs/find_replace/tests.rs index 8fc19c094e..dd1dc255e7 100644 --- a/crates/ui-egui/src/dialogs/find_replace/tests.rs +++ b/crates/ui-egui/src/dialogs/find_replace/tests.rs @@ -222,6 +222,40 @@ fn a_confirm_with_no_find_text_presses_nothing() { assert!(!is_open(&p)); } +/// A confirm presses only a button the open tab shows enabled: on the Find tab the Replace buttons +/// and on the Go To tab Find Next are refused, naming the tab, and the text, the undo steps, the +/// message and `action` stay as they were. Find Next runs on the Find tab, and Done closes the +/// dialog from the Go To tab. +#[test] +fn a_confirm_presses_only_a_button_the_tab_shows() { + let (app, ids) = app_with(&["cat cat"]); + let mut p = Panel::new(app, draw); + choose(&p); + p.frame(); + let fields = json!([{"field": "find", "value": "cat"}, {"field": "replace", "value": "dog"}, {"field": "tab", "value": "find"}]); + control(&p, "ui.dialog.set", json!({"dialog": "top", "fields": fields})).unwrap(); + let (before, said) = (undo_steps(&p), message(&p)); + for action in ["replace", "replaceFind", "replaceAll"] { + set_action(&p, action); + let r = control(&p, "ui.dialog.confirm", json!({})); + assert_eq!(r, Err(format!("the find tab has no button for `action` {action}")), "{action}"); + assert_eq!((plain(&p, ids[0]), undo_steps(&p), message(&p)), ("cat cat".to_string(), before, said.clone()), "{action}"); + assert_eq!(listed_action(&p), json!(action)); + } + set_action(&p, "find"); + control(&p, "ui.dialog.confirm", json!({})).unwrap(); + assert_eq!(matched(&p), Some((ids[0], 0, 3)), "Find Next is on the Find tab"); + + control(&p, "ui.dialog.set", json!({"dialog": "top", "field": "tab", "value": "goTo"})).unwrap(); + let (at, said) = (from(&p), message(&p)); + let r = control(&p, "ui.dialog.confirm", json!({})); + assert_eq!(r, Err("the goTo tab has no button for `action` find".to_string())); + assert_eq!((from(&p), message(&p), listed_action(&p)), (at, said, json!("find"))); + set_action(&p, "done"); + assert_eq!(control(&p, "ui.dialog.confirm", json!({})), Ok(Value::Null)); + assert!(!is_open(&p)); +} + /// `fields` given with `field` or `value` is refused, as the host refuses it, even when the list /// holds only `action`; nothing is set. #[test] From 74d8757ef50e2dac09e1276f86af07f6441c62cd Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 3 Dec 2026 06:01:21 -0800 Subject: [PATCH 5/5] Find and Replace: the params text and docs/mcp.md say which tab runs each action ui.dialog.confirm refuses an action whose button the open tab does not show. The ui.paramDialog params text (crates/mcp/src/ui_catalog.rs and crates/ui-egui/src/menus/ui_commands.rs) and docs/mcp.md now say so. --- crates/mcp/src/ui_catalog.rs | 2 +- crates/ui-egui/src/menus/ui_commands.rs | 2 +- docs/mcp.md | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/mcp/src/ui_catalog.rs b/crates/mcp/src/ui_catalog.rs index acbbb5d151..1046cae532 100644 --- a/crates/mcp/src/ui_catalog.rs +++ b/crates/mcp/src/ui_catalog.rs @@ -1018,7 +1018,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "ui.paramDialog", "Command Dialog", "", - "{command, label?, params} open a parameter dialog for any command. For commands other than edit.findReplace, params.__doc may be an unsigned document uid: the dialog previews and confirms only while that document is active, and OK after a document switch closes it without running the command. An invalid __doc is an error; omitting it keeps the existing behavior. For edit.findReplace it opens Find and Replace (dialog `findReplace`), which stays open: fields find, replace, matchCase, wholeWord, backwards, hiddenLayers, lockedLayers, and action, which ui.dialog.confirm runs: find (the next match, edit.findNext, shown in the dialog with its place among the matches), replace (the match found, edit.replaceMatch), replaceFind (both), replaceAll (edit.findReplace) or done (close). After each one action is find again. Replace and Replace & Find change a match that find reached with the find text and options as they are now, and find one otherwise. The dialog opens on the last search, with the fields params gives in its place (a null field gives none; a field of the wrong type is an error, and ui.dialog.confirm then runs no action), and Edit › Find Next goes on from it in the same document while the dialog is closed", + "{command, label?, params} open a parameter dialog for any command. For commands other than edit.findReplace, params.__doc may be an unsigned document uid: the dialog previews and confirms only while that document is active, and OK after a document switch closes it without running the command. An invalid __doc is an error; omitting it keeps the existing behavior. For edit.findReplace it opens Find and Replace (dialog `findReplace`), which stays open: fields find, replace, matchCase, wholeWord, backwards, hiddenLayers, lockedLayers, and action, which ui.dialog.confirm runs: find (the next match, edit.findNext, shown in the dialog with its place among the matches), replace (the match found, edit.replaceMatch), replaceFind (both), replaceAll (edit.findReplace) or done (close). An action whose button the open tab does not show is refused: find runs on the Find and Replace tabs, replace, replaceFind and replaceAll on the Replace tab, done on every tab. After each one action is find again. Replace and Replace & Find change a match that find reached with the find text and options as they are now, and find one otherwise. The dialog opens on the last search, with the fields params gives in its place (a null field gives none; a field of the wrong type is an error, and ui.dialog.confirm then runs no action), and Edit › Find Next goes on from it in the same document while the dialog is closed", ), ( "ui.recolorDialog", diff --git a/crates/ui-egui/src/menus/ui_commands.rs b/crates/ui-egui/src/menus/ui_commands.rs index e131421a2a..458bdf224b 100644 --- a/crates/ui-egui/src/menus/ui_commands.rs +++ b/crates/ui-egui/src/menus/ui_commands.rs @@ -1019,7 +1019,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "ui.paramDialog", "Command Dialog", "", - "{command, label?, params} open a parameter dialog for any command. For commands other than edit.findReplace, params.__doc may be an unsigned document uid: the dialog previews and confirms only while that document is active, and OK after a document switch closes it without running the command. An invalid __doc is an error; omitting it keeps the existing behavior. For edit.findReplace it opens Find and Replace (dialog `findReplace`), which stays open: fields find, replace, matchCase, wholeWord, backwards, hiddenLayers, lockedLayers, and action, which ui.dialog.confirm runs: find (the next match, edit.findNext, shown in the dialog with its place among the matches), replace (the match found, edit.replaceMatch), replaceFind (both), replaceAll (edit.findReplace) or done (close). After each one action is find again. Replace and Replace & Find change a match that find reached with the find text and options as they are now, and find one otherwise. The dialog opens on the last search, with the fields params gives in its place (a null field gives none; a field of the wrong type is an error, and ui.dialog.confirm then runs no action), and Edit › Find Next goes on from it in the same document while the dialog is closed", + "{command, label?, params} open a parameter dialog for any command. For commands other than edit.findReplace, params.__doc may be an unsigned document uid: the dialog previews and confirms only while that document is active, and OK after a document switch closes it without running the command. An invalid __doc is an error; omitting it keeps the existing behavior. For edit.findReplace it opens Find and Replace (dialog `findReplace`), which stays open: fields find, replace, matchCase, wholeWord, backwards, hiddenLayers, lockedLayers, and action, which ui.dialog.confirm runs: find (the next match, edit.findNext, shown in the dialog with its place among the matches), replace (the match found, edit.replaceMatch), replaceFind (both), replaceAll (edit.findReplace) or done (close). An action whose button the open tab does not show is refused: find runs on the Find and Replace tabs, replace, replaceFind and replaceAll on the Replace tab, done on every tab. After each one action is find again. Replace and Replace & Find change a match that find reached with the find text and options as they are now, and find one otherwise. The dialog opens on the last search, with the fields params gives in its place (a null field gives none; a field of the wrong type is an error, and ui.dialog.confirm then runs no action), and Edit › Find Next goes on from it in the same document while the dialog is closed", ), ( "ui.recolorDialog", diff --git a/docs/mcp.md b/docs/mcp.md index 1419fc3194..42af992810 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -14318,7 +14318,9 @@ In the app, Edit › Find and Replace… opens a dialog that stays open (kind `f fields, Match Case, Find Whole Word, Search Backwards, Check Hidden Layers and Check Locked Layers, and Find, Replace, Replace & Find, Replace All and Done. Its fields are `find`, `replace`, `matchCase`, `wholeWord`, `backwards`, `hiddenLayers`, `lockedLayers` and `action`; `ui.dialog.confirm` runs the `action`: `find` (the default), `replace`, -`replaceFind`, `replaceAll` or `done`, which closes the dialog. The dialog shows the match found in its line of text +`replaceFind`, `replaceAll` or `done`, which closes the dialog. It refuses an `action` whose button the open tab does +not show: `find` runs on the Find and Replace tabs, `replace`, `replaceFind` and `replaceAll` on the Replace tab, and +`done` on every tab. The dialog shows the match found in its line of text with its place among the matches. Replace and Replace & Find change a match that Find reached with the Find text and the options as they are now, and find one otherwise. Enter runs Find. Edit › Find Next goes on from the dialog's last search while the dialog is closed, and opens the dialog when there has been none. A place the search reached in one