From d7a61a8889ed3b391022f558dc48209c756bcb73 Mon Sep 17 00:00:00 2001 From: David Mann Date: Fri, 20 Nov 2026 19:45:06 +0100 Subject: [PATCH] Snap targets can reach past their spans A spanned target catches only a point or box overlapping its span on the other axis since canvas-w4, which keeps a layer's edge on its page from catching a box on another page. Photoshop-style layer edges snap a box beside them too (side-by-side boxes align their tops), so PhotoCraft's Move tool stopped snapping to other layers' edges. SnapTargets::reach_past_spans() lets every target catch along its whole line, the span only shaping the drawn smart guide. The default is unchanged, and filtered() keeps the setting. --- crates/behavior/src/geometry/snap.rs | 29 ++++++++++++++++++++------ crates/behavior/tests/geometry/snap.rs | 23 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/crates/behavior/src/geometry/snap.rs b/crates/behavior/src/geometry/snap.rs index b01e503a7..818d8e44c 100644 --- a/crates/behavior/src/geometry/snap.rs +++ b/crates/behavior/src/geometry/snap.rs @@ -233,16 +233,20 @@ pub enum SnapGesture { #[derive(Clone, Debug, Default, PartialEq)] pub struct SnapTargets { targets: Vec, + /// Whether a target with a span catches a point or box anywhere along its line, the span only shaping the drawn + /// smart guide ([`reach_past_spans`](Self::reach_past_spans)). + past_spans: bool, } /// Distances closer than this are equal. const TIE: f64 = 1e-9; /// Whether `target` reaches a point or box spanning `across` on the other axis: a target with a span (a layer's -/// edge on its page) only where the two overlap; a target without a span, or a query without an extent, always. -fn reaches(target: &SnapTarget, across: Option<(f64, f64)>) -> bool { +/// edge on its page) only where the two overlap, unless `past_spans`; a target without a span, or a query without an +/// extent, always. +fn reaches(target: &SnapTarget, across: Option<(f64, f64)>, past_spans: bool) -> bool { match (target.span, across) { - (Some((a, b)), Some((lo, hi))) => lo <= b && a <= hi, + (Some((a, b)), Some((lo, hi))) if !past_spans => lo <= b && a <= hi, _ => true, } } @@ -260,6 +264,17 @@ impl SnapTargets { Self::default() } + /// These targets with every span reaching past its extent: a target catches a point or box anywhere along its + /// line, and its span only shapes the drawn smart guide between the two objects. Photoshop snaps a moved layer's + /// edge to another layer's edge on the same line even when the two do not overlap across it (boxes side by side + /// align their tops); without this, a spanned target catches only what overlaps its span (a layer's edge on its + /// page). + #[must_use] + pub fn reach_past_spans(mut self) -> Self { + self.past_spans = true; + self + } + /// Add one target. pub fn add(&mut self, target: SnapTarget) { self.targets.push(target); @@ -330,7 +345,7 @@ impl SnapTargets { /// Only the targets `keep` accepts (Snap To with one kind off). pub fn filtered(&self, keep: impl Fn(SnapKind) -> bool) -> SnapTargets { - SnapTargets { targets: self.targets.iter().copied().filter(|t| keep(t.kind)).collect() } + SnapTargets { targets: self.targets.iter().copied().filter(|t| keep(t.kind)).collect(), past_spans: self.past_spans } } /// Whether there is no target. @@ -349,7 +364,7 @@ impl SnapTargets { /// span, it is `nearest_of`. pub fn nearest_within(&self, along: Axis, value: f64, across: Option<(f64, f64)>, tolerance: f64, kinds: SnapKinds) -> Option { let mut best: Option<(f64, SnapTarget)> = None; - for target in self.targets.iter().filter(|t| t.along == along && kinds.contains(t.kind) && reaches(t, across)) { + for target in self.targets.iter().filter(|t| t.along == along && kinds.contains(t.kind) && reaches(t, across, self.past_spans)) { let distance = (target.position - value).abs(); if distance > tolerance { continue; @@ -433,7 +448,9 @@ impl SnapTargets { let hit = self .targets .iter() - .filter(|t| t.along == along && kinds.contains(t.kind) && reaches(t, Some(other)) && (t.position - moved).abs() < 1e-6) + .filter(|t| { + t.along == along && kinds.contains(t.kind) && reaches(t, Some(other), self.past_spans) && (t.position - moved).abs() < 1e-6 + }) .min_by_key(|t| u8::from(t.kind != SnapKind::Guide)); if let Some(t) = hit { lines.push(line(t, other.0, other.1)); diff --git a/crates/behavior/tests/geometry/snap.rs b/crates/behavior/tests/geometry/snap.rs index 0a8c44c09..e40cc3702 100644 --- a/crates/behavior/tests/geometry/snap.rs +++ b/crates/behavior/tests/geometry/snap.rs @@ -56,3 +56,26 @@ fn a_spanned_target_catches_only_a_box_that_overlaps_its_span_on_the_other_axis( let (offset, _) = targets.snap_rect(Rect::new(402.0, 50.0, 450.0, 90.0), 4.0); assert_eq!(offset.x, -2.0, "an unspanned target is unchanged"); } + +/// With `reach_past_spans` (Photoshop's layer edges), a spanned target catches a box anywhere along its line: boxes +/// side by side align their tops, and the snapped line still reaches from one box to the other. +#[test] +fn reach_past_spans_lets_a_box_beside_a_layer_snap_to_its_edge() { + let mut targets = SnapTargets::new().reach_past_spans(); + // A layer at x 300..350, y 200..260. + targets.add_rect(Rect::new(300.0, 200.0, 350.0, 260.0), SnapKind::LayerEdge, SnapKind::LayerCenter); + + // A box left of the layer, its top 3 units above the layer's top: no overlap in x, still caught. + let (offset, lines) = targets.snap_rect(Rect::new(40.0, 197.0, 90.0, 237.0), 4.0); + assert_eq!(offset.y, 3.0, "the box's top snaps to the layer's top"); + let line = lines.iter().find(|l| l.along == Axis::X).expect("a horizontal smart guide"); + assert_eq!(line.span, Some((40.0, 350.0)), "the guide runs from the box to the layer"); + + // A point the same way. + let (at, _) = targets.snap_point(Point::new(60.0, 202.0), 4.0); + assert_eq!(at.y, 200.0); + + // The filtered copy keeps the reach. + let (offset, _) = targets.filtered(|kind| kind == SnapKind::LayerEdge).snap_rect(Rect::new(40.0, 197.0, 90.0, 237.0), 4.0); + assert_eq!(offset.y, 3.0, "Snap To with a kind off keeps the reach"); +}