From 96fd81c16c7a2a280ef209b3b0f94d13da6c544a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 17:29:23 -0800 Subject: [PATCH 1/2] Forget the previous text match after Revert --- crates/ui-egui/src/dialogs/find_replace.rs | 32 +++++++------- .../ui-egui/src/dialogs/find_replace/tests.rs | 42 +++++++++++++++++++ 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/crates/ui-egui/src/dialogs/find_replace.rs b/crates/ui-egui/src/dialogs/find_replace.rs index 61bba50596..cffc3ab7e3 100644 --- a/crates/ui-egui/src/dialogs/find_replace.rs +++ b/crates/ui-egui/src/dialogs/find_replace.rs @@ -14,10 +14,9 @@ //! closed, and opens the dialog when there has been none. //! //! The walk lives in `UiState::last_find`: the Find text and the switches of the last request, its -//! `replace` text and `backwards`, the place it reached in `from` (`{id, start, end}`), the `uid` of -//! that document in `doc` and the match found in `found` (`{index, count}`). A place in a document -//! other than the active one, or reached with other Find text or switches, is not used: the search -//! starts again. [`pump`] runs the dialog's requests once per frame. +//! `replace` text and `backwards`, the place it reached in `from` (`{id, start, end}`), the drawing's +//! UID and Revert counter in `doc`, and the match in `found` (`{index, count}`). Changing the +//! loaded drawing, Find text, or switches restarts the search. [`pump`] runs the requests each frame. use craft::text::find::FindOptions; use craft::text::ui::find::{FindCheck, FindReplaceDialog, FindRequest, FindTab, ReplaceButtons}; @@ -56,9 +55,13 @@ pub fn menu_params() -> Value { json!({"find": null}) } -/// The `uid` of the active document. -fn active_uid(app: &VectorcraftApp) -> Option { - app.session.active().map(|d| d.uid) +/// The active drawing and its loaded generation. +fn active_drawing(app: &VectorcraftApp) -> Option<(u64, u64)> { + app.session.active().map(|d| (d.uid, d.reverts)) +} + +fn same_drawing(last: &Map, (uid, reverts): (u64, u64)) -> bool { + last.get("doc").and_then(Value::as_array).is_some_and(|doc| doc.len() == 2 && doc[0].as_u64() == Some(uid) && doc[1].as_u64() == Some(reverts)) } /// The match found belongs to an open dialog: the last search keeps its place, not the match. @@ -127,12 +130,13 @@ pub fn find_next(app: &mut VectorcraftApp) -> Result { return open(app, &Map::new()); }; forget_found(app); - let uid = active_uid(app); + let drawing = active_drawing(app); if let Some(o) = last.as_object_mut() { + let current = drawing.is_some_and(|d| same_drawing(o, d)); o.remove("replace"); o.remove("found"); - // A place in another document is no place in this one. - if uid.is_none() || o.remove("doc").and_then(|d| d.as_u64()) != uid { + o.remove("doc"); + if !current { o.remove("from"); } } @@ -143,7 +147,7 @@ pub fn find_next(app: &mut VectorcraftApp) -> Result { } if let Some(l) = app.ui.last_find.as_mut() { l["from"] = json!({ "id": r["id"], "start": r["start"], "end": r["end"] }); - l["doc"] = json!(uid); + l["doc"] = json!(drawing); } Ok(r) } @@ -178,9 +182,9 @@ fn form(d: &FindReplaceDialog) -> Form { /// The place the last search reached, `{id, start, end}`, while its document is the active one and /// the Find text and the switches are `query`, those it was reached with. fn place<'a>(app: &VectorcraftApp, last: &'a Value, query: &Value) -> Option<&'a Value> { - let uid = active_uid(app)?; + let drawing = active_drawing(app)?; let last = last.as_object()?; - let same = last.get("doc").and_then(Value::as_u64) == Some(uid) && query_in(last) == *query; + let same = same_drawing(last, drawing) && query_in(last) == *query; same.then(|| last.get("from")).flatten().filter(|at| at.is_object()) } @@ -204,7 +208,7 @@ fn remember(app: &mut VectorcraftApp, form: &Form, at: Option, found: Opt last["backwards"] = json!(form.backwards); if let Some(at) = at { last["from"] = at; - last["doc"] = json!(active_uid(app)); + last["doc"] = json!(active_drawing(app)); } if let Some(found) = found { last["found"] = found; diff --git a/crates/ui-egui/src/dialogs/find_replace/tests.rs b/crates/ui-egui/src/dialogs/find_replace/tests.rs index d773e5a99b..b4aac82b75 100644 --- a/crates/ui-egui/src/dialogs/find_replace/tests.rs +++ b/crates/ui-egui/src/dialogs/find_replace/tests.rs @@ -97,6 +97,48 @@ fn selected(p: &Panel) -> Vec { p.app().session.active().unwrap().selection.objects.iter().map(|id| id.0).collect() } +#[test] +fn a_cached_match_cannot_replace_text_restored_by_revert() { + let (mut app, ids) = app_with(&["Needle Needle"]); + let id = ids[0]; + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + let dir = vectorcraft_testkit::TempDir::new("find-replace-reverted-match"); + app.run("file.saveAs", json!({"path": dir.join("text.vectorcraft")})).unwrap(); + app.run("shape.rectangle", json!({"x": 100, "y": 100, "width": 10, "height": 10})).unwrap(); + app.run("select.none", json!({})).unwrap(); + let st = app.session.active().unwrap(); + let version = (st.uid, st.reverts); + let mut p = Panel::new(app, draw); + choose(&p); + set(&p, "find", json!("Needle")); + set(&p, "replace", json!("Changed")); + p.click("Find Next"); + assert_eq!(matched(&p), Some((id, 0, 6))); + + p.app().run("file.revert", json!({"confirmed": true})).unwrap(); + { + let app = p.app(); + let st = app.session.active().unwrap(); + assert_eq!(st.uid, version.0); + assert_ne!(st.reverts, version.1); + } + let before = undo_steps(&p); + let form = with_dialog(&p, |d| super::form(d)); + // A Replace action dispatched before the dialog has refreshed must still check the match. + super::replace(&mut p.app(), &form, false).unwrap(); + assert_eq!(plain(&p, id), "Needle Needle"); + assert_eq!(undo_steps(&p), before); + pump(&p); + assert_eq!(matched(&p), None); + assert!(message(&p).is_empty()); + + p.click("Find Next"); + assert_eq!(matched(&p), Some((id, 0, 6))); + p.click("Replace"); + assert_eq!(plain(&p, id), "Changed Needle"); +} + /// The Edit menu's rows for Find and Replace, Check Spelling (in its Spelling submenu) and Edit Custom Dictionary /// are enabled, and invoking each puts that dialog on the host. #[test] From effd5f349590a7fef726991a6017279349235507 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 19:05:21 -0800 Subject: [PATCH 2/2] Start Find Next at the live selection after File Revert --- crates/ui-egui/src/dialogs/find_replace.rs | 21 +++++++++++---- .../ui-egui/src/dialogs/find_replace/tests.rs | 27 +++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/dialogs/find_replace.rs b/crates/ui-egui/src/dialogs/find_replace.rs index cffc3ab7e3..78a24f8868 100644 --- a/crates/ui-egui/src/dialogs/find_replace.rs +++ b/crates/ui-egui/src/dialogs/find_replace.rs @@ -60,8 +60,13 @@ fn active_drawing(app: &VectorcraftApp) -> Option<(u64, u64)> { app.session.active().map(|d| (d.uid, d.reverts)) } -fn same_drawing(last: &Map, (uid, reverts): (u64, u64)) -> bool { - last.get("doc").and_then(Value::as_array).is_some_and(|doc| doc.len() == 2 && doc[0].as_u64() == Some(uid) && doc[1].as_u64() == Some(reverts)) +fn saved_drawing(last: &Map) -> Option<(u64, u64)> { + let [uid, reverts] = last.get("doc")?.as_array()?.as_slice() else { return None }; + Some((uid.as_u64()?, reverts.as_u64()?)) +} + +fn same_drawing(last: &Map, drawing: (u64, u64)) -> bool { + saved_drawing(last) == Some(drawing) } /// The match found belongs to an open dialog: the last search keeps its place, not the match. @@ -132,12 +137,18 @@ pub fn find_next(app: &mut VectorcraftApp) -> Result { forget_found(app); let drawing = active_drawing(app); if let Some(o) = last.as_object_mut() { - let current = drawing.is_some_and(|d| same_drawing(o, d)); + let saved = saved_drawing(o); o.remove("replace"); o.remove("found"); o.remove("doc"); - if !current { - o.remove("from"); + if saved != drawing || drawing.is_none() { + let restored = saved.zip(drawing).is_some_and(|((old_uid, _), (uid, _))| old_uid == uid); + if restored { + // An explicit start ignores the whole text frame currently selected. + o.insert("from".into(), Value::Null); + } else { + o.remove("from"); + } } } let r = app.run(NEXT, last)?; diff --git a/crates/ui-egui/src/dialogs/find_replace/tests.rs b/crates/ui-egui/src/dialogs/find_replace/tests.rs index b4aac82b75..e90efe3f8a 100644 --- a/crates/ui-egui/src/dialogs/find_replace/tests.rs +++ b/crates/ui-egui/src/dialogs/find_replace/tests.rs @@ -416,3 +416,30 @@ fn a_search_place_stays_in_its_document_and_given_params_win() { choose(&p); assert_eq!((field(&p, "find"), field(&p, "matchCase")), (json!("cat"), json!(true))); } + +#[test] +fn edit_find_next_starts_at_the_first_restored_match_with_that_frame_selected() { + let (mut app, ids) = app_with(&["cat in the back", "cat cat in front"]); + let first = ids[1]; + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + let dir = vectorcraft_testkit::TempDir::new("menu-find-next-revert"); + app.run("file.saveAs", json!({"path": dir.join("text.vectorcraft")})).unwrap(); + app.run("shape.rectangle", json!({"x": 100, "y": 100, "width": 10, "height": 10})).unwrap(); + let mut p = Panel::new(app, draw); + choose(&p); + set(&p, "find", json!("cat")); + p.click("Find Next"); + assert_eq!(matched(&p), Some((first, 0, 3))); + p.click("Find Next"); + assert_eq!(matched(&p), Some((first, 4, 7))); + p.click("Close"); + + p.app().run("file.revert", json!({"confirmed": true})).unwrap(); + p.app().run("select.set", json!({"ids": [first]})).unwrap(); + let before = undo_steps(&p); + choose_find_next(&p); + assert_eq!(from(&p), json!({"id": first, "start": 0, "end": 3})); + assert_eq!(selected(&p), vec![first]); + assert_eq!(undo_steps(&p), before); +}