Keep the Find and Replace match line in sync

This commit is contained in:
dotCooCoo committed 2026-11-26 16:07:46 -08:00
1 parent 5c7bbeecaa
commit 4b670b1d53
2 files changed
+49 -2

No files matched your search

+18 -2
View File
@@ -293,6 +293,22 @@ fn shown_match(app: &VectorcraftApp, at: &Value, found: &Value) -> Option<String
Some(format!("{} / {} {lead}{before}{hit}{after}{tail}", found["index"], found["count"]))
}
/// Refreshes the match line when a drawing or query change makes its match ineligible or current again.
fn refresh_match(app: &mut VectorcraftApp, query: &Value) -> (bool, Option<String>) {
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::<FindReplaceDialog>() {
if let Some(message) = message {
if let Some(message) = message.or(match_message) {
d.set_message(message);
}
d.set_matched(matched);
@@ -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