Merge pull request #13497 from RickiNano/fix/13459-custom-shape-layer-numbering

Custom Shape tool: layers are numbered per shape, Heart 1, Heart 2 (#13459)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-15 15:46:41 +01:00
commit 0b4e40a522
4 files changed
+70 -4

No files matched your search

@@ -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");
}
+13 -2
View File
@@ -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<Value> {
}
};
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<CommandSpec> {
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,
+1 -1
View File
@@ -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.
+1 -1
View File
@@ -822,7 +822,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(_)));
}