From d69fc936e7de8c66cb815e6b0d1ed1b15a147ea4 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Thu, 26 Nov 2026 11:17:43 -0500 Subject: [PATCH] fix(desktop): stop the main window opening at its minimum size With no saved size, Load applied the 0, 0 default, which Windows raised to the window's minimum (588 x 381); the next normal close saved that minimum, and from then on the window always opened at it. Before settings survived updates (v0.7.0) every update started from the default, and the settings migration then carried the saved minimum into user-settings.json. Load now ignores a saved size that is empty or no larger than the minimum and keeps the designer start, maximized, so existing settings recover. Save keeps the restored bounds of a maximized or minimized window and never saves the minimized state, as the Drawing Sets window already does. --- .../Forms/MainWindowSettingsTests.cs | 63 +++++++++++++++++++ OpenNest/Forms/MainWindowSettings.cs | 49 ++++++++++----- 2 files changed, 98 insertions(+), 14 deletions(-) create mode 100644 OpenNest.WinForms.Tests/Forms/MainWindowSettingsTests.cs diff --git a/OpenNest.WinForms.Tests/Forms/MainWindowSettingsTests.cs b/OpenNest.WinForms.Tests/Forms/MainWindowSettingsTests.cs new file mode 100644 index 00000000..2d1d0e31 --- /dev/null +++ b/OpenNest.WinForms.Tests/Forms/MainWindowSettingsTests.cs @@ -0,0 +1,63 @@ +using System.Drawing; +using System.Windows.Forms; +using OpenNest.Forms; + +namespace OpenNest.WinForms.Tests.Forms; + +public class MainWindowSettingsTests +{ + private static readonly Size Minimum = new(588, 381); + private static readonly Size ScreenSize = new(1920, 1080); + + [Theory] + [InlineData(0, 0)] // The default: no size was ever saved. + [InlineData(588, 381)] // The minimum the default shrank to, saved on a later close. + [InlineData(500, 300)] + [InlineData(1921, 800)] // Wider than the screen. + [InlineData(1200, 1081)] // Taller than the screen. + public void SizesNotWorthRestoringAreIgnored(int width, int height) => + Assert.False( + MainWindowSettings.IsRestorableSize(new Size(width, height), Minimum, ScreenSize)); + + [Theory] + [InlineData(1281, 654)] + [InlineData(589, 381)] + [InlineData(588, 382)] + [InlineData(1920, 1080)] + public void SizesAboveTheMinimumThatFitAreRestored(int width, int height) => + Assert.True( + MainWindowSettings.IsRestorableSize(new Size(width, height), Minimum, ScreenSize)); + + [Fact] + public void ANormalWindowSavesItsBounds() + { + var bounds = new Rectangle(100, 50, 1200, 800); + + var saved = MainWindowSettings.SavedPlacement( + FormWindowState.Normal, bounds, Rectangle.Empty); + + Assert.Equal((bounds, FormWindowState.Normal), saved); + } + + [Fact] + public void AMaximizedWindowSavesTheBoundsItRestoresTo() + { + var restore = new Rectangle(100, 50, 1200, 800); + + var saved = MainWindowSettings.SavedPlacement( + FormWindowState.Maximized, new Rectangle(-8, -8, 1936, 1056), restore); + + Assert.Equal((restore, FormWindowState.Maximized), saved); + } + + [Fact] + public void AMinimizedWindowSavesItsRestoreBoundsAsNormal() + { + var restore = new Rectangle(100, 50, 1200, 800); + + var saved = MainWindowSettings.SavedPlacement( + FormWindowState.Minimized, new Rectangle(-32000, -32000, 160, 28), restore); + + Assert.Equal((restore, FormWindowState.Normal), saved); + } +} diff --git a/OpenNest/Forms/MainWindowSettings.cs b/OpenNest/Forms/MainWindowSettings.cs index 0bd878e5..fcae6176 100644 --- a/OpenNest/Forms/MainWindowSettings.cs +++ b/OpenNest/Forms/MainWindowSettings.cs @@ -1,4 +1,5 @@ using System; +using System.Drawing; using System.Windows.Forms; using OpenNest.Data; using OpenNest.Desktop; @@ -10,33 +11,53 @@ namespace OpenNest.Forms /// The main window's persisted placement and its one-time settings upgrades. internal static class MainWindowSettings { - /// Restores the saved placement when it still fits the primary screen. + /// + /// Restores the saved placement when it still fits the primary screen. Without a usable + /// saved size the window keeps its designer start, maximized. + /// public static void Load(Form form) { var screen = Screen.PrimaryScreen.Bounds; + if (!IsRestorableSize(Settings.Default.MainWindowSize, form.MinimumSize, screen.Size)) + return; if (screen.Contains(Settings.Default.MainWindowLocation)) form.Location = Settings.Default.MainWindowLocation; - - if ( - Settings.Default.MainWindowSize.Width <= screen.Width - && Settings.Default.MainWindowSize.Height <= screen.Height - ) - { - form.Size = Settings.Default.MainWindowSize; - } - - form.WindowState = Settings.Default.MainWindowState; + form.Size = Settings.Default.MainWindowSize; + form.WindowState = Settings.Default.MainWindowState == FormWindowState.Maximized + ? FormWindowState.Maximized : FormWindowState.Normal; } public static void Save(Form form) { - Settings.Default.MainWindowLocation = form.Location; - Settings.Default.MainWindowSize = form.Size; - Settings.Default.MainWindowState = form.WindowState; + var (bounds, state) = SavedPlacement(form.WindowState, form.Bounds, form.RestoreBounds); + Settings.Default.MainWindowLocation = bounds.Location; + Settings.Default.MainWindowSize = bounds.Size; + Settings.Default.MainWindowState = state; Settings.Default.Save(); } + /// + /// Whether a saved main-window size is worth restoring: it fits the screen and is larger + /// than the window's minimum. The 0, 0 default, applied, shrinks the window to its minimum, + /// and a later close used to save that minimum, so the window opened at it from then on. + /// + internal static bool IsRestorableSize(Size saved, Size minimum, Size screen) => + saved.Width > 0 && saved.Height > 0 + && (saved.Width > minimum.Width || saved.Height > minimum.Height) + && saved.Width <= screen.Width && saved.Height <= screen.Height; + + /// + /// What to remember on close: the restored bounds of a maximized or minimized window, and + /// never the minimized state. + /// + internal static (Rectangle Bounds, FormWindowState State) SavedPlacement( + FormWindowState state, Rectangle bounds, Rectangle restoreBounds) => + state == FormWindowState.Normal + ? (bounds, FormWindowState.Normal) + : (restoreBounds, state == FormWindowState.Maximized + ? FormWindowState.Maximized : FormWindowState.Normal); + /// /// Places the Drawing Sets window where it last closed when that still shows on a /// screen; the first time, maximized on the main window's screen.