fix(layers): align a lone layer in an artboard to that artboard (#7945)

With one layer selected and no pixel selection, Align used the canvas
bounds, so a layer inside an artboard jumped out of its board (to the
middle of the canvas, between other boards). As in Photoshop, it now
aligns to the artboard holding it; a layer outside any artboard, and an
artboard itself, still align to the canvas. An explicit `to` is unchanged.
This commit is contained in:
NaufalDzz committed 2026-10-20 18:15:47 +07:00
1 parent 03d0c7e3b3
commit 0ce3a2ff2b
2 files changed
+37 -2

No files matched your search

+28
View File
@@ -85,6 +85,34 @@ fn from_layers_and_group_then_compose_clips() {
}
}
/// #7945: one layer selected inside an artboard aligns to that artboard, as in Photoshop, not to
/// the canvas (which would move it out of the board). The board itself still aligns to the canvas.
#[test]
fn a_lone_layer_in_an_artboard_aligns_to_the_artboard() {
for depth in [8, 16, 32] {
let mut s = session(depth);
let red = add_square(&mut s, Rect::new(10, 10, 20, 20), [1.0, 0.0, 0.0, 1.0]);
let board = LayerId(s.execute("layer.new.artboardFromLayers", json!({})).unwrap()["layer"].as_u64().unwrap());
s.execute("layer.artboard.set", json!({"x": 140, "y": 20, "width": 40, "height": 50})).unwrap();
let bounds = |s: &Session, id: LayerId| crate::layer_multi_cmds::layer_bounds(doc(s).layer(id).unwrap()).unwrap();
assert_eq!(bounds(&s, red), Rect::new(140, 20, 150, 30), "{depth}");
s.execute("layer.select", json!({"layer": red.0})).unwrap();
s.execute("layer.align.rightEdges", json!({})).unwrap();
assert_eq!(bounds(&s, red), Rect::new(170, 20, 180, 30), "{depth}: to the board's right edge");
s.execute("layer.align.verticalCenters", json!({})).unwrap();
assert_eq!(bounds(&s, red), Rect::new(170, 40, 180, 50), "{depth}: to the board's centre");
// An explicit `to` still wins.
s.execute("layer.align.topEdges", json!({"to": "canvas"})).unwrap();
assert_eq!(bounds(&s, red).y0, 0, "{depth}");
// The artboard alone has no board around it: its contents align to the canvas.
s.execute("layer.select", json!({"layer": board.0})).unwrap();
s.execute("layer.align.leftEdges", json!({})).unwrap();
assert_eq!(bounds(&s, red).x0, 0, "{depth}");
}
}
#[test]
fn clear_artboard_guides_only_inside() {
let mut s = session(8);
+9 -2
View File
@@ -965,8 +965,9 @@ fn align(s: &mut Session, p: &Value, kind: &str) -> Result<Value> {
// Photoshop: with an active selection, the layers align to its bounds (Layer › Align
// Layers to Selection), however many are selected.
(Some(m), _) if to == "auto" && !m.content_bounds().is_empty() => m.content_bounds(),
// One layer with no selection aligns to the canvas (otherwise it would align to itself).
(None, 1) if to == "auto" => doc.bounds(),
// One layer with no selection aligns to the canvas (otherwise it would align to
// itself), or to the artboard it sits in, as in Photoshop (#7945).
(None, 1) if to == "auto" => items.first().map_or_else(|| doc.bounds(), |(id, _)| board_or_canvas(doc, *id)),
_ => items.iter().fold(Rect::EMPTY, |a, (_, b)| a.union(b)),
},
other => return Err(bad(&cmd, format!("`to` must be auto|layers|selection|canvas, not `{other}`"))),
@@ -991,6 +992,12 @@ fn align(s: &mut Session, p: &Value, kind: &str) -> Result<Value> {
Ok(json!({"moved": moved}))
}
/// The bounds a lone layer aligns to: the artboard holding it, else the canvas. An artboard
/// itself aligns to the canvas.
fn board_or_canvas(doc: &Document, id: LayerId) -> Rect {
doc.artboard_of(id).filter(|board| *board != id).and_then(|board| doc.layer(board)?.artboard().map(|a| a.rect)).unwrap_or_else(|| doc.bounds())
}
/// One move per layer (first wins), skipping layers nested in another moved layer.
fn dedup_moves(doc: &Document, moves: Vec<(LayerId, i32, i32)>) -> Vec<(LayerId, i32, i32)> {
let mut out: Vec<(LayerId, i32, i32)> = Vec::new();