Skip to content

Commit 23b2ea5

Browse files
authored
Fix Avalonia main window growing on every restart (Windows) (#5056)
1 parent 0dddfab commit 23b2ea5

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎src/UniGetUI.Avalonia/Views/MainWindow.axaml.cs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ public partial class MainWindow : Window
9494
private bool _allowClose;
9595
private int _isQuitting;
9696

97+
// Saved outer size (DIPs) awaiting a native, exact restore in OnOpened on Windows.
98+
private double _pendingRestoreWidth;
99+
private double _pendingRestoreHeight;
100+
97101
// Last user-chosen height (px) of the operations panel; restored when re-expanded.
98102
private double _operationsPanelHeight = 240;
99103

@@ -161,6 +165,21 @@ protected override void OnOpened(EventArgs e)
161165
}
162166

163167
SetupMicaAndAccentBorder();
168+
169+
// Restore the saved size as an exact outer rect in physical pixels. Because our
170+
// WM_NCCALCSIZE keeps client == window rect, this round-trips with SaveGeometry with no
171+
// frame drift, unlike Avalonia's Width/Height setter (see RestoreGeometry).
172+
if (_pendingRestoreWidth > 0 && _pendingRestoreHeight > 0 && WindowState == WindowState.Normal
173+
&& TryGetPlatformHandle()?.Handle is { } sizeHandle && sizeHandle != 0)
174+
{
175+
uint dpi = NativeMethods.GetDpiForWindow(sizeHandle);
176+
if (dpi == 0) dpi = 96;
177+
double scale = dpi / 96.0;
178+
NativeMethods.SetWindowPos(sizeHandle, 0, 0, 0,
179+
(int)Math.Round(_pendingRestoreWidth * scale), (int)Math.Round(_pendingRestoreHeight * scale),
180+
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
181+
}
182+
_pendingRestoreWidth = _pendingRestoreHeight = 0;
164183
}
165184

166185
protected override void OnClosing(WindowClosingEventArgs e)
@@ -660,6 +679,15 @@ private void RestoreGeometry()
660679
Width = width;
661680
Height = height;
662681
Position = new PixelPoint(x, y);
682+
// On Windows our WM_NCCALCSIZE makes the client rect equal the full window rect, so the
683+
// saved size is the outer size. Avalonia's Width/Height setter re-adds WS_THICKFRAME via
684+
// AdjustWindowRectEx, inflating the window a little on every restart; OnOpened restores
685+
// the exact outer rect natively to cancel that drift.
686+
if (OperatingSystem.IsWindows())
687+
{
688+
_pendingRestoreWidth = width;
689+
_pendingRestoreHeight = height;
690+
}
663691
}
664692
else
665693
{

0 commit comments

Comments
 (0)