From 16635b77912ec1039d4b27c539fc4deda7b63488 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Wed, 11 Nov 2026 17:22:48 -0500 Subject: [PATCH] feat(avalonia): save, save as and export nests, and ask to save on close File > Save (Ctrl+S), Save As and Export .nest, with Save and Save As on the toolbar. Save writes to the file saved to this session, else asks for one in the nest's folder; a failed save says why, keeps the nest unsaved and points to Save As. One save or export runs at a time, a nest cannot be closed while one is under way, and the nest Auto Nest is working on is not written, checked again after the file picker. Closing a nest with unsaved changes asks Save / Don't Save / Cancel, as the WinForms nest window does; Exit asks about each unsaved nest in turn, showing it, and reads the list again after each answer, since Auto Nest can finish while a question is open. Cancel, or a save that does not happen, keeps the app open; an unsaved nest still being nested stops Exit with how to stop Auto Nest. Nest load warnings are shown after opening, one dialog at a time and scrolling when long; for nests opened from the command line they appear once the window opens, and --snapshot only notes them in the status. Co-Authored-By: Claude Opus 5.5 --- OpenNest.Avalonia/ConfirmDialog.cs | 46 +++-- OpenNest.Avalonia/MainWindow.axaml | 7 + OpenNest.Avalonia/MainWindow.axaml.cs | 275 ++++++++++++++++++++++++-- 3 files changed, 301 insertions(+), 27 deletions(-) diff --git a/OpenNest.Avalonia/ConfirmDialog.cs b/OpenNest.Avalonia/ConfirmDialog.cs index dfb612b9..d8a4f5c6 100644 --- a/OpenNest.Avalonia/ConfirmDialog.cs +++ b/OpenNest.Avalonia/ConfirmDialog.cs @@ -7,11 +7,13 @@ using Avalonia.Media; namespace OpenNest.Avalonia { /// - /// A two-button question. Cancel is the default and Esc, so a stray Enter never discards work. + /// A message with a row of buttons ending in Cancel (OK for a plain message). That last + /// button is the default and Esc, so a stray Enter never discards work, and closing the + /// dialog counts as it. /// internal sealed class ConfirmDialog : Window { - private ConfirmDialog(string title, string message, string confirmText) + private ConfirmDialog(string title, string message, string[] choices, string cancelText) { Title = title; Width = 460; @@ -19,30 +21,52 @@ namespace OpenNest.Avalonia CanResize = false; WindowStartupLocation = WindowStartupLocation.CenterOwner; - var confirm = new Button { Content = confirmText }; - var cancel = new Button { Content = "Cancel", IsDefault = true, IsCancel = true }; - confirm.Click += (_, _) => Close(true); - cancel.Click += (_, _) => Close(false); - var buttons = new StackPanel { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Right, Spacing = 8, }; - buttons.Children.Add(confirm); + for (var i = 0; i < choices.Length; i++) + { + var index = i; + var choice = new Button { Content = choices[i] }; + choice.Click += (_, _) => Close(index); + buttons.Children.Add(choice); + } + var cancel = new Button { Content = cancelText, IsDefault = true, IsCancel = true }; + cancel.Click += (_, _) => Close(null); buttons.Children.Add(cancel); var panel = new StackPanel { Margin = new Thickness(18), Spacing = 18 }; - panel.Children.Add(new TextBlock { Text = message, TextWrapping = TextWrapping.Wrap }); + // A long message (many load warnings) scrolls rather than pushing the buttons away. + panel.Children.Add(new ScrollViewer + { + MaxHeight = 420, + Content = new TextBlock { Text = message, TextWrapping = TextWrapping.Wrap }, + }); panel.Children.Add(buttons); Content = panel; Opened += (_, _) => cancel.Focus(); } /// True when the user chose . - public static Task AskAsync( + public static async Task AskAsync( Window owner, string title, string message, string confirmText) => - new ConfirmDialog(title, message, confirmText).ShowDialog(owner); + await ChooseAsync(owner, title, message, confirmText) == 0; + + /// + /// The index of the choice the user clicked, or -1 for Cancel (also Esc, Enter and + /// closing the dialog). + /// + public static async Task ChooseAsync( + Window owner, string title, string message, params string[] choices) => + await new ConfirmDialog(title, message, choices, "Cancel") + .ShowDialog(owner) ?? -1; + + /// A message with only an OK button. + public static Task ShowAsync(Window owner, string title, string message) => + new ConfirmDialog(title, message, System.Array.Empty(), "OK") + .ShowDialog(owner); } } diff --git a/OpenNest.Avalonia/MainWindow.axaml b/OpenNest.Avalonia/MainWindow.axaml index b9d9bbda..07879def 100644 --- a/OpenNest.Avalonia/MainWindow.axaml +++ b/OpenNest.Avalonia/MainWindow.axaml @@ -7,6 +7,11 @@ + + + + + @@ -28,6 +33,8 @@