mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
fix(review): transforms keep untargeted Backgrounds out, duplicates start outside the source link group, warp guard judges the widened set (#11217)
This commit is contained in:
1 parent
519c0a48e4
commit
64cf1678ba
10 files changed
+206
-6
No files matched your search
@@ -575,6 +575,8 @@ fn build() -> Vec<CommandSpec> {
|
||||
dup.name.clone_from(name);
|
||||
}
|
||||
let nid = doc.insert_above(Some(id), dup);
|
||||
// The copy starts unlinked from its original, as in Photoshop (#11217).
|
||||
crate::layer_multi_cmds::relink_copies(doc, &[nid])?;
|
||||
if !in_place {
|
||||
crate::artboard_cmds::place_copy(doc, nid)?;
|
||||
}
|
||||
|
||||
@@ -610,6 +610,8 @@ fn layer_via(s: &mut Session, cut: bool) -> Result<Value> {
|
||||
dup.name = doc.copy_name(&src.name);
|
||||
dup.locks = Default::default();
|
||||
let nid = doc.insert_above(Some(id), dup);
|
||||
// The copy starts unlinked from its original, as in Photoshop (#11217).
|
||||
crate::layer_multi_cmds::relink_copies(doc, &[nid])?;
|
||||
*active = Some(nid);
|
||||
Ok(nid)
|
||||
})?;
|
||||
|
||||
@@ -32,7 +32,7 @@ pub use moves::{move_targets, move_to, moved, moved_target, moves_mask_alone, pr
|
||||
use selection::{doc_state, find_layers, select_all_layers, select_linked};
|
||||
pub(crate) use selection::{mask_targets, recorded_step, reselect, targets, top_level};
|
||||
pub use selection::{select, selected, set_selection};
|
||||
pub(crate) use structure::{check_group_depth, deletion_neighbours};
|
||||
pub(crate) use structure::{check_group_depth, deletion_neighbours, relink_copies};
|
||||
pub use structure::{delete_selected, duplicate_selected, group_layers, set_visible_selected};
|
||||
use structure::{link_layers, lock_layers, merge_layers, rename_layer, reverse};
|
||||
|
||||
|
||||
@@ -287,6 +287,7 @@ pub fn duplicate_selected(s: &mut Session, in_place: bool, label: &str) -> Resul
|
||||
}
|
||||
copies.push(nid);
|
||||
}
|
||||
relink_copies(doc, &copies)?;
|
||||
let a = new_active.or(copies.last().copied());
|
||||
*active = a;
|
||||
Ok((copies, a))
|
||||
@@ -296,6 +297,42 @@ pub fn duplicate_selected(s: &mut Session, in_place: bool, label: &str) -> Resul
|
||||
Ok(json!({"layers": out, "layer": active.map(|l| l.0)}))
|
||||
}
|
||||
|
||||
/// Duplicates start outside their originals' link groups, as in Photoshop (#11217): a copy that
|
||||
/// joined the original's group would drag the originals along with it (⌥⌘T's box over the copy
|
||||
/// framed them too). Copies of several layers of one link group stay linked to each other, under
|
||||
/// a group of their own, as `layer_copy_cmds::adapt_copies` remaps copies landing in another
|
||||
/// document; a lone copy of a linked layer is unlinked. `copies` are the copies just inserted in
|
||||
/// `doc` (the layers in copied groups are remapped with them); the originals keep their groups.
|
||||
pub(crate) fn relink_copies(doc: &mut Document, copies: &[LayerId]) -> Result<()> {
|
||||
// Every copied layer with the link group it was copied with. An explicit stack rather than
|
||||
// recursion: documents can nest deeply.
|
||||
let mut linked: Vec<(LayerId, u64)> = Vec::new();
|
||||
let mut stack: Vec<&Layer> = copies.iter().filter_map(|id| doc.layer(*id)).collect();
|
||||
while let Some(l) = stack.pop() {
|
||||
if let Some(group) = l.link_group {
|
||||
linked.push((l.id, group));
|
||||
}
|
||||
if let LayerContent::Group(g) = &l.content {
|
||||
stack.extend(g.children.iter());
|
||||
}
|
||||
}
|
||||
let mut groups: Vec<u64> = linked.iter().map(|(_, group)| *group).collect();
|
||||
groups.sort_unstable();
|
||||
groups.dedup();
|
||||
for group in groups {
|
||||
let members: Vec<LayerId> = linked.iter().filter(|(_, g)| *g == group).map(|(id, _)| *id).collect();
|
||||
// The id is picked after the previous group's copies took theirs, so no two share one.
|
||||
let fresh =
|
||||
if members.len() < 2 { None } else { Some(doc.unused_link_group().ok_or_else(|| EngineError::Other("no free link group id is left".into()))?) };
|
||||
for id in members {
|
||||
if let Some(l) = doc.layer_mut(id) {
|
||||
l.link_group = fresh;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Show/hide every selected layer in one step.
|
||||
pub fn set_visible_selected(s: &mut Session, visible: bool) -> Result<Value> {
|
||||
let sel = selected(s);
|
||||
|
||||
@@ -1456,6 +1456,60 @@ fn inspect_reports_selection() {
|
||||
assert_eq!(v["layers"][0]["selected"], true);
|
||||
}
|
||||
|
||||
fn link_group(s: &Session, id: LayerId) -> Option<u64> {
|
||||
doc(s).layer(id).unwrap().link_group
|
||||
}
|
||||
|
||||
/// A and B linked (red blocks), C unlinked: (session, A, B, C, A and B's link group).
|
||||
fn linked_pair() -> (Session, LayerId, LayerId, LayerId, Option<u64>) {
|
||||
let mut s = session(8);
|
||||
let a = rect_layer(&mut s, Rect::new(0, 0, 5, 5));
|
||||
let b = rect_layer(&mut s, Rect::new(10, 0, 15, 5));
|
||||
let c = rect_layer(&mut s, Rect::new(20, 0, 25, 5));
|
||||
select_all(&mut s, &[a, b]);
|
||||
assert_eq!(s.execute("layer.linkLayers", json!({})).unwrap()["linked"], true);
|
||||
let group = link_group(&s, a);
|
||||
assert!(group.is_some() && link_group(&s, b) == group);
|
||||
(s, a, b, c, group)
|
||||
}
|
||||
|
||||
/// #11217 review: a copy of one linked layer starts unlinked, as in Photoshop, so ⌥⌘T's box over
|
||||
/// the copy doesn't take the originals along. The originals stay linked.
|
||||
#[test]
|
||||
fn duplicating_one_linked_layer_leaves_the_copy_unlinked() {
|
||||
let (mut s, a, b, c, group) = linked_pair();
|
||||
// One layer (what ⌥⌘T duplicates), and with an unlinked layer beside it (several selected).
|
||||
s.execute("layer.select", json!({"layer": a.0})).unwrap();
|
||||
let copy = LayerId(s.execute("layer.duplicate", json!({"inPlace": true})).unwrap()["layer"].as_u64().unwrap());
|
||||
assert_eq!(link_group(&s, copy), None, "the copy is unlinked");
|
||||
select_all(&mut s, &[a, c]);
|
||||
let r = s.execute("layer.duplicate", json!({})).unwrap();
|
||||
let copies: Vec<LayerId> = r["layers"].as_array().unwrap().iter().map(|v| LayerId(v.as_u64().unwrap())).collect();
|
||||
assert_eq!(copies.len(), 2);
|
||||
assert!(copies.iter().all(|id| link_group(&s, *id).is_none()), "a lone copy of a group has nothing to stay linked to");
|
||||
assert_eq!((link_group(&s, a), link_group(&s, b), link_group(&s, c)), (group, group, None), "the originals keep their links");
|
||||
}
|
||||
|
||||
/// #11217 review: copies of a linked pair stay linked to each other under a new group, not the
|
||||
/// originals', which keep theirs.
|
||||
#[test]
|
||||
fn duplicating_a_linked_pair_links_the_copies_to_each_other() {
|
||||
let (mut s, a, b, _, group) = linked_pair();
|
||||
select_all(&mut s, &[a, b]);
|
||||
let r = s.execute("layer.duplicate", json!({})).unwrap();
|
||||
let copies: Vec<LayerId> = r["layers"].as_array().unwrap().iter().map(|v| LayerId(v.as_u64().unwrap())).collect();
|
||||
assert_eq!(copies.len(), 2);
|
||||
let fresh = link_group(&s, copies[0]);
|
||||
assert!(fresh.is_some() && fresh != group, "a group of their own: {fresh:?} vs {group:?}");
|
||||
assert_eq!(link_group(&s, copies[1]), fresh, "the copies are linked to each other");
|
||||
assert_eq!((link_group(&s, a), link_group(&s, b)), (group, group), "the originals keep theirs");
|
||||
// Moving a copy moves its partner copy only.
|
||||
let (a0, b0) = (bounds(&s, a), bounds(&s, b));
|
||||
s.execute("layer.translate", json!({"layer": copies[0].0, "dx": 3})).unwrap();
|
||||
assert_eq!(bounds(&s, copies[1]), b0.translate(3, 0));
|
||||
assert_eq!((bounds(&s, a), bounds(&s, b)), (a0, b0));
|
||||
}
|
||||
|
||||
/// #7538: `selection: true` moves the selection with the layers in the same step, as the Move
|
||||
/// tool does with several layers selected; without it the selection stays.
|
||||
#[test]
|
||||
|
||||
@@ -283,6 +283,16 @@ pub(crate) fn targets_of(doc: &Document, selected: &[LayerId], active: Option<La
|
||||
if ids.is_empty() { active.into_iter().collect() } else { ids }
|
||||
}
|
||||
|
||||
/// The layers one box over `targets` moves: `targets` with the layers linked to them, as the Move
|
||||
/// tool takes them along. In Photoshop transforming one linked layer transforms the whole link
|
||||
/// group (#11217). A linked Background joins only when it is a target itself: `targets_of`
|
||||
/// leaves it out of a box over other layers, and a link doesn't bring it back in.
|
||||
pub fn box_targets(doc: &Document, targets: &[LayerId]) -> Vec<LayerId> {
|
||||
let mut ids = crate::layer_multi_cmds::move_targets(doc, targets);
|
||||
ids.retain(|id| targets.contains(id) || !doc.is_background(*id));
|
||||
ids
|
||||
}
|
||||
|
||||
/// The `layers` param: the layers one box transforms together (at most 10000, each in the
|
||||
/// document), without those a listed group carries. `None` when it isn't given.
|
||||
fn layers_param(doc: &Document, p: &Value) -> Result<Option<Vec<LayerId>>> {
|
||||
@@ -828,10 +838,11 @@ fn transform(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Err(EngineError::NoLayer(*missing));
|
||||
}
|
||||
// The named layer or the selection takes its linked layers along, as the Move tool does: in
|
||||
// Photoshop transforming one linked layer transforms the whole link group (#11217). A caller
|
||||
// listing `layers` gets exactly those.
|
||||
// Photoshop transforming one linked layer transforms the whole link group (#11217), but not a
|
||||
// linked Background it didn't target ([`box_targets`]). A caller listing `layers` gets
|
||||
// exactly those.
|
||||
if !listed {
|
||||
ids = crate::layer_multi_cmds::move_targets(&st.doc, &ids);
|
||||
ids = box_targets(&st.doc, &ids);
|
||||
}
|
||||
let rect = match p.get("rect").and_then(Value::as_array) {
|
||||
Some(r) if r.len() == 4 => {
|
||||
|
||||
@@ -380,6 +380,50 @@ fn a_locked_linked_layer_refuses_the_transform_of_its_link_group() {
|
||||
assert!(st.doc == before && st.revision == revision);
|
||||
}
|
||||
|
||||
/// The Background (white, 200 x 200) linked to "A", a red block at x 10..30, y 10..30; A is
|
||||
/// selected, and active.
|
||||
fn background_linked_to_a_layer() -> (Session, LayerId, LayerId) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 200, "height": 200, "background": "white"})).unwrap();
|
||||
let bg = s.active().unwrap().doc.layers[0].id;
|
||||
let a = LayerId(s.execute("layer.new.layer", json!({"name": "A"})).unwrap()["layer"].as_u64().unwrap());
|
||||
s.edit("paint", |doc, _| {
|
||||
doc.layer_mut(a).unwrap().surface_mut().unwrap().fill_rect(Rect::new(10, 10, 30, 30), &[1.0, 0.0, 0.0, 1.0]);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("layer.select", json!({"layer": bg.0})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": a.0, "mode": "add"})).unwrap();
|
||||
assert_eq!(s.execute("layer.linkLayers", json!({})).unwrap()["linked"], true);
|
||||
s.execute("layer.select", json!({"layer": a.0})).unwrap();
|
||||
(s, bg, a)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_linked_background_stays_out_of_a_transform_of_its_partner() {
|
||||
// #11217 review: the link group widening must not pull in a Background the transform didn't
|
||||
// target, any more than a selection of several does.
|
||||
let (mut s, bg, a) = background_linked_to_a_layer();
|
||||
for p in [json!({"matrix": [1, 0, 0, 1, 5, 0]}), json!({"layer": a.0, "matrix": [1, 0, 0, 1, 5, 0]})] {
|
||||
let r = s.execute("edit.transform", p.clone()).unwrap();
|
||||
assert_eq!(moved_layers(&r), [a.0], "{p}");
|
||||
let st = s.active().unwrap();
|
||||
assert!(st.doc.is_background(bg), "{p}: still the Background");
|
||||
assert_eq!(st.doc.layer(bg).unwrap().surface().unwrap().content_bounds(), Rect::new(0, 0, 200, 200), "{p}: and where it was");
|
||||
}
|
||||
assert_eq!(bounds(&s, a), Rect::new(20, 10, 40, 30), "A moved both times");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_transform_of_a_linked_background_still_transforms_it() {
|
||||
let (mut s, bg, a) = background_linked_to_a_layer();
|
||||
let r = s.execute("edit.transform", json!({"layer": bg.0, "matrix": [1, 0, 0, 1, 5, 0]})).unwrap();
|
||||
assert!(moved_layers(&r).contains(&bg.0), "the targeted Background moves: {r}");
|
||||
let st = s.active().unwrap();
|
||||
assert!(!st.doc.is_background(bg), "turned into a normal layer first, as in Photoshop");
|
||||
assert_eq!(bounds(&s, a), Rect::new(15, 10, 35, 30), "with the layer linked to it");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_recorded_transform_of_other_layers_than_the_selection_keeps_their_ids() {
|
||||
let (mut s, paint, _) = two_layers();
|
||||
|
||||
@@ -130,6 +130,9 @@ pub fn disabled(app: &crate::PhotocraftApp, id: &str) -> bool {
|
||||
// warp, and doesn't distort while editable type is among them.
|
||||
if transform {
|
||||
let targets = photocraft_engine::transform_cmds::selection_targets(st);
|
||||
// Judged on the layers the box moves, linked ones included (#11217): one linked layer
|
||||
// selected is a box over its link group, which doesn't warp either.
|
||||
let targets = photocraft_engine::transform_cmds::box_targets(&st.doc, &targets);
|
||||
if targets.len() > 1 || targets.first() != st.active_layer.as_ref() {
|
||||
let text = targets.iter().any(|t| st.doc.layer(*t).is_some_and(|l| matches!(l.content, LayerContent::Text(_))));
|
||||
return match id {
|
||||
@@ -362,6 +365,27 @@ mod tests {
|
||||
assert_eq!(checked, selections.len() * 2 * commands.len());
|
||||
}
|
||||
|
||||
/// #11217 review: one linked layer selected opens a box over its link group, which refuses
|
||||
/// Warp, so the menu greys Warp the same way it does for several selected layers; an unlinked
|
||||
/// layer keeps it.
|
||||
#[test]
|
||||
fn warp_is_greyed_for_a_layer_with_linked_partners() {
|
||||
let mut s = session();
|
||||
let id = |r: serde_json::Value| photocraft_doc::LayerId(r["layer"].as_u64().unwrap());
|
||||
let a = id(s.execute("layer.new.layer", json!({})).unwrap());
|
||||
let b = id(s.execute("layer.new.layer", json!({})).unwrap());
|
||||
let c = id(s.execute("layer.new.layer", json!({})).unwrap());
|
||||
s.execute("layer.select", json!({"layer": a.0})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": b.0, "mode": "add"})).unwrap();
|
||||
assert_eq!(s.execute("layer.linkLayers", json!({})).unwrap()["linked"], true);
|
||||
s.execute("layer.select", json!({"layer": a.0})).unwrap();
|
||||
let mut app = crate::PhotocraftApp::new(s, crate::Services::default());
|
||||
assert!(disabled(&app, "edit.transform.warp"), "a linked layer warps with its partner: not available");
|
||||
assert!(!disabled(&app, "edit.freeTransform") && !disabled(&app, "edit.transform.distort"), "the box itself is");
|
||||
app.session.execute("layer.select", json!({"layer": c.0})).unwrap();
|
||||
assert!(!disabled(&app, "edit.transform.warp"), "an unlinked layer warps");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ordinary_layers_keep_their_items() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -30,8 +30,9 @@ pub fn begin(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), String>
|
||||
// Several layers selected: one box moves them all, as in Photoshop (#6501).
|
||||
let targets = photocraft_engine::transform_cmds::selection_targets(st);
|
||||
// The selection takes its linked layers along, as `edit.transform` does: in Photoshop the box
|
||||
// over one linked layer frames and moves the whole link group (#11217). Unlinked, unchanged.
|
||||
let targets = photocraft_engine::layer_multi_cmds::move_targets(&doc, &targets);
|
||||
// over one linked layer frames and moves the whole link group (#11217), but not a linked
|
||||
// Background (`box_targets`). Unlinked, unchanged.
|
||||
let targets = photocraft_engine::transform_cmds::box_targets(&doc, &targets);
|
||||
if targets != [id] {
|
||||
return begin_several(app, ctx, doc, revision, id, targets);
|
||||
}
|
||||
|
||||
@@ -1999,6 +1999,11 @@ fn free_transform_of_a_linked_layer_boxes_its_link_group() {
|
||||
assert_eq!(t.rect, [8.0, 8.0, 56.0, 24.0], "framing A and B together");
|
||||
let pv = app.transform_preview.as_ref().unwrap();
|
||||
assert!(pv.several.as_ref().is_some_and(|sv| sv.ids == [a, b]), "the preview moves both");
|
||||
// The moving pixels hold B's blue block where B is, beside A's red one: B moves with the box
|
||||
// on the canvas, not only at the commit.
|
||||
assert_eq!(moving_texel(&app, [48, 16]).0, Some(Color32::BLUE), "the linked layer's pixels are in the preview");
|
||||
assert_eq!(moving_texel(&app, [16, 16]).0, Some(Color32::RED), "and the active layer's");
|
||||
assert_eq!(moving_texel(&app, [16, 48]).0, None, "the unlinked layer is outside the moving pixels");
|
||||
let t = app.ui.transform.as_mut().unwrap();
|
||||
t.quad = t.quad.map(|[x, y]| [x + 4.0, y]);
|
||||
commit(&mut app);
|
||||
@@ -2022,6 +2027,26 @@ fn free_transform_of_an_unlinked_layer_boxes_that_layer_alone() {
|
||||
assert!(app.transform_preview.as_ref().unwrap().several.is_none());
|
||||
}
|
||||
|
||||
/// #11217 review: a layer linked to the Background opens the one-layer box: the link doesn't pull
|
||||
/// the Background into the box, as the selection doesn't either (#6501).
|
||||
#[test]
|
||||
fn free_transform_of_a_layer_linked_to_the_background_boxes_that_layer_alone() {
|
||||
let mut app = app_with_square(64, photocraft_geom::Rect::new(8, 8, 24, 24));
|
||||
let a = app.session.active().unwrap().active_layer.unwrap();
|
||||
let bg = app.session.active().unwrap().doc.layers[0].id;
|
||||
app.session.execute("layer.select", json!({"layer": bg.0})).unwrap();
|
||||
app.session.execute("layer.select", json!({"layer": a.0, "mode": "add"})).unwrap();
|
||||
assert_eq!(app.session.execute("layer.linkLayers", json!({})).unwrap()["linked"], true);
|
||||
app.session.execute("layer.select", json!({"layer": a.0})).unwrap();
|
||||
begin(&mut app, &egui::Context::default()).unwrap();
|
||||
let t = app.ui.transform.as_ref().unwrap();
|
||||
assert_eq!((t.layer, t.rect), (a.0, [8.0, 8.0, 24.0, 24.0]), "the box frames A, not the canvas-sized Background");
|
||||
assert!(t.layers.is_empty(), "a one-layer session");
|
||||
let pv = app.transform_preview.as_ref().unwrap();
|
||||
assert!(pv.several.is_none());
|
||||
assert!(pv.doc.layer(bg).unwrap().visible, "the Background stays under the box");
|
||||
}
|
||||
|
||||
/// #11217: a locked layer in the link group refuses the box before it opens, as a locked layer
|
||||
/// among several selected ones does.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user