using System.Collections.Generic; using Avalonia.Controls; using Avalonia.Interactivity; using OpenNest.Desktop.Interaction; using OpenNest.Forms; namespace OpenNest.Avalonia { /// /// Plate > Sheet Cut-Off, Trim/Extend Cut-Off and Automatic Scrap Cutoffs, Nest > /// Automatic Scrap Cutoffs (All Plates) and the Swap Through-Cut menu on a cut-off junction, /// as the WinForms main form: each edit is one undo step under the WinForms label (see /// and ). While Auto Nest works /// on a nest its plate takes none of them. /// public partial class MainWindow { /// Shows Swap Through-Cut when a junction on 's plate is right-clicked. private void WatchCutOffMenu(NestView view) => view.PlateView.CutOffMenuRequested += (_, junction) => ShowCutOffMenu(view, junction); private void OnSheetCutOff(object sender, RoutedEventArgs e) => StartTool(canvas => canvas.StartSheetCutOff()); private void OnTrimExtendCutOff(object sender, RoutedEventArgs e) => StartTool(canvas => canvas.StartTrimExtendCutOff()); private void ShowCutOffMenu(NestView view, Controls.CutOffJunction junction) { var canvas = view.PlateView; if (view != ActiveView || !ReferenceEquals(canvas.Plate, junction.Plate)) return; var swap = new MenuItem { Header = CutOffEdits.SwapLabel }; swap.Click += (_, _) => { // The view may have moved to another sheet, or Auto Nest started, while the menu was open. if (!ReferenceEquals(junction.Plate, canvas.Plate) || canvas.EditsLocked || view.Tab == nestingTab) return; var (_, status) = CutOffEdits.Swap(junction.Plate, junction.Through, junction.Stopper, canvas.CutOffSettings, label => view.Tab.Undo.BeginChange(label)); canvas.InvalidateVisual(); ShowNotice(status); UpdateUndoMenu(); }; var menu = new ContextMenu { ItemsSource = new List { swap } }; menu.Open(canvas); } private async void OnAutomaticCutOff(object sender, RoutedEventArgs e) => await ShowAutomaticCutOffAsync(allPlates: false); private async void OnNestAutomaticCutOff(object sender, RoutedEventArgs e) => await ShowAutomaticCutOffAsync(allPlates: true); private async System.Threading.Tasks.Task ShowAutomaticCutOffAsync(bool allPlates) { if (ActiveView is not { PlateView: { Plate: { } plate } canvas } view) return; var tab = view.Tab; bool IsBusy() => PlateWorkGuard.IsBusy(tab == nestingTab || arrangingTab == tab, new[] { canvas.Tools.Current }); if (IsBusy()) { await ConfirmDialog.ShowAsync(this, AutomaticCutOffSession.Title, PlateWorkGuard.BusyMessage); return; } try { // As WinForms, the dialog opens over Select with nothing selected. canvas.Tools.Select(); canvas.Selection.Clear(); var nest = tab.Document.Nest; var session = new AutomaticCutOffSession(plate, nest.Units, allPlates ? nest : null, IsBusy, () => canvas.Plate); await AutomaticCutOffDialog.ShowAsync(this, session, canvas, label => tab.Undo.BeginChange(label)); } finally { canvas.CutOffPreviewParts = null; UpdateUndoMenu(); } } } }