From ca6c0d9d131289016c888bed0efe634a6e7bb0fc Mon Sep 17 00:00:00 2001 From: David Mann Date: Tue, 1 Dec 2026 17:08:26 +0100 Subject: [PATCH] Move tool: a moved board takes in loose layers it covers (#18154) auto_nest now also looks at the loose (top-level) layers when a board moves: each one with more than a tenth of its area on a moved board joins it, on top of its stack, in the same Move step. The Alt-drag copy goes through the same path, so the layer joins the copy. A Prevent Auto-Nesting lock on the board or on the layer keeps it loose; layers of other boards are never taken. Co-Authored-By: Claude Opus 5.5 --- .../engine-core/src/cmd/layer_multi_cmds.rs | 3 + .../move_board_takes_layers_tests.rs | 139 ++++++++++++++++++ .../src/cmd/layer_multi_cmds/moves.rs | 32 +++- 3 files changed, 167 insertions(+), 7 deletions(-) create mode 100644 crates/engine-core/src/cmd/layer_multi_cmds/move_board_takes_layers_tests.rs diff --git a/crates/engine-core/src/cmd/layer_multi_cmds.rs b/crates/engine-core/src/cmd/layer_multi_cmds.rs index 32535415a2..2524314ff1 100644 --- a/crates/engine-core/src/cmd/layer_multi_cmds.rs +++ b/crates/engine-core/src/cmd/layer_multi_cmds.rs @@ -208,3 +208,6 @@ mod move_leave_board_top_tests; #[cfg(test)] mod move_nest_locked_target_tests; + +#[cfg(test)] +mod move_board_takes_layers_tests; diff --git a/crates/engine-core/src/cmd/layer_multi_cmds/move_board_takes_layers_tests.rs b/crates/engine-core/src/cmd/layer_multi_cmds/move_board_takes_layers_tests.rs new file mode 100644 index 0000000000..5d0c5a5603 --- /dev/null +++ b/crates/engine-core/src/cmd/layer_multi_cmds/move_board_takes_layers_tests.rs @@ -0,0 +1,139 @@ +//! #18154: a board moved with the Move tool (a drag or an Alt-drag copy) takes in each loose layer +//! that now has more than a tenth of its area on it, on top of its stack, in the same "Move" step. +//! A lock against auto-nesting on the board or on the layer keeps the layer loose, and layers of +//! other boards are never taken. + +use super::*; + +fn doc(s: &Session) -> &Document { + &s.active().unwrap().doc +} + +fn add_rect(s: &mut Session, r: Rect, name: &str) -> LayerId { + s.edit("rect", |doc, active| { + let fmt = doc.pixel_format(); + let mut l = Layer::raster(name.to_string(), fmt); + l.surface_mut().unwrap().fill_rect(r, &photocraft_raster::from_rgba(&fmt, [0.5, 0.5, 0.5, 1.0])); + let id = doc.insert_above(None, l); + *active = Some(id); + Ok(id) + }) + .unwrap() +} + +fn board(s: &mut Session, rect: [i32; 4], name: &str) -> LayerId { + LayerId(s.execute("layer.new.artboard", json!({"rect": rect, "name": name})).unwrap()["layer"].as_u64().unwrap()) +} + +fn into(s: &mut Session, id: LayerId, parent: LayerId) { + s.execute("layer.moveTo", json!({"layer": id.0, "target": parent.0, "position": "into"})).unwrap(); +} + +fn select(s: &mut Session, id: LayerId) { + s.execute("layer.select", json!({"layer": id.0, "mode": "replace"})).unwrap(); +} + +struct Doc { + s: Session, + b1: LayerId, + b2: LayerId, + c: LayerId, + d: LayerId, +} + +/// The issue's test document: Board1 (50,50-450,450) holds A, B and group G with G1; Board2 +/// (550,50-950,450) holds C; D (100,550-200,630) is a loose top-level layer. +fn setup() -> Doc { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 1000, "height": 700})).unwrap(); + let b1 = board(&mut s, [50, 50, 400, 400], "Board1"); + let b2 = board(&mut s, [550, 50, 400, 400], "Board2"); + let a = add_rect(&mut s, Rect::new(100, 100, 200, 200), "A"); + into(&mut s, a, b1); + let b = add_rect(&mut s, Rect::new(250, 250, 350, 350), "B"); + into(&mut s, b, b1); + let g = s + .edit("group", |doc, _| { + let fmt = doc.pixel_format(); + let mut g1 = Layer::raster("G1".to_string(), fmt); + g1.surface_mut().unwrap().fill_rect(Rect::new(300, 100, 400, 150), &photocraft_raster::from_rgba(&fmt, [0.5, 0.5, 0.5, 1.0])); + Ok(doc.insert_above(None, Layer::group("G", vec![g1]))) + }) + .unwrap(); + into(&mut s, g, b1); + let c = add_rect(&mut s, Rect::new(600, 100, 700, 200), "C"); + into(&mut s, c, b2); + let d = add_rect(&mut s, Rect::new(100, 550, 200, 630), "D"); + Doc { s, b1, b2, c, d } +} + +/// Drags the selected layers by `dx`,`dy` with the Move tool, auto-nesting on. +fn drag(s: &mut Session, dx: i32, dy: i32) { + s.execute("layer.translate", json!({"dx": dx, "dy": dy, "nest": true})).unwrap(); +} + +fn children(s: &Session, board: LayerId) -> Vec { + doc(s).layer(board).unwrap().children().unwrap().iter().map(|l| l.id).collect() +} + +#[test] +fn a_board_dragged_over_a_loose_layer_takes_it_in_on_top() { + let Doc { mut s, b1, d, .. } = setup(); + select(&mut s, b1); + let steps = s.active().unwrap().history.past_len(); + drag(&mut s, 0, 150); + assert_eq!(doc(&s).artboard_of(d), Some(b1), "Board1 covering 50 of D's 80 rows takes D in"); + assert_eq!(children(&s, b1).last(), Some(&d), "D lands on top of Board1's stack"); + assert_eq!(s.active().unwrap().history.past_len(), steps + 1, "the move and the nesting are one step"); + + s.undo(); + assert_eq!(doc(&s).artboard_of(d), None, "one Undo leaves D loose again"); + assert!(doc(&s).layers.iter().any(|l| l.id == d)); +} + +#[test] +fn more_than_a_tenth_of_the_layer_on_the_board_decides() { + // Board1's bottom edge at 450 + dy: 4 of D's 80 rows (5 %) at dy 104, 12 rows (15 %) at dy 112. + for (dy, taken) in [(104, false), (112, true)] { + let Doc { mut s, b1, d, .. } = setup(); + select(&mut s, b1); + drag(&mut s, 0, dy); + assert_eq!(doc(&s).artboard_of(d) == Some(b1), taken, "Board1 moved by {dy}"); + } +} + +#[test] +fn an_alt_drag_copy_takes_the_layer_into_the_copy() { + let Doc { mut s, b1, d, .. } = setup(); + let kept = children(&s, b1); + select(&mut s, b1); + // The Move tool's Alt-drag: an in-place copy, then the move of the copy. + s.execute("layer.duplicate", json!({"inPlace": true, "withLinks": true})).unwrap(); + let copy = s.active().unwrap().active_layer.unwrap(); + assert_ne!(copy, b1); + drag(&mut s, 0, 150); + assert_eq!(doc(&s).artboard_of(d), Some(copy), "D joins the copy"); + assert_eq!(children(&s, copy).last(), Some(&d)); + assert_eq!(children(&s, b1), kept, "the original board keeps just its own children"); + assert_eq!(doc(&s).walk().iter().filter(|(_, _, l)| l.name == "D").count(), 1, "D is not copied"); +} + +#[test] +fn a_lock_against_auto_nesting_keeps_the_layer_loose() { + for locked in ["board", "layer"] { + let Doc { mut s, b1, d, .. } = setup(); + select(&mut s, if locked == "board" { b1 } else { d }); + s.execute("layer.lockLayers", json!({"artboard": true})).unwrap(); + select(&mut s, b1); + drag(&mut s, 0, 150); + assert_eq!(doc(&s).artboard_of(d), None, "lock on the {locked}: D stays loose"); + } +} + +#[test] +fn layers_of_other_boards_are_never_taken() { + let Doc { mut s, b1, b2, c, .. } = setup(); + select(&mut s, b1); + drag(&mut s, 500, 0); + assert_eq!(doc(&s).artboard_of(c), Some(b2), "C stays in Board2 under the moved Board1"); +} diff --git a/crates/engine-core/src/cmd/layer_multi_cmds/moves.rs b/crates/engine-core/src/cmd/layer_multi_cmds/moves.rs index b0b31f91e2..1e295fd0e1 100644 --- a/crates/engine-core/src/cmd/layer_multi_cmds/moves.rs +++ b/crates/engine-core/src/cmd/layer_multi_cmds/moves.rs @@ -261,6 +261,10 @@ type Nest = ((usize, usize), LayerId, Option); /// with Lock › Prevent Auto-Nesting on the layer or a group around it. A board with that lock (or /// Lock All) takes no layer in: one moved onto it stays in its own board while it still overlaps /// it, else it goes to the top level (#18156). +/// +/// A moved board takes in the loose (top-level) layers that now have more than a tenth of their +/// area on it, on top of its stack (#18154), with the same locks, clip-group and depth rules. Layers +/// of other boards are never taken. /// Returns the layers that changed board, bottom to top. fn auto_nest(doc: &mut Document, ids: &[LayerId]) -> Vec { let boards: Vec<(LayerId, photocraft_geom::Rect)> = doc.artboards().into_iter().rev().map(|(id, _, board)| (id, board.rect)).collect(); @@ -273,8 +277,15 @@ fn auto_nest(doc: &mut Document, ids: &[LayerId]) -> Vec { let mut plan: Vec = Vec::new(); // The paths of the layers planned so far: a layer inside one of them goes with it. let mut carried: Vec> = Vec::new(); + // Moved boards take in the loose layers they now cover (#18154), unless locked against it. + let takers: Vec<(LayerId, photocraft_geom::Rect)> = boards.iter().filter(|(id, _)| ids.contains(id) && !locked_board(doc, *id)).copied().collect(); for (order, (path, _, l)) in doc.walk().into_iter().enumerate() { - if !ids.contains(&l.id) || l.artboard().is_some() || doc.is_background(l.id) || carried.iter().any(|c| path.starts_with(c)) { + if l.artboard().is_some() || doc.is_background(l.id) || carried.iter().any(|c| path.starts_with(c)) { + continue; + } + // Not moved itself: only a loose (top-level) layer, and only when a board moved. + let moved = ids.contains(&l.id); + if !moved && (path.len() != 1 || takers.is_empty()) { continue; } if l.clipped && crate::clip_group::base_of(doc, l.id).is_some() { @@ -283,12 +294,19 @@ fn auto_nest(doc: &mut Document, ids: &[LayerId]) -> Vec { let bounds = crate::layer_multi_cmds::layer_bounds(l); let Some(bounds) = bounds.filter(|b| !b.is_empty()) else { continue }; let from = doc.artboard_of(l.id); - let mut to = board_after_move(&boards, from, bounds); - // A board locked against auto-nesting takes nothing in (#18156): the layer stays in its own - // board while it still overlaps it, else it leaves for the top level. - if to.is_some_and(|b| locked_board(doc, b)) { - to = from.filter(|f| boards.iter().any(|(id, r)| id == f && !r.intersect(&bounds).is_empty())); - } + let to = if moved { + let to = board_after_move(&boards, from, bounds); + // A board locked against auto-nesting takes nothing in (#18156): the layer stays in its own + // board while it still overlaps it, else it leaves for the top level. + if to.is_some_and(|b| locked_board(doc, b)) { + from.filter(|f| boards.iter().any(|(id, r)| id == f && !r.intersect(&bounds).is_empty())) + } else { + to + } + } else { + // A loose layer joins a moved board holding more than a tenth of its area. + board_by_area(&takers, bounds) + }; let clipped = if l.clipped { Vec::new() } else { crate::clip_group::clipped_above(doc, l.id) }; let group: Vec<&Layer> = std::iter::once(l).chain(clipped.iter().filter_map(|id| doc.layer(*id))).collect(); if from == to || group.iter().any(|m| doc.effective_locks(m.id).artboard) {