Group from Layers: colour only the new group, keep the layers' tags (#18279)

GroupOptions::apply wrote the dialog's colour on the new group and on
every layer inside it, so Group from Layers with a colour overwrote the
moved layers' own tags. apply now takes tag_layers: Group from Layers
tags the group only, while Group Layers (Ctrl+G, not measured) keeps
passing the colour on, as before. New Group's group is empty, so its
call does not change.
This commit is contained in:
RickiNano committed 2026-11-30 20:33:28 +01:00
1 parent 6c28867ac0
commit 5b207688dd
4 files changed
+88 -9

No files matched your search

@@ -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()));
}
@@ -132,7 +132,7 @@ pub(super) fn group_from_layers(s: &mut Session, p: &Value) -> Result<Value> {
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<Value> {
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<Value> {
/// 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<Value> {
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<Value> {
})?;
Ok(Value::Null)
}
#[cfg(test)]
#[path = "group_from_layers_keeps_layer_tags_tests.rs"]
mod group_from_layers_keeps_layer_tags_tests;
@@ -41,7 +41,7 @@ fn creation() -> Vec<CommandSpec> {
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)?;
+6 -3
View File
@@ -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];