feat(avalonia): add parts from the drawing list

As in the WinForms nest window, a part can be added from the Drawings
tab two ways. Pressing a drawing picks it up: when the pointer then
enters the plate view, the place tool starts with one part of the
drawing selected in the list, placed by a click, Enter or F1 like a
copy. Dragging a drawing onto the plate adds one part centred where it
is dropped, as one Add Part step.

A drop is refused while Auto Nest runs on the nest and for a drawing of
another nest. A double-click, a drop (at once, before the pointer can
re-enter the plate on X11 and macOS), Edit > Cut or Copy, or an Undo
puts the pick-up down, and the plate takes it only once. The list
selects one drawing at a time and keeps it selected when rebuilt.

As in WinForms, the pointer entering the plate view gives it the
keyboard, and a dropped part leaves the cut-offs as they are.
This commit is contained in:
aj committed 2026-11-17 17:36:19 -05:00
1 parent 810cc1ed01
commit f11f77f879
4 files changed
+166 -4

No files matched your search

+23
View File
@@ -135,6 +135,20 @@ namespace OpenNest.Avalonia.Controls
/// </summary>
public bool CutSelection() => PlateClipboard.Cut(this, tools, ReadPlacementOrder) != null;
/// <summary>
/// A drawing picked in the drawing list and carried into the view: the place tool with one
/// new part of it.
/// </summary>
public void PlaceDrawing(Drawing drawing) =>
tools.SetTool(DrawingPlacement.CreatePlaceTool(this, drawing, ReadPlacementOrder));
/// <summary>
/// A drawing dragged from the drawing list and dropped at <paramref name="at"/> (view
/// coordinates): one part centred there, or null while edits are locked.
/// </summary>
public Part DropDrawing(Drawing drawing, Point at) =>
DrawingPlacement.AddPart(this, drawing, ToWorld(at));
private SettleOrder ReadPlacementOrder() =>
PlacementOrder?.Invoke() ?? SettleOrder.ClosestToCorner;
@@ -553,6 +567,15 @@ namespace OpenNest.Avalonia.Controls
tools.Current.PointerCaptureLost();
}
// As the WinForms plate view: the pointer entering the view gives it the keyboard, which
// also places a drawing picked up in the drawing list.
protected override void OnPointerEntered(PointerEventArgs e)
{
base.OnPointerEntered(e);
if (!IsFocused)
Focus();
}
protected override void OnPointerExited(PointerEventArgs e)
{
CursorWorld = null;
+9 -2
View File
@@ -873,14 +873,21 @@ namespace OpenNest.Avalonia
// 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())
if (ActiveView is { PlateView: { } canvas } view && canvas.CutSelection())
{
// Focusing the view would otherwise place a drawing picked up in the list.
view.CancelPickup();
canvas.Focus();
}
}
private void OnCopy(object sender, RoutedEventArgs e)
{
if (ActiveView?.PlateView is { } canvas && canvas.CopySelection())
if (ActiveView is { PlateView: { } canvas } view && canvas.CopySelection())
{
view.CancelPickup();
canvas.Focus();
}
}
// Read at each click, as WinForms reads its setting, so a change in Options applies now.
+1
View File
@@ -11,6 +11,7 @@
</TabItem>
<TabItem Header="Drawings" FontSize="14" MinHeight="34">
<DataGrid x:Name="DrawingGrid" x:DataType="local:DrawingRow" IsReadOnly="True"
SelectionMode="Single"
FontSize="13" AutoGenerateColumns="False"
GridLinesVisibility="Horizontal" CanUserSortColumns="True">
<DataGrid.Columns>
+133 -2
View File
@@ -1,14 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.VisualTree;
using OpenNest.Avalonia.Controls;
using OpenNest.Desktop;
using OpenNest.Desktop.Interaction;
namespace OpenNest.Avalonia
{
public sealed record DrawingRow(string Name, int Nested, int Required);
public sealed record DrawingRow(string Name, int Nested, int Required, Drawing Drawing);
/// <summary>
/// One open nest: Plates and Drawings tabs on the left, the plate header and plate view on
@@ -35,6 +39,126 @@ namespace OpenNest.Avalonia
// Starts watching the last two plates (the tab already put the empty plate there).
plates.EnsureSentinel();
Refresh(0);
// Adding parts from the drawing list, as the WinForms nest window: a press picks a
// drawing up for the plate view, a drag drops one part on the plate.
DrawingGrid.AddHandler(PointerPressedEvent, OnDrawingPressed, RoutingStrategies.Tunnel,
handledEventsToo: true);
DrawingGrid.AddHandler(PointerMovedEvent, OnDrawingDragged, RoutingStrategies.Tunnel,
handledEventsToo: true);
DrawingGrid.AddHandler(PointerReleasedEvent, (_, _) => pressedDrawing = null,
RoutingStrategies.Tunnel, handledEventsToo: true);
Canvas.GotFocus += OnCanvasFocused;
DragDrop.SetAllowDrop(Canvas, true);
Canvas.AddHandler(DragDrop.DragEnterEvent, OnDrawingDragOver);
Canvas.AddHandler(DragDrop.DragOverEvent, OnDrawingDragOver);
Canvas.AddHandler(DragDrop.DropEvent, OnDrawingDrop);
}
/// <summary>The drag format carrying a <see cref="Drawing"/> from the list.</summary>
public const string DrawingFormat = "OpenNest.Drawing";
private readonly DrawingPickup pickup = new();
private Drawing pressedDrawing;
private Point pressPoint;
private static Drawing DrawingUnder(PointerEventArgs e) =>
(e.Source as Visual)?.FindAncestorOfType<DataGridRow>(includeSelf: true)
?.DataContext is DrawingRow row
? row.Drawing
: null;
// As the WinForms list: a single left press picks the drawing up (a drag can start from
// it); a double-click puts it down.
private void OnDrawingPressed(object sender, PointerPressedEventArgs e)
{
var point = e.GetCurrentPoint(DrawingGrid);
pressPoint = point.Position;
pressedDrawing = null;
if (!point.Properties.IsLeftButtonPressed)
return;
if (e.ClickCount != 1)
{
pickup.PutDown();
return;
}
pressedDrawing = DrawingUnder(e);
if (pressedDrawing != null)
pickup.PickUp();
}
// A press moved more than 3 pixels drags the drawing; a drop on the plate puts it down.
private async void OnDrawingDragged(object sender, PointerEventArgs e)
{
var drawing = pressedDrawing;
var point = e.GetCurrentPoint(DrawingGrid);
if (drawing == null || !point.Properties.IsLeftButtonPressed)
return;
var dx = point.Position.X - pressPoint.X;
var dy = point.Position.Y - pressPoint.Y;
if (dx * dx + dy * dy <= 9)
return;
pressedDrawing = null;
var data = new DataObject();
data.Set(DrawingFormat, drawing);
try
{
var result = await DragDrop.DoDragDrop(e, data, DragDropEffects.Copy);
if (result != DragDropEffects.None)
pickup.PutDown();
}
catch (Exception ex)
{
// A platform drag failure leaves the drawing picked up, as a cancelled drag does.
System.Diagnostics.Debug.WriteLine($"Drawing drag failed: {ex}");
}
}
/// <summary>
/// Puts a picked-up drawing down: Edit &gt; Cut and Copy start their own place tool and
/// then focus the view, which would otherwise swap in the picked drawing.
/// </summary>
public void CancelPickup()
{
pickup.PutDown();
pressedDrawing = null;
}
private void OnCanvasFocused(object sender, GotFocusEventArgs e)
{
var selected = (DrawingGrid.SelectedItem as DrawingRow)?.Drawing;
if (pickup.TakeOnPlateEnter(selected) is { } drawing)
Canvas.PlaceDrawing(drawing);
}
// Only this nest's drawings, and none while edits are locked (WinForms refuses the drop).
private Drawing DroppedDrawing(DragEventArgs e) =>
e.Data.Get(DrawingFormat) is Drawing drawing
&& Tab.Document.Nest.Drawings.Any(d => ReferenceEquals(d, drawing))
&& !Canvas.EditsLocked
? drawing
: null;
private void OnDrawingDragOver(object sender, DragEventArgs e)
{
if (!e.Data.Contains(DrawingFormat))
return;
e.DragEffects = DroppedDrawing(e) != null ? DragDropEffects.Copy : DragDropEffects.None;
e.Handled = true;
}
private void OnDrawingDrop(object sender, DragEventArgs e)
{
if (!e.Data.Contains(DrawingFormat))
return;
var drawing = DroppedDrawing(e);
var dropped = drawing != null
&& Canvas.DropDrawing(drawing, e.GetPosition(Canvas)) != null;
// Put down now, not when the drag returns: on X11 and macOS the pointer can enter the
// plate (and focus it) before the awaiting drag source resumes.
if (dropped)
pickup.PutDown();
e.DragEffects = dropped ? DragDropEffects.Copy : DragDropEffects.None;
e.Handled = true;
}
// Undo and Redo: puts the copy read back from the history in the document and shows it
@@ -52,6 +176,8 @@ namespace OpenNest.Avalonia
// As WinForms (EditNestForm EndAction): a tool holds the old nest's parts, so it ends
// and Escape cannot bring it back.
Canvas.Tools.EndTool();
pickup.PutDown();
pressedDrawing = null;
Tab.Document.Nest = restored;
Canvas.CutOffSettings = cutOffSettings;
plates.Dispose();
@@ -106,10 +232,15 @@ namespace OpenNest.Avalonia
PlateList.SelectedIndex = nest.Plates.Count > 0
? System.Math.Clamp(selectIndex, 0, nest.Plates.Count - 1)
: -1;
// As the WinForms list: the selected drawing stays selected when the list is rebuilt,
// matched as WinForms matches it (by name), so it survives an Undo's new objects.
var selected = (DrawingGrid.SelectedItem as DrawingRow)?.Drawing;
DrawingGrid.ItemsSource = nest.Drawings
.Where(d => !d.IsCutOff)
.Select(d => new DrawingRow(d.Name, d.Quantity.Nested, d.Quantity.Required))
.Select(d => new DrawingRow(d.Name, d.Quantity.Nested, d.Quantity.Required, d))
.ToList();
DrawingGrid.SelectedItem = ((IEnumerable<DrawingRow>)DrawingGrid.ItemsSource)
.FirstOrDefault(row => Equals(row.Drawing, selected));
shownCounts = CountsText(nest);
ShowPlate();
}