Preserve locks when Group from Layers leaves a group

This commit is contained in:
yoriswind committed 2026-11-30 17:04:24 -08:00
1 parent 7f7d03bf84
commit a19deb5d2a
2 files changed
+149 -9

No files matched your search

@@ -0,0 +1,109 @@
use photocraft_doc::{Layer, LayerId, Locks};
use serde_json::json;
use crate::Session;
const GROUP_FROM_LAYERS: &str = "layer.new.groupFromLayers";
struct Fixture {
session: Session,
shared: LayerId,
source: LayerId,
leaving: LayerId,
staying: LayerId,
}
fn fixture(source_locks: Locks, shared_locks: Locks, own_locks: Locks) -> Fixture {
let mut session = crate::session();
session.execute("file.new", json!({"width": 16, "height": 16, "background": "white"})).unwrap();
let (shared, source, leaving, staying) = session
.edit("Fixture layers", |doc, active| {
let mut leaving = Layer::raster("Leaving", doc.pixel_format());
leaving.locks = own_locks;
let staying = Layer::raster("Staying", doc.pixel_format());
let (leaving_id, staying_id) = (leaving.id, staying.id);
let mut source = Layer::group("Source", vec![leaving]);
source.locks = source_locks;
let source_id = source.id;
let destination = Layer::group("Destination", vec![staying]);
let mut shared = Layer::group("Shared", vec![source, destination]);
shared.locks = shared_locks;
let shared_id = shared.id;
doc.layers.push(shared);
*active = Some(staying_id);
Ok((shared_id, source_id, leaving_id, staying_id))
})
.unwrap();
for (layer, mode) in [(leaving, "replace"), (staying, "add")] {
session.execute("layer.select", json!({"layer": layer.0, "mode": mode})).unwrap();
}
Fixture { session, shared, source, leaving, staying }
}
fn group(session: &mut Session, command: &str) -> LayerId {
LayerId(session.execute(command, json!({})).unwrap()["layer"].as_u64().unwrap())
}
#[test]
fn group_from_layers_carries_each_departed_lock_in_the_grouping_undo_step() {
for locks in [
Locks { all: true, ..Default::default() },
Locks { position: true, ..Default::default() },
Locks { transparency: true, ..Default::default() },
Locks { pixels: true, ..Default::default() },
Locks { artboard: true, ..Default::default() },
Locks { position: true, transparency: true, ..Default::default() },
] {
let Fixture { mut session, source, leaving, staying, .. } = fixture(locks, Locks::default(), Locks::default());
let state = session.active().unwrap();
let before = state.doc.clone();
let steps = state.history.past_len();
let group = group(&mut session, GROUP_FROM_LAYERS);
let state = session.active().unwrap();
let doc = &state.doc;
assert_eq!(state.history.past_len(), steps + 1);
assert!(doc.layer(source).unwrap().children().unwrap().is_empty());
assert_eq!(doc.layer(group).unwrap().children().unwrap().iter().map(|layer| layer.id).collect::<Vec<_>>(), [leaving, staying]);
assert_eq!(doc.layer(group).unwrap().locks, Locks::default());
assert_eq!(doc.layer(leaving).unwrap().locks, locks);
assert_eq!(doc.layer(staying).unwrap().locks, Locks::default());
session.undo();
assert_eq!(session.active().unwrap().doc, before);
}
}
#[test]
fn group_from_layers_keeps_shared_locks_inherited_and_keeps_own_locks() {
let shared_locks = Locks { pixels: true, position: true, ..Default::default() };
let source_locks = Locks { position: true, ..Default::default() };
let own_locks = Locks { artboard: true, ..Default::default() };
let Fixture { mut session, shared, leaving, staying, .. } = fixture(source_locks, shared_locks, own_locks);
let group = group(&mut session, GROUP_FROM_LAYERS);
let doc = &session.active().unwrap().doc;
let new_own_locks = own_locks.union(source_locks);
assert_eq!(doc.layer(leaving).unwrap().locks, new_own_locks);
assert_eq!(doc.layer(staying).unwrap().locks, Locks::default());
assert_eq!(doc.layer(group).unwrap().locks, Locks::default());
assert_eq!(doc.effective_locks(staying), shared_locks);
session
.edit("Unlock ancestor", |doc, _| {
doc.layer_mut(shared).unwrap().locks = Locks::default();
Ok(())
})
.unwrap();
let doc = &session.active().unwrap().doc;
assert_eq!(doc.effective_locks(leaving), new_own_locks);
assert!(!doc.effective_locks(staying).any());
let error = session.execute("layer.translate", json!({"layer": leaving.0, "dx": 1})).unwrap_err();
assert!(error.to_string().contains("locked"), "{error}");
}
#[test]
fn group_layers_shortcut_keeps_its_existing_lock_behavior() {
let locks = Locks { all: true, ..Default::default() };
let Fixture { mut session, leaving, .. } = fixture(locks, Locks::default(), Locks::default());
group(&mut session, "layer.groupLayers");
let doc = &session.active().unwrap().doc;
assert!(!doc.layer(leaving).unwrap().locks.any());
}
@@ -1,3 +1,7 @@
use std::collections::HashMap;
use photocraft_doc::Locks;
use super::selection::{reselect, selected, top_level};
use super::{Document, EngineError, Layer, LayerContent, LayerId, PixelFormat, Result, Session, Value, bad, json};
@@ -132,7 +136,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, false)
group_layers_with(s, p, &ids, true)
}
/// Group Layers (⌘G) moves the explicit layer or the selection into a new, collapsed group.
@@ -141,23 +145,24 @@ 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, true)
group_layers_with(s, p, &ids, false)
}
/// Creates the group at the topmost target, preserving layer order and applying the dialog's
/// 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> {
/// options in the same history step.
fn group_layers_with(s: &mut Session, p: &Value, ids: &[LayerId], from_layers: bool) -> Result<Value> {
if ids.is_empty() {
return Err(EngineError::Other("no layer selected".into()));
}
let opts = crate::group_options::GroupOptions::parse("layer.groupLayers", p)?;
let expanded = from_layers && ids.len() == 1;
let gid = s.edit("Group Layers", |doc, active| {
// A base takes the layers clipped to it into the group, still clipped, whether they were
// selected or not; a clipped layer that goes in without its base is released, as in the
// reference application (#14713).
let mut ids = top_level(doc, ids);
let bases: Vec<LayerId> = ids.clone();
for id in bases {
let selected_roots = top_level(doc, ids);
let mut ids = selected_roots.clone();
for &id in &selected_roots {
ids.extend(crate::clip_group::clipped_to(doc, id));
}
let ids = top_level(doc, &ids);
@@ -171,6 +176,24 @@ fn group_layers_with(s: &mut Session, p: &Value, ids: &[LayerId], expanded: bool
return Err(EngineError::Other("artboards can't go into a group".into()));
}
let top = *ids.last().ok_or(EngineError::NoLayer(LayerId(0)))?;
let mut carried_locks = HashMap::new();
if from_layers {
// Only ancestors the selected layer leaves become own locks. Shared ancestors still
// supply their inherited locks, including when the new group is nested below them.
let insertion_path = doc.insertion_path(top).ok_or(EngineError::NoLayer(top))?;
let (_, destination) = insertion_path.split_last().ok_or(EngineError::NoLayer(top))?;
for id in selected_roots {
let path = doc.path_of(id).ok_or(EngineError::NoLayer(id))?;
let (_, parents) = path.split_last().ok_or(EngineError::NoLayer(id))?;
let shared = parents.iter().zip(destination).take_while(|(old, new)| old == new).count();
let locks = (shared + 1..=parents.len())
.filter_map(|depth| doc.layer_at(&parents[..depth]))
.fold(Locks::default(), |locks, layer| locks.union(layer.locks));
if locks.any() {
carried_locks.insert(id, locks);
}
}
}
let name = opts.name.clone().unwrap_or_else(|| doc.next_layer_name("Group"));
let mut group = Layer::group(name, vec![]);
if let LayerContent::Group(g) = &mut group.content {
@@ -179,14 +202,18 @@ fn group_layers_with(s: &mut Session, p: &Value, ids: &[LayerId], expanded: bool
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))?);
let mut child = doc.remove(*id).ok_or(EngineError::NoLayer(*id))?;
if let Some(&locks) = carried_locks.get(id) {
child.locks = child.locks.union(locks);
}
children.push(child);
}
for child in children.iter_mut().filter(|l| release.contains(&l.id)) {
child.clipped = false;
}
let group = doc.layer_mut(gid).ok_or(EngineError::NoLayer(gid))?;
*group.children_mut().ok_or(EngineError::NoLayer(gid))? = children;
opts.apply(group, tag_layers);
opts.apply(group, !from_layers);
check_group_depth(doc, "Group Layers")?;
*active = Some(gid);
Ok(gid)
@@ -426,3 +453,7 @@ pub fn set_visible_selected(s: &mut Session, visible: bool) -> Result<Value> {
#[cfg(test)]
#[path = "group_from_layers_keeps_layer_tags_tests.rs"]
mod group_from_layers_keeps_layer_tags_tests;
#[cfg(test)]
#[path = "group_from_layers_lock_carry_tests.rs"]
mod group_from_layers_lock_carry_tests;