diff --git a/crates/engine-core/src/cmd/layer_multi_cmds/group_from_layers_keeps_layer_tags_tests.rs b/crates/engine-core/src/cmd/layer_multi_cmds/group_from_layers_keeps_layer_tags_tests.rs new file mode 100644 index 00000000000..e19cb94f406 --- /dev/null +++ b/crates/engine-core/src/cmd/layer_multi_cmds/group_from_layers_keeps_layer_tags_tests.rs @@ -0,0 +1,72 @@ +//! Group from Layers with a colour tags the new group only; the layers moved into it keep their +//! own colour tags, also inside nested groups (#18279). + +use super::*; + +const GROUP_FROM_LAYERS: &str = "layer.new.groupFromLayers"; + +fn session() -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 32, "height": 32})).unwrap(); + s +} + +fn create(s: &mut Session, command: &str, name: &str) -> LayerId { + LayerId(s.execute(command, json!({"name": name})).unwrap()["layer"].as_u64().unwrap()) +} + +fn create_with(s: &mut Session, p: Value) -> LayerId { + LayerId(s.execute(GROUP_FROM_LAYERS, p).unwrap()["layer"].as_u64().unwrap()) +} + +fn tag(s: &mut Session, id: LayerId, color: &str) { + s.execute("layer.setLabelColor", json!({"layer": id.0, "color": color})).unwrap(); +} + +/// The tag `document.inspect` reports for `id`, wherever it sits in the tree. +fn tag_of(s: &mut Session, id: LayerId) -> String { + fn find(layers: &Value, id: LayerId) -> Option<&Value> { + layers.as_array()?.iter().find_map(|l| if l["id"] == id.0 { Some(l) } else { find(&l["children"], id) }) + } + let state = s.execute("document.inspect", json!({})).unwrap(); + find(&state["layers"], id).expect("layer is in the tree")["labelColor"].as_str().unwrap().to_string() +} + +#[test] +fn the_colour_tags_the_new_group_and_the_moved_layers_keep_their_tags() { + let mut s = session(); + let blue = create(&mut s, "layer.new.layer", "Blue"); + let red = create(&mut s, "layer.new.layer", "Red"); + tag(&mut s, blue, "green"); + s.execute("layer.select", json!({"layers": [blue.0, red.0]})).unwrap(); + let before = s.active().unwrap().history.past_len(); + let group = create_with(&mut s, json!({"color": "red"})); + assert_eq!(tag_of(&mut s, group), "red"); + assert_eq!(tag_of(&mut s, blue), "green", "a tagged layer keeps its tag"); + assert_eq!(tag_of(&mut s, red), "none", "an untagged layer stays untagged"); + assert_eq!(s.active().unwrap().history.past_len(), before + 1, "one history step"); + + s.execute("edit.undo", json!({})).unwrap(); + assert!(s.active().unwrap().doc.layer(group).is_none()); + assert_eq!((tag_of(&mut s, blue), tag_of(&mut s, red)), ("green".into(), "none".into())); +} + +#[test] +fn layers_inside_a_moved_group_keep_their_tags() { + let mut s = session(); + let inner = create(&mut s, "layer.new.layer", "In"); + let group3 = create(&mut s, "layer.new.group", "Group 3"); + s.execute("layer.moveTo", json!({"layer": inner.0, "target": group3.0, "position": "into"})).unwrap(); + let green = create(&mut s, "layer.new.layer", "Green"); + tag(&mut s, inner, "blue"); + s.execute("layer.select", json!({"layers": [group3.0, green.0]})).unwrap(); + let group = create_with(&mut s, json!({"color": "violet"})); + assert_eq!(tag_of(&mut s, group), "violet"); + assert_eq!(tag_of(&mut s, inner), "blue"); + assert_eq!((tag_of(&mut s, group3), tag_of(&mut s, green)), ("none".into(), "none".into())); + assert_eq!(s.active().unwrap().doc.layers.index().parent(inner), Some(group3), "the tree is as grouped"); + + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(s.active().unwrap().doc.layers.index().parent(group3), None); + assert_eq!((tag_of(&mut s, inner), tag_of(&mut s, group3), tag_of(&mut s, green)), ("blue".into(), "none".into(), "none".into())); +} diff --git a/crates/engine-core/src/cmd/layer_multi_cmds/structure.rs b/crates/engine-core/src/cmd/layer_multi_cmds/structure.rs index ab63541aaa5..2a1f8213e09 100644 --- a/crates/engine-core/src/cmd/layer_multi_cmds/structure.rs +++ b/crates/engine-core/src/cmd/layer_multi_cmds/structure.rs @@ -132,7 +132,7 @@ pub(super) fn group_from_layers(s: &mut Session, p: &Value) -> Result { if ids.iter().any(|id| doc.is_background(*id)) { return Err(EngineError::Other("the Background layer can't go into a group".into())); } - group_layers_with(s, p, &ids, ids.len() == 1) + group_layers_with(s, p, &ids, ids.len() == 1, false) } /// Group Layers (⌘G) moves the explicit layer or the selection into a new, collapsed group. @@ -141,12 +141,12 @@ pub fn group_layers(s: &mut Session, p: &Value) -> Result { Some(id) => vec![id], None => selected(s), }; - group_layers_with(s, p, &ids, false) + group_layers_with(s, p, &ids, false, true) } /// Creates the group at the topmost target, preserving layer order and applying the dialog's -/// options in the same history step. -fn group_layers_with(s: &mut Session, p: &Value, ids: &[LayerId], expanded: bool) -> Result { +/// options in the same history step. `tag_layers` passes a colour on to the layers inside. +fn group_layers_with(s: &mut Session, p: &Value, ids: &[LayerId], expanded: bool, tag_layers: bool) -> Result { if ids.is_empty() { return Err(EngineError::Other("no layer selected".into())); } @@ -186,7 +186,7 @@ fn group_layers_with(s: &mut Session, p: &Value, ids: &[LayerId], expanded: bool } let group = doc.layer_mut(gid).ok_or(EngineError::NoLayer(gid))?; *group.children_mut().ok_or(EngineError::NoLayer(gid))? = children; - opts.apply(group); + opts.apply(group, tag_layers); check_group_depth(doc, "Group Layers")?; *active = Some(gid); Ok(gid) @@ -422,3 +422,7 @@ pub fn set_visible_selected(s: &mut Session, visible: bool) -> Result { })?; Ok(Value::Null) } + +#[cfg(test)] +#[path = "group_from_layers_keeps_layer_tags_tests.rs"] +mod group_from_layers_keeps_layer_tags_tests; diff --git a/crates/engine-core/src/cmd/table/model/layer_menu.rs b/crates/engine-core/src/cmd/table/model/layer_menu.rs index ac563892267..bd9a48769f0 100644 --- a/crates/engine-core/src/cmd/table/model/layer_menu.rs +++ b/crates/engine-core/src/cmd/table/model/layer_menu.rs @@ -41,7 +41,7 @@ fn creation() -> Vec { let id = s.edit("New Group", |doc, active| { let name = opts.name.clone().unwrap_or_else(|| doc.next_layer_name("Group")); let mut group = Layer::group(name, vec![]); - opts.apply(&mut group); + opts.apply(&mut group, false); let bg = crate::background_cmds::background_id(doc); let id = crate::layer_place::insert_new(doc, *active, group, place); crate::background_cmds::keep_background_at_bottom(bg, doc)?; diff --git a/crates/engine-core/src/group_options.rs b/crates/engine-core/src/group_options.rs index 4c72b2f0786..a3f673d07df 100644 --- a/crates/engine-core/src/group_options.rs +++ b/crates/engine-core/src/group_options.rs @@ -50,9 +50,11 @@ impl GroupOptions { Ok(Self { name, color, blend, opacity }) } - /// Sets the mode, opacity and colour of a new `group`. A colour also reaches every layer inside - /// it, as Layer › Layer Color does on a group; "No Color" leaves the layers' own colours alone. - pub(crate) fn apply(&self, group: &mut Layer) { + /// Sets the mode, opacity and colour of a new `group`. With `tag_layers` a colour also reaches + /// every layer inside it, as Layer › Layer Color does on a group; without it only the group takes + /// the colour and the layers keep their own tags, as Group from Layers does (#18279). "No Color" + /// leaves the layers' own colours alone. + pub(crate) fn apply(&self, group: &mut Layer, tag_layers: bool) { if let Some(blend) = self.blend { group.blend = blend; } @@ -61,6 +63,7 @@ impl GroupOptions { } match self.color { Some(LabelColor::None) | None => {} + Some(color) if !tag_layers => group.label = color, Some(color) => { // Explicit stack: documents can nest groups deeply. let mut stack = vec![group];