Skip to content

VectorCraft: An open-source, vector art tool written by scratch in pure Rust

Fix #1534: a dragged selection snaps to ruler guides with Smart Guides on too

bflatastic50911d860864
diff --git a/crates/tools/src/guides.rs b/crates/tools/src/guides.rs
index 64bd89926..3e7aa338d 100644
--- a/crates/tools/src/guides.rs
+++ b/crates/tools/src/guides.rs
@@ -384,6 +384,12 @@ impl Targets {
self
}
+ /// The ruler guides alone ([`Self::with_rulers`]): what a dragged selection's grabbed point
+ /// still snaps to with View → Snap to Point when Smart Guides line the selection up.
+ pub(crate) fn rulers(cx: &ToolContext) -> Self {
+ Self::default().styled(cx).with_rulers(cx)
+ }
+
/// What a dragged selection's grabbed point snaps to with View → Snap to Point and Smart
/// Guides off: the anchors of the art but `exclude` and the ruler guides.
pub fn snap_to_point(cx: &ToolContext, exclude: &[NodeId]) -> Option<Self> {
diff --git a/crates/tools/src/select.rs b/crates/tools/src/select.rs
index 33e1099e1..e828364e6 100644
--- a/crates/tools/src/select.rs
+++ b/crates/tools/src/select.rs
@@ -90,6 +90,9 @@ pub(crate) struct MoveSnap {
/// original.
copy_targets: Option<Targets>,
copy_points: Option<Targets>,
+ /// With Smart Guides on, the ruler guides View › Snap to Point still pulls the grabbed point
+ /// onto (#1534); `points` holds them with Smart Guides off.
+ rulers: Option<Targets>,
}
impl MoveSnap {
@@ -102,6 +105,7 @@ impl MoveSnap {
points: points(&cx.selection.objects),
copy_targets: guides.then(|| Targets::for_move(cx, &[])),
copy_points: points(&[]),
+ rulers: (guides && cx.snap_to_point).then(|| Targets::rulers(cx)),
}
}
@@ -133,6 +137,14 @@ impl MoveSnap {
guides = ov;
}
}
+ // A ruler guide within reach of the grabbed point wins over lining the bounds up.
+ if let Some(t) = &self.rulers {
+ let (q, ov) = t.snap_point(start + d, cx.tol(cx.snap_tolerance));
+ if q != start + d {
+ d = q - start;
+ guides = ov;
+ }
+ }
if cx.snap_to_pixel
&& let Some(b) = self.bounds
{
@@ -733,6 +745,29 @@ mod tests {
assert_eq!(a, vec![Action::Commit]);
}
+ /// #1534: with Smart Guides on (the default), the point a selection is dragged by still snaps
+ /// to a ruler guide within the Snap to Point distance; not with Snap to Point off.
+ #[test]
+ fn a_move_snaps_the_grabbed_point_to_a_ruler_guide_with_smart_guides_on() {
+ let (mut d, id) = doc_with_rect();
+ d.guides = vec![vectorcraft_doc::Guide::new(true, 173.4)];
+ let mut s = Selection::default();
+ s.add(id);
+ let p = paint();
+ let dx = |c: &ToolContext, to: f64| {
+ let mut t = SelectionTool::default();
+ t.pointer(c, &ev(PointerKind::Down, 150.0, 150.0));
+ let a = t.pointer(c, &ev(PointerKind::Drag, to, 150.0));
+ let Some(Action::Preview(_, v)) = a.last() else { panic!("{a:?}") };
+ v["matrix"][4].as_f64().unwrap()
+ };
+ let c = cx(&d, &s, &p);
+ assert!(c.smart_guides && c.snap_to_point);
+ assert!((dx(&c, 172.0) - 23.4).abs() < 1e-9, "onto the guide");
+ assert_eq!(dx(&c, 165.0), 15.0, "out of reach");
+ assert_eq!(dx(&ToolContext { snap_to_point: false, ..cx(&d, &s, &p) }, 172.0), 22.0);
+ }
+
/// Alt pressed during a move makes a copy, which snaps to the original it leaves behind (as
/// gathered before the previews moved it); released, the move doesn't.
#[test]