Snap rounds the points it gives to whole document units when the app turns on the pixel grid, after the target snap and with snapping off or suspended; the transform box reads the grid from the canvas snap and loses its own pixel_grid prop

This commit is contained in:
aiongg
2026-11-16 22:03:47 +00:00
parent 7d991dcb64
commit b1d4d02aa6
5 changed files with 64 additions and 29 deletions
@@ -64,7 +64,6 @@ pub struct TransformOverlay<'a, W> {
session: &'a TransformSession<W>,
settings: &'a TransformSettings,
rules: Option<BoxRules>,
pixel_grid: bool,
mods: Option<Mods>,
menu_rows: &'a [Entry<String>],
reach: TransformReach,
@@ -79,7 +78,6 @@ impl<'a, W: TransformWarp> TransformOverlay<'a, W> {
session,
settings,
rules: None,
pixel_grid: false,
mods: None,
menu_rows: &[],
reach: TransformReach::Box,
@@ -127,14 +125,6 @@ impl<'a, W: TransformWarp> TransformOverlay<'a, W> {
self
}
/// Rounds dragged boxes to whole document units (Snap Transforms to Pixel Grid; the app's rule for shape layers
/// and paths).
#[must_use]
pub fn pixel_grid(mut self, on: bool) -> Self {
self.pixel_grid = on;
self
}
/// The modifiers every test reads in place of the pointer's (a touch host's sticky modifiers).
#[must_use]
pub fn mods(mut self, mods: Option<Mods>) -> Self {
@@ -157,7 +147,6 @@ impl<'a, W: TransformWarp> TransformOverlay<'a, W> {
let mut tbox = TransformBox::new(self.label.clone(), Bind::given(*session.state()), session.rect(), session.skew_v())
.rules(rules)
.reference_point(session.reference_point())
.pixel_grid(self.pixel_grid)
.reach(self.reach)
.id_source(self.id);
if let Some(mods) = self.mods {
@@ -309,10 +309,6 @@ pub struct TransformBox<'a> {
/// Whether a press inside moves the box. Off leaves it to the tool.
#[prop]
inside_moves: bool,
/// Rounds an upright box and its corners to whole document units before it is proposed (Snap Transforms to Pixel
/// Grid). Keys are not rounded.
#[prop]
pixel_grid: bool,
/// The modifiers every test of this box reads in place of the pointer's: a touch host's sticky modifiers.
#[prop]
mods: Option<Mods>,
@@ -356,7 +352,6 @@ impl<'a> TransformBox<'a> {
reference_point: true,
rotate_zone: RotateZone::Outside,
inside_moves: true,
pixel_grid: false,
mods: None,
overflow: false,
session_arrows: true,
@@ -475,7 +470,8 @@ impl<'a> TransformBox<'a> {
_ => (to, Vec::new()),
};
let dragged = quad::drag(&grip.start, &grip.last, grip.part, grip.from, to, mods, &self.rules);
let dragged = if self.pixel_grid { on_pixel_grid(dragged) } else { dragged };
// The canvas's pixel grid rounds an upright box and its corners to whole document units; keys are not rounded.
let dragged = if snap.is_some_and(Snap::rounds_to_pixels) { on_pixel_grid(dragged) } else { dragged };
if kind == Some(BoxDrag::Distort)
&& let Some(at) = handle_point(&dragged, grip.part)
{
+8 -5
View File
@@ -135,10 +135,14 @@ fn harness(state: BoxState, setup: impl FnOnce(&mut Config)) -> Harness {
for target in &shown_config.borrow().targets {
targets.add(*target);
}
let snap = Snap::new(Arc::new(targets), SnapOptions::default());
let targets = Arc::new(targets);
let mut fixture = Fixture::new(move |ui| {
let room = ui.available_rect_before_wrap();
let config = shown_config.borrow().clone();
// A box on the pixel grid alone has the grid through a snap that snaps to nothing.
let mut options = SnapOptions::default();
options.on = config.snap;
let snap = Snap::new(Arc::clone(&targets), options).whole_pixels(config.pixel_grid);
let view = CanvasView::new(room, Point::new(f64::from(room.width()) / 2.0, f64::from(room.height()) / 2.0), 1.0)
.rotation(config.rotation)
.flip_x(config.flip);
@@ -149,8 +153,8 @@ fn harness(state: BoxState, setup: impl FnOnce(&mut Config)) -> Harness {
};
let canvas_id = ui.scope_id().with("document canvas");
let mut canvas = Canvas::new("Document").id(canvas_id).view(view);
if config.snap {
canvas = canvas.snap(snap.clone());
if config.snap || config.pixel_grid {
canvas = canvas.snap(snap);
}
if let Some(ruler) = config.rulers {
canvas = canvas.rulers(ruler, ruler, Point::ZERO);
@@ -170,8 +174,7 @@ fn harness(state: BoxState, setup: impl FnOnce(&mut Config)) -> Harness {
.inside_moves(config.inside_moves)
.reference_point(config.reference_point)
.centre_mark(config.centre_mark)
}
.pixel_grid(config.pixel_grid);
};
if let Some(mods) = config.mods {
control = control.mods(mods);
}
+34 -7
View File
@@ -20,6 +20,7 @@ pub struct Snap {
options: SnapOptions,
tolerance: Option<f64>,
os: Platform,
whole_pixels: bool,
}
/// The gesture's snap options in one argument: whether the gesture reads Control itself, and a kind filter.
@@ -49,22 +50,44 @@ impl GestureSnap {
impl Snap {
/// The snapping over `targets` with `options`; the tolerance is set by the canvas each pass.
pub fn new(targets: Arc<SnapTargets>, options: SnapOptions) -> Self {
Snap { targets, options, tolerance: None, os: Platform::current() }
Snap { targets, options, tolerance: None, os: Platform::current(), whole_pixels: false }
}
/// This snap rounding every point it gives to whole document units when `on`: the pixel grid for drawn points and
/// transform boxes (Preferences > Tools > Snap Vector Tools and Transforms to Pixel Grid, as the app decides it
/// applies). The rounding follows the target snap and holds with snapping off or suspended by Control.
#[must_use]
pub fn whole_pixels(mut self, on: bool) -> Self {
self.whole_pixels = on;
self
}
/// Whether this snap rounds its points to whole document units.
pub fn rounds_to_pixels(&self) -> bool {
self.whole_pixels
}
fn rounded(&self, position: f64) -> f64 {
if self.whole_pixels { position.round() } else { position }
}
/// `position` across a guide running `along`, snapped.
pub fn apply(&self, along: Axis, position: f64, mods: Mods) -> f64 {
let Some(tolerance) = self.reach(mods, false) else { return position };
self.targets.nearest(along, position, tolerance).map_or(position, |target| target.position)
let snapped = match self.reach(mods, false) {
Some(tolerance) => self.targets.nearest(along, position, tolerance).map_or(position, |target| target.position),
None => position,
};
self.rounded(snapped)
}
/// `point` snapped on both axes to the targets of `gesture.kinds`, with the lines to draw; the input and no lines
/// when snapping is off.
pub fn point(&self, point: Point, mods: Mods, gesture: GestureSnap) -> (Point, Vec<SnapLine>) {
match self.reach(mods, gesture.reads_control) {
let (at, lines) = match self.reach(mods, gesture.reads_control) {
Some(tolerance) => self.targets.snap_point_of(point, tolerance, gesture.kinds),
None => (point, Vec::new()),
}
};
(Point::new(self.rounded(at.x), self.rounded(at.y)), lines)
}
/// The offset that moves `rect` onto a snap target, with the lines to draw; zero when snapping is off.
@@ -102,14 +125,18 @@ impl Snap {
impl PartialEq for Snap {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.targets, &other.targets) && self.options == other.options && self.tolerance == other.tolerance && self.os == other.os
Arc::ptr_eq(&self.targets, &other.targets)
&& self.options == other.options
&& self.tolerance == other.tolerance
&& self.os == other.os
&& self.whole_pixels == other.whole_pixels
}
}
impl std::fmt::Debug for Snap {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// The targets can number thousands of grid lines, so they are left out.
f.debug_struct("Snap").field("options", &self.options).field("tolerance", &self.tolerance).field("os", &self.os).finish_non_exhaustive()
f.debug_struct("Snap").field("options", &self.options).field("tolerance", &self.tolerance).field("os", &self.os).field("whole_pixels", &self.whole_pixels).finish_non_exhaustive()
}
}
+20
View File
@@ -119,3 +119,23 @@ fn snap_targets_snap_points_and_boxes_unless_control_suspends() {
assert_eq!(snap.rect(DocRect::new(60.0, 10.0, 97.0, 40.0), Mods::NONE), (Vec2::ZERO, Vec::new()));
assert_eq!(snapped_with(&mut fixture, Modifiers::NONE), NEAR);
}
/// The pixel grid: a snap with whole pixels rounds the point a tool snaps after the target snap, also with View > Snap
/// off and with Control suspending it, and leaves the offset of a moved box to the targets.
#[trial]
fn whole_pixels_round_snapped_points_but_not_box_offsets() {
const OFF_GRID: Point = Point::new(110.4, 180.6);
let mut off = SnapOptions::default();
off.on = false;
for options in [SnapOptions::default(), off] {
let (mut fixture, seen) = rig(Snap::new(targets(), options).whole_pixels(true));
fixture.fixture().os(Platform::Mac).frame(Vec::new());
let snap = seen.borrow().clone().expect("the canvas publishes its snap to its body");
assert_eq!(snap.point(OFF_GRID, Mods::NONE, GestureSnap::default()).0, Point::new(110.0, 181.0), "on {}", options.on);
assert_eq!(snap.point(OFF_GRID, Mods::CTRL, GestureSnap::default()).0, Point::new(110.0, 181.0), "Control held, on {}", options.on);
assert_eq!(snap.apply(Axis::Y, 110.4, Mods::NONE), 110.0);
assert_eq!(snap.rect(DocRect::new(10.2, 10.0, 40.2, 40.0), Mods::NONE).0, Vec2::ZERO, "a box away from the targets keeps its offset");
}
let (mut fixture, _) = rig(Snap::new(targets(), SnapOptions::default()).whole_pixels(true));
assert_eq!(snapped_with(&mut fixture, Modifiers::NONE), Point::new(100.0, 200.0), "a point on a target stays on it");
}