From c472749a87fea457d3e7a7babbb154b678638f8b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 27 Nov 2026 13:11:29 -0800 Subject: [PATCH] =?UTF-8?q?Images:=20with=20several=20images=20selected,?= =?UTF-8?q?=20the=20Properties=20panel's=20Quick=20Actions=20and=20the=20C?= =?UTF-8?q?ontrol=20bar=20offer=20Embed=20for=20the=20linked=20ones=20and?= =?UTF-8?q?=20Unembed=E2=80=A6=20for=20the=20embedded=20ones,=20as=20they?= =?UTF-8?q?=20do=20for=20one=20image?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/ui-egui/src/panels/links.rs | 2 +- crates/ui-egui/src/panels/properties.rs | 9 +++ crates/ui-egui/src/panels/tests_links.rs | 59 ++++++++++++++++++ crates/ui-egui/src/place.rs | 79 ++++++++++++++++++++++-- 4 files changed, 144 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/panels/links.rs b/crates/ui-egui/src/panels/links.rs index d2765a0af9..a27be2773b 100644 --- a/crates/ui-egui/src/panels/links.rs +++ b/crates/ui-egui/src/panels/links.rs @@ -903,7 +903,7 @@ fn break_link(app: &mut VectorcraftApp, ids: Vec) { } /// Embed images `ids` (`links.embed`). -fn embed_images(app: &mut VectorcraftApp, ids: Vec) { +pub(crate) fn embed_images(app: &mut VectorcraftApp, ids: Vec) { let r = app.run("links.embed", json!({ "ids": ids })); report(app, r, |v| { let missing = v["missing"].as_array().map_or(0, Vec::len); diff --git a/crates/ui-egui/src/panels/properties.rs b/crates/ui-egui/src/panels/properties.rs index 201e2f909f..4fcefa0be3 100644 --- a/crates/ui-egui/src/panels/properties.rs +++ b/crates/ui-egui/src/panels/properties.rs @@ -156,7 +156,16 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui, first: Option<&Node>, n_sel: usiz } fn quick_action_buttons(app: &mut VectorcraftApp, ui: &mut Ui) { + // Several images and nothing else: Embed and Unembed… for them, first. + let images = crate::place::SeveralImages::selected(app); Grid::new(&HALVES).show(ui, |ui, _| { + if let Some(images) = &images { + for action in images.shown(app) { + if Button::new(action.label()).show(ui).clicked() { + images.run(app, action); + } + } + } for action in quick_actions(app) { Button::command(action.command).label(action.label).show(ui); } diff --git a/crates/ui-egui/src/panels/tests_links.rs b/crates/ui-egui/src/panels/tests_links.rs index 5c54fe7ef2..cb2555cda1 100644 --- a/crates/ui-egui/src/panels/tests_links.rs +++ b/crates/ui-egui/src/panels/tests_links.rs @@ -708,6 +708,65 @@ fn replace_image_with_a_desktop_path_remains_linked() { assert_eq!(image.link.as_ref().unwrap().path, path); } +/// The Properties panel and the Control bar, each drawn over `app` in a room wide enough for its +/// image controls (a narrow Control bar drops its lower ranks). +fn image_surfaces(app: impl Fn() -> VectorcraftApp) -> [(&'static str, Panel); 2] { + let mut bar = Panel::new(app(), crate::chrome::control_bar); + bar.room(vec2(2200.0, 200.0)).frame(); + [("Properties", Panel::new(app(), super::properties::show)), ("Control bar", bar)] +} + +/// Whether image `id` is embedded. +fn is_embedded(app: &mut VectorcraftApp, id: u64) -> bool { + app.run("links.info", json!({"id": id})).unwrap()["status"] == "embedded" +} + +/// With several linked images selected, the Properties panel's Quick Actions and the Control bar +/// offer Embed, which embeds every one of them in one undo step. +#[test] +fn embed_takes_several_selected_linked_images() { + let dir = Folder::new("embed-several"); + let placed = Cell::new(0); + let app = || { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + for i in 0..2 { + placed.set(placed.get() + 1); + let path = dir.file(&format!("linked-{}.png", placed.get()), &png(30, 20)); + app.run("file.place", json!({"path": path, "at": [50 + 100 * i, 50]})).unwrap(); + } + app.run("select.all", json!({})).unwrap(); + app + }; + for (surface, mut h) in image_surfaces(app) { + let ids = selected(&h.app()); + assert_eq!(ids.len(), 2, "{surface}"); + assert!(!h.shows("Unembed…"), "{surface}: no image is embedded"); + let undo = h.app().session.doc().unwrap().history.undo.len(); + h.click("Embed"); + let mut app = h.app(); + assert!(ids.iter().all(|id| is_embedded(&mut app, *id)), "{surface}: every selected image is embedded"); + assert_eq!(app.session.doc().unwrap().history.undo.len(), undo + 1, "{surface}: in one undo step"); + app.run("edit.undo", json!({})).unwrap(); + assert!(ids.iter().all(|id| !is_embedded(&mut app, *id)), "{surface}: one undo links them again"); + } +} + +/// With a linked and an embedded image selected, both surfaces offer Embed for the linked one and +/// Unembed… for the embedded one. +#[test] +fn several_mixed_images_offer_embed_and_unembed() { + let dir = Folder::new("mixed-several"); + let app = || { + let (mut app, linked, embedded) = app(&dir); + app.run("select.set", json!({"ids": [linked, embedded]})).unwrap(); + app + }; + for (surface, h) in image_surfaces(app) { + assert!(h.shows("Embed") && h.shows("Unembed…"), "{surface}"); + } +} + /// 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 8b8eeb94fa..cc8d35d82e 100644 --- a/crates/ui-egui/src/place.rs +++ b/crates/ui-egui/src/place.rs @@ -1112,22 +1112,93 @@ impl LinkAction { /// Runs the action for image `id`, named `name`. pub(crate) fn run(self, app: &mut VectorcraftApp, id: u64, name: &str) { match self { - LinkAction::Embed => { - app.run("links.embed", json!({ "ids": [id] })).ok(); - } + LinkAction::Embed => crate::panels::links::embed_images(app, vec![id]), LinkAction::EditOriginal => crate::menus::invoke(app, "links.editOriginal", json!({ "id": id })), LinkAction::Unembed => crate::panels::links::unembed(app, id, name), } } } +/// Several selected objects that are all images: the ids of the linked ones and of the embedded +/// ones, in selection order. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub(crate) struct SeveralImages { + /// The linked images. + pub(crate) linked: Vec, + /// The embedded images. + pub(crate) embedded: Vec, +} + +impl SeveralImages { + /// The selection as [`SeveralImages`], when two or more objects are selected and each is an + /// image. + pub(crate) fn selected(app: &VectorcraftApp) -> Option { + let st = app.session.active()?; + if st.selection.objects.len() < 2 { + return None; + } + 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) } + } + 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 + /// surfaces offer it for one image only. + pub(crate) fn shown(&self, app: &VectorcraftApp) -> Vec { + let mut shown = vec![]; + if !self.linked.is_empty() { + shown.push(LinkAction::Embed); + } + if crate::panels::links::can_unembed(app, self.embedded.len()) { + shown.push(LinkAction::Unembed); + } + shown + } + + /// Runs `action` on these images: Embed embeds every linked one, and Unembed… writes every + /// embedded one out, each in one undo step. + pub(crate) fn run(&self, app: &mut VectorcraftApp, action: LinkAction) { + match (action, &self.embedded[..]) { + (LinkAction::Embed, _) => crate::panels::links::embed_images(app, self.linked.clone()), + (LinkAction::EditOriginal, _) | (LinkAction::Unembed, []) => {} + (LinkAction::Unembed, [one]) => { + let info = app.session.execute("image.info", &json!({ "id": one })).unwrap_or_default(); + let (name, _) = image_summary(&info); + crate::panels::links::unembed(app, *one, &name); + } + (LinkAction::Unembed, ids) => crate::panels::links::unembed_several(app, ids.to_vec()), + } + } +} + +/// The Control bar for several selected images and nothing else: Embed and Unembed… for them +/// ([`SeveralImages::shown`]), each a ranked row. +fn control_bar_several(app: &mut VectorcraftApp, ui: &mut egui::Ui, images: &SeveralImages) { + for action in images.shown(app) { + Row::new().fit_rank(crate::chrome::rank::GROUP).show(ui, |ui, _| { + if clicked(&Button::new(action.label().to_string()).show(ui)) { + images.run(app, action); + } + }); + } +} + /// The Control bar for the one selected image: its file name, colour mode and effective /// resolution, Embed or Unembed, Edit Original (a linked file), Image Trace, Mask, Crop Image and /// Remove Background, each a ranked row that leaves a shrinking bar whole. While the Crop Image /// tool is in use, the bar shows the file name, colour mode and resolution only, and the tool's -/// cropping options follow them. Whether one image is selected. +/// cropping options follow them. For several selected images and nothing else, the bar shows +/// their Embed and Unembed… ([`SeveralImages`]). Whether one image is selected. pub fn control_bar_details(app: &mut VectorcraftApp, ui: &mut egui::Ui) -> bool { use crate::chrome::rank; + if let Some(images) = SeveralImages::selected(app) { + control_bar_several(app, ui, &images); + return false; + } let Some(i) = selected_image_info(app) else { return false }; let (name, details) = image_summary(i); let (Some(id), linked) = (i["id"].as_u64(), i["linked"] == true) else { return false };