diff --git a/crates/ui-egui/src/dialogs/relink.rs b/crates/ui-egui/src/dialogs/relink.rs index 7220750be8..2ee70056be 100644 --- a/crates/ui-egui/src/dialogs/relink.rs +++ b/crates/ui-egui/src/dialogs/relink.rs @@ -46,7 +46,8 @@ fn scope_label(scope: &str) -> Label { /// Open the dialog on the images and placed documents `ids` (default: the selected ones). pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { - let document = app.session.active().ok_or("no document open")?.uid; + let st = app.session.active().ok_or("no document open")?; + let document = (st.uid, st.reverts); let ids = match p.get("ids").filter(|v| !v.is_null()) { Some(v) => v.as_array().cloned().ok_or_else(|| format!("`ids` must be an array of object ids, not {v}"))?, None => crate::panels::links::selected_ids(app).into_iter().map(Value::from).collect(), @@ -86,10 +87,15 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { Ok(Value::Null) } -/// The relink rule: one `links.relink` call with the answer's parameters, and the status it earns. -fn relink(app: &mut VectorcraftApp, (document, params): &(u64, Value)) -> Result<(), String> { - if app.session.active().is_none_or(|st| st.uid != *document) { - let reason = tl!("Relink can only change links in the document where it was opened."); +/// The relink rule: one `links.relink` call with the answer's parameters, and the status it earns. OK is refused in +/// another document, and in the opening document after File > Revert has replaced its content. +fn relink(app: &mut VectorcraftApp, ((document, reverts), params): &((u64, u64), Value)) -> Result<(), String> { + let refusal = match app.session.active() { + Some(st) if st.uid == *document && st.reverts == *reverts => None, + Some(st) if st.uid == *document => Some(tl!("The document was reverted. Reopen this dialog to continue.")), + _ => Some(tl!("Relink can only change links in the document where it was opened.")), + }; + if let Some(reason) = refusal { app.status(reason); return Err(reason.to_owned()); } @@ -122,8 +128,8 @@ impl ItemSource for Names<'_> {} /// app (`HoldsOk`); with Each Link Separately it stays open until every link has a file. pub(crate) struct RelinkDialog { id: DialogId, - /// The document that owns the link IDs. - document: u64, + /// The document that owns the link IDs: its uid and revert count. + document: (u64, u64), ids: Vec, links: Vec, scope: String, @@ -135,7 +141,7 @@ pub(crate) struct RelinkDialog { } impl RelinkDialog { - fn new(document: u64, ids: Vec, links: Vec) -> Self { + fn new(document: (u64, u64), ids: Vec, links: Vec) -> Self { RelinkDialog { id: dialog_id(KEY), document, @@ -194,7 +200,7 @@ impl RelinkDialog { } impl HoldsOk for RelinkDialog { - type Answer = (u64, Value); + type Answer = ((u64, u64), Value); fn held_ok(self) -> Self { self @@ -451,6 +457,82 @@ mod tests { check_relink_stays_in_opening_drawing(true); } + /// Opens Relink on a saved document's linked image and chooses a replacement (in `path`, or from the file picker + /// with `native_picker`) around File > Revert, then presses OK: the restored image keeps its link, no undo step is + /// recorded and the status line reports the revert. Reopened on the restored document, Relink relinks again. + fn check_relink_refused_after_file_revert(native_picker: bool) { + use crate::tests::panel_harness::Panel; + + let (mut app, dir) = setup(if native_picker { "revert-picker" } else { "revert-path" }, &["photo.png"]); + let picker = scripted(&mut app); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + let original = dir.join("photo.png").to_string_lossy().into_owned(); + let id = place(&mut app, &dir.join("photo.png")); + app.run("file.saveAs", json!({"path": dir.join("Art.vectorcraft")})).unwrap(); + let chosen = new_file(&dir, "replacement.png"); + let mut panel = Panel::new(app, crate::tests::picks::dialogs); + panel.room(egui::vec2(900.0, 700.0)); + panel.app().run("ui.relinkDialog", json!({"ids": [id]})).unwrap(); + panel.frame(); + if native_picker { + panel.click("Relink…"); + assert_eq!(picker.requests().len(), 1); + } else { + let dialog = relink_dialog(&panel.app()).id; + answer_dialog(&mut panel.app(), dialog, &[("path", json!(chosen))], false); + } + panel.app().run("shape.rectangle", json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap(); + + panel.app().run("file.revert", json!({"confirmed": true})).unwrap(); + assert_eq!(link_path(&panel.app(), id), original, "the same id names the restored image"); + let undo = panel.app().session.active().unwrap().history.undo.len(); + if native_picker { + picker.answer(&[&chosen]); + panel.frame(); + panel.frame(); + } else { + panel.click("Relink…"); + panel.frame(); + } + { + let app = panel.app(); + assert_eq!(link_path(&app, id), original, "OK leaves the restored image's link unchanged"); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + assert_eq!(app.ui.status, "The document was reverted. Reopen this dialog to continue."); + let dialog = relink_dialog(&app).id; + assert!(matches!(app.dialogs().unwrap().with(|h| h.held_state(dialog)), Some(HeldOk::Refused(_)))); + } + + let ctx = ctx(&panel.app()); + let dialogs = panel.app().dialogs().expect("a window"); + let dialog = relink_dialog(&panel.app()).id; + dialogs.with(|host| host.cancel_by_id(&ctx, dialog)); + panel.frame(); + assert!(open_dialogs(&panel.app()).is_empty()); + panel.app().run("ui.relinkDialog", json!({"ids": [id]})).unwrap(); + panel.frame(); + let dialog = relink_dialog(&panel.app()).id; + answer_dialog(&mut panel.app(), dialog, &[("path", json!(chosen))], false); + panel.click("Relink…"); + panel.frame(); + let app = panel.app(); + assert!(open_dialogs(&app).is_empty()); + assert_eq!(link_path(&app, id), chosen, "reopened, Relink relinks the restored image"); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo + 1); + drop(app); + let _ = std::fs::remove_dir_all(dir); + } + + #[test] + fn relink_is_refused_after_file_revert_for_a_path_confirmation() { + check_relink_refused_after_file_revert(false); + } + + #[test] + fn relink_is_refused_after_file_revert_when_the_file_picker_answers() { + check_relink_refused_after_file_revert(true); + } + #[test] fn replace_popup_cancel_preserves_selected_instances_and_enter_relinks_all() { use crate::tests::panel_harness::Panel; diff --git a/docs/mcp.md b/docs/mcp.md index 8cb939c506..881f712f5c 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -10661,7 +10661,9 @@ one of them, in the layers, opacity-mask art, symbols, graph designs, patterns a `path` and `folder` and relinks each link to its own file. An unknown scope, `paths` without `each`, `each` with `ids`, `path` or `folder`, an entry without `path` or `ids`, an id in two entries and `missingInFolder` with `folder` are errors, and nothing is relinked. Each call is one undo step. The Links panel's Relink… opens the `relink` dialog -(`ui.relinkDialog`) with a Replace menu of these four choices. A PNG, JPEG, +(`ui.relinkDialog`) with a Replace menu of these four choices. Its OK relinks only in the document where the dialog +opened; after File › Revert it relinks nothing and the status line reads "The document was reverted. Reopen this +dialog to continue." A PNG, JPEG, another file (or each at the file of its name in a folder); images keep their bounds, one undo step each. `links.update`, and `document.open` when it reads a modified file, also read the file again for the images and placed documents inside symbols, graph designs, patterns, opacity masks and the art of the stored brushes (a scatter or art