mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-03 20:43:18 -05:00
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> tab has no button for `action` <action>" and leaves the document, the dialog's message and `action` as they were.
This commit is contained in:
1 parent
b6bca116e4
commit
787eafd7de
2 files changed
+57
-4
No files matched your search
@@ -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<Value, String> {
|
||||
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")?;
|
||||
let (form, tab) = dialogs
|
||||
.with(|host| host.get::<FindReplaceDialog>().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::<FindReplaceDialog>(ctx));
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user