PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(layers): align a lone layer in an artboard to that artboard (#7945)
NaufalDzz0ce3a2ff2b42
diff --git a/crates/engine/src/artboard_cmds/tests.rs b/crates/engine/src/artboard_cmds/tests.rsindex f3053e21f..65e20bd66 100644--- a/crates/engine/src/artboard_cmds/tests.rs+++ b/crates/engine/src/artboard_cmds/tests.rs@@ -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);diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rsindex 418b0051d..c30e2be29 100644--- a/crates/engine/src/layer_multi_cmds.rs+++ b/crates/engine/src/layer_multi_cmds.rs@@ -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();