From d7f550dda4214bc0da0e0ce17a8e204151d90904 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Tue, 17 Nov 2026 15:57:13 -0500 Subject: [PATCH] feat(avalonia): cut and copy parts into the place tool Edit > Cut (Ctrl+X) and Copy (Ctrl+C) start the shared place tool with the selected parts, as the WinForms Clone Parts action: the copy follows the pointer, drawn selected and unnumbered, and a left click, Enter or F1 places it as one Add Part/Add Parts step. Shift settles it toward the plate's corner on geometry and Ctrl alone on bounding boxes, in the order saved in Tools > Options > Clone push placement, read at each placement. Arrow keys, X/Y, a middle click and Shift+wheel move or turn the copy without an undo step; a middle double-click turns it rather than fitting the view. Delete removes the copied parts. Escape returns to Select. Cut also removes the parts as one Cut step. As in WinForms, the part under the cursor is highlighted only in Select, nothing is copied or placed while Auto Nest runs on the nest (a middle double-click then fits the view), and Undo or Redo ends the tool, which holds the old nest's parts, so Escape cannot bring it back. An ended tool's copies leave the drawing cache. The view keeps the keyboard after a menu click so Enter and the arrows reach the tool. --- OpenNest.Avalonia/Controls/PlateCanvas.cs | 78 +++++++++++++++++++---- OpenNest.Avalonia/MainWindow.axaml | 4 ++ OpenNest.Avalonia/MainWindow.axaml.cs | 33 ++++++++++ OpenNest.Avalonia/NestView.axaml.cs | 3 + 4 files changed, 107 insertions(+), 11 deletions(-) diff --git a/OpenNest.Avalonia/Controls/PlateCanvas.cs b/OpenNest.Avalonia/Controls/PlateCanvas.cs index 2f9329dd..98fa139e 100644 --- a/OpenNest.Avalonia/Controls/PlateCanvas.cs +++ b/OpenNest.Avalonia/Controls/PlateCanvas.cs @@ -14,6 +14,7 @@ using OpenNest.CommonLines; using OpenNest.CutOffs; using OpenNest.Desktop.Interaction; using OpenNest.Desktop.Rendering; +using OpenNest.Engine.Fill; using OpenNest.Geometry; using OpenNest.Support; using SkiaSharp; @@ -117,6 +118,26 @@ namespace OpenNest.Avalonia.Controls /// No edits while Auto Nest works on the nest (WinForms EditsLocked). public bool EditsLocked { get; set; } + /// + /// Which way Shift+click and Ctrl+click settle a placed copy (Tools > Options > Clone + /// push placement), read at each click. + /// + public Func PlacementOrder { get; set; } = () => SettleOrder.ClosestToCorner; + + /// + /// Edit > Copy: places copies of the selected parts with the place tool; false when + /// nothing is selected or edits are locked. + /// + public bool CopySelection() => PlateClipboard.Copy(this, tools, ReadPlacementOrder) != null; + + /// + /// Edit > Cut: as , then removes the parts as one Cut step. + /// + public bool CutSelection() => PlateClipboard.Cut(this, tools, ReadPlacementOrder) != null; + + private SettleOrder ReadPlacementOrder() => + PlacementOrder?.Invoke() ?? SettleOrder.ClosestToCorner; + Undo.ChangeScope IPlateToolHost.BeginChange(string label, object coalesceKey) => BeginChangeHandler?.Invoke(label, coalesceKey) ?? Undo.ChangeScope.Detached(); @@ -137,13 +158,19 @@ namespace OpenNest.Avalonia.Controls Focusable = true; hitTester = new PlateHitTester(part => PathsFor(part).Geometry); tools = new PlateToolManager(this); - Edits = new PlateEdits(this); + // While the place tool is active, edits move its copies. + Edits = new PlateEdits(this) { Tools = tools }; selection.Changed += (_, _) => { InvalidateVisual(); ViewChanged?.Invoke(this, EventArgs.Empty); }; - tools.StatusChanged += (_, _) => ViewChanged?.Invoke(this, EventArgs.Empty); + tools.StatusChanged += (_, _) => + { + // A place tool's copies leave the cache when the tool ends. + TrimCache(); + ViewChanged?.Invoke(this, EventArgs.Empty); + }; } void IPlateToolHost.Invalidate() => InvalidateVisual(); @@ -158,7 +185,8 @@ namespace OpenNest.Avalonia.Controls commonLineChecks.Clear(); } - // Keeps only the shown plate's parts, so closed plates do not hold their paths. + // Keeps only the shown plate's parts and the place tool's copies, so closed plates and + // ended tools do not hold their paths. private void TrimCache() { if (plate == null) @@ -168,6 +196,8 @@ namespace OpenNest.Avalonia.Controls return; } var shown = new HashSet(plate.Parts); + if (tools?.Current is PlaceTool place) + shown.UnionWith(place.Previews); foreach (var stale in paths.Keys.Where(part => !shown.Contains(part)).ToList()) paths.Remove(stale); var drawings = new HashSet(plate.Parts.Select(part => part.BaseDrawing)); @@ -289,6 +319,21 @@ namespace OpenNest.Avalonia.Controls (i + 1).ToString(), LabelPoint(part), selection.Contains(part), ReferenceEquals(part, HoveredPart), part.BaseDrawing.IsCutOff)); } + // The place tool's copies, drawn selected and unnumbered over the plate, as WinForms. + if (tools.Current is PlaceTool { Previews: var previews }) + { + foreach (var preview in previews) + { + var bounds = PathsFor(preview).StrokeBounds; + if (bounds == null) + continue; + var placed = bounds.Translate(preview.Location.X, preview.Location.Y); + var color = ToSkColor(preview.BaseDrawing.Color); + scene.Parts.Add(new PartItem(PathsFor(preview), preview.Location, placed, color, + palette.PartOutline(color), Label: null, preview.Location, Selected: true, + Hovered: false, IsCutOff: false)); + } + } if (options.DrawOffset && plate.PartSpacing > 0) { @@ -392,7 +437,7 @@ namespace OpenNest.Avalonia.Controls { // 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 + if (e.KeyModifiers.HasFlag(KeyModifiers.Shift) && Edits.HasPartsToEdit && !EditsLocked) { var delta = e.Delta.Y != 0 ? e.Delta.Y : e.Delta.X; @@ -417,7 +462,7 @@ namespace OpenNest.Avalonia.Controls { // 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) + if (e.ClickCount == 2 && (EditsLocked || !Edits.HasPartsToEdit)) { ZoomToFit(); return; @@ -455,8 +500,10 @@ namespace OpenNest.Avalonia.Controls var button = point.Properties.IsLeftButtonPressed ? ToolButton.Left : point.Properties.IsRightButtonPressed ? ToolButton.Right : ToolButton.None; tools.Current.PointerMoved(ToolPointerAt(at, button, e.KeyModifiers)); - // As WinForms: the part under the cursor is highlighted only while no button is held. - var hovered = button == ToolButton.None ? hitTester.PartAt(plate, CursorWorld.Value) : null; + // As WinForms: the part under the cursor is highlighted only in Select, while no + // button is held. + var hovered = button == ToolButton.None && tools.Current is SelectTool + ? hitTester.PartAt(plate, CursorWorld.Value) : null; if (!ReferenceEquals(hovered, HoveredPart)) { HoveredPart = hovered; @@ -480,7 +527,7 @@ namespace OpenNest.Avalonia.Controls 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 + if (middleDown is { } down && Edits.HasPartsToEdit && (at.X - down.X) * (at.X - down.X) + (at.Y - down.Y) * (at.Y - down.Y) < 25) Edits.RotateQuarterTurn(); middleDown = null; @@ -521,7 +568,10 @@ namespace OpenNest.Avalonia.Controls InvalidateVisual(); } - private ToolPointer ToolPointerAt(Point at, ToolButton button, KeyModifiers keys) + private ToolPointer ToolPointerAt(Point at, ToolButton button, KeyModifiers keys) => + new(ToWorld(at), new ScreenPoint(at.X, at.Y), button, ModifiersOf(keys)); + + private static ToolModifiers ModifiersOf(KeyModifiers keys) { var modifiers = ToolModifiers.None; if (keys.HasFlag(KeyModifiers.Shift)) @@ -531,7 +581,7 @@ namespace OpenNest.Avalonia.Controls modifiers |= ToolModifiers.Control; if (keys.HasFlag(KeyModifiers.Alt)) modifiers |= ToolModifiers.Alt; - return new ToolPointer(ToWorld(at), new ScreenPoint(at.X, at.Y), button, modifiers); + return modifiers; } protected override void OnKeyDown(KeyEventArgs e) @@ -546,6 +596,12 @@ namespace OpenNest.Avalonia.Controls ZoomToFit(); e.Handled = true; } + else if (e.Key is Key.Enter or Key.F1 && tools.Current is PlaceTool place) + { + // As WinForms: Enter and F1 place a copy, Shift or Ctrl held settling it. + place.Apply(ModifiersOf(e.KeyModifiers)); + e.Handled = true; + } else if (EditFor(e.Key, e.KeyModifiers) is { } edit) { edit(); @@ -879,7 +935,7 @@ namespace OpenNest.Avalonia.Controls canvas.Restore(); // A number shows only once the part is big enough on screen to hold it. - if (item.IsCutOff) + if (item.IsCutOff || item.Label == null) continue; var bounds = Screen(item.Bounds); var width = text.MeasureText(item.Label); diff --git a/OpenNest.Avalonia/MainWindow.axaml b/OpenNest.Avalonia/MainWindow.axaml index 1ae9afaa..d70d4cb0 100644 --- a/OpenNest.Avalonia/MainWindow.axaml +++ b/OpenNest.Avalonia/MainWindow.axaml @@ -28,6 +28,10 @@ + + + + diff --git a/OpenNest.Avalonia/MainWindow.axaml.cs b/OpenNest.Avalonia/MainWindow.axaml.cs index 4f967e05..43452203 100644 --- a/OpenNest.Avalonia/MainWindow.axaml.cs +++ b/OpenNest.Avalonia/MainWindow.axaml.cs @@ -89,6 +89,16 @@ namespace OpenNest.Avalonia Command = new Command(() => OnExit(null, null)), }); KeyBindings.Add(new KeyBinding + { + Gesture = new KeyGesture(Key.X, KeyModifiers.Control), + Command = new Command(() => OnCut(null, null)), + }); + KeyBindings.Add(new KeyBinding + { + Gesture = new KeyGesture(Key.C, KeyModifiers.Control), + Command = new Command(() => OnCopy(null, null)), + }); + KeyBindings.Add(new KeyBinding { Gesture = new KeyGesture(Key.A, KeyModifiers.Control), Command = new Command(() => OnSelectAll(null, null)), @@ -202,6 +212,7 @@ namespace OpenNest.Avalonia private void AddView(NestTab tab) { var view = new NestView(tab); + view.PlateView.PlacementOrder = ReadClonePlacementOrder; RestoreViewToggles(view); view.ViewChanged += (_, _) => { if (view == ActiveView) UpdateStatus(); }; views[tab] = view; @@ -307,6 +318,8 @@ namespace OpenNest.Avalonia CloseAllMenuItem.IsEnabled = view != null; SetOffsetIncrementMenuItem.IsEnabled = view != null; SetRotationIncrementMenuItem.IsEnabled = view != null; + CutMenuItem.IsEnabled = view != null; + CopyMenuItem.IsEnabled = view != null; UpdateExportCommands(view != null); Title = view == null ? "OpenNest (Avalonia prototype)" @@ -855,6 +868,26 @@ namespace OpenNest.Avalonia private void OnSelectAll(object sender, RoutedEventArgs e) => ActiveView?.PlateView.SelectAll(); + // Edit > Cut and Copy, as WinForms: the selected parts go to the place tool (not the + // system clipboard); Cut also removes them from the plate. The view keeps the keyboard, + // so Enter, arrows and Delete reach the tool after a menu click. + private void OnCut(object sender, RoutedEventArgs e) + { + if (ActiveView?.PlateView is { } canvas && canvas.CutSelection()) + canvas.Focus(); + } + + private void OnCopy(object sender, RoutedEventArgs e) + { + if (ActiveView?.PlateView is { } canvas && canvas.CopySelection()) + canvas.Focus(); + } + + // Read at each click, as WinForms reads its setting, so a change in Options applies now. + private static Engine.Fill.SettleOrder ReadClonePlacementOrder() => + DesktopOptions.Read(Data.UserSettingsFile.Read(Data.UserSettingsFile.DefaultPath)) + .ClonePlacementOrder; + private void OnPreviousPlate(object sender, RoutedEventArgs e) => StepPlate(-1); private void OnNextPlate(object sender, RoutedEventArgs e) => StepPlate(1); diff --git a/OpenNest.Avalonia/NestView.axaml.cs b/OpenNest.Avalonia/NestView.axaml.cs index 7231eb24..18fa7bb3 100644 --- a/OpenNest.Avalonia/NestView.axaml.cs +++ b/OpenNest.Avalonia/NestView.axaml.cs @@ -49,6 +49,9 @@ namespace OpenNest.Avalonia : (Array.Empty(), -1); // Read first: a throw here leaves the live nest in place, as the recorder expects. var cutOffSettings = CutOffSettingsFor(restored); + // As WinForms (EditNestForm EndAction): a tool holds the old nest's parts, so it ends + // and Escape cannot bring it back. + Canvas.Tools.EndTool(); Tab.Document.Nest = restored; Canvas.CutOffSettings = cutOffSettings; plates.Dispose();