using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Interactivity;
using OpenNest.Avalonia.Controls;
using OpenNest.Desktop;
using OpenNest.Desktop.Interaction;
using OpenNest.Undo;
namespace OpenNest.Avalonia
{
///
/// Plate > Arrange on the active nest's plate, through the shared ,
/// each one undo step under the WinForms label: align, evenly space and center parts, Expand
/// Spacing and Fix Spacing behind a Cancel window, and the common-line commands, which the
/// right-click menu on a selected part also offers. While Auto Nest works on the nest its plate
/// takes none of them; Expand and Fix Spacing then say why.
///
public partial class MainWindow
{
// The nest whose Expand or Fix Spacing search is running: its plate is locked until the
// worker stops, as the WinForms Cancel window keeps it.
private NestTab arrangingTab;
private PlateCanvas ArrangeCanvas =>
ActiveView?.PlateView is { Plate: not null } canvas ? canvas : null;
/// Shows the part menu when a selected part on 's plate is right-clicked.
private void WatchPartMenu(NestView view) =>
view.PlateView.PartMenuRequested += (_, _) => ShowPartMenu(view);
// As WinForms (ArrangeMenu_Opening): Cut Common Lines Twice shows the plate's setting.
// The other commands do nothing, or say why, when they have nothing to act on.
private void OnArrangeMenuOpened(object sender, RoutedEventArgs e)
{
var canvas = ArrangeCanvas;
var editable = canvas is { EditsLocked: false };
foreach (var item in new MenuItem[]
{
AlignLeftMenuItem, AlignRightMenuItem, AlignTopMenuItem, AlignBottomMenuItem,
AlignHorizontallyMenuItem, AlignVerticallyMenuItem, EvenlySpaceHorizontallyMenuItem,
EvenlySpaceVerticallyMenuItem, CenterPartsMenuItem, JoinCommonLineMenuItem,
RemoveCommonLineMenuItem, CutCommonLinesTwiceMenuItem,
})
item.IsEnabled = editable;
// Enabled during Auto Nest so they can say why they wait, as WinForms does.
ExpandSpacingMenuItem.IsEnabled = canvas != null && arrangingTab == null;
FixSpacingMenuItem.IsEnabled = canvas != null && arrangingTab == null;
CutCommonLinesTwiceMenuItem.IsChecked = canvas?.Plate.CutCommonLinesTwice == true;
}
private void OnAlign(object sender, RoutedEventArgs e)
{
if (ArrangeCanvas is { } canvas && (sender as MenuItem)?.Tag is string tag
&& Enum.TryParse(tag, out var type))
PlateArrange.Align(canvas, type);
}
private void OnCenterParts(object sender, RoutedEventArgs e)
{
if (ArrangeCanvas is { } canvas)
PlateArrange.CenterParts(canvas);
}
private async void OnJoinCommonLine(object sender, RoutedEventArgs e) => await JoinCommonLineAsync();
private async void OnRemoveCommonLine(object sender, RoutedEventArgs e) => await RemoveCommonLineAsync();
private void OnCutCommonLinesTwice(object sender, RoutedEventArgs e)
{
if (ArrangeCanvas is { } canvas)
PlateArrange.ToggleCutCommonLinesTwice(canvas);
CutCommonLinesTwiceMenuItem.IsChecked = ArrangeCanvas?.Plate.CutCommonLinesTwice == true;
}
// Joined at the kerf of the plate's cutting parameters, as WinForms.
private async Task JoinCommonLineAsync()
{
if (ActiveView is not { PlateView: { Plate: { } plate } canvas } view)
return;
var kerf = DesktopSettings.Machines.CuttingParametersFor(plate, view.Tab.Document.Nest).Kerf;
if (PlateArrange.JoinCommonLine(canvas, kerf) is { Succeeded: false } result)
await ConfirmDialog.ShowAsync(this, "Common Line", result.Failure);
}
private async Task RemoveCommonLineAsync()
{
if (ArrangeCanvas is { } canvas && PlateArrange.RemoveCommonLines(canvas) == false)
await ConfirmDialog.ShowAsync(this, "Common Line", PlateArrange.NoCommonLineNotice);
}
// As the WinForms part menu: Join needs two units to bring together, Remove a grouped part.
private void ShowPartMenu(NestView view)
{
var canvas = view.PlateView;
if (view != ActiveView || canvas.Plate == null)
return;
var (canJoin, canRemove) = PlateArrange.CommonLineCommands(canvas.Plate,
PlateArrange.EditableSelection(canvas) ?? Array.Empty());
var join = new MenuItem { Header = "Join Common Line", IsEnabled = canJoin };
join.Click += async (_, _) => await JoinCommonLineAsync();
var remove = new MenuItem { Header = "Remove Common Line", IsEnabled = canRemove };
remove.Click += async (_, _) => await RemoveCommonLineAsync();
var menu = new ContextMenu { ItemsSource = new List