@@ -94,6 +94,7 @@ public sealed partial class MainWindow : WindowEx,
9494 private WindowPosition _currentWindowPosition = new ( ) ;
9595
9696 private bool _preventHideWhenDeactivated ;
97+ private bool _isLoadedFromDock ;
9798
9899 private DevRibbon ? _devRibbon ;
99100
@@ -142,7 +143,7 @@ public MainWindow()
142143
143144 this . SetIcon ( ) ;
144145 AppWindow . Title = RS_ . GetString ( "AppName" ) ;
145- RestoreWindowPosition ( ) ;
146+ RestoreWindowPositionFromSavedSettings ( ) ;
146147 UpdateWindowPositionInMemory ( ) ;
147148
148149 WeakReferenceMessenger . Default . Register < DismissMessage > ( this ) ;
@@ -245,21 +246,36 @@ private void PositionCentered()
245246
246247 private void PositionCentered ( DisplayArea displayArea )
247248 {
249+ // Use the saved window size when available so that a dock-resized HWND
250+ // (hidden but not destroyed) doesn't dictate the size on normal reopen.
251+ SizeInt32 windowSize ;
252+ int windowDpi ;
253+
254+ if ( _currentWindowPosition . IsSizeValid )
255+ {
256+ windowSize = new SizeInt32 ( _currentWindowPosition . Width , _currentWindowPosition . Height ) ;
257+ windowDpi = _currentWindowPosition . Dpi ;
258+ }
259+ else
260+ {
261+ windowSize = AppWindow . Size ;
262+ windowDpi = ( int ) this . GetDpiForWindow ( ) ;
263+ }
264+
248265 var rect = WindowPositionHelper . CenterOnDisplay (
249- displayArea ,
250- AppWindow . Size ,
251- ( int ) this . GetDpiForWindow ( ) ) ;
266+ displayArea ,
267+ windowSize ,
268+ windowDpi ) ;
252269
253270 if ( rect is not null )
254271 {
255272 MoveAndResizeDpiAware ( rect . Value ) ;
256273 }
257274 }
258275
259- private void RestoreWindowPosition ( )
276+ private void RestoreWindowPosition ( WindowPosition ? savedPosition )
260277 {
261- var settings = App . Current . Services . GetService < SettingsModel > ( ) ;
262- if ( settings ? . LastWindowPosition is not { Width : > 0 , Height : > 0 } savedPosition )
278+ if ( savedPosition ? . IsSizeValid != true )
263279 {
264280 // don't try to restore if the saved position is invalid, just recenter
265281 PositionCentered ( ) ;
@@ -274,6 +290,17 @@ private void RestoreWindowPosition()
274290 MoveAndResizeDpiAware ( newRect ) ;
275291 }
276292
293+ private void RestoreWindowPositionFromSavedSettings ( )
294+ {
295+ var settings = App . Current . Services . GetService < SettingsModel > ( ) ;
296+ RestoreWindowPosition ( settings ? . LastWindowPosition ) ;
297+ }
298+
299+ private void RestoreWindowPositionFromMemory ( )
300+ {
301+ RestoreWindowPosition ( _currentWindowPosition ) ;
302+ }
303+
277304 /// <summary>
278305 /// Moves and resizes the window while suppressing WM_DPICHANGED.
279306 /// The caller is expected to provide a rect already scaled for the target display's DPI.
@@ -678,6 +705,8 @@ private static DisplayArea GetScreen(HWND currentHwnd, MonitorBehavior target)
678705
679706 public void Receive ( ShowWindowMessage message )
680707 {
708+ _isLoadedFromDock = false ;
709+
681710 var settings = App . Current . Services . GetService < SettingsModel > ( ) ! ;
682711
683712 // Start session tracking
@@ -690,6 +719,13 @@ public void Receive(ShowWindowMessage message)
690719
691720 internal void Receive ( ShowPaletteAtMessage message )
692721 {
722+ _isLoadedFromDock = true ;
723+
724+ // Reset the size in case users have resized a dock window.
725+ // Ideally in the future, we'll have defined sizes that opening
726+ // a dock window will adhere to, but alas, that's the future.
727+ RestoreWindowPositionFromMemory ( ) ;
728+
693729 ShowHwnd ( HWND . Null , message . PosPixels , message . Anchor ) ;
694730 }
695731
@@ -860,12 +896,17 @@ private void Uncloak()
860896 internal void MainWindow_Closed ( object sender , WindowEventArgs args )
861897 {
862898 var serviceProvider = App . Current . Services ;
863- UpdateWindowPositionInMemory ( ) ;
899+
900+ if ( ! _isLoadedFromDock )
901+ {
902+ UpdateWindowPositionInMemory ( ) ;
903+ }
864904
865905 var settings = serviceProvider . GetService < SettingsModel > ( ) ;
866906 if ( settings is not null )
867907 {
868- // a quick sanity check, so we don't overwrite correct values
908+ // If we were last shown from the dock, _currentWindowPosition still holds
909+ // the last non-dock placement because dock sessions intentionally skip updates.
869910 if ( _currentWindowPosition . IsSizeValid )
870911 {
871912 settings . LastWindowPosition = _currentWindowPosition ;
@@ -960,7 +1001,11 @@ internal void MainWindow_Activated(object sender, WindowActivatedEventArgs args)
9601001 if ( args . WindowActivationState == WindowActivationState . Deactivated )
9611002 {
9621003 // Save the current window position before hiding the window
963- UpdateWindowPositionInMemory ( ) ;
1004+ // but not when opened from dock — preserve the pre-dock size.
1005+ if ( ! _isLoadedFromDock )
1006+ {
1007+ UpdateWindowPositionInMemory ( ) ;
1008+ }
9641009
9651010 // If there's a debugger attached...
9661011 if ( System . Diagnostics . Debugger . IsAttached )
0 commit comments