From 4b670b1d53fbfd22581d9aeca992cc45e15f443d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 16:07:46 -0800 Subject: [PATCH] Keep the Find and Replace match line in sync --- crates/ui-egui/src/dialogs/find_replace.rs | 20 ++++++++++-- .../ui-egui/src/dialogs/find_replace/tests.rs | 31 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/dialogs/find_replace.rs b/crates/ui-egui/src/dialogs/find_replace.rs index 560c64605a..61bba50596 100644 --- a/crates/ui-egui/src/dialogs/find_replace.rs +++ b/crates/ui-egui/src/dialogs/find_replace.rs @@ -293,6 +293,22 @@ fn shown_match(app: &VectorcraftApp, at: &Value, found: &Value) -> Option (bool, Option) { + let Some(last) = app.ui.last_find.as_ref() else { return (false, None) }; + let Some(found) = last.get("found") else { return (false, None) }; + let at = place(app, last, query); + let matched = at.is_some(); + if found["visible"].as_bool().unwrap_or(true) == matched { + return (matched, None); + } + let message = at.and_then(|at| shown_match(app, at, found)).unwrap_or_default(); + if let Some(found) = app.ui.last_find.as_mut().and_then(|last| last.get_mut("found")) { + found["visible"] = json!(matched); + } + (matched, Some(message)) +} + /// Runs the request the dialog made this frame, then tells the dialog its message and whether a /// match is current. The match is evaluated in every pump, so a changed Find text or switch /// disables Replace and Replace & Find at once; a pass that has no request only refreshes it. @@ -309,10 +325,10 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { }; Some(ran.unwrap_or_else(|e| said(&e))) }); - let matched = app.ui.last_find.as_ref().is_some_and(|last| current_match(app, last, &form.query).is_some()); + let (matched, match_message) = refresh_match(app, &form.query); dialogs.with(|host| { if let Some(d) = host.get_mut::() { - if let Some(message) = message { + if let Some(message) = message.or(match_message) { d.set_message(message); } d.set_matched(matched); diff --git a/crates/ui-egui/src/dialogs/find_replace/tests.rs b/crates/ui-egui/src/dialogs/find_replace/tests.rs index 2f633e0d25..d773e5a99b 100644 --- a/crates/ui-egui/src/dialogs/find_replace/tests.rs +++ b/crates/ui-egui/src/dialogs/find_replace/tests.rs @@ -296,6 +296,37 @@ fn a_changed_search_starts_again() { assert_eq!(matched(&p), Some((bottom, 4, 7)), "the same search goes on"); } +#[test] +fn the_match_line_follows_the_active_drawing_and_find_query() { + let (app, _) = app_with(&["source cat match"]); + let mut p = Panel::new(app, draw); + p.app().run("file.new", json!({"width": 800, "height": 600})).unwrap(); + p.app().run("text.create", json!({"x": 50, "y": 50, "text": "destination dog match"})).unwrap(); + p.app().run("document.activate", json!({"index": 0})).unwrap(); + choose(&p); + set(&p, "find", json!("cat")); + p.click("Find Next"); + assert!(message(&p).contains("source cat match")); + + p.app().run("document.activate", json!({"index": 1})).unwrap(); + pump(&p); + assert_eq!(matched(&p), None); + assert_eq!(message(&p), "", "the line must not describe a match from another drawing"); + + p.app().run("document.activate", json!({"index": 0})).unwrap(); + pump(&p); + assert!(matched(&p).is_some()); + assert!(message(&p).contains("source cat match")); + + set(&p, "find", json!("dog")); + pump(&p); + assert_eq!(matched(&p), None); + assert_eq!(message(&p), "", "the line must not describe a match for the previous query"); + p.click("Find Next"); + pump(&p); + assert_eq!(message(&p), "No matches found", "refreshing the dialog leaves the search result visible"); +} + /// The place a search reached belongs to its document: in another document, Edit › Find Next and /// the dialog start at that document's first match, even where an object there has the same id. /// The params of a `ui.paramDialog` call take the place of the last search's, a switch that is off