PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Snapping: layer targets ignore the selection, so a moved selection snaps to guides and the document (#6290) (#6355)
David Manna002e66824f9
diff --git a/crates/engine/src/snap.rs b/crates/engine/src/snap.rsindex 3ab1ad73b..ef0e72f5d 100644--- a/crates/engine/src/snap.rs+++ b/crates/engine/src/snap.rs@@ -89,10 +89,21 @@ pub fn layer_rect(doc: &Document, id: LayerId) -> Option<[f64; 4]> {}fn rect_of(doc: &Document, l: &photocraft_doc::Layer) -> Option<[f64; 4]> {- if l.is_group() || (matches!(l.content, LayerContent::Adjustment(_)) && l.mask.is_none()) {- return None;- }- let b = crate::transform_cmds::transform_bounds(doc, l);+ snappable(l).then(|| crate::transform_cmds::transform_bounds(doc, l)).and_then(to_rect)+}++/// Bounds of a layer as a Snap To Layers target: all of its content, whatever the selection+/// holds of it (#6290: clipped to the selection, every layer's edges and centre were the+/// selection's, so a moved selection snapped back to where it started).+fn target_rect_of(l: &photocraft_doc::Layer) -> Option<[f64; 4]> {+ snappable(l).then(|| crate::transform_cmds::whole_layer_bounds(l)).and_then(to_rect)+}++fn snappable(l: &photocraft_doc::Layer) -> bool {+ !(l.is_group() || (matches!(l.content, LayerContent::Adjustment(_)) && l.mask.is_none()))+}++fn to_rect(b: photocraft_geom::Rect) -> Option<[f64; 4]> {(!b.is_empty()).then_some([b.x0 as f64, b.y0 as f64, b.x1 as f64, b.y1 as f64])}@@ -121,7 +132,7 @@ impl SnapTargets {if !l.visible || exclude.contains(&l.id) {continue;}- if let Some(r) = rect_of(doc, l) {+ if let Some(r) = target_rect_of(l) {t.add_rect(r, SnapKind::LayerEdge, SnapKind::LayerCenter);}}@@ -365,6 +376,17 @@ mod tests {let t = SnapTargets::from_document(&doc, &SnapOptions { guides: false, ..opts }, &[id]);assert!(!t.x.iter().any(|t| t.kind == SnapKind::Guide || (t.kind == SnapKind::LayerEdge && t.pos == 30.0)));assert_eq!(layer_rect(&doc, id), Some([10.0, 20.0, 30.0, 60.0]));+ // #6290: a selection doesn't move the layers' targets onto its own bounds.+ let mut sel = photocraft_raster::Surface::new(photocraft_color::PixelFormat::GRAY8);+ sel.fill_rect(photocraft_geom::Rect::new(64, 66, 124, 86), &[1.0]);+ doc.selection = Some(sel);+ let t = SnapTargets::from_document(&doc, &SnapOptions { selection: false, ..opts }, &[]);+ assert!(t.x.iter().any(|t| t.kind == SnapKind::LayerEdge && t.pos == 30.0));+ assert!(+ !t.x.iter()+ .chain(&t.y)+ .any(|t| matches!(t.kind, SnapKind::LayerEdge | SnapKind::LayerCenter) && [64.0, 66.0, 76.0, 86.0, 94.0, 124.0].contains(&t.pos))+ );assert_eq!(union([[0.0, 0.0, 1.0, 1.0], [2.0, -1.0, 3.0, 0.5]]), Some([0.0, -1.0, 3.0, 1.0]));}}diff --git a/crates/engine/src/transform_cmds.rs b/crates/engine/src/transform_cmds.rsindex 1c0153a42..310bb13bb 100644--- a/crates/engine/src/transform_cmds.rs+++ b/crates/engine/src/transform_cmds.rs@@ -54,7 +54,7 @@ fn framed_by_selection(doc: &Document, bounds: Rect) -> Rect {}/// A group moves all its descendants, so its frame must not clip raster children to the selection.-fn whole_layer_bounds(layer: &Layer) -> Rect {+pub(crate) fn whole_layer_bounds(layer: &Layer) -> Rect {let content = match &layer.content {LayerContent::Group(g) => g.children.iter().map(whole_layer_bounds).fold(Rect::EMPTY, |a, b| a.union(&b)),_ => layer.surface().map_or(Rect::EMPTY, photocraft_compose::bounds::content_bounds),diff --git a/crates/ui-egui/src/marquee_tests.rs b/crates/ui-egui/src/marquee_tests.rsindex 169d4588b..321a31e73 100644--- a/crates/ui-egui/src/marquee_tests.rs+++ b/crates/ui-egui/src/marquee_tests.rs@@ -546,6 +546,29 @@ fn mouse_drag_inside_the_selection_moves_it() {assert_eq!(selection(&h), Rect::new(120, 90, 220, 170), "moved by (20, 10)");}+/// #6290: with a fresh app's View › Snap To, a selection moved through the real canvas snaps its+/// edges to a guide and the document bounds. Layers' targets were clipped to the selection, so+/// it snapped back to its own starting edges and centre (shown as layer alignments) instead.+#[test]+fn a_moved_selection_snaps_to_guides_and_the_document_not_its_start() {+ for (to, want) in [+ // Right edge 3 px left of the guide at x = 200; 3 px down stays put (not back to y = 52).+ ([145.0, 103.0], Rect::new(100, 55, 200, 105)),+ // 5 and 7 px from the document's top-left corner (its centre was 3 px off its start).+ ([53.0, 55.0], Rect::new(0, 0, 100, 50)),+ ] {+ let mut h = harness(Tool::RectMarquee);+ h.state_mut().run("view.newGuide", json!({"orientation": "vertical", "position": 200})).unwrap();+ h.state_mut().run("select.rect", json!({"x": 52, "y": 52, "width": 100, "height": 50})).unwrap();+ h.run_steps(2);+ drag_to(&mut h, [100.0, 100.0], to);+ let up = screen(&h, to[0], to[1]);+ button(&mut h, up, false, Modifiers::NONE);+ h.run_steps(2);+ assert_eq!(selection(&h), want, "dragged to {to:?}");+ }+}+/// #5646: the Move tool keeps Photoshop's arrow pointer instead of the 4-way move cursor (the/// hover outline still marks the layer under the pointer).#[test]