Smart Guides: a press reuses the targets the hover gathered for the same document state

Part of #8296.

DrawSnap::press gathered the snap targets from all the art in the window on
every press, while a hover reuses them until the document's revision, what is
left out, the window or a snapping option changes. Every drawing tool hovers
over the point it then presses, so each Pen click (and each press of the
shape, Type, Artboard and Scissors tools) walked the whole document again for
targets equal to the ones it already held.

A press now gathers only when that key differs from the one the targets were
gathered for, the same check hover makes. The drag a press starts still keeps
the press's targets.

The Scissors test that pressed over a different document under the hover's
revision now gives that document a new revision, since the engine moves the
revision with every change to the document.
This commit is contained in:
dotCooCoo committed 2026-11-27 12:39:00 -08:00
1 parent 98b56f3d97
commit bde2e10eec
3 files changed
+27 -18

No files matched your search

+2 -2
View File
@@ -520,7 +520,7 @@ fn scissors_shows_paths_alignment_and_configured_anchor_snaps() {
}
/// A cached snap is for the same raw position, document state and drawing options only. A press
/// gathers fresh targets, even when a previous hover's document state is reused by a caller.
/// gathers fresh targets when the document's revision moved since the hover.
#[test]
fn scissors_cursor_and_press_use_current_snaps() {
let (d, rect) = doc_with_rect();
@@ -564,7 +564,7 @@ fn scissors_cursor_and_press_use_current_snaps() {
let before = cx(&crossing, &selected, &paint);
let mut tool = AnchorTool::new("scissors");
tool.pointer(&before, &PointerEvent::new(PointerKind::Move, 152.0, 101.5));
let after = cx(&d, &selected, &paint);
let after = ToolContext { revision: (0, 1), ..cx(&d, &selected, &paint) };
let actions = tool.pointer(&after, &PointerEvent::new(PointerKind::Down, 152.0, 101.5));
assert!(scissors_cut_at(&d, rect, &actions).distance(Point::new(152.0, 100.0)) < 1e-6, "{actions:?}");
}
+17 -10
View File
@@ -325,9 +325,10 @@ pub fn square(d: Vec2) -> Vec2 {
/// too, so they all snap alike, and shows its [`Self::guides`].
///
/// The targets ([`Targets::for_drawing`], the art in the window) are gathered once per document
/// state: hovering gathers them again only after the document, the selection, what is left out,
/// the window or the snapping options changed; a press always gathers them afresh, and the drag
/// it starts keeps them, the document then showing the drag's own previews.
/// state: hovering and pressing gather them again only after the document, the selection, what is
/// left out, the window or the snapping options changed, so a press keeps the targets the hover
/// before it gathered. The drag a press starts keeps them, the document then showing the drag's
/// own previews.
///
/// The Measure and Measure & Inspect tools keep one too, for the ends of a drag only. They do not
/// ask it while hovering. [`Self::measure`] gathers [`Targets::for_measure`] instead, and a press
@@ -415,17 +416,14 @@ impl DrawSnap {
/// Snap the pointer hovering at `p` (the art but `exclude` as targets, the anchors of
/// `exclude` still pulling), reusing the targets while nothing they depend on changed.
pub fn hover(&mut self, cx: &ToolContext, p: Point, exclude: &[NodeId], from: Option<&Leave>) -> Point {
let key = SnapKey::of(cx, exclude);
if self.targets.as_ref().is_none_or(|(k, _)| k.as_ref() != Some(&key)) {
self.gather(cx, key);
}
self.gather_unless_current(cx, exclude);
self.snap(cx, p, from)
}
/// Snap the point pressed at `p`, the targets gathered afresh: the drag that follows keeps
/// them ([`Self::drag`]).
/// Snap the point pressed at `p`, reusing the targets the last hover or press gathered while
/// nothing they depend on changed: the drag that follows keeps them ([`Self::drag`]).
pub fn press(&mut self, cx: &ToolContext, p: Point, exclude: &[NodeId], from: Option<&Leave>) -> Point {
self.gather(cx, SnapKey::of(cx, exclude));
self.gather_unless_current(cx, exclude);
self.snap(cx, p, from)
}
@@ -544,6 +542,15 @@ impl DrawSnap {
};
}
/// Gather the targets for the art but `exclude` unless the last hover or press gathered them
/// for the same [`SnapKey`].
fn gather_unless_current(&mut self, cx: &ToolContext, exclude: &[NodeId]) {
let key = SnapKey::of(cx, exclude);
if self.targets.as_ref().is_none_or(|(k, _)| k.as_ref() != Some(&key)) {
self.gather(cx, key);
}
}
fn gather(&mut self, cx: &ToolContext, key: SnapKey) {
let snap = PointSnap::new(cx, || Targets::for_drawing(cx.doc, &key.exclude, visible(cx), key.snap_to_glyph));
self.targets = Some((Some(key), snap));
+8 -6
View File
@@ -682,8 +682,8 @@ fn drawn_points_snap_to_ruler_guides_with_smart_guides_on() {
assert_eq!(snap.press(&off, Point::new(260.0, 315.0), &[], None), Point::new(260.0, 315.0));
}
/// A drawing tool's snapper reuses its targets while hovering over the same document state, and
/// gathers them again when it changes, and on every press.
/// A drawing tool's snapper reuses its targets while hovering and pressing over the same document
/// state, and gathers them again when it changes.
#[test]
fn draw_snap_gathers_once_per_document_state() {
let (d, _) = doc_with_rect();
@@ -694,17 +694,19 @@ fn draw_snap_gathers_once_per_document_state() {
let c = ToolContext { revision: (1, 1), ..cx(&d, &s, &p) };
assert_eq!(snap.hover(&c, at, &[], None), Point::new(200.0, 200.0));
assert_eq!(labels(snap.guides()), ["anchor"]);
// The same revision: the targets are the ones gathered (the document isn't read again).
// The same revision: the targets are the ones gathered (the document isn't read again), for
// a hover and for the press after it.
let c = ToolContext { revision: (1, 1), ..cx(&empty, &s, &p) };
assert_eq!(snap.hover(&c, at, &[], None), Point::new(200.0, 200.0));
// A new revision, or a press, reads it.
assert_eq!(snap.press(&c, at, &[], None), Point::new(200.0, 200.0));
// A new revision reads it, for a hover and for a press.
let c = ToolContext { revision: (1, 2), ..cx(&empty, &s, &p) };
assert_eq!(snap.hover(&c, at, &[], None), at);
assert!(snap.guides().is_empty());
let c = ToolContext { revision: (1, 2), ..cx(&d, &s, &p) };
let c = ToolContext { revision: (1, 3), ..cx(&d, &s, &p) };
assert_eq!(snap.press(&c, at, &[], None), Point::new(200.0, 200.0));
// The drag keeps what the press gathered, though the document now shows its preview.
let c = ToolContext { revision: (1, 3), ..cx(&empty, &s, &p) };
let c = ToolContext { revision: (1, 4), ..cx(&empty, &s, &p) };
assert_eq!(snap.drag(&c, at, None), Point::new(200.0, 200.0));
snap.clear();
assert!(snap.guides().is_empty());