Find and Replace: confirming with a field of the wrong type runs no action

ui.dialog.confirm used to read a replace that is not a string as empty text, so Replace All could delete every match, and a switch that is not true or false as off. It now returns the error, which shows under the match found, and runs nothing; the match stays for Replace once the field is set right.
This commit is contained in:
dotCooCoo
2026-11-17 22:36:13 -08:00
parent d4b1ff8f60
commit 2275ca2fe0
5 changed files with 58 additions and 15 deletions
+1 -1
View File
@@ -847,7 +847,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
"ui.paramDialog",
"Command Dialog",
"",
"{command, label?, params} open a parameter dialog for any command. 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 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 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",
),
(
"ui.recolorDialog",
+27 -9
View File
@@ -88,13 +88,7 @@ pub fn open(app: &mut VectorcraftApp, params: &Map<String, Value>) -> Result<Val
for key in SWITCHES.into_iter().chain(["backwards"]) {
fields.insert(key.into(), json!(false));
}
for (k, v) in params.iter().filter(|(_, v)| !v.is_null()) {
match fields.get(k) {
Some(Value::String(_)) if !v.is_string() => return Err(format!("`{k}` must be a string")),
Some(Value::Bool(_)) if !v.is_boolean() => return Err(format!("`{k}` must be true or false")),
_ => {}
}
}
check_types(params, true)?;
let last = app.ui.last_find.as_ref().and_then(Value::as_object).cloned().unwrap_or_default();
for (k, v) in last.iter().chain(params) {
// Only the dialog's own fields, each of its own type.
@@ -115,6 +109,21 @@ pub fn open(app: &mut VectorcraftApp, params: &Map<String, Value>) -> Result<Val
Ok(Value::Null)
}
/// The first of the dialog's fields in `fields` that holds a value of another type than its own,
/// as an error: `find` and `replace` are strings, and `backwards` and the [`SWITCHES`] are true or
/// false. A null value is no error when `null_gives_none` is true.
fn check_types(fields: &Map<String, Value>, null_gives_none: bool) -> Result<(), String> {
for (k, v) in fields.iter().filter(|(_, v)| !(null_gives_none && v.is_null())) {
if matches!(k.as_str(), "find" | "replace") && !v.is_string() {
return Err(format!("`{k}` must be a string"));
}
if (k == "backwards" || SWITCHES.contains(&k.as_str())) && !v.is_boolean() {
return Err(format!("`{k}` must be true or false"));
}
}
Ok(())
}
/// Edit › Find Next with no text to find: the next match of the last search, from the place it
/// reached, with the options the dialog had. With no search yet, it opens the dialog.
pub fn find_next(app: &mut VectorcraftApp) -> Result<Value, String> {
@@ -249,10 +258,17 @@ fn replace_all(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<Value, String
}
/// A button or `ui.dialog.confirm`: run the `action` and keep the dialog open, or close it for
/// `done`. What a command refuses is the note, and the dialog stays open.
/// `done`. What a command refuses is the note, and the dialog stays open. A field set to a value
/// of another type than its own ([`check_types`]) runs no action: the error is the note, shown
/// under the match found, which Replace still changes once the field is set right.
fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
let mut d = d.clone();
let action = d.fields.insert("action".into(), json!("find")).and_then(|a| a.as_str().map(str::to_string)).unwrap_or_default();
if let Err(e) = check_types(&d.fields, false) {
d.fields.insert(NOTE.into(), json!(e));
app.ui.dialog = Some(d);
return Err(e);
}
let r = match action.as_str() {
"done" => {
remember(app, &d);
@@ -345,7 +361,9 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> DialogAc
ui.colored_label(t.text_strong, egui::RichText::new(hit).strong().underline());
widgets::dim_name(ui, &after);
});
} else if let Some(note) = d.fields.get(NOTE).and_then(Value::as_str) {
}
// Find and Replace remove the note, so it shows under a match only when an action was refused.
if let Some(note) = d.fields.get(NOTE).and_then(Value::as_str) {
widgets::dim_name(ui, &crate::i18n::message(crate::i18n::current(), note));
}
// Enter finds, as the Find button does.
@@ -132,10 +132,34 @@ fn the_edit_menu_opens_the_find_and_replace_dialog() {
}
/// A field given a value of another type than its own is an error and the dialog does not open; a
/// null value gives no field.
/// null value gives no field. A field set to such a value once the dialog is open runs no action:
/// the error is the note, shown under the match found, and the text, the match and the last search
/// stay as they were.
#[test]
fn the_dialog_rejects_a_field_of_the_wrong_type() {
let (mut app, _) = app_with(&["cat"]);
let (mut app, ids) = app_with(&["cat"]);
choose(&mut app);
set(&mut app, "find", json!("cat"));
crate::dialogs::confirm(&mut app).unwrap();
let last = app.ui.last_find.clone();
for (k, v, want) in [
("replace", json!(5), "`replace` must be a string"),
("replace", Value::Null, "`replace` must be a string"),
("matchCase", json!("yes"), "`matchCase` must be true or false"),
] {
set(&mut app, k, v.clone());
set(&mut app, "action", json!("replaceAll"));
assert_eq!(crate::dialogs::confirm(&mut app).unwrap_err(), want, "{k}: {v}");
assert_eq!((plain(&app, ids[0]), field(&app, "__note"), app.ui.last_find.clone()), ("cat".to_string(), json!(want), last.clone()));
assert_eq!(matched(&app), Some((ids[0], 0, 3)), "Replace still changes the match found");
let ctx = context();
frame(&mut app, &ctx, vec![]);
let texts: Vec<String> = frame(&mut app, &ctx, vec![]).into_iter().map(|(t, _)| t).collect();
assert!(texts.iter().any(|t| t == want) && texts.iter().any(|t| t == "cat"), "the note shows under the match: {texts:?}");
set(&mut app, k, if k == "replace" { json!("dog") } else { json!(false) });
}
set(&mut app, "action", json!("done"));
crate::dialogs::confirm(&mut app).unwrap();
let open = |app: &mut VectorcraftApp, params: Value| app.run("ui.paramDialog", json!({"command": super::COMMAND, "params": params}));
for (params, want) in [
(json!({"find": 17}), "`find` must be a string"),
@@ -147,7 +171,7 @@ fn the_dialog_rejects_a_field_of_the_wrong_type() {
assert!(app.ui.dialog.is_none(), "{params}");
}
open(&mut app, json!({"find": null, "replace": "dog", "wholeWord": true})).unwrap();
assert_eq!((field(&app, "find"), field(&app, "replace"), field(&app, "wholeWord")), (json!(""), json!("dog"), json!(true)));
assert_eq!((field(&app, "find"), field(&app, "replace"), field(&app, "wholeWord")), (json!("cat"), json!("dog"), json!(true)));
}
/// The dialog stays open from match to match: Find goes to the next match and selects its
+1 -1
View File
@@ -848,7 +848,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
"ui.paramDialog",
"Command Dialog",
"",
"{command, label?, params} open a parameter dialog for any command. 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 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 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",
),
(
"ui.recolorDialog",
+2 -1
View File
@@ -9997,7 +9997,8 @@ search while the dialog is closed, and opens the dialog when there has been none
document is not used in another, nor once the Find text or an option that decides a match changes: the search then
starts again as it does without `from`. `ui.paramDialog` opens
the dialog on the last search, with the fields its `params` give in its place. A null field gives none, and a field of
the wrong type is an error that leaves the dialog closed.
the wrong type is an error that leaves the dialog closed. Once it is open, `ui.dialog.confirm` with a field of the
wrong type runs no action and returns the error.
## Justification options