mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
fix(engine): Frame from Layers frames a hidden layer (#17330)
The frame renders the layers it takes in as visible when every one of them is hidden, so a hidden layer becomes the frame's smart object instead of failing with "the layer is empty, so there is nothing to convert". A selection that also holds a visible layer keeps its hidden layers hidden, as Convert to Smart Object does. A locked layer keeps working and its content comes out unlocked, as the reference does. Co-Authored-By: Codebuff <[email protected]>
This commit is contained in:
1 parent
7199576a93
commit
d973ce2bf6
2 files changed
+104
-1
No files matched your search
@@ -166,7 +166,15 @@ fn frame_from_layers(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
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 group = doc.layer(id).ok_or(EngineError::NoLayer(id))?;
|
||||
let children = group.children().ok_or_else(|| bad("not a group"))?.to_vec();
|
||||
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
|
||||
// smart object renders, as the reference does (#17330). A selection that also holds a
|
||||
// visible layer keeps its hidden layers hidden, as Convert to Smart Object does.
|
||||
if children.iter().all(|child| !child.visible) {
|
||||
for child in &mut children {
|
||||
child.visible = true;
|
||||
}
|
||||
}
|
||||
let image = crate::smart_cmds::layers_to_smart(doc, &group.name, children)?;
|
||||
let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;
|
||||
if let Some(children) = l.children_mut() {
|
||||
@@ -407,6 +415,8 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
#[cfg(test)]
|
||||
mod content_name_tests;
|
||||
#[cfg(test)]
|
||||
mod from_layers_hidden_tests;
|
||||
#[cfg(test)]
|
||||
mod mode_tests;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,93 @@
|
||||
//! Frame from Layers on a hidden or locked layer (#17330): the frame is made from the layer's
|
||||
//! pixels, so its content comes out as a visible, unlocked smart object.
|
||||
|
||||
use super::*;
|
||||
use photocraft_doc::{Layer, LayerContent};
|
||||
use photocraft_geom::Rect;
|
||||
|
||||
/// A 300 x 220 document with one red pixel layer covering 50,50..150,130.
|
||||
fn session_with_layer(name: &str, visible: bool, locked: bool) -> (Session, LayerId) {
|
||||
let mut s = crate::session();
|
||||
s.execute("file.new", json!({"width": 300, "height": 220})).unwrap();
|
||||
let id = s
|
||||
.edit("setup", |doc, _| {
|
||||
let fmt = doc.pixel_format();
|
||||
let mut l = Layer::raster(name, fmt);
|
||||
l.surface_mut().unwrap().fill_rect(Rect::new(50, 50, 150, 130), &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 0.0, 1.0]));
|
||||
l.visible = visible;
|
||||
l.locks.all = locked;
|
||||
let id = l.id;
|
||||
doc.layers.push(l);
|
||||
Ok(id)
|
||||
})
|
||||
.unwrap();
|
||||
(s, id)
|
||||
}
|
||||
|
||||
/// The framed group's one child, which must be the layer's pixels as a smart object.
|
||||
fn framed_child(s: &Session, r: &Value) -> Layer {
|
||||
let gid = LayerId(r["layer"].as_u64().unwrap());
|
||||
let g = s.active().unwrap().doc.layer(gid).expect("the frame's group");
|
||||
assert!(g.frame.is_some(), "the group is a frame");
|
||||
let children = g.children().expect("the frame holds the content");
|
||||
assert_eq!(children.len(), 1, "the framed content replaces the layer");
|
||||
children[0].clone()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hidden_layer_frames_as_a_visible_smart_object() {
|
||||
let (mut s, id) = session_with_layer("Red", false, false);
|
||||
let r = s.execute("layer.new.frameFromLayers", json!({"layer": id})).expect("a hidden layer can be framed");
|
||||
assert_eq!(r["frame"], json!([50, 50, 100, 80]), "the frame fits the layer's pixels");
|
||||
let child = framed_child(&s, &r);
|
||||
assert!(matches!(child.content, LayerContent::Smart(_)), "the content is a smart object");
|
||||
assert!(child.visible, "the reference makes the framed content visible");
|
||||
let LayerContent::Smart(smart) = &child.content else { panic!("the content is a smart object") };
|
||||
let cache = smart.cache.as_ref().expect("the smart object holds the rendered pixels");
|
||||
assert_eq!((cache.content_bounds().width(), cache.content_bounds().height()), (100, 80), "the layer's pixels came through");
|
||||
}
|
||||
|
||||
/// A visible blue layer at 20,20..60,60 and a hidden red layer at 50,50..150,130.
|
||||
fn session_with_visible_and_hidden() -> (Session, LayerId, LayerId) {
|
||||
let mut s = crate::session();
|
||||
s.execute("file.new", json!({"width": 300, "height": 220})).unwrap();
|
||||
let (hidden, shown) = s
|
||||
.edit("setup", |doc, _| {
|
||||
let fmt = doc.pixel_format();
|
||||
let mut hidden = Layer::raster("Red", fmt);
|
||||
hidden.surface_mut().unwrap().fill_rect(Rect::new(50, 50, 150, 130), &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 0.0, 1.0]));
|
||||
hidden.visible = false;
|
||||
let mut shown = Layer::raster("Blue", fmt);
|
||||
shown.surface_mut().unwrap().fill_rect(Rect::new(20, 20, 60, 60), &photocraft_raster::from_rgba(&fmt, [0.0, 0.0, 1.0, 1.0]));
|
||||
let ids = (hidden.id, shown.id);
|
||||
doc.layers.push(hidden);
|
||||
doc.layers.push(shown);
|
||||
Ok(ids)
|
||||
})
|
||||
.unwrap();
|
||||
(s, hidden, shown)
|
||||
}
|
||||
|
||||
/// A selection that also holds a visible layer keeps its hidden layers hidden, as Convert to
|
||||
/// Smart Object does; only the all-hidden case above shows them.
|
||||
#[test]
|
||||
fn a_hidden_layer_beside_a_visible_one_stays_out_of_the_content() {
|
||||
let (mut s, hidden, shown) = session_with_visible_and_hidden();
|
||||
s.execute("layer.select", json!({"layer": hidden.0, "mode": "replace"})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": shown.0, "mode": "add"})).unwrap();
|
||||
let r = s.execute("layer.new.frameFromLayers", json!({})).expect("the visible layer gives the frame content");
|
||||
assert_eq!(r["frame"], json!([20, 20, 130, 110]), "the frame still fits both layers");
|
||||
let child = framed_child(&s, &r);
|
||||
let LayerContent::Smart(smart) = &child.content else { panic!("the content is a smart object") };
|
||||
let cache = smart.cache.as_ref().expect("the smart object holds the rendered pixels");
|
||||
assert_eq!((cache.content_bounds().width(), cache.content_bounds().height()), (40, 40), "the hidden layer's pixels stay out of the content");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_locked_layer_keeps_framing_and_comes_out_unlocked() {
|
||||
let (mut s, id) = session_with_layer("Red", true, true);
|
||||
let r = s.execute("layer.new.frameFromLayers", json!({"layer": id})).expect("a locked layer can still be framed");
|
||||
let child = framed_child(&s, &r);
|
||||
assert!(child.visible, "the framed content is visible");
|
||||
assert!(!child.locks.all, "the content smart object comes out unlocked");
|
||||
}
|
||||
Reference in new issue
Block a user