using System.Linq; using System.Threading.Tasks; using Avalonia.Input; using Avalonia.Interactivity; using OpenNest.Forms; namespace OpenNest.Avalonia { /// /// Plate > Calculate Cut Time, Nest > Calculate Cut Time and Plate > 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. /// 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; } var nestMachine = view.Tab.Document.Nest.Machine; var machine = nestMachine == null ? null : machines.FirstOrDefault(m => m.Id == nestMachine.Id); simulator = new CutSimulatorWindow(view, machine); simulator.Closed += (_, _) => { inspection?.Dispose(); inspection = null; simulator = null; }; simulator.Show(this); } /// /// 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) { // 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; } } }