PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Relink to File: ask for the file (menu bar and layer context menu)
Joedfb8fe071a25
diff --git a/crates/ui-egui/src/file_dialog/tests.rs b/crates/ui-egui/src/file_dialog/tests.rsindex 6a3ccfcf9..64df522b0 100644--- a/crates/ui-egui/src/file_dialog/tests.rs+++ b/crates/ui-egui/src/file_dialog/tests.rs@@ -765,6 +765,45 @@ fn replace_contents_picks_a_file_for_every_instance() {assert!(matches!(smart_source(&app, ids[1]), photocraft_doc::SmartSource::Embedded { ref file_name, .. } if file_name == "art.png"));}+/// Layer › Smart Objects › Relink to File… (and the layer context menu item) asks for a file and+/// links every instance to its path; it ran without a path before, so it always failed.+#[test]+fn relink_to_file_picks_a_file_for_every_instance() {+ let (mut app, open, _, ids) = smart_app();+ let ctx = egui::Context::default();+ let before = ids.map(|id| smart_source(&app, id));+ // Cancelling changes nothing.+ menus::invoke(&mut app, &ctx, "layer.smartObjects.relinkToFile", json!({})).unwrap();+ app.poll_file_dialog(&ctx, None);+ assert!(matches!(open.borrow().as_slice(), [(FileDialogRequest::Open { multiple: false, .. }, _)]));+ answer(&open, None);+ app.poll_file_dialog(&ctx, None);+ assert_eq!(ids.map(|id| smart_source(&app, id)), before);+ // The web picker answers only a name and bytes: nothing to link to, so the contents stay.+ let png = photocraft_io::export(&app.session.active().unwrap().doc, "png", &Default::default()).unwrap().bytes;+ menus::invoke(&mut app, &ctx, "layer.smartObjects.relinkToFile", json!({})).unwrap();+ app.poll_file_dialog(&ctx, None);+ answer(&open, Some(FileDialogAnswer::Contents("art.png".into(), png.clone())));+ app.poll_file_dialog(&ctx, None);+ assert!(app.ui.status_error);+ assert_eq!(ids.map(|id| smart_source(&app, id)), before);+ // A file picked on disk links both instances to its path.+ let dir = std::env::temp_dir().join(format!("pcraft-relink-{}", std::process::id()));+ std::fs::create_dir_all(&dir).unwrap();+ let path = dir.join("art.png");+ std::fs::write(&path, &png).unwrap();+ let path = path.to_string_lossy().into_owned();+ menus::invoke(&mut app, &ctx, "layer.smartObjects.relinkToFile", json!({})).unwrap();+ app.poll_file_dialog(&ctx, None);+ answer(&open, Some(FileDialogAnswer::Paths(vec![path.clone()])));+ app.poll_file_dialog(&ctx, None);+ assert!(!app.ui.status_error, "{}", app.ui.status);+ std::fs::remove_dir_all(&dir).ok();+ for id in ids {+ assert_eq!(smart_source(&app, id), photocraft_doc::SmartSource::Linked { path: path.clone() });+ }+}+#[test]fn export_contents_suggests_the_contents_file_name() {let (mut app, open, written, _) = smart_app();diff --git a/crates/ui-egui/src/layer_menu_ui.rs b/crates/ui-egui/src/layer_menu_ui.rsindex beb3e9bc3..57cf1b501 100644--- a/crates/ui-egui/src/layer_menu_ui.rs+++ b/crates/ui-egui/src/layer_menu_ui.rs@@ -77,6 +77,8 @@ pub fn entries(l: &Layer, multi: bool, has_selection: bool) -> Vec<Entry> {LayerContent::Shape(_) => v.push(Some(("Rasterize Layer", "layer.rasterize.shape"))),LayerContent::Smart(_) => {v.push(Some((tl!("Edit Contents"), "layer.smartObjects.editContents")));+ v.push(Some((tl!("Replace Contents…"), "layer.smartObjects.replaceContents")));+ v.push(Some((tl!("Relink to File…"), "layer.smartObjects.relinkToFile")));v.push(Some((tl!("Convert to Layers"), "layer.smartObjects.convertToLayers")));v.push(Some(("Rasterize Layer", "layer.rasterize.smartObject")));}@@ -574,7 +576,10 @@ mod tests {s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap();let ids: Vec<_> = entries(&layer(&s), false, false).into_iter().flatten().map(|e| e.1).collect();let edit = ids.iter().position(|id| *id == "layer.smartObjects.editContents").expect("Edit Contents entry");- assert_eq!(ids[edit + 1..edit + 3], ["layer.smartObjects.convertToLayers", "layer.rasterize.smartObject"]);+ assert_eq!(+ ids[edit + 1..edit + 5],+ ["layer.smartObjects.replaceContents", "layer.smartObjects.relinkToFile", "layer.smartObjects.convertToLayers", "layer.rasterize.smartObject"]+ );}/// Right-clicking inside a multi-layer selection offers one Rasterize item for the whole@@ -592,7 +597,14 @@ mod tests {assert!(ids(false).contains(&"layer.smartObjects.editContents"), "a single smart object keeps its items");let multi = ids(true);assert!(multi.contains(&"layer.rasterize.layer"));- for single in ["layer.rasterize.smartObject", "layer.rasterize.shape", "layer.smartObjects.editContents", "layer.smartObjects.convertToLayers"] {+ for single in [+ "layer.rasterize.smartObject",+ "layer.rasterize.shape",+ "layer.smartObjects.editContents",+ "layer.smartObjects.replaceContents",+ "layer.smartObjects.relinkToFile",+ "layer.smartObjects.convertToLayers",+ ] {assert!(!multi.contains(&single), "{single} acts on one layer, not on a selection");}let label = entries(&smart, true, false).into_iter().flatten().find(|e| e.1 == "layer.rasterize.layer").unwrap().0;diff --git a/crates/ui-egui/src/view_cmds.rs b/crates/ui-egui/src/view_cmds.rsindex 1da1f918d..813e3dc01 100644--- a/crates/ui-egui/src/view_cmds.rs+++ b/crates/ui-egui/src/view_cmds.rs@@ -584,6 +584,7 @@ fn wraps(id: &str) -> bool {| "file.placeLinked"| "layer.smartObjects.replaceContents"| "layer.smartObjects.exportContents"+ | "layer.smartObjects.relinkToFile"| "layer.smartObjects.convertToLinked"| "file.closeAll"| "file.closeOthers"@@ -1237,6 +1238,18 @@ fn front(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, params: &Value)}))}"layer.smartObjects.exportContents" => Some(export_contents(app)),+ "layer.smartObjects.relinkToFile" => {+ let st = app.session.active()?;+ let (doc, layer) = (st.doc.id, st.active_layer?);+ // A link needs a file on disk: the desktop picker answers its full path, the web+ // picker only a name and the bytes, which can't be linked to.+ Some(app.pick_file_bytes(move |app, path, _| {+ if !std::path::Path::new(&path).is_absolute() {+ return Err(format!("{path}: Relink to File needs a file on disk"));+ }+ app.with_document(doc, |app| app.run("layer.smartObjects.relinkToFile", json!({"layer": layer.0, "path": path})))+ }))+ }"layer.smartObjects.convertToLinked" => {let st = app.session.active()?;let (doc, layer) = (st.doc.id, st.active_layer?);