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.
This commit is contained in:
aj committed 2026-11-26 11:17:43 -05:00
1 parent f88b1b37fa
commit d69fc936e7
2 files changed
+98 -14

No files matched your search

@@ -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);
}
}
+35 -14
View File
@@ -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
/// <summary>The main window's persisted placement and its one-time settings upgrades.</summary>
internal static class MainWindowSettings
{
/// <summary>Restores the saved placement when it still fits the primary screen.</summary>
/// <summary>
/// Restores the saved placement when it still fits the primary screen. Without a usable
/// saved size the window keeps its designer start, maximized.
/// </summary>
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();
}
/// <summary>
/// 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.
/// </summary>
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;
/// <summary>
/// What to remember on close: the restored bounds of a maximized or minimized window, and
/// never the minimized state.
/// </summary>
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);
/// <summary>
/// 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.