diff --git a/crates/ui-egui/src/panels/properties/object_sections.rs b/crates/ui-egui/src/panels/properties/object_sections.rs index 977c21324e..844305a187 100644 --- a/crates/ui-egui/src/panels/properties/object_sections.rs +++ b/crates/ui-egui/src/panels/properties/object_sections.rs @@ -32,7 +32,7 @@ pub(super) fn image_section(app: &mut VectorcraftApp, ui: &mut Ui) { Text::new(details.clone()).shade(Shade::Muted).show(ui); Grid::new(&HALVES).show(ui, |ui, _| { // PLAN: assumed `crate::place::LinkAction::{shown(linked) -> &'static [LinkAction], label(self), run(self, app, id, &name)}` (files-w14, fl.A18) - for action in crate::place::LinkAction::shown(linked) { + for action in crate::place::LinkAction::shown(linked, crate::place::is_dcs_image(app, id)) { if Button::new(action.label()).sizing(Sizing::FillWidth).show(ui).clicked() { action.run(app, id, &name); } diff --git a/crates/ui-egui/src/panels/tests_links.rs b/crates/ui-egui/src/panels/tests_links.rs index cb2555cda1..4fbb74375e 100644 --- a/crates/ui-egui/src/panels/tests_links.rs +++ b/crates/ui-egui/src/panels/tests_links.rs @@ -767,6 +767,77 @@ fn several_mixed_images_offer_embed_and_unembed() { } } +/// A single-file DCS 2.0 file with one Cyan plate, a 2 × 2 grayscale image on a 20 × 10 pt page, +/// made from scratch as http://www.telegraphics.net/doc/DCS.txt describes it; without a clipping +/// path it places as a plain linked image. +fn dcs_file() -> Vec { + let plate = "%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 20 10\n%%EndComments\n20 10 scale\n2 2 8 [2 0 0 -2 0 2] <0000ffff> image\nshowpage\n%%Trailer\n%%EOF\n"; + let header = |at: usize| { + format!( + "%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 20 10\n%%PlateFile: (Cyan) EPS #{at:010} {:010}\n%%EndComments\n20 10 scale 2 2 8 [2 0 0 -2 0 2] <808080808080808080808080> false 3 colorimage showpage\n%%EOF\n", + plate.len() + ) + }; + let mut out = header(header(0).len()).into_bytes(); + out.extend(plate.as_bytes()); + out +} + +/// Images placed from DCS files stay linked (`links.embed` refuses them): with one selected, +/// neither surface offers Embed; with a DCS image and a linked image selected, Embed embeds the +/// linked one and leaves the DCS image linked; with a DCS image and an embedded one, only +/// Unembed… is offered. +#[test] +fn several_images_leave_dcs_images_out_of_embed() { + let dir = Folder::new("dcs-several"); + let placed = Cell::new(0); + let setup = |with: &'static str| { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + placed.set(placed.get() + 1); + let dcs = dir.file(&format!("plates-{}.eps", placed.get()), &dcs_file()); + let r = app.run("file.place", json!({"path": dcs, "at": [50, 50]})).unwrap(); + let dcs = r["ids"][0].as_u64().unwrap(); + let png = dir.file(&format!("other-{}.png", placed.get()), &png(30, 20)); + let other = match with { + "linked" => Some(app.run("file.place", json!({"path": png, "at": [200, 50]})).unwrap()["ids"][0].as_u64().unwrap()), + "embedded" => Some(app.run("file.place", json!({"path": png, "at": [200, 50], "link": false})).unwrap()["ids"][0].as_u64().unwrap()), + _ => None, + }; + let ids: Vec = [Some(dcs), other].into_iter().flatten().collect(); + app.run("select.set", json!({"ids": ids})).unwrap(); + (app, dcs, other) + }; + let alone = || setup("alone").0; + { + let app = alone(); + let st = app.session.active().unwrap(); + let id = st.selection.objects[0]; + assert!(matches!(st.doc.node(id).unwrap().kind, vectorcraft_doc::NodeKind::Image(_)), "placed as one image"); + } + for (surface, h) in image_surfaces(alone) { + assert!(h.shows("Edit Original") && !h.shows("Embed"), "{surface}: one DCS image"); + } + let ids = RefCell::new(vec![]); + let with_linked = || { + let (app, dcs, other) = setup("linked"); + ids.borrow_mut().push((dcs, other.unwrap())); + app + }; + // Each surface draws its own app, made in this order. + for (n, (surface, mut h)) in image_surfaces(with_linked).into_iter().enumerate() { + let (dcs, linked) = ids.borrow()[n]; + assert_eq!(selected(&h.app()).len(), 2, "{surface}"); + h.click("Embed"); + let mut app = h.app(); + assert!(is_embedded(&mut app, linked), "{surface}: the linked image is embedded"); + assert!(!is_embedded(&mut app, dcs), "{surface}: the DCS image stays linked"); + } + for (surface, h) in image_surfaces(|| setup("embedded").0) { + assert!(h.shows("Unembed…") && !h.shows("Embed"), "{surface}: a DCS image and an embedded one"); + } +} + /// Unembed… acts on several selected embedded images: it asks for one folder, writes a file for /// each image there and links each one to its file; with no folder picked, nothing changes. #[test] diff --git a/crates/ui-egui/src/place.rs b/crates/ui-egui/src/place.rs index cc8d35d82e..81c79d7f48 100644 --- a/crates/ui-egui/src/place.rs +++ b/crates/ui-egui/src/place.rs @@ -1095,9 +1095,14 @@ pub(crate) enum LinkAction { } impl LinkAction { - /// The actions that apply to an image, `linked` or embedded. - pub(crate) fn shown(linked: bool) -> &'static [LinkAction] { - if linked { &[LinkAction::Embed, LinkAction::EditOriginal] } else { &[LinkAction::Unembed] } + /// The actions that apply to an image, `linked` or embedded. An image placed from a DCS file + /// (`dcs`) stays linked, as `links.embed` refuses it, so it has no Embed. + pub(crate) fn shown(linked: bool, dcs: bool) -> &'static [LinkAction] { + match (linked, dcs) { + (true, false) => &[LinkAction::Embed, LinkAction::EditOriginal], + (true, true) => &[LinkAction::EditOriginal], + (false, _) => &[LinkAction::Unembed], + } } /// The action's label in the interface language. @@ -1119,11 +1124,19 @@ impl LinkAction { } } -/// Several selected objects that are all images: the ids of the linked ones and of the embedded -/// ones, in selection order. +/// Is image `id` of the active document placed from a DCS file (its pixels hold plates), which +/// `links.embed` refuses? +pub(crate) fn is_dcs_image(app: &VectorcraftApp, id: u64) -> bool { + let Some(st) = app.session.active() else { return false }; + let Some(NodeKind::Image(im)) = st.doc.node(vectorcraft_doc::NodeId(id)).map(|n| &n.kind) else { return false }; + im.link.is_some() && st.doc.images.get(&im.key).is_some_and(vectorcraft_doc::ImageBlob::is_separated) +} + +/// Several selected objects that are all images: the ids of the linked ones that can be embedded +/// and of the embedded ones, in selection order. Images placed from DCS files are in neither. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub(crate) struct SeveralImages { - /// The linked images. + /// The linked images, except those placed from DCS files. pub(crate) linked: Vec, /// The embedded images. pub(crate) embedded: Vec, @@ -1140,13 +1153,18 @@ impl SeveralImages { let mut out = SeveralImages::default(); for id in &st.selection.objects { let NodeKind::Image(im) = &st.doc.node(*id)?.kind else { return None }; - if im.link.is_some() { out.linked.push(id.0) } else { out.embedded.push(id.0) } + match &im.link { + None => out.embedded.push(id.0), + Some(_) if st.doc.images.get(&im.key).is_some_and(vectorcraft_doc::ImageBlob::is_separated) => {} + Some(_) => out.linked.push(id.0), + } } Some(out) } - /// The actions that apply to these images: Embed while one of them is linked, and Unembed… - /// while one is embedded and they can be written out. Edit Original opens one file, so the + /// The actions that apply to these images: Embed while one of them is linked and not placed + /// from a DCS file, and Unembed… while one is embedded and they can be written out. Edit + /// Original opens one file, so the /// surfaces offer it for one image only. pub(crate) fn shown(&self, app: &VectorcraftApp) -> Vec { let mut shown = vec![]; @@ -1234,7 +1252,7 @@ pub fn control_bar_details(app: &mut VectorcraftApp, ui: &mut egui::Ui) -> bool if app.session.tool_id() == vectorcraft_tools::cropimage::ID { return true; } - for action in LinkAction::shown(linked) { + for action in LinkAction::shown(linked, is_dcs_image(app, id)) { let at = if *action == LinkAction::EditOriginal { rank::DETAIL } else { rank::GROUP }; Row::new().fit_rank(at).show(ui, |ui, _| { if clicked(&Button::new(action.label().to_string()).show(ui)) {