feat(avalonia): delete, nudge, push, rotate and drag parts on the plate

The plate view edits the selection through the shared PlateEdits and
Select tool, recording each edit in its nest tab's undo history under
the WinForms labels:

- Delete (with any modifiers) removes the selected parts and cut-offs.
- Arrow keys nudge by 10; repeated nudges undo as one Nudge.
- Shift+arrows, X, Shift+X, Shift+Y and Y push to contact (Ctrl+X does
  nothing until Cut comes with the place tool).
- A middle click (moved under 5 px) turns the selection 90 degrees, a
  double-click 180; a middle drag still pans, and a middle double-click
  fits only with nothing selected.
- Shift+wheel turns it by 10 degrees per notch, Ctrl+Shift+wheel by 1,
  away from the user clockwise; with nothing selected the wheel zooms.
- Dragging a selected part moves the selection as one Move; losing the
  pointer mid-drag ends it there, and a middle click during the drag
  turns the selection without ending it.

As in WinForms, each nest keeps a plate manager that keeps one empty
plate at the end inside the edit's own step, so deleting every part on
the last full plate undoes in one step. Each nest's cut-offs regenerate
with its own cut-off settings (saved head lock in the nest's units,
then the machine's cut-off defaults), read again before Undo or Redo
swaps a nest in. The Plates and Drawings lists rebuild after the step
closes when their counts changed, keeping the plate, zoom and
selection. While Auto Nest runs on a nest its plate takes no edits, as
the WinForms modal progress window prevents them.
This commit is contained in:
aj committed 2026-11-14 20:31:13 -05:00
1 parent 203945dabf
commit f061fcacea
3 files changed
+142 -6

No files matched your search

+75 -3
View File
@@ -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;
/// <summary>Delete, nudge, push and rotate the selection, as WinForms does.</summary>
public PlateEdits Edits { get; }
/// <summary>
/// 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);
+7
View File
@@ -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);
}
}
+60 -3
View File
@@ -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<int>(), -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<int> Indices, int PartCount) keptSelection = (Array.Empty<int>(), -1);
/// <summary>Raised when the shown plate, zoom, hover or selection changes.</summary>
@@ -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<int>(), -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)