From cabcec642ededd72d1c3ba90baa56d073de1a4fb Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Mon, 30 Nov 2026 19:09:40 -0500 Subject: [PATCH] fix(avalonia): lock the nest while the simulator opens and keep a dragged cut-off selected Simulate Cutting took its view-only lock only after reading the machines, so Auto Nest or Nest > Edit started in that wait ran under the open simulator, and closing it restored a stale lock that left the plate refusing edits. The lock is now taken before the read and released if the simulator does not open. A cut-off drag that changed how many segments the line is trimmed into rebuilt the selection by part index, which drops selected cut-offs; the view now keeps them by reference while the plate is the same. Co-Authored-By: Claude Opus 5.5 --- OpenNest.Avalonia/Controls/PlateCanvas.cs | 8 +++++++ OpenNest.Avalonia/MainWindow.CutTime.cs | 26 +++++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/OpenNest.Avalonia/Controls/PlateCanvas.cs b/OpenNest.Avalonia/Controls/PlateCanvas.cs index e6d5a024..efa2bc0d 100644 --- a/OpenNest.Avalonia/Controls/PlateCanvas.cs +++ b/OpenNest.Avalonia/Controls/PlateCanvas.cs @@ -82,10 +82,18 @@ namespace OpenNest.Avalonia.Controls /// public void ReplacePlate(Plate value, IEnumerable selectedIndices) { + // Cut-offs are kept by reference while the plate stays the same: an edit that + // re-trims them (a cut-off drag) changes the segment parts, so the part-index + // selection would otherwise drop the selected line. + var keptCutOffs = ReferenceEquals(plate, value) + ? selection.CutOffs.Where(c => value.CutOffs.Any(d => ReferenceEquals(c, d))).ToList() + : new List(); var fit = fitPending; Plate = value; fitPending = fit; selection.SelectIndices(selectedIndices); + if (keptCutOffs.Count > 0) + selection.Select(keptCutOffs); } /// The colours drawn with: the application's active scheme unless set. diff --git a/OpenNest.Avalonia/MainWindow.CutTime.cs b/OpenNest.Avalonia/MainWindow.CutTime.cs index e735a015..baa4939e 100644 --- a/OpenNest.Avalonia/MainWindow.CutTime.cs +++ b/OpenNest.Avalonia/MainWindow.CutTime.cs @@ -63,15 +63,22 @@ namespace OpenNest.Avalonia var view = ActiveView; if (view?.PlateView.Plate == null || IsNesting(view.Tab, "simulating its cutting")) return; - var machines = await DesktopSettings.RefreshMachinesAsync(); - // The nest may have changed or closed, or the simulator opened, while the machines were read. - if (closed || simulator != null || ActiveView != view || view.PlateView.Plate == null) - return; - // As WinForms: the plate tool ends and the nest is view-only until the simulator closes. + // The lock is taken before the machines are read, so Auto Nest, Nest > Edit or + // another edit cannot start while they load and then run under the simulator. view.PlateView.Tools.EndTool(); view.CancelPickup(); - inspection = new InspectionLock(this, view.PlateView, view.InspectionControls); + var opening = new InspectionLock(this, view.PlateView, view.InspectionControls); + inspection = opening; + var machines = await DesktopSettings.RefreshMachinesAsync(); + // The window may have closed, or the user switched tabs, while the machines were read. + if (closed || ActiveView != view || view.PlateView.Plate == null) + { + opening.Dispose(); + if (ReferenceEquals(inspection, opening)) + inspection = null; + return; + } simulator = new CutSimulatorWindow(view, machines); simulator.Closed += (_, _) => { @@ -83,18 +90,19 @@ namespace OpenNest.Avalonia } /// - /// While the simulator is open, as the WinForms main window while inspecting: Escape + /// While the simulator is open (or opening), as the WinForms main window while inspecting: Escape /// closes it, the zoom keys zoom, and other Ctrl, Alt and command shortcuts (which could /// change the nest) are swallowed. True when the key was taken. /// private bool ProcessSimulatorKeys(KeyEventArgs e) { - if (simulator == null) + // Also while the simulator is opening: the lock is already held then. + if (inspection == null) return false; var modifiers = e.KeyModifiers; if (e.Key == Key.Escape && modifiers == KeyModifiers.None) { - simulator.Close(); + simulator?.Close(); return true; } if (modifiers == KeyModifiers.Control && e.Key is Key.OemPlus or Key.Add)