mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge #17976 into the merge queue batch
Frame from Layers: "Frame N", the layer's name on its content, "Add Frame to Layers" (#17329)
This commit is contained in:
commit
eb1819ffed
3 files changed
+104
-5
No files matched your search
@@ -161,10 +161,16 @@ fn frame_from_layers(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.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).
|
||||
// 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(|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();
|
||||
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 +181,9 @@ fn frame_from_layers(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
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 +430,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;
|
||||
@@ -0,0 +1,89 @@
|
||||
//! #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<String>) {
|
||||
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<String>) {
|
||||
(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 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();
|
||||
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"));
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user