mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-03 20:43:18 -05:00
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`.
This commit is contained in:
1 parent
f29cce450d
commit
b6bca116e4
2 files changed
+54
-5
No files matched your search
@@ -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<Option<String>, 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<Value, String> {
|
||||
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::<FindReplaceDialog>().map(form)).ok_or("no hosted dialog is open")?;
|
||||
if action == "done" {
|
||||
app.ui.find_action = None;
|
||||
dialogs.with(|host| host.cancel::<FindReplaceDialog>(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"),
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user