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]