fix(engine): Collapse All Groups leaves one history step

The Layers panel's Collapse All Groups runs layer.setExpanded with
{all: true, expanded: false} and no layer. The whole command was treated
as a view change, so closing every group left nothing for Edit > Undo;
Photoshop records one "Collapse All Groups" step for it. The layer-less
close-all now goes through Session::edit with that label, so one undo
reopens the groups that were open. A single or row-addressed toggle,
the open-all form and a close-all over an already-closed tree keep
today's no-step view change.
This commit is contained in:
hnoerenberg committed 2026-11-28 18:50:32 +01:00
1 parent f7d76f5be4
commit e29b573919
3 files changed
+111 -3

No files matched your search

+15 -3
View File
@@ -2,7 +2,9 @@
//!
//! A group's open/closed state is document data ([`photocraft_doc::Group::expanded`], the PSD
//! section divider's open/closed folder type, saved in `.pcraft` too) but, as in Photoshop,
//! toggling it is a view change: no history step, and a clean document stays clean.
//! toggling it is a view change: no history step, and a clean document stays clean. The panel's
//! Collapse All Groups is the one exception: it adds a single `Collapse All Groups` history step
//! (#17363), so one undo reopens the groups that were open.
use std::sync::Arc;
@@ -44,7 +46,9 @@ fn set_all(layers: &mut [Layer], expanded: bool) -> usize {
/// `{"layer":id?,"expanded":bool?,"all":bool?}`: open or close a group (no `expanded`: toggle).
/// With `all`, every group in the document takes the new state (Photoshop's ⌥-click on a
/// disclosure triangle); without a layer, `all` toggles from "any group open".
/// disclosure triangle); without a layer, `all` toggles from "any group open". Without a layer,
/// closing every group is the panel's Collapse All Groups: one `Collapse All Groups` history
/// step (#17363); the row-addressed form and a single toggle stay view changes.
fn set_expanded(s: &mut Session, p: &Value) -> Result<Value> {
let want = opt_bool(p, "expanded")?;
let all = opt_bool(p, "all")?.unwrap_or(false);
@@ -62,6 +66,14 @@ fn set_expanded(s: &mut Session, p: &Value) -> Result<Value> {
None => st.doc.walk().iter().any(|(_, _, l)| matches!(&l.content, LayerContent::Group(g) if g.expanded)),
};
let expanded = want.unwrap_or(!current);
if all && layer.is_none() && !expanded && current {
// The panel's Collapse All Groups, unlike any other use of this command: one history
// step named like the menu entry, so one undo reopens the groups that were open.
// `current` here means "some group is open"; over an already-closed tree the command
// stays the no-op view change it is today and records no empty step.
let changed = s.edit("Collapse All Groups", |doc, _| Ok(set_all(&mut doc.layers, false)))?;
return Ok(json!({"expanded": expanded, "groups": changed}));
}
let st = s.active_mut().ok_or(EngineError::NoDocument)?;
// Copy-on-write: history snapshots keep their own state (layers share tiles, so this is cheap).
let doc = Arc::make_mut(st.doc_mut());
@@ -86,7 +98,7 @@ pub fn specs() -> Vec<CommandSpec> {
label: "Expand/Collapse Group",
menu: &[],
shortcut: None,
params: r##"{"layer":id?,"expanded":bool?,"all":bool?} (no expanded: toggle; all: every group; not an undo step)"##,
params: r##"{"layer":id?,"expanded":bool?,"all":bool?} (no expanded: toggle; all: every group; the layer-less close-all is the panel's Collapse All Groups, one undo step; a single or row-addressed toggle is not)"##,
enabled: has_group,
run: set_expanded,
journal: false,
@@ -0,0 +1,95 @@
//! Collapse All Groups as one history step. The Layers panel's Collapse All Groups
//! (`layer.setExpanded` with `all: true, expanded: false` and no layer) closes every group and
//! must leave exactly one undoable step named `Collapse All Groups`; one undo reopens the groups
//! that were open. Opening or closing one group, and the layer-addressed `all` form a modifier
//! click on a triangle sends, stay view changes with no step, as does an all-collapse over a
//! tree that is already closed.
use photocraft_doc::{LayerContent, LayerId};
use photocraft_engine::Session;
use serde_json::json;
fn expanded(s: &Session, id: LayerId) -> bool {
matches!(&s.active().unwrap().doc.layer(id).unwrap().content, LayerContent::Group(g) if g.expanded)
}
/// Three groups nested three deep, every one open.
fn three_nested_open() -> (Session, LayerId, LayerId, LayerId) {
let mut s = Session::new();
s.execute("file.new", json!({"width": 32, "height": 32})).unwrap();
s.execute("layer.new.layer", json!({"name": "deep"})).unwrap();
let inner = LayerId(s.execute("layer.groupLayers", json!({"name": "inner"})).unwrap()["layer"].as_u64().unwrap());
let middle = LayerId(s.execute("layer.groupLayers", json!({"layer": inner.0, "name": "middle"})).unwrap()["layer"].as_u64().unwrap());
let outer = LayerId(s.execute("layer.groupLayers", json!({"layer": middle.0, "name": "outer"})).unwrap()["layer"].as_u64().unwrap());
// Group Layers creates collapsed groups; the tests start with every group open.
s.execute("layer.setExpanded", json!({"all": true, "expanded": true})).unwrap();
assert!(expanded(&s, inner) && expanded(&s, middle) && expanded(&s, outer), "setup: all three groups open");
(s, inner, middle, outer)
}
fn collapse_all(s: &mut Session) {
s.execute("layer.setExpanded", json!({"all": true, "expanded": false})).unwrap();
}
fn history_len(s: &Session) -> usize {
s.active().unwrap().history.entries().len()
}
#[test]
fn collapse_all_groups_adds_exactly_one_named_history_step() {
let (mut s, inner, middle, outer) = three_nested_open();
let before = history_len(&s);
collapse_all(&mut s);
assert_eq!(history_len(&s), before + 1, "Collapse All Groups gains exactly one step");
assert_eq!(s.active().unwrap().history.undo_label().unwrap(), "Collapse All Groups", "the step is named like the menu entry");
assert!(!expanded(&s, inner) && !expanded(&s, middle) && !expanded(&s, outer), "every group, nested included, is closed");
}
#[test]
fn one_undo_reopens_every_group_that_was_open() {
let (mut s, inner, middle, outer) = three_nested_open();
let before = history_len(&s);
collapse_all(&mut s);
assert!(s.undo(), "the step is undoable");
assert_eq!(history_len(&s), before, "undo leaves one step, the state before Collapse All Groups");
assert!(expanded(&s, inner) && expanded(&s, middle) && expanded(&s, outer), "one undo restores all three expanded states");
}
#[test]
fn collapsing_one_group_by_id_still_adds_no_step() {
let (mut s, inner, middle, outer) = three_nested_open();
let before = history_len(&s);
s.execute("layer.setExpanded", json!({"layer": inner.0, "expanded": false})).unwrap();
assert_eq!(history_len(&s), before, "a single group's arrow toggle stays no step");
assert!(!expanded(&s, inner) && expanded(&s, middle) && expanded(&s, outer), "only that group closed");
}
#[test]
fn a_modifier_click_collapse_all_on_one_row_adds_no_step() {
// The panel's modifier-click on a disclosure triangle addresses that row with `all`.
let (mut s, inner, middle, outer) = three_nested_open();
let before = history_len(&s);
s.execute("layer.setExpanded", json!({"layer": outer.0, "expanded": false, "all": true})).unwrap();
assert_eq!(history_len(&s), before, "the row-addressed all form stays a view change");
assert!(!expanded(&s, inner) && !expanded(&s, middle) && !expanded(&s, outer), "it still closes every group");
}
#[test]
fn expanding_all_groups_adds_no_step() {
let (mut s, _, _, _) = three_nested_open();
collapse_all(&mut s);
let before = history_len(&s);
s.execute("layer.setExpanded", json!({"all": true, "expanded": true})).unwrap();
assert_eq!(history_len(&s), before, "Expand All keeps today's no-step view change (not measured)");
}
#[test]
fn collapse_all_on_an_already_collapsed_tree_adds_no_step() {
let (mut s, inner, middle, outer) = three_nested_open();
collapse_all(&mut s);
let before = history_len(&s);
let back = s.execute("layer.setExpanded", json!({"all": true, "expanded": false})).unwrap();
assert_eq!(history_len(&s), before, "nothing to close: no empty journal entry, as today");
assert_eq!(back["groups"], 3, "the reply keeps today's count of the groups it touched");
assert!(!expanded(&s, inner) && !expanded(&s, middle) && !expanded(&s, outer), "the tree stays closed");
}
+1
View File
@@ -47,6 +47,7 @@ mod canvas_artboard_crop;
mod clipping_semantics;
mod close_document;
mod cmyk_flatten_profile;
mod collapse_all_groups_history;
mod command_lookup;
mod content_aware_fill_atn;
mod content_aware_fill_options;