Skip to content

Commit abaeb01

Browse files
PrimeBuild-pcclaude
andcommitted
fix: restore from maximised returns to the previous window size
Restoring left the window filling the screen, so it had to be dragged back by a border. OnStateChanged corrected the window placement synchronously, but at that point WindowState is already Normal while ActualWidth/Left still describe the maximised frame - so the placement helper saw a screen-sized window, "corrected" it, and wrote the working area into Width/Height, destroying the restore bounds. Every later restore repeated it. The correction is now posted at Background priority, after the transition has settled, and does nothing when the restored bounds are already sane. When there are no smaller bounds to return to - the window was maximised from the start - it falls back to the app's own default size, centred on the monitor the window was actually on rather than the primary. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 88f2767 commit abaeb01

3 files changed

Lines changed: 128 additions & 7 deletions

File tree

‎Helpers/WindowPlacementHelper.cs‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ public static class WindowPlacementHelper
3434
private const double DefaultWindowWidth = 1280;
3535
private const double DefaultWindowHeight = 864;
3636
private const double MinimumVisibleAreaRatio = 0.25;
37+
38+
// A window this close to filling its monitor's working area is treated as having no
39+
// usable windowed bounds (e.g. restored from maximised without a smaller size to go back to).
40+
private const double FillsWorkingAreaRatio = 0.98;
41+
private const double FallbackWorkingAreaRatio = 0.9;
3742
private const double Epsilon = 0.001;
3843

3944
public static WindowPlacementCorrection CorrectWindowBounds(
4045
WindowBounds currentBounds,
41-
IReadOnlyCollection<MonitorWorkingArea> workingAreas)
46+
IReadOnlyCollection<MonitorWorkingArea> workingAreas,
47+
bool shrinkWhenFillingWorkingArea = false)
4248
{
4349
var validWorkingAreas = workingAreas
4450
.Where(IsValidWorkingArea)
@@ -56,6 +62,20 @@ public static WindowPlacementCorrection CorrectWindowBounds(
5662
}
5763

5864
var targetArea = SelectTargetWorkingArea(currentBounds, validWorkingAreas);
65+
66+
// The target monitor is resolved from the original bounds first, so the fallback size
67+
// stays centred on the monitor the window was already on.
68+
if (shrinkWhenFillingWorkingArea && FillsWorkingArea(currentBounds, targetArea))
69+
{
70+
currentBounds = currentBounds with
71+
{
72+
Left = double.NaN,
73+
Top = double.NaN,
74+
Width = Math.Min(DefaultWindowWidth, targetArea.Width * FallbackWorkingAreaRatio),
75+
Height = Math.Min(DefaultWindowHeight, targetArea.Height * FallbackWorkingAreaRatio),
76+
};
77+
}
78+
5979
var correctedWidth = Math.Min(ResolveDimension(currentBounds.Width, DefaultWindowWidth), targetArea.Width);
6080
var correctedHeight = Math.Min(ResolveDimension(currentBounds.Height, DefaultWindowHeight), targetArea.Height);
6181
var sanitizedBounds = new WindowBounds(
@@ -88,7 +108,7 @@ public static WindowPlacementCorrection CorrectWindowBounds(
88108
return new WindowPlacementCorrection(correctedBounds, !BoundsAreEquivalent(currentBounds, correctedBounds));
89109
}
90110

91-
public static bool TryCorrectWindowPlacement(Window window)
111+
public static bool TryCorrectWindowPlacement(Window window, bool shrinkWhenFillingWorkingArea = false)
92112
{
93113
ArgumentNullException.ThrowIfNull(window);
94114

@@ -106,7 +126,7 @@ public static bool TryCorrectWindowPlacement(Window window)
106126
ResolveWindowDimension(window.ActualWidth, window.Width, DefaultWindowWidth),
107127
ResolveWindowDimension(window.ActualHeight, window.Height, DefaultWindowHeight));
108128

109-
var correction = CorrectWindowBounds(currentBounds, workingAreas);
129+
var correction = CorrectWindowBounds(currentBounds, workingAreas, shrinkWhenFillingWorkingArea);
110130
if (!correction.WasCorrected)
111131
{
112132
return false;
@@ -219,6 +239,14 @@ private static double GetSquaredDistanceToArea(double x, double y, MonitorWorkin
219239
return (deltaX * deltaX) + (deltaY * deltaY);
220240
}
221241

242+
private static bool FillsWorkingArea(WindowBounds bounds, MonitorWorkingArea area)
243+
{
244+
return IsFinite(bounds.Width)
245+
&& IsFinite(bounds.Height)
246+
&& bounds.Width >= area.Width * FillsWorkingAreaRatio
247+
&& bounds.Height >= area.Height * FillsWorkingAreaRatio;
248+
}
249+
222250
private static bool HasSufficientIntersection(WindowBounds bounds, MonitorWorkingArea area)
223251
{
224252
var intersectionArea = GetIntersectionArea(bounds, area);

‎MainWindow.Behaviors.partial.cs‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,8 +1400,13 @@ private void OnProcessMonitorManagerStatusChanged(object? sender, ServiceStatusE
14001400
}
14011401
}
14021402

1403+
private WindowState lastObservedWindowState = WindowState.Normal;
1404+
14031405
protected override void OnStateChanged(EventArgs e)
14041406
{
1407+
var previousWindowState = this.lastObservedWindowState;
1408+
this.lastObservedWindowState = this.WindowState;
1409+
14051410
try
14061411
{
14071412
if (this.WindowState == WindowState.Minimized)
@@ -1420,7 +1425,15 @@ protected override void OnStateChanged(EventArgs e)
14201425
else if (this.WindowState == WindowState.Normal || this.WindowState == WindowState.Maximized)
14211426
{
14221427
this.ShowInTaskbar = true;
1423-
this.EnsureDashboardVisibleOnScreen();
1428+
1429+
// While StateChanged runs, ActualWidth/ActualHeight/Left/Top still describe the
1430+
// pre-transition window, so correcting placement here would write the maximised
1431+
// size back into Width/Height and destroy the restore bounds. Let it settle first.
1432+
var restoredFromMaximized = this.WindowState == WindowState.Normal
1433+
&& previousWindowState == WindowState.Maximized;
1434+
_ = this.Dispatcher.BeginInvoke(
1435+
new Action(() => this.EnsureDashboardVisibleOnScreen(restoredFromMaximized)),
1436+
System.Windows.Threading.DispatcherPriority.Background);
14241437

14251438
this.ApplyAppRefreshPolicy(this.GetForegroundActivityState());
14261439
}
@@ -1625,10 +1638,11 @@ private void ShowWindowFromTray(string? tabTag = null)
16251638

16261639
if (this.WindowState == WindowState.Minimized)
16271640
{
1641+
// OnStateChanged schedules the placement correction once the restore has settled;
1642+
// doing it here would read the pre-restore metrics.
16281643
this.WindowState = WindowState.Normal;
16291644
}
16301645

1631-
this.EnsureDashboardVisibleOnScreen();
16321646
this.ShowInTaskbar = true;
16331647

16341648
if (tabTag != null)
@@ -1653,9 +1667,9 @@ private void ShowWindowFromTray(string? tabTag = null)
16531667
: AppActivityState.ForegroundOtherTab);
16541668
}
16551669

1656-
internal bool EnsureDashboardVisibleOnScreen()
1670+
internal bool EnsureDashboardVisibleOnScreen(bool shrinkWhenFillingWorkingArea = false)
16571671
{
1658-
return WindowPlacementHelper.TryCorrectWindowPlacement(this);
1672+
return WindowPlacementHelper.TryCorrectWindowPlacement(this, shrinkWhenFillingWorkingArea);
16591673
}
16601674

16611675
private void SelectMainTab(string tag)

‎Tests/ThreadPilot.Core.Tests/WindowPlacementHelperTests.cs‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,85 @@ public void CorrectWindowBounds_WhenPartiallyOutside_ClampsInsideIntersectingWor
6363
Assert.Equal(864, result.Bounds.Height);
6464
}
6565

66+
[Fact]
67+
public void CorrectWindowBounds_WhenFillingWorkingAreaAndShrinkRequested_CentersDefaultSizeOnSameMonitor()
68+
{
69+
var monitors = new[]
70+
{
71+
new MonitorWorkingArea(0, 0, 1920, 1040, true),
72+
new MonitorWorkingArea(1920, 0, 2560, 1400, false),
73+
};
74+
75+
// Restored from maximised on the secondary monitor with no smaller windowed size to return to.
76+
var result = WindowPlacementHelper.CorrectWindowBounds(
77+
new WindowBounds(1920, 0, 2560, 1400),
78+
monitors,
79+
shrinkWhenFillingWorkingArea: true);
80+
81+
Assert.True(result.WasCorrected);
82+
Assert.Equal(1280, result.Bounds.Width);
83+
Assert.Equal(864, result.Bounds.Height);
84+
Assert.Equal(1920 + ((2560 - 1280) / 2), result.Bounds.Left);
85+
Assert.Equal((1400 - 864) / 2, result.Bounds.Top);
86+
}
87+
88+
[Fact]
89+
public void CorrectWindowBounds_WhenFillingSmallWorkingAreaAndShrinkRequested_StaysClearlySmallerThanWorkingArea()
90+
{
91+
var monitors = new[]
92+
{
93+
new MonitorWorkingArea(0, 0, 1366, 728, true),
94+
};
95+
96+
var result = WindowPlacementHelper.CorrectWindowBounds(
97+
new WindowBounds(0, 0, 1366, 728),
98+
monitors,
99+
shrinkWhenFillingWorkingArea: true);
100+
101+
Assert.True(result.WasCorrected);
102+
Assert.True(result.Bounds.Width < 1366);
103+
Assert.True(result.Bounds.Height < 728);
104+
Assert.Equal(1229, result.Bounds.Width);
105+
Assert.Equal(655, result.Bounds.Height);
106+
}
107+
108+
[Fact]
109+
public void CorrectWindowBounds_WhenFillingWorkingAreaAndShrinkNotRequested_KeepsSize()
110+
{
111+
var monitors = new[]
112+
{
113+
new MonitorWorkingArea(0, 0, 1920, 1040, true),
114+
};
115+
116+
var result = WindowPlacementHelper.CorrectWindowBounds(
117+
new WindowBounds(0, 0, 1920, 1040),
118+
monitors);
119+
120+
Assert.False(result.WasCorrected);
121+
Assert.Equal(1920, result.Bounds.Width);
122+
Assert.Equal(1040, result.Bounds.Height);
123+
}
124+
125+
[Fact]
126+
public void CorrectWindowBounds_WhenSmallerThanWorkingAreaAndShrinkRequested_KeepsWindowedBounds()
127+
{
128+
var monitors = new[]
129+
{
130+
new MonitorWorkingArea(0, 0, 1920, 1040, true),
131+
};
132+
133+
var result = WindowPlacementHelper.CorrectWindowBounds(
134+
new WindowBounds(300, 200, 1100, 700),
135+
monitors,
136+
shrinkWhenFillingWorkingArea: true);
137+
138+
Assert.False(result.WasCorrected);
139+
Assert.Equal(300, result.Bounds.Left);
140+
Assert.Equal(200, result.Bounds.Top);
141+
Assert.Equal(1100, result.Bounds.Width);
142+
Assert.Equal(700, result.Bounds.Height);
143+
}
144+
66145
[Fact]
67146
public void CorrectWindowBounds_WhenAlreadyVisibleOnSecondaryMonitor_DoesNotMoveWindow()
68147
{

0 commit comments

Comments
 (0)