Merge pull request #8721 from storytold/train/carry8708

Artboard tool: Shift held during a move keeps it to its line (#8580) (#8708)
This commit is contained in:
BFlat
2026-11-19 21:46:19 +07:00
committed by GitHub
6 changed files with 163 additions and 23 deletions
+12
View File
@@ -127,6 +127,18 @@ pub fn move_delta(from: Point, to: Point, shift: bool, base: f64) -> Vec2 {
if shift { vectorcraft_geom::constrain_angle_from(d, 45.0, base) } else { d }
}
/// How far to slide along the line of unit direction `u` to take away the offset `pull` on one
/// axis (a snap of a move Shift keeps to that line): on an axis the line runs along, the shorter
/// way of the two; 0 when it runs along neither.
pub(crate) fn slide_along(pull: Vec2, u: Vec2) -> f64 {
[(pull.x, u.x), (pull.y, u.y)]
.into_iter()
.filter(|(_, c)| c.abs() > 1e-9)
.map(|(p, c)| p / c)
.min_by(|a, b| a.abs().total_cmp(&b.abs()))
.unwrap_or(0.0)
}
#[cfg(test)]
mod tests {
use super::*;
+2 -11
View File
@@ -45,7 +45,7 @@ use vectorcraft_doc::hit::{Hit, HitKind, hit_test, marquee_in_view, objects_at,
use vectorcraft_doc::{NodeId, OrientedBox};
use vectorcraft_geom::{Affine, Point, Rect, Vec2};
use crate::bbox::{Handle, hit_handle, in_rotate_zone, move_delta, rotate_for_drag, scale_for_drag};
use crate::bbox::{Handle, hit_handle, in_rotate_zone, move_delta, rotate_for_drag, scale_for_drag, slide_along};
use crate::blendwidget::{self, BlendWidgetDrag};
use crate::corners::{self, CornerDrag, over_widget};
use crate::guides::{PointSnap, Targets, distance_guides, hold_on_path};
@@ -397,16 +397,7 @@ impl MoveSnap {
return (d, vec![]);
}
let u = d / len;
// The way along the line that takes away the offset `pull` on one axis: on the axis the
// line runs along, the shorter way of the two.
let along = |pull: Vec2| {
[(pull.x, u.x), (pull.y, u.y)]
.into_iter()
.filter(|(_, c)| c.abs() > 1e-9)
.map(|(p, c)| p / c)
.min_by(|a, b| a.abs().total_cmp(&b.abs()))
.unwrap_or(0.0)
};
let along = |pull: Vec2| slide_along(pull, u);
let (targets, points) = if copy { (&self.copy_targets, &self.copy_points) } else { (&self.targets, &self.points) };
if let Some(ov) = targets.as_ref().and_then(|t| t.snap_back(self.bounds, d, cx.snap_tol())) {
return (Vec2::ZERO, ov);
+43 -3
View File
@@ -9,7 +9,8 @@
//! all of them (#1139). Shift-drag inside an artboard draws a new artboard there, and Shift-drag on
//! the pasteboard is a marquee: the artboards it touches leave the selection or join it. Drag inside
//! moves it (with its artwork when the `moveArt` option is on; Alt moves a copy and
//! leaves the artboard where it was), drag a handle resizes it
//! leaves the artboard where it was; Shift held once the drag is under way keeps the move or copy
//! to the level, upright or 45° line, counted from General › Constrain Angle), drag a handle resizes it
//! (Shift proportional, Alt from centre; with the `scaleArt` option on, Scale Artwork with Artboard,
//! always proportional, and the art fully inside scales with it), drag on the pasteboard draws a new artboard, Delete removes
//! the active one (Copy, Cut and Paste take it with its art: `artboard.copy`), the arrow keys move the
@@ -36,7 +37,7 @@ use vectorcraft_doc::ArtboardSide;
use vectorcraft_geom::{Affine, Point, Rect, Vec2};
use super::{BLUE, polygon, rect_corners};
use crate::bbox::{Handle, hit_handle, scale_for_drag};
use crate::bbox::{Handle, hit_handle, move_delta, scale_for_drag, slide_along};
use crate::guides::{DrawSnap, Leave, Targets};
use crate::{Action, Cursor, Mods, Overlay, PixelOp, PointerEvent, PointerKind, Tool, ToolContext, ToolKey};
@@ -247,6 +248,36 @@ impl ArtboardTool {
d + adj
}
/// [`Self::snap_move`] for a move Shift keeps to its line (through the press, along `d`):
/// every snap slides the move along that line. Whole pixels and the grid slide it by the
/// shorter of the ways that put its top-left on a whole pixel or a gridline on either axis
/// ([`slide_along`]); Smart Guides slide it until an edge or the centre lines up
/// ([`Targets::snap_rects_along`]). A move of zero length does not snap.
fn snap_move_along(&mut self, cx: &ToolContext, rect: Rect, d: Vec2, copy: bool) -> Vec2 {
self.guides.clear();
let len = d.hypot();
if !len.is_finite() || len <= 1e-9 {
return d;
}
let u = d / len;
let tl = Point::new(rect.x0, rect.y0) + d;
if cx.snaps_to_pixel(PixelOp::Moving) {
return d + u * slide_along(Vec2::new(tl.x.round() - tl.x, tl.y.round() - tl.y), u);
}
if cx.snap_to_grid {
return d + u * slide_along(vectorcraft_geom::snap::snap_point_to_grid(tl, cx.grid_step()) - tl, u);
}
let Some(t) = (if copy { &self.copy_targets } else { &self.targets }) else { return d };
let moved = rect + d;
let (adj, ov) = if cx.doc.setup.has_bleed() {
t.snap_rects_along(&[moved, cx.doc.setup.bleed_rect(moved)], u, cx.snap_tol())
} else {
t.snap_rects_along(&[moved], u, cx.snap_tol())
};
self.guides = ov;
d + adj
}
fn end_drag(&mut self) {
self.drag = None;
self.click_art = None;
@@ -433,7 +464,11 @@ impl Tool for ArtboardTool {
out.push(Action::Begin(label(m.alt).into()));
}
self.drag = Some(Drag::Move { index, many, start, rect, began: true, copy: m.alt });
let d = self.snap_move(cx, rect, p - start, m.alt);
// Shift held once the drag is under way keeps the move (or copy) to the level,
// upright or 45° line, as a Selection tool move does (#8580); a Shift-press draws
// or selects instead.
let d = move_delta(start, p, m.shift, cx.constrain_angle);
let d = if m.shift { self.snap_move_along(cx, rect, d, m.alt) } else { self.snap_move(cx, rect, d, m.alt) };
self.preview = Some(rect + d);
let mut v = json!({ "index": index, "dx": d.x, "dy": d.y, "moveArt": self.move_art, "copy": m.alt });
if many {
@@ -679,6 +714,11 @@ impl Tool for ArtboardTool {
}
fn cursor(&self, cx: &ToolContext, p: Point, m: Mods) -> Cursor {
// A move under way keeps the move cursor wherever the pointer is: Shift may keep the
// artboard to a line the pointer leaves, and the document shows the moved artboard.
if matches!(self.drag, Some(Drag::Move { began: true, .. })) {
return Cursor::Move;
}
if self.edge_button_at(cx, p).is_some() {
return Cursor::Arrow;
}
+100 -6
View File
@@ -367,10 +367,20 @@ fn a_shift_drag_on_the_pasteboard_selects_the_artboards_it_touches() {
assert_eq!(t.options()["selected"], json!([1, 2]));
}
/// A drag begun without Shift still moves the artboard, and on the pasteboard still draws one;
/// Shift pressed during either no longer keeps the move at 45° or the new artboard square.
/// The `(dx, dy)` of an `artboard.move` preview.
fn moved_by(v: &Value) -> (f64, f64) {
(v["dx"].as_f64().unwrap(), v["dy"].as_f64().unwrap())
}
fn near(a: (f64, f64), b: (f64, f64)) -> bool {
(a.0 - b.0).abs() < 1e-9 && (a.1 - b.1).abs() < 1e-9
}
/// Shift held once a move is under way keeps it to the level, upright or 45° line, at the length
/// dragged (#8580); without Shift it follows the pointer again. (A Shift-press draws or
/// selects rather than moving: `a_shift_drag_inside_an_artboard_draws_a_new_one`.)
#[test]
fn a_plain_drag_still_moves_and_shift_no_longer_constrains_it() {
fn shift_held_during_a_move_keeps_it_to_its_line() {
let d = two_boards();
let (s, p) = (Selection::default(), paint());
let mut c = cx(&d, &s, &p);
@@ -378,15 +388,99 @@ fn a_plain_drag_still_moves_and_shift_no_longer_constrains_it() {
let shift = Mods { shift: true, ..Default::default() };
let mut t = ArtboardTool::default();
t.pointer(&c, &ev(PointerKind::Down, 700.0, 100.0));
let v = preview_params(&t.pointer(&c, &ev(PointerKind::Drag, 730.0, 110.0).with_mods(shift)));
assert_eq!((v["index"].as_u64(), v["dx"].as_f64(), v["dy"].as_f64()), (Some(1), Some(30.0), Some(10.0)));
assert_eq!(t.pointer(&c, &ev(PointerKind::Up, 730.0, 110.0).with_mods(shift)), vec![Action::Commit]);
let mut drag = |dx: f64, dy: f64, m: Mods| moved_by(&preview_params(&t.pointer(&c, &ev(PointerKind::Drag, 700.0 + dx, 100.0 + dy).with_mods(m))));
assert!(near(drag(30.0, 10.0, shift), (1000f64.sqrt(), 0.0)), "level");
assert!(near(drag(12.0, 40.0, shift), (0.0, 1744f64.sqrt())), "upright");
let (dx, dy) = drag(30.0, 28.0, shift);
assert!(dx > 0.0 && (dx - dy).abs() < 1e-9, "45°: {dx} {dy}");
// Shift released: the move follows the pointer.
assert_eq!(drag(30.0, 10.0, Mods::default()), (30.0, 10.0));
assert_eq!(t.pointer(&c, &ev(PointerKind::Up, 730.0, 110.0)), vec![Action::Commit]);
// A new artboard drawn on the pasteboard isn't kept square by Shift held during the drag.
t.pointer(&c, &ev(PointerKind::Down, 1000.0, 1000.0));
t.pointer(&c, &ev(PointerKind::Drag, 1100.0, 1050.0).with_mods(shift));
let a = t.pointer(&c, &ev(PointerKind::Up, 1100.0, 1050.0).with_mods(shift));
assert_eq!(a, create_artboard(Rect::new(1000.0, 1000.0, 1100.0, 1050.0)));
}
/// An Alt-drag copy keeps to its line with Shift too, and the line turns with General › Constrain
/// Angle.
#[test]
fn shift_keeps_an_alt_drag_copy_to_its_line_from_the_constrain_angle() {
let d = two_boards();
let (s, p) = (Selection::default(), paint());
let mut c = cx(&d, &s, &p);
c.smart_guides = false;
let alt = Mods { alt: true, ..Default::default() };
let both = Mods { alt: true, shift: true, ..Default::default() };
let mut t = ArtboardTool::default();
t.pointer(&c, &ev(PointerKind::Down, 700.0, 100.0).with_mods(alt));
let v = preview_params(&t.pointer(&c, &ev(PointerKind::Drag, 730.0, 110.0).with_mods(both)));
assert!(near(moved_by(&v), (1000f64.sqrt(), 0.0)) && v["copy"] == true, "{v}");
// Alt released and pressed again mid-drag: a constrained move, then a constrained copy again.
let a = t.pointer(&c, &ev(PointerKind::Drag, 730.0, 110.0).with_mods(Mods { shift: true, ..Default::default() }));
assert_eq!(a[..2], [Action::Cancel, Action::Begin("Move Artboard".into())]);
let v = preview_params(&a);
assert!(near(moved_by(&v), (1000f64.sqrt(), 0.0)) && v["copy"] == false, "{v}");
let a = t.pointer(&c, &ev(PointerKind::Drag, 730.0, 110.0).with_mods(both));
assert_eq!(a[..2], [Action::Cancel, Action::Begin("Duplicate Artboard".into())]);
assert!(near(moved_by(&preview_params(&a)), (1000f64.sqrt(), 0.0)));
t.pointer(&c, &ev(PointerKind::Up, 730.0, 110.0).with_mods(both));
// Constrain Angle 30°: the lines run in 45° steps from 30° up the page (30° up, 15° down, …),
// and a drag 18.4° below level keeps to the one 15° below level (down the page is positive y).
c.constrain_angle = 30.0;
t.pointer(&c, &ev(PointerKind::Down, 700.0, 100.0));
let (dx, dy) =
moved_by(&preview_params(&t.pointer(&c, &ev(PointerKind::Drag, 730.0, 110.0).with_mods(Mods { shift: true, ..Default::default() }))));
assert!((dy.atan2(dx).to_degrees() - 15.0).abs() < 1e-9, "{dx} {dy}");
}
/// Snaps slide a move Shift keeps to its line along that line: Smart Guides, the grid and whole
/// pixels never take it off the line.
#[test]
fn snaps_keep_a_shift_move_on_its_line() {
let d = two_boards();
let (s, p) = (Selection::default(), paint());
let shift = Mods { shift: true, ..Default::default() };
let mut c = cx(&d, &s, &p);
let mut t = ArtboardTool::default();
let mut drag = |c: &ToolContext, dx: f64, dy: f64| {
t.pointer(c, &ev(PointerKind::Down, 700.0, 100.0));
let v = moved_by(&preview_params(&t.pointer(c, &ev(PointerKind::Drag, 700.0 + dx, 100.0 + dy).with_mods(shift))));
t.pointer(c, &ev(PointerKind::Up, 700.0 + dx, 100.0 + dy).with_mods(shift));
v
};
// Level, 101.5 pt left: the left edge, 1.5 pt past artboard 1's right edge (500), slides back
// onto it, and the move stays level.
assert!(near(drag(&c, -97.0, 30.0), (-100.0, 0.0)));
c.smart_guides = false;
c.snap_to_grid = true;
// Grid step 72 / 8 = 9: x 620.9 → 621, y stays 0 (not 9, where it would snap off the line).
assert!(near(drag(&c, 20.0, 6.0), (21.0, 0.0)));
c.snap_to_grid = false;
c.snap_to_pixel = true;
assert!(near(drag(&c, 10.0, 3.0), (10.0, 0.0)));
}
/// While Shift keeps a move to its line the cursor stays the move cursor, also where the pointer
/// has left the moved artboard; before a press, Shift shows the crosshair of a drag that draws or
/// selects.
#[test]
fn shift_during_a_move_keeps_the_move_cursor() {
let mut d = two_boards();
let (s, p) = (Selection::default(), paint());
let shift = Mods { shift: true, ..Default::default() };
let mut t = ArtboardTool::default();
let over = Point::new(1100.0, 250.0);
assert_eq!(t.cursor(&cx(&d, &s, &p), Point::new(700.0, 100.0), shift), Cursor::Crosshair);
t.pointer(&cx(&d, &s, &p), &ev(PointerKind::Down, 700.0, 100.0));
let (dx, dy) = moved_by(&preview_params(&t.pointer(&cx(&d, &s, &p), &ev(PointerKind::Drag, over.x, over.y).with_mods(shift))));
// The engine shows the preview: the level move leaves the pointer 50 pt below the artboard.
d.artboards[1].rect = d.artboards[1].rect + Vec2::new(dx, dy);
assert!(!d.artboards[1].rect.contains(over));
assert_eq!(t.cursor(&cx(&d, &s, &p), over, shift), Cursor::Move);
}
/// Escape during a marquee or while a new artboard is being drawn ends the gesture without
/// selecting or drawing anything and keeps the tool; the next Escape leaves it.
#[test]
File diff suppressed because one or more lines are too long
+3 -1
View File
File diff suppressed because one or more lines are too long