From 23d1a505aab58020466dfc752e5e7defdf447448 Mon Sep 17 00:00:00 2001 From: dholzric Date: Sun, 29 Nov 2026 11:29:34 -0600 Subject: [PATCH 1/2] Frame from Layers: "Frame N", the layer's name on its content, "Add Frame to Layers" (#17329) Frame from Layers named the frame "Group N", gave its smart object the same name and recorded the step as "Frame from Layers". As in the reference application, the frame is now the next "Frame N" unless a name is given, the smart object keeps the name of the upper layer it holds, and the History state is "Add Frame to Layers". Co-Authored-By: Claude Fable 5.2 --- crates/engine-core/src/cmd/frame_cmds.rs | 15 +++- .../cmd/frame_cmds/from_layers_names_tests.rs | 76 +++++++++++++++++++ .../engine-core/src/cmd/frame_cmds/tests.rs | 4 +- 3 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 crates/engine-core/src/cmd/frame_cmds/from_layers_names_tests.rs diff --git a/crates/engine-core/src/cmd/frame_cmds.rs b/crates/engine-core/src/cmd/frame_cmds.rs index 5850fce2ef..cf91c38b67 100644 --- a/crates/engine-core/src/cmd/frame_cmds.rs +++ b/crates/engine-core/src/cmd/frame_cmds.rs @@ -161,10 +161,15 @@ fn frame_from_layers(s: &mut Session, p: &Value) -> Result { .filter(|b| !b.is_empty()) .unwrap_or(st.doc.bounds()); let f = geometry(&json!({"x":bounds.x0,"y":bounds.y0,"width":bounds.width(),"height":bounds.height()}))?; + // The frame is the next "Frame N", as an empty frame is, unless the caller names it (#17329). + let mut grouping = p.clone(); + if p.get("name").is_none_or(Value::is_null) { + grouping["name"] = json!(st.doc.next_layer_name("Frame")); + } let journal = s.journal.len(); - let grouped = s.execute("layer.new.groupFromLayers", p.clone())?; + let grouped = s.execute("layer.new.groupFromLayers", grouping)?; let id = LayerId(grouped.get("layer").and_then(Value::as_u64).ok_or_else(|| bad("grouping returned no layer"))?); - let r = s.edit("Frame from Layers", |doc, _| { + let r = s.edit("Add Frame to Layers", |doc, _| { let group = doc.layer(id).ok_or(EngineError::NoLayer(id))?; let mut children = group.children().ok_or_else(|| bad("not a group"))?.to_vec(); // A frame shows what it frames: when every layer it takes in is hidden, show them so the @@ -175,7 +180,9 @@ fn frame_from_layers(s: &mut Session, p: &Value) -> Result { child.visible = true; } } - let image = crate::smart_cmds::layers_to_smart(doc, &group.name, children)?; + // The image keeps the name of the upper layer it holds, not the frame's (#17329). + let name = children.last().map_or_else(|| group.name.clone(), |upper| upper.name.clone()); + let image = crate::smart_cmds::layers_to_smart(doc, &name, children)?; let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; if let Some(children) = l.children_mut() { *children = vec![image]; @@ -422,6 +429,8 @@ mod convert_resize_tests; #[cfg(test)] mod from_layers_hidden_tests; #[cfg(test)] +mod from_layers_names_tests; +#[cfg(test)] mod mode_tests; #[cfg(test)] mod tests; diff --git a/crates/engine-core/src/cmd/frame_cmds/from_layers_names_tests.rs b/crates/engine-core/src/cmd/frame_cmds/from_layers_names_tests.rs new file mode 100644 index 0000000000..f5e7f760de --- /dev/null +++ b/crates/engine-core/src/cmd/frame_cmds/from_layers_names_tests.rs @@ -0,0 +1,76 @@ +//! #17329: Frame from Layers names the frame "Frame N", its smart object keeps the name of the +//! upper layer it holds, and the step is "Add Frame to Layers", as the reference application +//! does. An explicit `name` still names the frame. + +use super::*; +use photocraft_doc::Layer; +use photocraft_geom::Rect; + +/// The issue's document, cut down to what the names need: 600 x 400 px with the pixel layers Red +/// (100 x 80 at 50,50) and, above it, Blue. +fn session() -> (Session, LayerId, LayerId) { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 600, "height": 400})).unwrap(); + let (red, blue) = s + .edit("setup", |doc, _| { + let fmt = doc.pixel_format(); + let mut red = Layer::raster("Red", fmt); + red.surface_mut().unwrap().fill_rect(Rect::new(50, 50, 150, 130), &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 0.0, 1.0])); + let mut blue = Layer::raster("Blue", fmt); + blue.surface_mut().unwrap().fill_rect(Rect::new(300, 200, 420, 260), &photocraft_raster::from_rgba(&fmt, [0.0, 0.0, 1.0, 1.0])); + let (r, b) = (red.id, blue.id); + doc.layers.push(red); + doc.layers.push(blue); + Ok((r, b)) + }) + .unwrap(); + (s, red, blue) +} + +/// Selects `layers` in that order, runs Frame from Layers and returns the frame's name and the +/// names of what it holds. +fn frame_from(s: &mut Session, layers: &[LayerId]) -> (String, Vec) { + for (i, id) in layers.iter().enumerate() { + s.execute("layer.select", json!({"layer": id.0, "mode": if i == 0 { "replace" } else { "add" }})).unwrap(); + } + let r = s.execute("layer.new.frameFromLayers", json!({})).unwrap(); + let g = s.active().unwrap().doc.layer(LayerId(r["layer"].as_u64().unwrap())).unwrap(); + (g.name.clone(), g.children().unwrap().iter().map(|l| l.name.clone()).collect()) +} + +fn named(frame: &str, content: &str) -> (String, Vec) { + (frame.to_string(), vec![content.to_string()]) +} + +#[test] +fn frames_are_numbered_and_the_content_keeps_the_layers_name() { + let (mut s, red, blue) = session(); + assert_eq!(frame_from(&mut s, &[red]), named("Frame 1", "Red")); + assert_eq!(frame_from(&mut s, &[blue]), named("Frame 2", "Blue")); +} + +#[test] +fn several_layers_give_one_content_named_after_the_upper_layer() { + let (mut s, red, blue) = session(); + // Selected top first: the name follows the stack, not the order of selection. + assert_eq!(frame_from(&mut s, &[blue, red]), named("Frame 1", "Blue")); +} + +#[test] +fn convert_to_frame_does_not_take_a_frame_number() { + let (mut s, red, _) = session(); + let rect = s.execute("shape.create", json!({"kind": "rect", "rect": [200, 60, 80, 60], "fill": "#e07020"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("layer.frame.convert", json!({"layer": rect})).unwrap(); + assert_eq!(frame_from(&mut s, &[red]), named("Frame 1", "Red")); +} + +#[test] +fn the_step_is_add_frame_to_layers_and_a_given_name_wins() { + let (mut s, red, _) = session(); + let before = s.active().unwrap().history.entries().len(); + let r = s.execute("layer.new.frameFromLayers", json!({"layer": red.0, "name": "Hero"})).unwrap(); + let st = s.active().unwrap(); + assert_eq!(st.history.entries()[before..], ["Add Frame to Layers"]); + let g = st.doc.layer(LayerId(r["layer"].as_u64().unwrap())).unwrap(); + assert_eq!((g.name.as_str(), g.children().unwrap()[0].name.as_str()), ("Hero", "Red")); +} diff --git a/crates/engine-core/src/cmd/frame_cmds/tests.rs b/crates/engine-core/src/cmd/frame_cmds/tests.rs index a09ada1282..f3ecb45dc4 100644 --- a/crates/engine-core/src/cmd/frame_cmds/tests.rs +++ b/crates/engine-core/src/cmd/frame_cmds/tests.rs @@ -37,7 +37,7 @@ fn frames_selected_layers_into_a_masked_group() { assert!(matches!(g.content, LayerContent::Group(_)), "it's a group"); assert!(g.frame.is_some() && g.vector_mask.is_some(), "the group has a clip mask"); // Undoable (last step is the frame). - assert_eq!(st.history.entries().last().map(|e| e.as_str()), Some("Frame from Layers")); + assert_eq!(st.history.entries().last().map(|e| e.as_str()), Some("Add Frame to Layers")); } #[test] @@ -51,7 +51,7 @@ fn frame_from_layers_is_one_history_step() { s.execute("layer.select", json!({"layer": b.0, "mode": "add"})).unwrap(); let gid = LayerId(s.execute("layer.new.frameFromLayers", json!({})).unwrap()["layer"].as_u64().unwrap()); let mut framed = entries.clone(); - framed.push("Frame from Layers".into()); + framed.push("Add Frame to Layers".into()); assert_eq!(s.active().unwrap().history.entries(), framed); assert!(s.undo()); assert_eq!(s.active().unwrap().history.entries(), entries); From 35668afe6963fcb6aae0efd5404bd5de78a1e7cd Mon Sep 17 00:00:00 2001 From: dholzric Date: Sun, 29 Nov 2026 15:22:51 -0600 Subject: [PATCH 2/2] Frame from Layers: a blank name takes "Frame N" too (#17329) A `name` of "" or spaces passed the guard and reached the grouping, which reads a blank name as none and named the frame "Group N". Treat a blank name as absent here as well. A name that is not text is still forwarded, so the grouping refuses it as before. Co-Authored-By: Claude Fable 5.2 --- crates/engine-core/src/cmd/frame_cmds.rs | 3 ++- .../src/cmd/frame_cmds/from_layers_names_tests.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/engine-core/src/cmd/frame_cmds.rs b/crates/engine-core/src/cmd/frame_cmds.rs index cf91c38b67..3be1a9f73d 100644 --- a/crates/engine-core/src/cmd/frame_cmds.rs +++ b/crates/engine-core/src/cmd/frame_cmds.rs @@ -162,8 +162,9 @@ fn frame_from_layers(s: &mut Session, p: &Value) -> Result { .unwrap_or(st.doc.bounds()); let f = geometry(&json!({"x":bounds.x0,"y":bounds.y0,"width":bounds.width(),"height":bounds.height()}))?; // The frame is the next "Frame N", as an empty frame is, unless the caller names it (#17329). + // A blank name is no name, as it is to the grouping; one that is not text is left for it to refuse. let mut grouping = p.clone(); - if p.get("name").is_none_or(Value::is_null) { + if p.get("name").is_none_or(|n| n.is_null() || n.as_str().is_some_and(|n| n.trim().is_empty())) { grouping["name"] = json!(st.doc.next_layer_name("Frame")); } let journal = s.journal.len(); diff --git a/crates/engine-core/src/cmd/frame_cmds/from_layers_names_tests.rs b/crates/engine-core/src/cmd/frame_cmds/from_layers_names_tests.rs index f5e7f760de..c2e2553ebf 100644 --- a/crates/engine-core/src/cmd/frame_cmds/from_layers_names_tests.rs +++ b/crates/engine-core/src/cmd/frame_cmds/from_layers_names_tests.rs @@ -64,6 +64,19 @@ fn convert_to_frame_does_not_take_a_frame_number() { assert_eq!(frame_from(&mut s, &[red]), named("Frame 1", "Red")); } +#[test] +fn a_blank_name_takes_the_frame_number_as_no_name_does() { + for blank in [json!(""), json!(" "), Value::Null] { + let (mut s, red, _) = session(); + let r = s.execute("layer.new.frameFromLayers", json!({"layer": red.0, "name": blank})).unwrap(); + let g = s.active().unwrap().doc.layer(LayerId(r["layer"].as_u64().unwrap())).unwrap(); + assert_eq!(g.name, "Frame 1", "name {blank:?}"); + } + // A name that is not text is still refused, not replaced. + let (mut s, red, _) = session(); + assert!(s.execute("layer.new.frameFromLayers", json!({"layer": red.0, "name": 7})).is_err()); +} + #[test] fn the_step_is_add_frame_to_layers_and_a_given_name_wins() { let (mut s, red, _) = session();