diff --git a/OpenNest.Avalonia/Controls/PlateCanvas.cs b/OpenNest.Avalonia/Controls/PlateCanvas.cs index e8a11046..2f9329dd 100644 --- a/OpenNest.Avalonia/Controls/PlateCanvas.cs +++ b/OpenNest.Avalonia/Controls/PlateCanvas.cs @@ -47,6 +47,8 @@ namespace OpenNest.Avalonia.Controls private Plate plate; private Point? panStart; + // Where the middle button went down: a press released within 5 px turns the selection. + private Point? middleDown; private bool fitPending = true; private sealed record OffsetEntry(CNC.Program Program, double Rotation, double Spacing, double Tolerance, @@ -100,6 +102,9 @@ namespace OpenNest.Avalonia.Controls public PlateHitTester HitTester => hitTester; + /// Delete, nudge, push and rotate the selection, as WinForms does. + public PlateEdits Edits { get; } + /// /// Opens one undoable edit of the shown nest (its tab's history); unset, edits are not /// recorded. @@ -132,6 +137,7 @@ namespace OpenNest.Avalonia.Controls Focusable = true; hitTester = new PlateHitTester(part => PathsFor(part).Geometry); tools = new PlateToolManager(this); + Edits = new PlateEdits(this); selection.Changed += (_, _) => { InvalidateVisual(); @@ -384,6 +390,17 @@ namespace OpenNest.Avalonia.Controls protected override void OnPointerWheelChanged(PointerWheelEventArgs e) { + // As WinForms: Shift+wheel turns the selection (Ctrl+Shift by a tenth); without a + // selection the wheel zooms. Some platforms report Shift+wheel as horizontal. + if (e.KeyModifiers.HasFlag(KeyModifiers.Shift) && selection.Parts.Count > 0 + && !EditsLocked) + { + var delta = e.Delta.Y != 0 ? e.Delta.Y : e.Delta.X; + Edits.RotateByWheel( + (int)System.Math.Truncate(delta), fine: IsControl(e.KeyModifiers)); + e.Handled = true; + return; + } var at = e.GetPosition(this); view.ZoomAt(new ScreenPoint(at.X, at.Y), System.Math.Pow(1.2, e.Delta.Y)); InvalidateVisual(); @@ -398,12 +415,15 @@ namespace OpenNest.Avalonia.Controls var properties = point.Properties; if (properties.IsMiddleButtonPressed) { - if (e.ClickCount == 2) + // As WinForms: a middle double-click fits only with nothing selected; with a + // selection each click turns it, so a double-click turns it 180 degrees. + if (e.ClickCount == 2 && selection.Parts.Count == 0) { ZoomToFit(); return; } panStart = point.Position; + middleDown = point.Position; e.Pointer.Capture(this); return; } @@ -456,11 +476,34 @@ namespace OpenNest.Avalonia.Controls _ => ToolButton.None, }; if (button == ToolButton.Middle) + { panStart = null; + // A middle click, not a drag, turns the selection 90 degrees. + var at = e.GetPosition(this); + if (middleDown is { } down && selection.Parts.Count > 0 + && (at.X - down.X) * (at.X - down.X) + (at.Y - down.Y) * (at.Y - down.Y) < 25) + Edits.RotateQuarterTurn(); + middleDown = null; + } else if (button != ToolButton.None && tools.Current.PointerReleased(ToolPointerAt(e.GetPosition(this), button, e.KeyModifiers))) e.Handled = true; - e.Pointer.Capture(null); + // Kept while another button is still down: a middle click during a left drag must + // not end the drag. + var held = e.GetCurrentPoint(this).Properties; + if (!held.IsLeftButtonPressed && !held.IsMiddleButtonPressed + && !held.IsRightButtonPressed) + e.Pointer.Capture(null); + } + + // The pointer was taken away mid-gesture (Alt+Tab during a drag): the tool ends what it + // was doing, and a pan or middle click stops. + protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e) + { + base.OnPointerCaptureLost(e); + panStart = null; + middleDown = null; + tools.Current.PointerCaptureLost(); } protected override void OnPointerExited(PointerEventArgs e) @@ -484,7 +527,7 @@ namespace OpenNest.Avalonia.Controls if (keys.HasFlag(KeyModifiers.Shift)) modifiers |= ToolModifiers.Shift; // Command on macOS stands in for Ctrl, as Avalonia's own gestures do. - if (keys.HasFlag(KeyModifiers.Control) || keys.HasFlag(KeyModifiers.Meta)) + if (IsControl(keys)) modifiers |= ToolModifiers.Control; if (keys.HasFlag(KeyModifiers.Alt)) modifiers |= ToolModifiers.Alt; @@ -503,8 +546,37 @@ namespace OpenNest.Avalonia.Controls ZoomToFit(); e.Handled = true; } + else if (EditFor(e.Key, e.KeyModifiers) is { } edit) + { + edit(); + e.Handled = true; + } } + private static bool IsControl(KeyModifiers keys) => + keys.HasFlag(KeyModifiers.Control) || keys.HasFlag(KeyModifiers.Meta); + + // The WinForms plate view's editing keys: Delete; arrows nudge; Shift+arrows, X, Shift+X, + // Shift+Y and Y push to contact. + private Action EditFor(Key key, KeyModifiers modifiers) => (key, modifiers) switch + { + // WinForms deletes on the Delete key whatever modifiers are held. + (Key.Delete, _) => Edits.Delete, + (Key.Left, KeyModifiers.None) => () => Edits.Nudge(PushDirection.Left), + (Key.Right, KeyModifiers.None) => () => Edits.Nudge(PushDirection.Right), + (Key.Up, KeyModifiers.None) => () => Edits.Nudge(PushDirection.Up), + (Key.Down, KeyModifiers.None) => () => Edits.Nudge(PushDirection.Down), + (Key.Left, KeyModifiers.Shift) or (Key.X, KeyModifiers.None) => + () => Edits.Push(PushDirection.Left), + (Key.Right, KeyModifiers.Shift) or (Key.X, KeyModifiers.Shift) => + () => Edits.Push(PushDirection.Right), + (Key.Up, KeyModifiers.Shift) or (Key.Y, KeyModifiers.Shift) => + () => Edits.Push(PushDirection.Up), + (Key.Down, KeyModifiers.Shift) or (Key.Y, KeyModifiers.None) => + () => Edits.Push(PushDirection.Down), + _ => null, + }; + protected override void OnSizeChanged(global::Avalonia.Controls.SizeChangedEventArgs e) { base.OnSizeChanged(e); diff --git a/OpenNest.Avalonia/MainWindow.axaml.cs b/OpenNest.Avalonia/MainWindow.axaml.cs index 98e28c03..f2eb6c51 100644 --- a/OpenNest.Avalonia/MainWindow.axaml.cs +++ b/OpenNest.Avalonia/MainWindow.axaml.cs @@ -743,6 +743,11 @@ namespace OpenNest.Avalonia nesting = new CancellationTokenSource(); nestingTab = tab; + // As WinForms behind its modal progress window: the nest's plate takes no edits + // while Auto Nest works on it. + var lockedView = views.GetValueOrDefault(tab); + if (lockedView != null) + lockedView.PlateView.EditsLocked = true; var token = nesting.Token; SetNesting(true); ShowNotice($"{engineName}: starting..."); @@ -797,6 +802,8 @@ namespace OpenNest.Avalonia nesting.Dispose(); nesting = null; nestingTab = null; + if (lockedView != null) + lockedView.PlateView.EditsLocked = false; SetNesting(false); } } diff --git a/OpenNest.Avalonia/NestView.axaml.cs b/OpenNest.Avalonia/NestView.axaml.cs index a9d1e70b..7231eb24 100644 --- a/OpenNest.Avalonia/NestView.axaml.cs +++ b/OpenNest.Avalonia/NestView.axaml.cs @@ -26,7 +26,14 @@ namespace OpenNest.Avalonia { Tab = tab; Canvas.ViewChanged += (_, _) => ViewChanged?.Invoke(this, EventArgs.Empty); + Canvas.BeginChangeHandler = (label, key) => tab.Undo.BeginChange(label, key); tab.Undo.SwapIn = SwapIn; + // Deleting parts changes the plate and drawing counts the lists show. + tab.Undo.Changed += (_, _) => RefreshCounts(); + Canvas.CutOffSettings = CutOffSettingsFor(tab.Document.Nest); + plates = new PlateManager(tab.Document.Nest); + // Starts watching the last two plates (the tab already put the empty plate there). + plates.EnsureSentinel(); Refresh(0); } @@ -40,7 +47,13 @@ namespace OpenNest.Avalonia keptSelection = shown != null ? (Canvas.Selection.PartIndices, shown.Parts.Count) : (Array.Empty(), -1); + // Read first: a throw here leaves the live nest in place, as the recorder expects. + var cutOffSettings = CutOffSettingsFor(restored); Tab.Document.Nest = restored; + Canvas.CutOffSettings = cutOffSettings; + plates.Dispose(); + plates = new PlateManager(restored); + plates.EnsureSentinel(); // Reading recolours drawings from the file; the view keeps the application's scheme. // Cut-offs stay as reading regenerated them, as when the nest was opened. ColorSchemeRegistry.RecolorDrawings(restored); @@ -58,6 +71,10 @@ namespace OpenNest.Avalonia // While an Undo or Redo swaps its copy in: the plate is shown without zooming to fit, // and the parts selected before (their indices, the plate's part count then) are selected again. private bool keepView; + + // Keeps one empty plate at the end of the nest as edits add or remove parts on the last + // two plates, inside the edit's own undo step, as the WinForms nest window's manager does. + private PlateManager plates; private (IReadOnlyList Indices, int PartCount) keptSelection = (Array.Empty(), -1); /// Raised when the shown plate, zoom, hover or selection changes. @@ -76,9 +93,8 @@ namespace OpenNest.Avalonia { var nest = Tab.Document.Nest; // As the WinForms nest window: one empty plate stays at the end to nest onto, so a - // new nest shows Plate 1. Files leave empty plates out, so saving is unchanged. - using (var plates = new PlateManager(nest)) - plates.EnsureSentinel(); + // new nest shows Plate 1. Files leave empty plates out, so saving is unchanged. The + // tab put it there on opening and the manager keeps it, so this only reads. Canvas.ClearCache(); PlateList.ItemsSource = nest.Plates .Select((plate, i) => @@ -91,9 +107,50 @@ namespace OpenNest.Avalonia .Where(d => !d.IsCutOff) .Select(d => new DrawingRow(d.Name, d.Quantity.Nested, d.Quantity.Required)) .ToList(); + shownCounts = CountsText(nest); ShowPlate(); } + // As the WinForms nest window binding a nest: the saved head-lock defaults in the nest's + // units, then the machine's cut-off defaults for its material and thickness. + private static CutOffs.CutOffSettings CutOffSettingsFor(Nest nest) + { + var settings = new CutOffs.CutOffSettings(); + SavedCutOffHeadLock.Read(Data.UserSettingsFile.Read(Data.UserSettingsFile.DefaultPath)) + .ApplyTo(settings, nest.Units); + DesktopSettings.Machines.ApplyCutOffDefaults(settings, nest); + return settings; + } + + // After an edit: rebuilds the lists only when their counts changed, keeping the shown + // plate, its zoom and the selection, as an Undo does. + private void RefreshCounts() + { + var nest = Tab.Document.Nest; + var counts = CountsText(nest); + if (counts == shownCounts) + return; + var shown = Canvas.Plate; + keptSelection = shown != null + ? (Canvas.Selection.PartIndices, shown.Parts.Count) + : (Array.Empty(), -1); + keepView = true; + try + { + Refresh(System.Math.Max(0, PlateList.SelectedIndex)); + } + finally + { + keepView = false; + } + } + + private string shownCounts; + + private static string CountsText(Nest nest) => + string.Join(",", nest.Plates.Select(p => p.Parts.Count)) + "|" + + string.Join(",", nest.Drawings.Select(d => d.Quantity.Nested)); + public void ShowPlate(int index) { if (PlateCount > 0)