From 039028afc7f9cb679eb0fba7a9aaf88e0749c8e2 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Tue, 1 Dec 2026 22:02:24 -0500 Subject: [PATCH] fix(select): ignore part drags under 3 px A click to select or find a part often nudged it by a pixel or two. A press on a part now moves it only once the cursor travels 3 control pixels; the part then catches up so the grab point stays under the cursor. A click that never crosses the threshold records no undo entry and skips the cut-off regeneration. Co-Authored-By: Claude Opus 5.5 --- .../Forms/UndoRedoTests.cs | 47 +++++++++++++++++++ OpenNest/Actions/ActionSelect.cs | 31 +++++++++++- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/OpenNest.WinForms.Tests/Forms/UndoRedoTests.cs b/OpenNest.WinForms.Tests/Forms/UndoRedoTests.cs index 49be1276..f2547c07 100644 --- a/OpenNest.WinForms.Tests/Forms/UndoRedoTests.cs +++ b/OpenNest.WinForms.Tests/Forms/UndoRedoTests.cs @@ -149,6 +149,53 @@ public class UndoRedoTests Assert.Equal(0, editor.History.UndoCount); }); + [Fact] + public void AJitterBelowTheDragThresholdLeavesThePartInPlace() => Run(env => + { + var editor = env.Editor; + var view = editor.PlateView; + editor.PlateManager.LoadAt(0); + view.ZoomToFit(); + var part = view.Plate.Parts[0]; + var start = part.Location; + var grab = view.PointWorldToControl(part.BoundingBox.Center); + + Mouse(view, "OnMouseMove", MouseButtons.None, grab); + Mouse(view, "OnMouseDown", MouseButtons.Left, grab); + Mouse(view, "OnMouseMove", MouseButtons.Left, new Point(grab.X + 1, grab.Y + 1)); + Mouse(view, "OnMouseMove", MouseButtons.Left, new Point(grab.X + 2, grab.Y)); + Mouse(view, "OnMouseUp", MouseButtons.Left, new Point(grab.X + 2, grab.Y)); + + Assert.Equal(start, part.Location); + Assert.Single(view.SelectedParts); + Assert.Equal(0, editor.History.UndoCount); + }); + + [Fact] + public void ADragPastTheThresholdKeepsTheGrabPointUnderTheCursor() => Run(env => + { + var editor = env.Editor; + var view = editor.PlateView; + editor.PlateManager.LoadAt(0); + view.ZoomToFit(); + var part = view.Plate.Parts[0]; + var start = part.Location; + var grab = view.PointWorldToControl(part.BoundingBox.Center); + var release = new Point(grab.X + 10, grab.Y); + + Mouse(view, "OnMouseMove", MouseButtons.None, grab); + Mouse(view, "OnMouseDown", MouseButtons.Left, grab); + Mouse(view, "OnMouseMove", MouseButtons.Left, new Point(grab.X + 2, grab.Y)); + Mouse(view, "OnMouseMove", MouseButtons.Left, release); + Mouse(view, "OnMouseUp", MouseButtons.Left, release); + + var expected = view.PointControlToWorld(release) - view.PointControlToWorld(grab); + var moved = part.Location - start; + Assert.Equal(expected.X, moved.X, 6); + Assert.Equal(expected.Y, moved.Y, 6); + Assert.Equal(1, editor.History.UndoCount); + }); + [Fact] public void AScopeLeftIncompleteRollsBack() => Run(env => { diff --git a/OpenNest/Actions/ActionSelect.cs b/OpenNest/Actions/ActionSelect.cs index 25e1a34f..d8d49121 100644 --- a/OpenNest/Actions/ActionSelect.cs +++ b/OpenNest/Actions/ActionSelect.cs @@ -26,6 +26,14 @@ namespace OpenNest.Actions // Open from the press on a selected part to the release: a drag undoes as one move. private ChangeScope drag; + // A press on a part moves it only once the cursor travels this far (control pixels), + // so a click to select or find a part never nudges it by a stray pixel. + public const int DragThreshold = 3; + + private Point dragStartControl; + private Vector dragStartWorld; + private bool dragMoved; + public ActionSelect(PlateView plateView) : base(plateView) { @@ -78,6 +86,7 @@ namespace OpenNest.Actions { var scope = drag; drag = null; + dragMoved = false; if (scope == null) return; scope.Complete(); @@ -99,7 +108,22 @@ namespace OpenNest.Actions if (plateView.SelectedParts.Count == 0) return; - var offset = plateView.CurrentPoint - plateView.LastPoint; + Vector offset; + if (dragMoved || drag == null) + { + offset = plateView.CurrentPoint - plateView.LastPoint; + } + else + { + var dx = e.X - dragStartControl.X; + var dy = e.Y - dragStartControl.Y; + if (dx * dx + dy * dy < DragThreshold * DragThreshold) + return; + + // Catch up to the cursor so the grab point stays under it. + dragMoved = true; + offset = plateView.CurrentPoint - dragStartWorld; + } drag?.MarkChanged(); foreach (var part in plateView.SelectedParts) @@ -130,6 +154,9 @@ namespace OpenNest.Actions { EndDrag(); drag = plateView.BeginChange("Move"); + dragStartControl = e.Location; + dragStartWorld = plateView.CurrentPoint; + dragMoved = false; return; } @@ -175,7 +202,7 @@ namespace OpenNest.Actions plateView.Invalidate(); status = Status.SetFirstPoint; } - else if (plateView.SelectedParts.Count > 0) + else if (dragMoved && plateView.SelectedParts.Count > 0) { // Part drag completed — regenerate cut-off programs if (plateView.Plate.CutOffs.Count > 0)