mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-21 09:54:09 +01:00
Tools: a move stops snapping to ruler guides hidden during it (#8363)
A Selection or Direct Selection move gathers the ruler guides to snap to when it begins. When they stopped showing during the move (Trim View or Hide Guides turned on), the move still snapped to them. Now the ruler guides are dropped from the move's targets once they are hidden, for the rest of the move; its other snapping stays.
This commit is contained in:
1 parent
1cb320d4a0
commit
e2298698d3
5 files changed
+69
-7
No files matched your search
@@ -1657,7 +1657,7 @@ impl DirectSelectionTool {
|
||||
// The grabbed anchor snaps and the others follow it. Shift keeps the move at its
|
||||
// angle, and the anchor slides along it onto what Smart Guides find there (#886).
|
||||
self.guides.clear();
|
||||
if segment && let Some(grid) = &self.partial_grid {
|
||||
if segment && let Some(grid) = &mut self.partial_grid {
|
||||
(d, self.guides) = if ev.mods.shift { grid.snap_along(cx, grab, d, false) } else { grid.snap(cx, grab, d, false) };
|
||||
} else if let Some(snap) = &self.anchor_snap {
|
||||
let shift = ev.mods.shift.then(|| Leave::segment(cx, grab, true));
|
||||
@@ -1710,7 +1710,7 @@ impl DirectSelectionTool {
|
||||
&& let Some(snap) = &self.partial_snap
|
||||
{
|
||||
let held = grab.unwrap_or(hold);
|
||||
if let Some(grid) = &self.partial_grid {
|
||||
if let Some(grid) = &mut self.partial_grid {
|
||||
(d, self.guides) = if ev.mods.shift { grid.snap_along(cx, held, d, false) } else { grid.snap(cx, held, d, false) };
|
||||
} else {
|
||||
let shift = ev.mods.shift.then(|| Leave::segment(cx, held, true));
|
||||
@@ -1730,7 +1730,7 @@ impl DirectSelectionTool {
|
||||
let shift = ev.mods.shift.then(|| Leave::segment(cx, grab, true));
|
||||
let (q, guides) = snap.snap_from(cx, grab + (p - start), shift.as_ref());
|
||||
(d, self.guides) = (q - grab, guides);
|
||||
} else if let Some(snap) = &self.move_snap {
|
||||
} else if let Some(snap) = &mut self.move_snap {
|
||||
(d, self.guides) = if ev.mods.shift { snap.snap_along(cx, hold, d, ev.mods.alt) } else { snap.snap(cx, hold, d, ev.mods.alt) };
|
||||
}
|
||||
self.snapped = snapped(d, unsnapped, ev.mods.shift, &self.guides);
|
||||
|
||||
@@ -707,6 +707,12 @@ impl Targets {
|
||||
self
|
||||
}
|
||||
|
||||
/// These targets without the ruler guides, once they stop showing during the drag they were
|
||||
/// gathered for (#8363).
|
||||
pub(crate) fn forget_rulers(&mut self) {
|
||||
self.rulers.clear();
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
|
||||
@@ -309,10 +309,24 @@ impl MoveSnap {
|
||||
Self { bounds, targets: None, points: None, copy_targets: None, copy_points: None, rulers: None, by_edge: false }
|
||||
}
|
||||
|
||||
/// The ruler guides stopped showing since the move began (View › Hide Guides, or Trim View,
|
||||
/// #8363): the move no longer snaps to them, for the rest of it, while its other targets stay.
|
||||
/// They were gathered when it began, and nothing pulls toward a guide the canvas does not draw.
|
||||
fn forget_hidden_rulers(&mut self, cx: &ToolContext) {
|
||||
if cx.guides {
|
||||
return;
|
||||
}
|
||||
self.rulers = None;
|
||||
for t in [&mut self.points, &mut self.copy_points].into_iter().flatten() {
|
||||
t.forget_rulers();
|
||||
}
|
||||
}
|
||||
|
||||
/// The move (`copy`: the copy) by `d` of the selection grabbed at `start`, snapped, and its
|
||||
/// guides. With Smart Guides › Snap to Last Location, a move within Snapping Tolerance of where
|
||||
/// it began goes back there before anything else snaps ([`Targets::snap_back`]).
|
||||
pub(crate) fn snap(&self, cx: &ToolContext, start: Point, mut d: Vec2, copy: bool) -> (Vec2, Vec<Overlay>) {
|
||||
pub(crate) fn snap(&mut self, cx: &ToolContext, start: Point, mut d: Vec2, copy: bool) -> (Vec2, Vec<Overlay>) {
|
||||
self.forget_hidden_rulers(cx);
|
||||
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);
|
||||
@@ -391,7 +405,8 @@ impl MoveSnap {
|
||||
/// passes an anchor or crosses a ruler guide ([`Targets::snap_point_along`]). A move of zero
|
||||
/// length does not snap, and Snap to Last Location goes back to where the move began first, as
|
||||
/// in [`Self::snap`].
|
||||
pub(crate) fn snap_along(&self, cx: &ToolContext, start: Point, mut d: Vec2, copy: bool) -> (Vec2, Vec<Overlay>) {
|
||||
pub(crate) fn snap_along(&mut self, cx: &ToolContext, start: Point, mut d: Vec2, copy: bool) -> (Vec2, Vec<Overlay>) {
|
||||
self.forget_hidden_rulers(cx);
|
||||
let len = d.hypot();
|
||||
if !len.is_finite() || len <= 1e-9 {
|
||||
return (d, vec![]);
|
||||
@@ -1957,7 +1972,7 @@ impl SelectionTool {
|
||||
}
|
||||
let mut d = move_delta(start, p, m.shift, cx.constrain_angle);
|
||||
let unsnapped = d;
|
||||
if let Some(snap) = &self.moving {
|
||||
if let Some(snap) = &mut self.moving {
|
||||
(d, self.guides) = if m.shift { snap.snap_along(cx, hold, d, m.alt) } else { snap.snap(cx, hold, d, m.alt) };
|
||||
}
|
||||
// Shift has already constrained `d`: any change is a snap.
|
||||
|
||||
@@ -2198,7 +2198,7 @@ fn move_follows_the_while_moving_option() {
|
||||
let p = paint();
|
||||
let pixel_snap = PixelSnap { moving: false, ..PixelSnap::default() };
|
||||
let c = ToolContext { snap_to_pixel: true, pixel_snap, smart_guides: false, snap_to_point: false, ..cx(&d, &s, &p) };
|
||||
let snap = MoveSnap::new(&c);
|
||||
let mut snap = MoveSnap::new(&c);
|
||||
let (dv, _) = snap.snap(&c, Point::new(150.0, 150.0), Vec2::new(3.3, 4.6), false);
|
||||
assert!((dv - Vec2::new(3.3, 4.6)).hypot() < 1e-9, "{dv:?}");
|
||||
let on = ToolContext { pixel_snap: PixelSnap::default(), ..c };
|
||||
|
||||
@@ -15563,3 +15563,44 @@ fn trim_view_toggled_during_a_ruler_origin_drag_cancels_it() {
|
||||
assert_eq!(origin(&app) != was, sets, "Trim View {start} then {end}: origin set {sets}");
|
||||
}
|
||||
}
|
||||
|
||||
/// #8363: a Selection move begun with the guides shown gathers the ruler guides to snap to. When
|
||||
/// they stop showing during the move (Trim View or Hide Guides turned on), it no longer snaps to
|
||||
/// them; with them shown throughout, it does.
|
||||
#[test]
|
||||
fn a_move_stops_snapping_to_guides_hidden_during_it() {
|
||||
// What hides the guides mid-move (none: they stay shown), and whether the move then snaps.
|
||||
for (hide, snaps) in [(None, true), (Some("trim"), false), (Some("guides"), false)] {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
|
||||
app.run("shape.rectangle", json!({"x": 50, "y": 50, "width": 50, "height": 50})).unwrap();
|
||||
app.session.execute("guide.add", &json!({"vertical": true, "pos": 200})).unwrap();
|
||||
app.select_tool("selection");
|
||||
app.ui.view.smart_guides = false;
|
||||
app.ui.view.snap_to_point = true;
|
||||
app.ui.view.guides = true;
|
||||
let ctx = egui::Context::default();
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
let rect = app.canvas_rect.unwrap();
|
||||
let xf = Xf::new(rect, app.view().unwrap());
|
||||
let button = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
|
||||
// Grabbed at its centre, moved first well away from the guide, then 1.5 px short of it.
|
||||
let grab = xf.to_screen(Point::new(75.0, 75.0));
|
||||
let away = xf.to_screen(Point::new(120.0, 75.0));
|
||||
let near = xf.to_screen(Point::new(200.0, 75.0)) - vec2(1.5, 0.0);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(grab)]);
|
||||
frame(&mut app, &ctx, vec![button(grab, true)]);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(away)]);
|
||||
match hide {
|
||||
Some("trim") => app.ui.view.trim_view = true,
|
||||
Some(_) => app.ui.view.guides = false,
|
||||
None => {}
|
||||
}
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(near)]);
|
||||
frame(&mut app, &ctx, vec![button(near, false)]);
|
||||
let b = app.session.active().unwrap().doc.art_bounds().unwrap();
|
||||
let centre = (b.x0 + b.x1) / 2.0;
|
||||
assert_eq!((centre - 200.0).abs() < 1e-6, snaps, "{hide:?}: the grabbed centre at {centre}");
|
||||
assert!((centre - xf.to_doc(near).x).abs() < 1e-6 || snaps, "{hide:?}: unsnapped, it follows the pointer: {centre}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user