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.
This commit is contained in:
aj committed 2026-11-17 16:11:14 -05:00
1 parent c00ae58ac0
commit d7f550dda4
4 files changed
+107 -11

No files matched your search

+67 -11
View File
@@ -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
/// <summary>No edits while Auto Nest works on the nest (WinForms EditsLocked).</summary>
public bool EditsLocked { get; set; }
/// <summary>
/// Which way Shift+click and Ctrl+click settle a placed copy (Tools &gt; Options &gt; Clone
/// push placement), read at each click.
/// </summary>
public Func<SettleOrder> PlacementOrder { get; set; } = () => SettleOrder.ClosestToCorner;
/// <summary>
/// Edit &gt; Copy: places copies of the selected parts with the place tool; false when
/// nothing is selected or edits are locked.
/// </summary>
public bool CopySelection() => PlateClipboard.Copy(this, tools, ReadPlacementOrder) != null;
/// <summary>
/// Edit &gt; Cut: as <see cref="CopySelection"/>, then removes the parts as one Cut step.
/// </summary>
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<Part>(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<Drawing>(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);
+4
View File
@@ -28,6 +28,10 @@
<MenuItem x:Name="UndoMenuItem" Header="_Undo" InputGesture="Ctrl+Z" Click="OnUndo" />
<MenuItem x:Name="RedoMenuItem" Header="_Redo" InputGesture="Ctrl+Y" Click="OnRedo" />
<Separator />
<!-- Into the place tool, not the system clipboard, as WinForms. -->
<MenuItem x:Name="CutMenuItem" Header="Cu_t" InputGesture="Ctrl+X" Click="OnCut" />
<MenuItem x:Name="CopyMenuItem" Header="_Copy" InputGesture="Ctrl+C" Click="OnCopy" />
<Separator />
<MenuItem x:Name="SelectAllMenuItem" Header="Select _All" InputGesture="Ctrl+A" Click="OnSelectAll" />
</MenuItem>
<MenuItem Header="_Nest">
+33
View File
@@ -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);
+3
View File
@@ -49,6 +49,9 @@ namespace OpenNest.Avalonia
: (Array.Empty<int>(), -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();