Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Layers: Group Layers creates the group collapsed (#5571)

Khawar Zamman Wani70886c0d88d9
diff --git a/crates/engine/src/group_view_cmds.rs b/crates/engine/src/group_view_cmds.rs
index b95acff6a..52eadb782 100644
--- a/crates/engine/src/group_view_cmds.rs
+++ b/crates/engine/src/group_view_cmds.rs
@@ -102,6 +102,9 @@ mod tests {
s.execute("layer.new.layer", json!({"name": "deep"})).unwrap();
let inner = s.execute("layer.groupLayers", json!({"name": "inner"})).unwrap()["layer"].as_u64().unwrap();
let outer = s.execute("layer.groupLayers", json!({"layer": inner, "name": "outer"})).unwrap()["layer"].as_u64().unwrap();
+ // Group Layers creates collapsed groups; the tests start from both open.
+ assert!(!expanded(&s, inner) && !expanded(&s, outer));
+ s.execute(ID, json!({"all": true, "expanded": true})).unwrap();
(s, inner, outer)
}
diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rs
index 80da3bdd8..f887f46cc 100644
--- a/crates/engine/src/layer_multi_cmds.rs
+++ b/crates/engine/src/layer_multi_cmds.rs
@@ -1218,7 +1218,8 @@ pub(crate) fn check_group_depth(doc: &Document, what: &str) -> Result<()> {
}
/// Group Layers (⌘G) / Group from Layers: the explicit layer, or every selected layer, moves
-/// into a new group placed where the top-most of them was. Bottom-to-top order is preserved.
+/// into a new, collapsed group placed where the top-most of them was. Bottom-to-top order is
+/// preserved.
pub fn group_layers(s: &mut Session, p: &Value) -> Result<Value> {
let ids = match crate::commands::explicit_layer(s, p)? {
Some(id) => vec![id],
@@ -1232,7 +1233,12 @@ pub fn group_layers(s: &mut Session, p: &Value) -> Result<Value> {
let ids = top_level(doc, &ids);
let top = *ids.last().ok_or(EngineError::NoLayer(LayerId(0)))?;
let name = name.unwrap_or_else(|| doc.next_layer_name("Group"));
- let gid = doc.insert_above(Some(top), Layer::group(name, vec![]));
+ // Photoshop creates the group collapsed, so the panel shows one row in place of the layers.
+ let mut group = Layer::group(name, vec![]);
+ if let LayerContent::Group(g) = &mut group.content {
+ g.expanded = false;
+ }
+ let gid = doc.insert_above(Some(top), group);
let mut children = Vec::with_capacity(ids.len());
for id in &ids {
children.push(doc.remove(*id).ok_or(EngineError::NoLayer(*id))?);
@@ -2193,6 +2199,7 @@ mod tests {
let docu = doc(&s);
let gl = docu.layer(g).unwrap();
assert_eq!(gl.name, "Pair");
+ assert!(matches!(&gl.content, LayerContent::Group(gr) if !gr.expanded), "created collapsed");
assert_eq!(gl.children().unwrap().iter().map(|l| l.id).collect::<Vec<_>>(), vec![a, c]);
// Root (bottom-to-top): Background, b, Pair (where c was), d.
assert_eq!(docu.layers.iter().map(|l| l.id).skip(1).collect::<Vec<_>>(), vec![b, g, d]);
@@ -2201,8 +2208,10 @@ mod tests {
assert_eq!(doc(&s).layers.len(), 5);
// ⌘G with several selected groups them too.
select_all(&mut s, &[b, d]);
- s.execute("layer.groupLayers", json!({})).unwrap();
+ let g = LayerId(s.execute("layer.groupLayers", json!({})).unwrap()["layer"].as_u64().unwrap());
assert_eq!(doc(&s).layers.len(), 4);
+ // Both create the group collapsed, as Photoshop does (#5571).
+ assert!(matches!(&doc(&s).layer(g).unwrap().content, LayerContent::Group(gr) if !gr.expanded));
}
#[test]
diff --git a/crates/engine/src/layer_nav_cmds.rs b/crates/engine/src/layer_nav_cmds.rs
index bd1ecb661..b41a18b59 100644
--- a/crates/engine/src/layer_nav_cmds.rs
+++ b/crates/engine/src/layer_nav_cmds.rs
@@ -118,6 +118,7 @@ mod tests {
s.execute("layer.select", json!({"layer": c.0, "mode": "add"})).unwrap();
s.execute("layer.groupLayers", json!({})).unwrap();
let group = s.active().unwrap().doc.layers.iter().find(|l| l.is_group()).unwrap().id;
+ s.execute("layer.setExpanded", json!({"layer": group.0, "expanded": true})).unwrap();
let d = id(&mut s, "layer.new.layer", json!({"name": "D"}));
(s, [bg, a, group, b, c, d])
}
diff --git a/crates/ui-egui/src/layer_row_ui/tests.rs b/crates/ui-egui/src/layer_row_ui/tests.rs
index 6ec1241b4..cca7d4e83 100644
--- a/crates/ui-egui/src/layer_row_ui/tests.rs
+++ b/crates/ui-egui/src/layer_row_ui/tests.rs
@@ -37,6 +37,8 @@ fn busy() -> photocraft_engine::Session {
s.execute("layer.layerStyle.dropShadow", json!({"layer": g})).unwrap();
prev = Some(g);
}
+ // Group Layers creates collapsed groups (#5571); these tests start with every group open.
+ s.execute("layer.setExpanded", json!({"all": true, "expanded": true})).unwrap();
s
}
diff --git a/crates/ui-egui/src/layer_tree_ui/tests.rs b/crates/ui-egui/src/layer_tree_ui/tests.rs
index 13e73353b..0b3d4c272 100644
--- a/crates/ui-egui/src/layer_tree_ui/tests.rs
+++ b/crates/ui-egui/src/layer_tree_ui/tests.rs
@@ -21,6 +21,8 @@ fn layered() -> photocraft_engine::Session {
s.execute("layer.new.layer", json!({"name": "in-outer"})).unwrap();
s.execute("layer.select", json!({"layer": outer})).unwrap();
s.execute("layer.new.layer", json!({"name": "top"})).unwrap();
+ // Group Layers creates collapsed groups (#5571); these tests start with every group open.
+ s.execute("layer.setExpanded", json!({"all": true, "expanded": true})).unwrap();
s
}
diff --git a/crates/ui-egui/src/shortcut_dispatch/tests.rs b/crates/ui-egui/src/shortcut_dispatch/tests.rs
index 458f22c05..c573991e2 100644
--- a/crates/ui-egui/src/shortcut_dispatch/tests.rs
+++ b/crates/ui-egui/src/shortcut_dispatch/tests.rs
@@ -21,6 +21,8 @@ fn realistic() -> photocraft_engine::Session {
s.execute("file.new", json!({"width": 240, "height": 160})).unwrap();
s.execute("layer.new.layer", json!({"name": "in-group"})).unwrap();
s.execute("layer.groupLayers", json!({"name": "group"})).unwrap();
+ // Group Layers creates collapsed groups (#5571); the walk below expects it open.
+ s.execute("layer.setExpanded", json!({"all": true, "expanded": true})).unwrap();
s.execute("type.create", json!({"x": 20, "y": 60, "text": "Hello"})).unwrap();
let paint = s.execute("layer.new.layer", json!({"name": "paint"})).unwrap()["layer"].as_u64().unwrap();
s.execute("select.rect", json!({"x": 20, "y": 20, "width": 80, "height": 60})).unwrap();