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.