mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-28 09:21:55 +01:00
Images: with several images selected, the Properties panel's Quick Actions and the Control bar offer Embed for the linked ones and Unembed… for the embedded ones, as they do for one image
This commit is contained in:
1 parent
98b56f3d97
commit
c472749a87
4 files changed
+144
-5
No files matched your search
@@ -903,7 +903,7 @@ fn break_link(app: &mut VectorcraftApp, ids: Vec<u64>) {
|
||||
}
|
||||
|
||||
/// Embed images `ids` (`links.embed`).
|
||||
fn embed_images(app: &mut VectorcraftApp, ids: Vec<u64>) {
|
||||
pub(crate) fn embed_images(app: &mut VectorcraftApp, ids: Vec<u64>) {
|
||||
let r = app.run("links.embed", json!({ "ids": ids }));
|
||||
report(app, r, |v| {
|
||||
let missing = v["missing"].as_array().map_or(0, Vec::len);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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<u64>,
|
||||
/// The embedded images.
|
||||
pub(crate) embedded: Vec<u64>,
|
||||
}
|
||||
|
||||
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<SeveralImages> {
|
||||
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<LinkAction> {
|
||||
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 };
|
||||
|
||||
Reference in new issue
Block a user