From 0b96511b6c9344db41e3625c407c2f16498880d9 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Thu, 12 Nov 2026 16:17:09 +0100 Subject: [PATCH 1/3] Custom Shape tool: layers are numbered per shape, Heart 1, Heart 2 (#13459) `shape.presets.place` without a `name` now names the layer after the shape through `Document::next_layer_name`, so each shape counts on its own (Heart 1, Heart 2, then Star 1), as Rectangle N and Ellipse N do. An explicit `name` is still used exactly as given. A preset whose name already ends in a number keeps today's name, since that case was not measured. --- .../presets/custom_shape_layer_name_tests.rs | 55 +++++++++++++++++++ crates/engine/src/presets/shapes.rs | 15 ++++- crates/engine/src/presets/tests.rs | 2 +- 3 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 crates/engine/src/presets/custom_shape_layer_name_tests.rs diff --git a/crates/engine/src/presets/custom_shape_layer_name_tests.rs b/crates/engine/src/presets/custom_shape_layer_name_tests.rs new file mode 100644 index 0000000000..a0b43b7c58 --- /dev/null +++ b/crates/engine/src/presets/custom_shape_layer_name_tests.rs @@ -0,0 +1,55 @@ +//! `shape.presets.place` names its layer after the shape with a number, each shape counting on its +//! own (#13459). + +use serde_json::{Value, json}; + +use crate::Session; +use photocraft_doc::LayerId; + +fn session() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 640, "height": 480})).unwrap(); + s +} + +fn place(s: &mut Session, p: Value) -> String { + let r = s.execute("shape.presets.place", p).unwrap(); + let id = LayerId(r["layer"].as_u64().unwrap()); + s.active().unwrap().doc.layer(id).unwrap().name.clone() +} + +fn drag(s: &mut Session, preset: &str) -> String { + place(s, json!({"preset": preset, "rect": [400, 300, 200, 100], "keepAspect": false})) +} + +#[test] +fn each_shape_counts_its_own_layers() { + let mut s = session(); + assert_eq!(drag(&mut s, "Heart"), "Heart 1"); + assert_eq!(drag(&mut s, "Heart"), "Heart 2"); + assert_eq!(drag(&mut s, "Star"), "Star 1"); + assert_eq!(drag(&mut s, "Heart"), "Heart 3"); +} + +#[test] +fn numbering_skips_names_already_taken() { + let mut s = session(); + s.execute("layer.new.layer", json!({"name": "Heart 1"})).unwrap(); + assert_eq!(drag(&mut s, "Heart"), "Heart 2"); +} + +#[test] +fn an_explicit_name_is_used_as_given() { + let mut s = session(); + assert_eq!(place(&mut s, json!({"preset": "Heart", "name": "Valentine"})), "Valentine"); + assert_eq!(place(&mut s, json!({"preset": "Heart", "name": "Heart"})), "Heart"); + assert_eq!(drag(&mut s, "Heart"), "Heart 1"); +} + +#[test] +fn a_preset_name_ending_in_a_number_keeps_todays_name() { + let mut s = session(); + s.execute("shape.create", json!({"kind": "star", "rect": [0, 0, 30, 30]})).unwrap(); + s.execute("edit.defineCustomShape", json!({"name": "Badge 2"})).unwrap(); + assert_eq!(drag(&mut s, "Badge 2"), "Badge 2"); +} diff --git a/crates/engine/src/presets/shapes.rs b/crates/engine/src/presets/shapes.rs index 821a69a083..ecd0ebf0c3 100644 --- a/crates/engine/src/presets/shapes.rs +++ b/crates/engine/src/presets/shapes.rs @@ -18,6 +18,10 @@ use crate::{EngineError, Result, Session}; mod csh; +#[cfg(test)] +#[path = "custom_shape_layer_name_tests.rs"] +mod custom_shape_layer_name_tests; + /// Group listing Edit › Define Custom Shape results (`EditState::custom_shapes`). pub const CUSTOM_GROUP: &str = "Custom Shapes"; @@ -382,7 +386,14 @@ fn place(s: &mut Session, p: &Value) -> Result { } }; let path = fit(&shape.path, rect, keep); - let mut q = json!({"kind": "path", "path": path_json(&path), "name": str_param(p, "name").unwrap_or(&shape.name)}); + // Layers are numbered per shape (Heart 1, Heart 2, Star 1), as Rectangle N / Ellipse N are. + // A preset name already ending in a number is used as is: that case wasn't measured. + let name = match str_param(p, "name") { + Some(n) => n.to_string(), + None if shape.name.rsplit_once(' ').is_some_and(|(_, n)| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit())) => shape.name.clone(), + None => d.doc.next_layer_name(&shape.name), + }; + let mut q = json!({"kind": "path", "path": path_json(&path), "name": name}); for k in ["fill", "stroke", "addTo", "op"] { if let Some(v) = p.get(k) { q[k] = v.clone(); @@ -485,7 +496,7 @@ pub fn specs() -> Vec { label: "Place Custom Shape", menu: &[], shortcut: None, - params: r##"{"preset":name,"group":name?,"rect":[x,y,w,h]? (default: centred, half the canvas),"keepAspect":bool=true,"fill":…?=foreground,"stroke":…?,"name":str?,"addTo":layerId?,"op":"combine|subtract|intersect|exclude"?} → shape.info (Custom Shape tool / Shapes panel; fill and stroke as in shape.create)"##, + params: r##"{"preset":name,"group":name?,"rect":[x,y,w,h]? (default: centred, half the canvas),"keepAspect":bool=true,"fill":…?=foreground,"stroke":…?,"name":str? (default: the shape's name numbered, "Heart 1", "Heart 2"),"addTo":layerId?,"op":"combine|subtract|intersect|exclude"?} → shape.info (Custom Shape tool / Shapes panel; fill and stroke as in shape.create)"##, enabled: has_doc, run: place, journal: true, diff --git a/crates/engine/src/presets/tests.rs b/crates/engine/src/presets/tests.rs index 9b64b756d8..94828debbd 100644 --- a/crates/engine/src/presets/tests.rs +++ b/crates/engine/src/presets/tests.rs @@ -542,7 +542,7 @@ fn shapes_place_as_shape_layers() { let r = s.execute("shape.presets.place", json!({"preset": "Heart", "rect": [10, 10, 40, 20], "fill": "#ff0000"})).unwrap(); let id = LayerId(r["layer"].as_u64().unwrap()); let l = s.active().unwrap().doc.layer(id).unwrap().clone(); - assert_eq!(l.name, "Heart"); + assert_eq!(l.name, "Heart 1"); let LayerContent::Shape(sh) = &l.content else { panic!("not a shape layer") }; let (x0, y0, x1, y1) = sh.path.control_bounds().unwrap(); // Keep aspect: 20 px tall, centred horizontally in the 40 px rect. From 6badf960b655a8e8d7228e542d50feecccc42832 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:01:28 +0100 Subject: [PATCH 2/3] Custom Shape tool test: the placed Star layer is named Star 1 (#13459) --- crates/ui-egui/src/panels/preset_panels.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ui-egui/src/panels/preset_panels.rs b/crates/ui-egui/src/panels/preset_panels.rs index 29b5d85a95..3d0865a091 100644 --- a/crates/ui-egui/src/panels/preset_panels.rs +++ b/crates/ui-egui/src/panels/preset_panels.rs @@ -815,7 +815,7 @@ mod tests { finish_custom_shape(&mut app, [4.0, 4.0, 20.0, 20.0], true, json!("#ff0000"), Value::Null); let d = app.session.active().unwrap(); let l = d.doc.layer(d.active_layer.unwrap()).unwrap(); - assert_eq!(l.name, "Star"); + assert_eq!(l.name, "Star 1"); assert!(matches!(l.content, photocraft_doc::LayerContent::Shape(_))); } From c508b6233c4b5917b90d873e1c4c01e6111fc946 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:56:27 +0100 Subject: [PATCH 3/3] Re-run CI: type_tool panels_toggle_tests failed on the font-load flake (#13408)