CmdPal: Stop dock window resizes from saving for normal window opens (#46118)

Currently, if you resize a window opened from the dock (i.e. performance
monitor commands) then exit CmdPal, the resized "size" persists on
normal hotkey opens. This change tells CmdPal to revert the size when
opened and only save the size on normal window close.

Fixes #45591

---------

Co-authored-by: Jiří Polášek <me@jiripolasek.com>
This commit is contained in:
Michael Jolley
2026-03-16 11:47:54 -07:00
committed by GitHub
parent 149e7b1efe
commit 77173cd075

View File

@@ -94,6 +94,7 @@ public sealed partial class MainWindow : WindowEx,
private WindowPosition _currentWindowPosition = new(); private WindowPosition _currentWindowPosition = new();
private bool _preventHideWhenDeactivated; private bool _preventHideWhenDeactivated;
private bool _isLoadedFromDock;
private DevRibbon? _devRibbon; private DevRibbon? _devRibbon;
@@ -142,7 +143,7 @@ public sealed partial class MainWindow : WindowEx,
this.SetIcon(); this.SetIcon();
AppWindow.Title = RS_.GetString("AppName"); AppWindow.Title = RS_.GetString("AppName");
RestoreWindowPosition(); RestoreWindowPositionFromSavedSettings();
UpdateWindowPositionInMemory(); UpdateWindowPositionInMemory();
WeakReferenceMessenger.Default.Register<DismissMessage>(this); WeakReferenceMessenger.Default.Register<DismissMessage>(this);
@@ -245,10 +246,26 @@ public sealed partial class MainWindow : WindowEx,
private void PositionCentered(DisplayArea displayArea) private void PositionCentered(DisplayArea displayArea)
{ {
// Use the saved window size when available so that a dock-resized HWND
// (hidden but not destroyed) doesn't dictate the size on normal reopen.
SizeInt32 windowSize;
int windowDpi;
if (_currentWindowPosition.IsSizeValid)
{
windowSize = new SizeInt32(_currentWindowPosition.Width, _currentWindowPosition.Height);
windowDpi = _currentWindowPosition.Dpi;
}
else
{
windowSize = AppWindow.Size;
windowDpi = (int)this.GetDpiForWindow();
}
var rect = WindowPositionHelper.CenterOnDisplay( var rect = WindowPositionHelper.CenterOnDisplay(
displayArea, displayArea,
AppWindow.Size, windowSize,
(int)this.GetDpiForWindow()); windowDpi);
if (rect is not null) if (rect is not null)
{ {
@@ -256,10 +273,9 @@ public sealed partial class MainWindow : WindowEx,
} }
} }
private void RestoreWindowPosition() private void RestoreWindowPosition(WindowPosition? savedPosition)
{ {
var settings = App.Current.Services.GetService<SettingsModel>(); if (savedPosition?.IsSizeValid != true)
if (settings?.LastWindowPosition is not { Width: > 0, Height: > 0 } savedPosition)
{ {
// don't try to restore if the saved position is invalid, just recenter // don't try to restore if the saved position is invalid, just recenter
PositionCentered(); PositionCentered();
@@ -274,6 +290,17 @@ public sealed partial class MainWindow : WindowEx,
MoveAndResizeDpiAware(newRect); MoveAndResizeDpiAware(newRect);
} }
private void RestoreWindowPositionFromSavedSettings()
{
var settings = App.Current.Services.GetService<SettingsModel>();
RestoreWindowPosition(settings?.LastWindowPosition);
}
private void RestoreWindowPositionFromMemory()
{
RestoreWindowPosition(_currentWindowPosition);
}
/// <summary> /// <summary>
/// Moves and resizes the window while suppressing WM_DPICHANGED. /// Moves and resizes the window while suppressing WM_DPICHANGED.
/// The caller is expected to provide a rect already scaled for the target display's DPI. /// The caller is expected to provide a rect already scaled for the target display's DPI.
@@ -678,6 +705,8 @@ public sealed partial class MainWindow : WindowEx,
public void Receive(ShowWindowMessage message) public void Receive(ShowWindowMessage message)
{ {
_isLoadedFromDock = false;
var settings = App.Current.Services.GetService<SettingsModel>()!; var settings = App.Current.Services.GetService<SettingsModel>()!;
// Start session tracking // Start session tracking
@@ -690,6 +719,13 @@ public sealed partial class MainWindow : WindowEx,
internal void Receive(ShowPaletteAtMessage message) internal void Receive(ShowPaletteAtMessage message)
{ {
_isLoadedFromDock = true;
// Reset the size in case users have resized a dock window.
// Ideally in the future, we'll have defined sizes that opening
// a dock window will adhere to, but alas, that's the future.
RestoreWindowPositionFromMemory();
ShowHwnd(HWND.Null, message.PosPixels, message.Anchor); ShowHwnd(HWND.Null, message.PosPixels, message.Anchor);
} }
@@ -860,12 +896,17 @@ public sealed partial class MainWindow : WindowEx,
internal void MainWindow_Closed(object sender, WindowEventArgs args) internal void MainWindow_Closed(object sender, WindowEventArgs args)
{ {
var serviceProvider = App.Current.Services; var serviceProvider = App.Current.Services;
if (!_isLoadedFromDock)
{
UpdateWindowPositionInMemory(); UpdateWindowPositionInMemory();
}
var settings = serviceProvider.GetService<SettingsModel>(); var settings = serviceProvider.GetService<SettingsModel>();
if (settings is not null) if (settings is not null)
{ {
// a quick sanity check, so we don't overwrite correct values // If we were last shown from the dock, _currentWindowPosition still holds
// the last non-dock placement because dock sessions intentionally skip updates.
if (_currentWindowPosition.IsSizeValid) if (_currentWindowPosition.IsSizeValid)
{ {
settings.LastWindowPosition = _currentWindowPosition; settings.LastWindowPosition = _currentWindowPosition;
@@ -960,7 +1001,11 @@ public sealed partial class MainWindow : WindowEx,
if (args.WindowActivationState == WindowActivationState.Deactivated) if (args.WindowActivationState == WindowActivationState.Deactivated)
{ {
// Save the current window position before hiding the window // Save the current window position before hiding the window
// but not when opened from dock — preserve the pre-dock size.
if (!_isLoadedFromDock)
{
UpdateWindowPositionInMemory(); UpdateWindowPositionInMemory();
}
// If there's a debugger attached... // If there's a debugger attached...
if (System.Diagnostics.Debugger.IsAttached) if (System.Diagnostics.Debugger.IsAttached)