Files
OpenNest/OpenNest.Avalonia/MainWindow.CutTime.cs
T
ajandClaude Opus 5.5 cabcec642e 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 <noreply@anthropic.com>
2026-11-30 19:09:40 -05:00

124 lines
5.3 KiB
C#

using System.Threading.Tasks;
using Avalonia.Input;
using Avalonia.Interactivity;
using OpenNest.Forms;
namespace OpenNest.Avalonia
{
/// <summary>
/// Plate &gt; Calculate Cut Time, Nest &gt; Calculate Cut Time and Plate &gt; Simulate Cutting (also
/// on the toolbar), as the WinForms main window. Calculate Cut Time asks for the feeds,
/// pierce, acceleration and etch choice, then shows the timing. The simulator holds the nest
/// view-only while it is open: only the plate view, its header's plate and zoom buttons and
/// the zoom keys stay live, and Escape closes it.
/// </summary>
public partial class MainWindow
{
private CutSimulatorWindow simulator;
private InspectionLock inspection;
private void UpdateCutTimeCommands()
{
var hasPlate = ActiveView?.PlateView.Plate != null;
PlateCutTimeMenuItem.IsEnabled = hasPlate;
SimulateCuttingMenuItem.IsEnabled = hasPlate;
SimulateCuttingButton.IsEnabled = hasPlate;
NestCutTimeMenuItem.IsEnabled = ActiveView != null;
}
private async void OnCalculatePlateCutTime(object sender, RoutedEventArgs e) => await CalculateCutTimeAsync(wholeNest: false);
private async void OnCalculateNestCutTime(object sender, RoutedEventArgs e) => await CalculateCutTimeAsync(wholeNest: true);
private async Task CalculateCutTimeAsync(bool wholeNest)
{
var view = ActiveView;
if (view == null || simulator != null || (!wholeNest && view.PlateView.Plate == null)
|| IsNesting(view.Tab, "calculating its cut time"))
return;
// As the WinForms machine list, read each time it opens.
var machines = await DesktopSettings.RefreshMachinesAsync();
if (closed)
return;
var nest = view.Tab.Document.Nest;
var parameters = await CutTimeDialog.ShowAsync(this, nest.Units, machines, saveSettings: !snapshotMode);
if (parameters == null || closed)
return;
var plate = view.PlateView.Plate;
if (!wholeNest && plate == null)
return;
var report = wholeNest
? CutTimeReport.ForNest(nest, parameters)
: CutTimeReport.ForPlate(plate, parameters, nest.Units);
await TimingDialog.ShowAsync(this, report);
}
private async void OnSimulateCutting(object sender, RoutedEventArgs e)
{
if (simulator != null)
{
simulator.Activate();
return;
}
var view = ActiveView;
if (view?.PlateView.Plate == null || IsNesting(view.Tab, "simulating its cutting"))
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();
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 += (_, _) =>
{
inspection?.Dispose();
inspection = null;
simulator = null;
};
simulator.Show(this);
}
/// <summary>
/// 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.
/// </summary>
private bool ProcessSimulatorKeys(KeyEventArgs e)
{
// 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();
return true;
}
if (modifiers == KeyModifiers.Control && e.Key is Key.OemPlus or Key.Add)
{
OnZoomIn(null, null);
return true;
}
if (modifiers == KeyModifiers.Control && e.Key is Key.OemMinus or Key.Subtract)
{
OnZoomOut(null, null);
return true;
}
// F5 and F6 open the increment dialogs, whose menu items are locked.
return (modifiers & (KeyModifiers.Control | KeyModifiers.Alt | KeyModifiers.Meta)) != 0
|| e.Key is Key.F5 or Key.F6;
}
}
}