Merge pull request #3336 from dotCooCoo/fix/issue-1816

Part of #1816: Snap to Grid lands a moved object by its nearest edge and no longer snaps its center
This commit is contained in:
BFlat
2026-10-21 17:32:01 +07:00
committed by GitHub
4 changed files with 76 additions and 21 deletions
+3 -3
View File
@@ -1784,8 +1784,8 @@ fn bounding_box_handles_snap_to_the_grid() {
}
}
/// A Shift-constrained move with Snap to Grid on stays level: the square's center lands on a
/// gridline across, and its top stays where it was, off the grid.
/// A Shift-constrained move with Snap to Grid on stays level: the square's left or right edge
/// lands on a gridline across, and its top stays where it was, off the grid.
#[test]
fn a_shift_move_snapped_to_the_grid_stays_on_its_line() {
let mut s = session();
@@ -1799,7 +1799,7 @@ fn a_shift_move_snapped_to_the_grid_stays_on_its_line() {
s.pointer(&PointerEvent::new(kind, x, y).with_mods(shift), v).unwrap();
}
let b = s.doc().unwrap().doc.node(a).unwrap().geometric_bounds().unwrap();
assert!(on_grid(b.center().x) && (b.y0 - 101.0).abs() < 1e-9, "{b:?}, grid step {step}");
assert!((on_grid(b.x0) || on_grid(b.x1)) && (b.y0 - 101.0).abs() < 1e-9, "{b:?}, grid step {step}");
}
/// Follow-up to #1254: toggling or adding anchors of a path selected as a whole starts from all
+23
View File
@@ -2020,6 +2020,29 @@ mod tests {
}
}
/// #1816: Group Selection and Direct Selection move a whole path with Snap to Grid as the
/// Selection tool does, landing an edge on the grid; the center does not snap. A 45 pt square at
/// 90–135 on the 9 pt grid, dragged right by 22 to 112–157, lands its left edge on the gridline
/// 108 and moves by 18. Its center, at 134.5 after the drag, is nearer the gridline 135 and
/// does not snap.
#[test]
fn a_move_of_objects_lands_an_edge_on_the_grid() {
let mut d = vectorcraft_doc::Document::new(500.0, 500.0);
let l = d.layers[0].id;
let id = d.alloc_id();
let sq = vectorcraft_geom::shapes::rectangle(Rect::new(90.0, 90.0, 135.0, 135.0));
d.insert(Some(l), 0, vectorcraft_doc::Node::path(id, sq, vectorcraft_doc::Appearance::default_art())).unwrap();
let p = paint();
let mut s = Selection::default();
s.set([id]);
let c = ToolContext { snap_to_grid: true, ..cx(&d, &s, &p) };
for group in [true, false] {
let mut t = DirectSelectionTool::new(group);
let (v, _) = drag(&mut t, &c, (110.0, 110.0), (132.0, 110.0), Mods::default());
assert_eq!(v["matrix"], json!([1.0, 0.0, 0.0, 1.0, 18.0, 0.0]), "group: {group}");
}
}
/// Group Selection and Direct Selection on a whole path keep a Shift-constrained move on its
/// line when it snaps, as the Selection tool does: level by 13.04 with Snap to Grid on, B's
/// right side (413.04) lands on the gridline 414, and its top stays at 300, between 297 and
+49 -17
View File
@@ -104,7 +104,7 @@ pub struct SelectionTool {
}
/// Snapping for the selection moved as a whole, its targets gathered when the move begins: View →
/// Snap to Grid lands its bounds on the grid (#740), taking over from the rest; otherwise Smart
/// Snap to Grid lands its edges on the grid (#740), taking over from the rest; otherwise Smart
/// Guides line its bounds up with the other art, and with them off, View → Snap to Point lands the
/// point it was grabbed by (an anchor pressed near, else the pressed point) on an anchor or a ruler
/// guide. Snap to Pixel puts its top-left on whole pixels. A copy (Alt, which can go down or up at
@@ -246,12 +246,12 @@ impl MoveSnap {
}
/// The shift that lands `r` on the grid (a gridline every `step`), on each axis by whichever of its
/// two edges and its centre is nearest a gridline, however far that is.
/// two edges is nearest a gridline, however far that is. The center does not snap (#1816).
fn grid_pull(r: Rect, step: f64) -> Vec2 {
let pull = |vs: [f64; 3]| {
let pull = |vs: [f64; 2]| {
vs.iter().map(|v| vectorcraft_geom::snap::snap_to_grid(*v, step, 0.0) - v).min_by(|a, b| a.abs().total_cmp(&b.abs())).unwrap_or(0.0)
};
Vec2::new(pull([r.x0, r.center().x, r.x1]), pull([r.y0, r.center().y, r.y1]))
Vec2::new(pull([r.x0, r.x1]), pull([r.y0, r.y1]))
}
pub fn matrix_json(a: Affine) -> Value {
@@ -1232,10 +1232,10 @@ mod tests {
assert_eq!(t.cursor(&c, Point::new(377.0, 371.0), alt), Cursor::SelectionCopy);
}
/// #740: with Snap to Grid on, a moved object lands on the grid (every 9 pt by default) by
/// whichever of its edges or its centre is nearest a gridline, on each axis.
/// #740, #1816: with Snap to Grid on, a moved object lands on the grid (every 9 pt by default)
/// by whichever of its edges is nearest a gridline, on each axis.
#[test]
fn a_move_lands_the_nearest_edge_or_centre_on_the_grid() {
fn a_move_lands_the_nearest_edge_on_the_grid() {
let (d, id) = doc_with_rect();
let mut s = Selection::default();
s.add(id);
@@ -1250,9 +1250,10 @@ mod tests {
other => panic!("{other:?}"),
}
};
// Bounds 100–200 moved by (13, 4): the centres (163, 154) are nearest gridlines (162, 153).
// Bounds 100–200 moved by (13, 4): the right and bottom edges (213, 204) are nearest
// gridlines (216, 207).
let (x, y) = moved((163.0, 154.0));
assert!((x - 12.0).abs() < 1e-9 && (y - 3.0).abs() < 1e-9, "{x} {y}");
assert!((x - 16.0).abs() < 1e-9 && (y - 7.0).abs() < 1e-9, "{x} {y}");
// Moved by (7, 0): the right edge is on a gridline (207) and the top goes up to one (99).
let (x, y) = moved((157.0, 150.0));
assert!((x - 7.0).abs() < 1e-9 && (y + 1.0).abs() < 1e-9, "{x} {y}");
@@ -1277,7 +1278,7 @@ mod tests {
/// A Shift-constrained move snapped to the grid stays on its line: a level or upright one lands
/// on the grid along its own axis only, and a diagonal one slides along the diagonal until an
/// edge or the center is on a gridline on the axis that needs the shorter way there.
/// edge is on a gridline on the axis that needs the shorter way there.
#[test]
fn a_shift_move_snapped_to_the_grid_stays_on_its_line() {
let (d, id) = doc_with_rect();
@@ -1285,16 +1286,47 @@ mod tests {
s.add(id);
let p = paint();
let cx = ToolContext { snap_to_grid: true, ..cx(&d, &s, &p) };
// Level by 13.04: the center (163.04) lands on the gridline 162, and the top stays at 100,
// between the gridlines 99 and 108.
// Level by 13.04: the right edge (213.04) lands on the gridline 216, and the top stays at
// 100, between the gridlines 99 and 108.
let (m, _) = shift_moved(&cx, (163.0, 151.0));
assert!(moved_by(m, (12.0, 0.0)), "{m:?}");
assert!(moved_by(m, (16.0, 0.0)), "{m:?}");
let (m, _) = shift_moved(&cx, (151.0, 163.0));
assert!(moved_by(m, (0.0, 12.0)), "{m:?}");
// Up and to the right by 13 on each axis: the center (163, 137) is 1 from the gridline 162
// and 2 from 135, so the move goes back along the diagonal by 1 on each axis.
assert!(moved_by(m, (0.0, 16.0)), "{m:?}");
// Up and to the right by 13 on each axis: the bottom edge (187) is 2 from the gridline 189,
// and the right edge (213) is 3 from 216, so the move goes back along the diagonal by 2 on
// each axis.
let (m, _) = shift_moved(&cx, (163.0, 137.0));
assert!(moved_by(m, (12.0, -12.0)), "{m:?}");
assert!(moved_by(m, (11.0, -11.0)), "{m:?}");
}
/// #1816: on a 20 pt grid (gridlines every 100, 5 subdivisions), the 100 pt square at 100–200
/// sits on the grid with its center half a step off. A move by (8, 8) brings the left and top
/// edges back onto the gridlines at 100, so the square stays where it was; a move by (31, 29)
/// lands its edges on the nearest gridlines (140, 120). The center does not snap.
#[test]
fn a_move_keeps_an_object_on_the_grid_when_its_center_is_off_it() {
let (mut d, id) = doc_with_rect();
d.grid = vectorcraft_doc::GridPrefs { spacing: 100.0, subdivisions: 5 };
let mut s = Selection::default();
s.add(id);
let p = paint();
let cx = ToolContext { snap_to_grid: true, ..cx(&d, &s, &p) };
let moved = |to: (f64, f64)| {
let mut t = SelectionTool::default();
t.pointer(&cx, &ev(PointerKind::Down, 150.0, 150.0));
let a = t.pointer(&cx, &ev(PointerKind::Drag, to.0, to.1));
let Some(Action::Preview(_, v)) = a.get(1) else { panic!("{a:?}") };
Vec2::new(v["matrix"][4].as_f64().unwrap(), v["matrix"][5].as_f64().unwrap())
};
let m = moved((158.0, 158.0));
assert!(moved_by(m, (0.0, 0.0)), "{m:?}");
let m = moved((181.0, 179.0));
assert!(moved_by(m, (40.0, 20.0)), "{m:?}");
// With Shift, level and diagonal moves by 8 come back to where they started too.
let (m, _) = shift_moved(&cx, (158.0, 150.0));
assert!(moved_by(m, (0.0, 0.0)), "{m:?}");
let (m, _) = shift_moved(&cx, (158.0, 158.0));
assert!(moved_by(m, (0.0, 0.0)), "{m:?}");
}
/// A Shift-constrained move with Snap to Pixel on puts the top-left on a whole pixel on the
+1 -1
View File
@@ -599,7 +599,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
Object › Arrange › Send to Current Layer returns the error "the target layer is locked or hidden" and leaves the selected objects where they are when the current layer is hidden or locked, or is inside a hidden or locked layer.
Selected objects are deselected, the key object included, when a row drag moves them into a hidden layer or group, when `node.move` moves them into a hidden or locked layer or group, when Merge Selected moves them into a hidden or locked layer, when Collect in New Layer moves them into a new layer inside a hidden or locked layer, when Flatten Artwork moves them into a locked layer, and when applying a data set hides them.
Panel Options › Locate Selected Objects (beyond Illustrator, off by default): when the last selected object changes on the canvas or by a command, the panel opens the layers and groups around it and scrolls to its row, as Locate Object does, without highlighting the row; when Show Layers Only, a search or the filter leaves the object without a row, the panel opens no rows for it and scrolls as with the option off.
- **Drawing aids:** Smart Guides and snapping (moving, with the Selection, Group Selection or Direct Selection tool, and resizing by the bounding-box handles with the Selection or Free Transform tool, Shift and Alt too: the dragged corner or side lands on the other objects' anchors and centres ("anchor", "center") or lines up with their edges and centres (a line and "align"); with Smart Guides › Spacing Guides, a moved selection that lines up with nothing along an axis lands as far from its neighbour as two other objects of its row or column are apart, or halfway between its two neighbours, the equal gaps marked; with View › Snap to Grid on, a moved selection lands on the grid instead, by whichever of its edges or its centre is nearest a gridline on each axis, #740; the Rotate, Scale, Shear and Reflect tools drag from the anchor they grab and snap the pointer the same way, so a corner dragged onto another object's anchor scales exactly onto it, #482; anchors dragged with Direct Selection: the one pressed on lands on other anchors, its own path's that stay put among them, on path segments or in line with them, the others following, Shift keeps 45° steps, and with Smart Guides off Snap to Point pulls it onto anchors; the Artboard tool snaps artboards, and their bleed, to other artboards, bleeds and art, the grid or whole pixels while moving, copying and resizing them, and the Transform panel shows and edits the active artboard's X/Y/W/H; drawn points, #506: hovering shows where the press goes, the point lands on the other objects' anchors, centres and paths ("anchor", "center", "path") or lines up with their anchors, edges and centres ("align"), the Pen's on the path being drawn too, near a construction guide through the anchor before it on that guide (Preferences › Smart Guides › Construction Guides and Angles), and with Shift kept to its 45° step or a square's diagonal it slides along that way into line; the targets are gathered once per document state, from the art in the window, and the anchors, centres and lines near the pointer are found by bisection, so hovering never rescans the document), guides dragged out of the rulers (#451: they snap as moved guides do, Shift to the ruler's ticks; with the Artboard tool they are artboard guides, which run across their artboard only and move, copy and delete with it, `guide.add {artboard}`) that the Selection, Direct Selection and Group Selection tools then pick and drag (#414: the selected guides highlight and move together, Alt copies them, Shift snaps the dragged one to the ticks, otherwise it snaps to pixels, the grid or the edges, centres (so the sides' midpoints) and anchors of the art and the artboards, or with Smart Guides off Snap to Point pulls it onto anchors; Shift-clicked they join the selected art, and Shift-added art keeps them, so guides and art are selected together in either order, Delete then deleting both in one step; the arrow keys nudge them, Delete deletes them, dragging one back onto its ruler deletes it, Lock Guides and Hide Guides leave them alone; `guide.list`, `guide.select`, `guide.move` and `guide.remove` reach agents), a right-click on either ruler or the origin box to pick the document's units (the current one checked; `document.setUnits`, one undo step, #528), and Draw Normal / Behind / Inside modes. Smart Guides: a point drawn with the Pen or a shape tool, and an anchor dragged with Direct Selection, snaps to where paths and ruler guides cross, labelled "intersect" (#1658, part of #1602; a moved selection's own anchors don't).
- **Drawing aids:** Smart Guides and snapping (moving, with the Selection, Group Selection or Direct Selection tool, and resizing by the bounding-box handles with the Selection or Free Transform tool, Shift and Alt too: the dragged corner or side lands on the other objects' anchors and centres ("anchor", "center") or lines up with their edges and centres (a line and "align"); with Smart Guides › Spacing Guides, a moved selection that lines up with nothing along an axis lands as far from its neighbour as two other objects of its row or column are apart, or halfway between its two neighbours, the equal gaps marked; with View › Snap to Grid on, a moved selection lands on the grid instead, by whichever of its edges is nearest a gridline on each axis (the center does not snap), #740, #1816; the Rotate, Scale, Shear and Reflect tools drag from the anchor they grab and snap the pointer the same way, so a corner dragged onto another object's anchor scales exactly onto it, #482; anchors dragged with Direct Selection: the one pressed on lands on other anchors, its own path's that stay put among them, on path segments or in line with them, the others following, Shift keeps 45° steps, and with Smart Guides off Snap to Point pulls it onto anchors; the Artboard tool snaps artboards, and their bleed, to other artboards, bleeds and art, the grid or whole pixels while moving, copying and resizing them, and the Transform panel shows and edits the active artboard's X/Y/W/H; drawn points, #506: hovering shows where the press goes, the point lands on the other objects' anchors, centres and paths ("anchor", "center", "path") or lines up with their anchors, edges and centres ("align"), the Pen's on the path being drawn too, near a construction guide through the anchor before it on that guide (Preferences › Smart Guides › Construction Guides and Angles), and with Shift kept to its 45° step or a square's diagonal it slides along that way into line; the targets are gathered once per document state, from the art in the window, and the anchors, centres and lines near the pointer are found by bisection, so hovering never rescans the document), guides dragged out of the rulers (#451: they snap as moved guides do, Shift to the ruler's ticks; with the Artboard tool they are artboard guides, which run across their artboard only and move, copy and delete with it, `guide.add {artboard}`) that the Selection, Direct Selection and Group Selection tools then pick and drag (#414: the selected guides highlight and move together, Alt copies them, Shift snaps the dragged one to the ticks, otherwise it snaps to pixels, the grid or the edges, centres (so the sides' midpoints) and anchors of the art and the artboards, or with Smart Guides off Snap to Point pulls it onto anchors; Shift-clicked they join the selected art, and Shift-added art keeps them, so guides and art are selected together in either order, Delete then deleting both in one step; the arrow keys nudge them, Delete deletes them, dragging one back onto its ruler deletes it, Lock Guides and Hide Guides leave them alone; `guide.list`, `guide.select`, `guide.move` and `guide.remove` reach agents), a right-click on either ruler or the origin box to pick the document's units (the current one checked; `document.setUnits`, one undo step, #528), and Draw Normal / Behind / Inside modes. Smart Guides: a point drawn with the Pen or a shape tool, and an anchor dragged with Direct Selection, snaps to where paths and ruler guides cross, labelled "intersect" (#1658, part of #1602; a moved selection's own anchors don't).
A move that Shift keeps level, upright or diagonal stays on that line when it snaps, with the Selection, Group Selection or Direct Selection tool. It slides along the line until the selection lands on the grid or on whole pixels, lines up with the other objects or is spaced evenly with them, or until the line passes an anchor or crosses a ruler guide near the point the selection was grabbed by. A level or upright move snaps along its own axis only.
A bounding-box handle dragged with the Selection or Free Transform tool snaps what it moves as a dragged point snaps (#2054). With View › Snap to Grid on, it snaps to the nearest gridline, and with Snap to Pixel on, it snaps to whole pixels. With Smart Guides off and Snap to Point on, it snaps to the other objects' anchors and the ruler guides within the Snap to Point distance. With Smart Guides and Snap to Point both on, it lines up with the ruler guides as well as the other objects. Shift still keeps the proportions, and Alt still scales about the center.
A user slice's edge or corner handle dragged with the Slice Selection tool snaps as the Slice tool's drag does. With View › Snap to Grid on, it snaps to the nearest gridline, and with Snap to Pixel on, it snaps to whole pixels. Otherwise, with Smart Guides on, it lands on or lines up with the anchors, centers and edges of the art and the artboards, and with Smart Guides off and Snap to Point on, it snaps to the art's anchors and the ruler guides within the Snap to Point distance. Then a ruler guide within 5 px, shown and unlocked, moves the side or corner onto it. Shift still keeps the proportions, and Alt still resizes about the center.