From 2b62cea4c8265f348167643ea61a0bdd25f73b83 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Thu, 26 Nov 2026 20:19:17 -0500 Subject: [PATCH] fix(plate-view): keep a part still when its context menu is dismissed A context menu swallows mouse moves while it is open, so the view's cursor stayed where the part was right-clicked. The click that closed the menu then re-selected that part and the first move dragged it to the click. Sync the cursor on every left or right press so a press acts where it lands. Co-Authored-By: Claude Opus 5.5 --- .../Controls/PartContextMenuDismissTests.cs | 63 +++++++++++++++++++ OpenNest/Controls/PlateView.cs | 5 ++ 2 files changed, 68 insertions(+) create mode 100644 OpenNest.WinForms.Tests/Controls/PartContextMenuDismissTests.cs diff --git a/OpenNest.WinForms.Tests/Controls/PartContextMenuDismissTests.cs b/OpenNest.WinForms.Tests/Controls/PartContextMenuDismissTests.cs new file mode 100644 index 00000000..34cd5591 --- /dev/null +++ b/OpenNest.WinForms.Tests/Controls/PartContextMenuDismissTests.cs @@ -0,0 +1,63 @@ +using System.Drawing; +using System.Reflection; +using System.Windows.Forms; +using OpenNest.Controls; +using OpenNest.Geometry; + +namespace OpenNest.WinForms.Tests.Controls; + +/// +/// A context menu swallows the mouse moves made while it is open, so the click that dismisses +/// it must not drag the part that was right-clicked. +/// +public class PartContextMenuDismissTests +{ + [Fact] + public void ClickingOffThePartAfterARightClick_LeavesThePartWhereItWas() + { + using var view = NewView(out var part); + var onPart = ToControl(view, new Vector(12, 12)); + var offPart = ToControl(view, new Vector(40, 80)); + + Mouse(view, "OnMouseMove", MouseButtons.None, onPart); + Mouse(view, "OnMouseDown", MouseButtons.Right, onPart); + Mouse(view, "OnMouseUp", MouseButtons.Right, onPart); + Assert.Contains(view.SelectedParts, p => p.BasePart == part); + + // The menu was open while the cursor traveled: no move reaches the view before the press. + Mouse(view, "OnMouseDown", MouseButtons.Left, offPart); + Mouse(view, "OnMouseMove", MouseButtons.Left, new Point(offPart.X + 1, offPart.Y)); + Mouse(view, "OnMouseUp", MouseButtons.Left, new Point(offPart.X + 1, offPart.Y)); + + Assert.Equal(new Vector(10, 10), part.Location); + Assert.Empty(view.SelectedParts); + } + + private static PlateView NewView(out Part part) + { + var program = new CNC.Program(); + program.Codes.Add(new CNC.RapidMove(new Vector(0, 0))); + program.Codes.Add(new CNC.LinearMove(new Vector(4, 0))); + program.Codes.Add(new CNC.LinearMove(new Vector(4, 4))); + program.Codes.Add(new CNC.LinearMove(new Vector(0, 4))); + program.Codes.Add(new CNC.LinearMove(new Vector(0, 0))); + + var plate = new Plate(new OpenNest.Geometry.Size(60, 120)) { Quadrant = 1 }; + part = new Part(new Drawing("square", program), new Vector(10, 10)); + plate.Parts.Add(part); + var view = new PlateView { Plate = plate, Size = new System.Drawing.Size(800, 600) }; + view.ZoomToFit(); + return view; + } + + private static Point ToControl(PlateView view, Vector world) + { + var at = view.PointWorldToControl(world); + return new Point((int)at.X, (int)at.Y); + } + + private static void Mouse(PlateView view, string handler, MouseButtons button, Point at) => + typeof(PlateView).GetMethod(handler, BindingFlags.Instance | BindingFlags.NonPublic, + [typeof(MouseEventArgs)])! + .Invoke(view, [new MouseEventArgs(button, 1, at.X, at.Y, 0)]); +} diff --git a/OpenNest/Controls/PlateView.cs b/OpenNest/Controls/PlateView.cs index 07c09faa..b838aee6 100644 --- a/OpenNest/Controls/PlateView.cs +++ b/OpenNest/Controls/PlateView.cs @@ -517,6 +517,11 @@ namespace OpenNest.Controls if (!Focused) Focus(); + // Moves made while a context menu was open never reached the view, so the cursor + // may be stale: a press must act where it lands, and a drag must start from it. + if (e.Button != MouseButtons.Middle) + LastPoint = CurrentPoint = PointControlToWorld(e.Location); + if (e.Button == MouseButtons.Middle) middleMouseDownPoint = e.Location;