Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(selection): load shape and path thumbnails with Ctrl-click (#3449)

kotulsky51c671c5a021
diff --git a/crates/engine/src/channel_cmds.rs b/crates/engine/src/channel_cmds.rs
index f5f9dd710..310d7ab2e 100644
--- a/crates/engine/src/channel_cmds.rs
+++ b/crates/engine/src/channel_cmds.rs
@@ -359,8 +359,19 @@ fn ref_planes(doc: &Document, layer: Option<LayerId>, active: Option<LayerId>, r
s.read_rgba_into(area, &mut px);
vec![px.iter().map(|p| p[3]).collect()]
}
- // Adjustment and fill layers cover the whole canvas.
- None => vec![vec![1.0; area.width() as usize * area.height() as usize]],
+ // A shape can have no pixel cache yet (for example after loading a document).
+ // Use its existing renderer rather than treating it as a canvas-wide fill.
+ None => match &l.content {
+ LayerContent::Shape(shape) => vec![
+ photocraft_vector::CompiledShape::new(shape, photocraft_vector::DEFAULT_TOLERANCE, area)
+ .render_rgba(area)
+ .into_iter()
+ .map(|p| p[3])
+ .collect(),
+ ],
+ // Adjustment and fill layers cover the whole canvas.
+ _ => vec![vec![1.0; area.width() as usize * area.height() as usize]],
+ },
}
}
ChanRef::LayerMask => {
diff --git a/crates/engine/src/channel_cmds/tests.rs b/crates/engine/src/channel_cmds/tests.rs
index 16b2b4294..1223c0589 100644
--- a/crates/engine/src/channel_cmds/tests.rs
+++ b/crates/engine/src/channel_cmds/tests.rs
@@ -740,3 +740,27 @@ fn clear_with_an_alpha_channel_or_quick_mask_targeted_fills_it_not_the_layer() {
assert_eq!(q, 0.0);
assert_eq!(px(&s, 5, 5), layer, "the layer is untouched in Quick Mask mode");
}
+
+#[test]
+fn thumbnail_selection_shape_without_pixel_cache_uses_its_transparency() {
+ for depth in [8, 16, 32] {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width":100,"height":80,"depth":depth,"background":"transparent"})).unwrap();
+ let id = s.execute("shape.create", json!({"kind":"rect","rect":[20,10,40,30],"fill":"#cc3333"})).unwrap()["layer"].as_u64().unwrap();
+ s.edit("Drop render cache", |doc, _| {
+ let l = doc.layer_mut(photocraft_doc::LayerId(id)).unwrap();
+ if let LayerContent::Shape(shape) = &mut l.content {
+ shape.cache = None;
+ }
+ l.visible = false;
+ l.opacity = 0.25;
+ Ok(())
+ })
+ .unwrap();
+ s.execute("select.loadSelection", json!({"channel":"transparency","layer":id})).unwrap();
+ assert!(sel_at(&s, 30, 20) > 0.99);
+ assert!(sel_at(&s, 0, 0) < 0.01, "shape transparency must not select the whole canvas at depth {depth}");
+ let b = s.active().unwrap().doc.selection.as_ref().unwrap().content_bounds();
+ assert_eq!((b.x0, b.y0, b.width(), b.height()), (20, 10, 40, 30));
+ }
+}
diff --git a/crates/ui-egui/src/path_selection_tests.rs b/crates/ui-egui/src/path_selection_tests.rs
index 55042703e..89dcd49c5 100644
--- a/crates/ui-egui/src/path_selection_tests.rs
+++ b/crates/ui-egui/src/path_selection_tests.rs
@@ -4,6 +4,7 @@
use egui::{Event, Key, Modifiers, vec2};
use egui_kittest::Harness;
+use egui_kittest::kittest::Queryable;
use serde_json::json;
use crate::PhotocraftApp;
@@ -121,3 +122,108 @@ fn without_a_path_it_says_why_and_the_key_can_be_rebound() {
press(&mut h, Key::Y, m);
assert_eq!(selection(&h), Some([10, 10, 20, 20]), "{}", h.state().ui.status);
}
+
+fn panel(h: &mut Harness<'_, PhotocraftApp>, tab: usize) {
+ let ctx = h.ctx.clone();
+ let (req, _) = crate::control::ControlRequest::new(
+ "ui.set",
+ json!({"theme":"promedium", "dockTabs":{"layers":tab}, "dock":{"collapsed":["color","properties","history","navigator"]}}),
+ );
+ crate::control::handle(h.state_mut(), &ctx, &req);
+ h.run_steps(4);
+}
+
+fn click(h: &mut Harness<'_, PhotocraftApp>, at: egui::Pos2, modifiers: Modifiers) {
+ h.hover_at(at);
+ h.event(Event::ModifiersChanged(modifiers));
+ h.run_steps(1);
+ h.event(Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: true, modifiers });
+ h.run_steps(1);
+ h.event(Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: false, modifiers });
+ h.run_steps(1);
+ h.event(Event::ModifiersChanged(Modifiers::NONE));
+ h.run_steps(2);
+}
+
+#[test]
+fn thumbnail_selection_ctrl_click_shape_keeps_active_layer_and_supports_undo() {
+ let mut h = harness();
+ let shape =
+ h.state_mut().run("shape.create", json!({"kind":"rect","rect":[40,20,60,40],"fill":"#cc3333","name":"Badge"})).unwrap()["layer"].as_u64().unwrap();
+ let active = h.state_mut().run("layer.new.layer", json!({"name":"Other"})).unwrap()["layer"].as_u64().unwrap();
+ panel(&mut h, 0);
+ let row = crate::layer_row_ui::recorded(&h.ctx).into_iter().find(|r| r.layer == shape).unwrap();
+ click(&mut h, row.name.unwrap().center(), command());
+ assert!(selection(&h).is_none(), "Ctrl-click on a shape's name only selects the layer");
+ h.state_mut().run("layer.select", json!({"layer": active})).unwrap();
+ h.run_steps(2);
+ click(&mut h, egui::pos2(row.row.left() + 46.0, row.row.center().y), command());
+ assert_eq!(selection(&h), Some([40, 20, 60, 40]), "{}", h.state().ui.status);
+ assert_eq!(h.state().session.active().unwrap().active_layer, Some(photocraft_doc::LayerId(active)));
+ h.state_mut().run("edit.undo", json!({})).unwrap();
+ assert!(selection(&h).is_none());
+ h.state_mut().run("edit.redo", json!({})).unwrap();
+ assert_eq!(selection(&h), Some([40, 20, 60, 40]));
+}
+
+#[test]
+fn thumbnail_selection_ctrl_click_paths_targets_thumbnail_and_clicked_path() {
+ let mut h = harness();
+ h.state_mut().run("path.set", json!({"name":"work","path":rect_path(10.0,10.0,50.0,40.0)})).unwrap();
+ h.state_mut().run("path.set", json!({"name":"Saved","path":rect_path(100.0,40.0,60.0,50.0)})).unwrap();
+ h.state_mut().ui.selected_path = Some("Saved".into());
+ panel(&mut h, 2);
+ let row = h.get_by_label("Work Path").rect();
+ click(&mut h, egui::pos2(row.left() + 70.0, row.center().y), command());
+ assert!(selection(&h).is_none(), "Ctrl-click on the name only selects the path row");
+ h.state_mut().ui.selected_path = Some("Saved".into());
+ h.run_steps(2);
+ click(&mut h, egui::pos2(row.left() + 22.0, row.center().y), command());
+ assert_eq!(selection(&h), Some([10, 10, 50, 40]), "{}", h.state().ui.status);
+ assert_eq!(h.state().ui.selected_path.as_deref(), Some("Saved"), "loading a thumbnail does not change the selected path");
+ h.state_mut().run("edit.undo", json!({})).unwrap();
+ assert!(selection(&h).is_none());
+ h.state_mut().run("edit.redo", json!({})).unwrap();
+ assert_eq!(selection(&h), Some([10, 10, 50, 40]));
+ h.state_mut().run("select.deselect", json!({})).unwrap();
+ let saved = h.get_by_label("Saved").rect();
+ click(&mut h, egui::pos2(saved.left() + 22.0, saved.center().y), command());
+ assert_eq!(selection(&h), Some([100, 40, 60, 50]));
+}
+
+#[test]
+fn thumbnail_selection_ctrl_click_paths_combines_with_shift_and_alt() {
+ let mut h = harness();
+ h.state_mut().run("path.set", json!({"name":"work","path":rect_path(10.0,10.0,50.0,40.0)})).unwrap();
+ panel(&mut h, 2);
+ let row = h.get_by_label("Work Path").rect();
+ let at = egui::pos2(row.left() + 22.0, row.center().y);
+ h.state_mut().run("select.rect", json!({"x":100,"y":40,"width":20,"height":20})).unwrap();
+ click(&mut h, at, command() | Modifiers::SHIFT);
+ let selected = h.state().session.active().unwrap().doc.selection.as_ref().unwrap();
+ assert!(selected.sample_channel(20, 20, 0) > 0.99 && selected.sample_channel(110, 50, 0) > 0.99);
+ h.state_mut().run("select.all", json!({})).unwrap();
+ click(&mut h, at, command() | Modifiers::ALT);
+ let selected = h.state().session.active().unwrap().doc.selection.as_ref().unwrap();
+ assert!(selected.sample_channel(20, 20, 0) < 0.01 && selected.sample_channel(110, 50, 0) > 0.99);
+ h.state_mut().run("select.rect", json!({"x":30,"y":0,"width":100,"height":100})).unwrap();
+ click(&mut h, at, command() | Modifiers::SHIFT | Modifiers::ALT);
+ assert_eq!(selection(&h), Some([30, 10, 30, 40]));
+}
+
+#[test]
+fn thumbnail_selection_ctrl_click_layer_path_preserves_panel_target() {
+ let mut h = harness();
+ h.state_mut().run("shape.create", json!({"kind":"rect","rect":[40,20,60,40],"fill":"#cc3333","name":"Badge"})).unwrap();
+ h.state_mut().run("path.set", json!({"name":"work","path":rect_path(0.0,0.0,10.0,10.0)})).unwrap();
+ h.state_mut().ui.selected_path = Some("work".into());
+ panel(&mut h, 2);
+ let row = h.get_by_label("Badge Shape Path").rect();
+ click(&mut h, egui::pos2(row.left() + 22.0, row.center().y), command());
+ assert_eq!(selection(&h), Some([40, 20, 60, 40]));
+ assert_eq!(h.state().ui.selected_path.as_deref(), Some("work"));
+ assert!(!h.state().ui.mask_target && !h.state().ui.vector_mask_target);
+ if let Ok(path) = std::env::var("PHOTOCRAFT_THUMBNAIL_SELECTION_SCREENSHOT") {
+ h.render().unwrap().save(path).unwrap();
+ }
+}
diff --git a/crates/ui-egui/src/vector_ui.rs b/crates/ui-egui/src/vector_ui.rs
index 4925e06bb..6baad29ad 100644
--- a/crates/ui-egui/src/vector_ui.rs
+++ b/crates/ui-egui/src/vector_ui.rs
@@ -1468,7 +1468,8 @@ pub fn paths_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
} else if resp.hovered() {
ui.painter().rect_filled(r, 0.0, t.hover.gamma_multiply(0.5));
}
- thumb(ui, Rect::from_min_size(pos2(r.left() + 8.0, r.top() + 4.0), vec2(28.0, 28.0)), path, &doc);
+ let thumbnail = Rect::from_min_size(pos2(r.left() + 8.0, r.top() + 4.0), vec2(28.0, 28.0));
+ thumb(ui, thumbnail, path, &doc);
let mut job = egui::text::LayoutJob::default();
// Temporary paths (the work path, the selected layer's shape path or vector mask) are italic.
let italics = *kind != PathRow::Saved;
@@ -1479,11 +1480,20 @@ pub fn paths_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
);
let g = ui.painter().layout_job(job);
ui.painter().galley(pos2(r.left() + 46.0, r.center().y - g.size().y / 2.0), g, t.text);
- if resp.clicked() {
+ // Ctrl/Cmd-click loads only the thumbnail's path, without changing the panel target.
+ let modifiers = ui.input(|i| i.modifiers);
+ let load = resp.clicked() && modifiers.command && resp.interact_pointer_pos().is_some_and(|p| thumbnail.contains(p));
+ if load {
+ let mode = match crate::channels_panel::load_operation(modifiers) {
+ "new" => "replace",
+ mode => mode,
+ };
+ action = Some(("path.toSelection", json!({"name": key, "mode": mode})));
+ } else if resp.clicked() {
app.ui.selected_path = Some(key.clone());
}
// Double-clicking the Work Path saves it (Photoshop's "Save Path").
- if resp.double_clicked() && *kind == PathRow::Work {
+ if !load && resp.double_clicked() && *kind == PathRow::Work {
let n = doc.paths.len() + 1;
action = Some(("path.rename", json!({"name": "work", "to": format!("Path {n}")})));
}
diff --git a/docs/ui-design.md b/docs/ui-design.md
index 183670932..f3193b268 100644
--- a/docs/ui-design.md
+++ b/docs/ui-design.md
@@ -55,6 +55,7 @@ curl -sfL -o assets/icons/<name>.svg https://raw.githubusercontent.com/lucide-ic
| Quick layer pick | `quick_pick.rs` | macOS: ⌘⌥⌃-click with any tool selects the topmost visible layer with pixels under the pointer (`layer.pickAt`) without switching tools; the drag and release are swallowed. Windows/Linux have no third modifier distinct from the ⌃⌥ brush resize, so the gesture is a no-op there (use the Move tool's Auto-Select). A failed pick is a status-bar error. |
| Layers rows | `panels.rs` | Double-click: on the name renames in place; on the Background makes it a normal layer; on an adjustment or fill thumbnail opens its Properties; on a Smart Object thumbnail opens its contents (Edit Contents); anywhere else on the row opens Layer Style. |
| Layer masks | `panels.rs` | Clicking the mask thumbnail targets the mask (corner-bracket frame; the tab reads "Layer, Layer Mask/8"). Brush, eraser (paints background colour), gradient, bucket and moving the selected pixels (⌘-drag, `select.float`; the hole takes the background colour) then send `"target": "mask"`. Adjustment and fill layers target their mask automatically. |
+| Path thumbnails | `vector_ui.rs` | Ctrl/Cmd-click the thumbnail of a Work Path, saved path or layer path to load it as a selection; Shift adds, Alt subtracts, Shift+Alt intersects. Clicking the row name only selects the path. |
| Levels / Curves | `tone.rs` | Histogram of the image *below* the adjustment. Curves: click to add a point, drag out to delete. Every change is a coalesced `layer.setAdjustment`, so one drag = one undo step and the canvas updates at full resolution on the GPU. |
| Shape Stroke Options | `shape_stroke_ui.rs`, `vector_ui.rs` | Direct editing: valid changes apply immediately, with one numeric drag per undo step in Properties. Escape/outside click closes the popup and retains changes; a child dropdown keeps the parent open. The options bar sets new-shape defaults; Properties edits the selected shape. |