Move tool: join an artboard holding more than a tenth of the layer (#18152)

A layer moved off its own board joins a board that holds more than 10 % of
its area, not only the board under its centre; with 10 % or less it goes to
the top level.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-12-01 14:38:25 +01:00
1 parent c0ef473423
commit 15068b2c17
4 files changed
+125 -11

No files matched your search

@@ -1144,15 +1144,15 @@ fn a_layer_straddling_two_boards_joins_the_one_under_its_middle() {
/// -0.5, left of a board starting at x = 0, so it stays off the board. Truncating toward zero put
/// its middle at 0, inside the board.
#[test]
fn a_layer_centred_just_left_of_a_board_at_negative_coordinates_stays_off_it() {
fn a_layer_a_tenth_on_a_board_at_negative_coordinates_stays_off_it() {
let mut s = session(8);
let b = LayerId(s.execute("layer.new.artboard", json!({"rect": [0, 0, 20, 20], "name": "B"})).unwrap()["layer"].as_u64().unwrap());
let id = add_square(&mut s, Rect::new(-24, 5, -17, 10), [1.0, 0.0, 0.0, 1.0]);
let id = add_square(&mut s, Rect::new(-24, 5, -14, 10), [1.0, 0.0, 0.0, 1.0]);
assert_eq!(doc(&s).artboard_of(id), None);
s.execute("layer.translate", json!({"layer": id.0, "dx": 20, "dy": 0, "nest": true})).unwrap();
assert_eq!(pixel_bounds(&s, id), Rect::new(-4, 5, 3, 10));
assert_eq!(doc(&s).artboard_of(id), None, "centred at -0.5, left of B");
// One pixel further right (-3..4, middle 0.5) is on the board.
s.execute("layer.translate", json!({"layer": id.0, "dx": 15, "dy": 0, "nest": true})).unwrap();
assert_eq!(pixel_bounds(&s, id), Rect::new(-9, 5, 1, 10));
assert_eq!(doc(&s).artboard_of(id), None, "1 of 10 columns on B is not more than a tenth");
// One pixel further right (-8..2) puts two tenths on the board, its centre still left of it (#18152).
s.execute("layer.translate", json!({"layer": id.0, "dx": 1, "dy": 0, "nest": true})).unwrap();
assert_eq!(doc(&s).artboard_of(id), Some(b));
}
@@ -199,3 +199,6 @@ mod group_from_layers_one_layer_expanded_tests;
#[cfg(test)]
mod move_artboard_overlap_tests;
#[cfg(test)]
mod move_join_board_by_area_tests;
@@ -0,0 +1,93 @@
//! #18152: after a Move drag a layer that no longer overlaps its own artboard joins another board
//! as soon as more than a tenth of its area lies on it, even with its centre off that board; with a
//! tenth or less it goes to the top level. It joins at the top of the board's stack, in one "Move" step.
use super::*;
fn doc(s: &Session) -> &Document {
&s.active().unwrap().doc
}
fn add_rect(s: &mut Session, r: Rect) -> LayerId {
s.edit("rect", |doc, active| {
let fmt = doc.pixel_format();
let mut l = Layer::raster(doc.next_layer_name("Layer"), fmt);
l.surface_mut().unwrap().fill_rect(r, &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 1.0, 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())
}
/// Board1 at 50,50-450,450 and Board2 at 550,50-950,450, C (600,100-700,200) in Board2, and a
/// loose top-level layer at `loose`.
fn two_boards(loose: Rect) -> (Session, LayerId, LayerId, LayerId, LayerId) {
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 c = add_rect(&mut s, Rect::new(600, 100, 700, 200));
s.execute("layer.moveTo", json!({"layer": c.0, "target": b2.0, "position": "into"})).unwrap();
let l = add_rect(&mut s, loose);
assert_eq!(doc(&s).artboard_of(l), None);
(s, b1, b2, c, l)
}
fn move_layer(s: &mut Session, id: LayerId, dx: i32, dy: i32) {
s.execute("layer.select", json!({"layer": id.0, "mode": "replace"})).unwrap();
let steps = s.active().unwrap().history.past_len();
s.execute("layer.translate", json!({"dx": dx, "dy": dy, "nest": true})).unwrap();
let hist = &s.active().unwrap().history;
assert_eq!((hist.past_len(), hist.undo_label()), (steps + 1, Some("Move")), "one Move step");
}
fn children(s: &Session, board: LayerId) -> Vec<LayerId> {
doc(s).layer(board).unwrap().children().unwrap().iter().map(|l| l.id).collect()
}
#[test]
fn a_loose_layer_joins_a_board_holding_more_than_a_tenth_of_it() {
// A 100x100 layer from 100,550: at x 470-570 a fifth is on Board2, at x 490-590 two fifths
// with its centre (540) still off the board.
for dx in [370, 390] {
let (mut s, _, b2, c, a) = two_boards(Rect::new(100, 550, 200, 650));
move_layer(&mut s, a, dx, -450);
assert_eq!(doc(&s).artboard_of(a), Some(b2), "dx {dx}");
assert_eq!(children(&s, b2), vec![c, a], "on top of the board's stack");
s.undo();
assert_eq!(doc(&s).artboard_of(a), None, "Undo takes it back");
}
// At x 460-560 a tenth is on Board2: not more than a tenth, so it stays loose.
let (mut s, _, _, _, a) = two_boards(Rect::new(100, 550, 200, 650));
move_layer(&mut s, a, 360, -450);
assert_eq!(doc(&s).artboard_of(a), None);
}
#[test]
fn a_wide_layer_joins_by_area_with_its_centre_far_off_the_board() {
// 200x40 below Board1, moved up: 5 px (an eighth) on the board joins it, 4 px (a tenth) doesn't.
let (mut s, b1, _, _, d) = two_boards(Rect::new(100, 550, 300, 590));
move_layer(&mut s, d, 0, -105);
assert_eq!(doc(&s).artboard_of(d), Some(b1));
let (mut s, _, _, _, d) = two_boards(Rect::new(100, 550, 300, 590));
move_layer(&mut s, d, 0, -104);
assert_eq!(doc(&s).artboard_of(d), None);
}
#[test]
fn a_layer_leaving_its_board_joins_another_by_area() {
// C leaves Board2 for x 420-520: three tenths on Board1, its centre (470) on no board.
let (mut s, b1, b2, c, _) = two_boards(Rect::new(100, 550, 200, 650));
move_layer(&mut s, c, -180, 0);
assert_eq!(doc(&s).artboard_of(c), Some(b1));
assert!(children(&s, b2).is_empty());
// x 440-540: a tenth on Board1, so the top level.
let (mut s, _, _, c, _) = two_boards(Rect::new(100, 550, 200, 650));
move_layer(&mut s, c, -160, 0);
assert_eq!(doc(&s).artboard_of(c), None);
}
@@ -214,11 +214,28 @@ fn board_under(boards: &[(LayerId, photocraft_geom::Rect)], bounds: photocraft_g
boards.iter().find(|(_, r)| r.contains(cx, cy)).map(|(id, _)| *id)
}
/// The artboard a moved layer at `bounds` belongs to, as in Photoshop (#18140): the board under its
/// centre ([`board_under`]); with its centre on no board, its own board `from` while it still
/// overlaps it, else none (the top level).
/// The artboard holding more than a tenth of the area of a layer at `bounds` (#18152): the one
/// holding the most of it, the topmost on a tie. `boards` are top to bottom.
fn board_by_area(boards: &[(LayerId, photocraft_geom::Rect)], bounds: photocraft_geom::Rect) -> Option<LayerId> {
let area = u128::from(bounds.size().area());
let mut best: Option<(LayerId, u128)> = None;
for (id, r) in boards {
let on = u128::from(r.intersect(&bounds).size().area());
if on * 10 > area && best.is_none_or(|(_, b)| on > b) {
best = Some((*id, on));
}
}
best.map(|(id, _)| id)
}
/// The artboard a moved layer at `bounds` belongs to, as in Photoshop: the board under its centre
/// ([`board_under`]); with its centre on no board, its own board `from` while it still overlaps it
/// (#18140), else a board holding more than a tenth of its area ([`board_by_area`], #18152), else
/// none (the top level).
fn board_after_move(boards: &[(LayerId, photocraft_geom::Rect)], from: Option<LayerId>, bounds: photocraft_geom::Rect) -> Option<LayerId> {
board_under(boards, bounds).or_else(|| from.filter(|f| boards.iter().any(|(id, r)| id == f && !r.intersect(&bounds).is_empty())))
board_under(boards, bounds)
.or_else(|| from.filter(|f| boards.iter().any(|(id, r)| id == f && !r.intersect(&bounds).is_empty())))
.or_else(|| board_by_area(boards, bounds))
}
/// One planned auto-nest move: its order (walk order, then its place above a clip base), the
@@ -228,7 +245,8 @@ type Nest = ((usize, usize), LayerId, Option<LayerId>, Option<LayerId>);
/// Move-tool auto-nesting, as in Photoshop: after a Move drag, a layer now on another artboard
/// joins that board, on top of its stack, and a layer moved off every board leaves its board for
/// the top level, directly above the board it left. A layer whose centre is on no board but that
/// still overlaps its own board stays in it ([`board_after_move`]). Artboards share one canvas, so without this a
/// still overlaps its own board stays in it, and one off its board joins a board holding more than a tenth of
/// its area ([`board_after_move`]). Artboards share one canvas, so without this a
/// layer dragged onto another board stayed in its old board and was clipped away by it.
///
/// A clipping group moves as one: a base that changes board takes the layers clipped to it along,