Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions src/modules/cmdpal/Microsoft.CmdPal.UI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public sealed partial class MainWindow : WindowEx,
private WindowPosition _currentWindowPosition = new();

private bool _preventHideWhenDeactivated;
private bool _isLoadedFromDock;

private DevRibbon? _devRibbon;

Expand Down Expand Up @@ -245,10 +246,26 @@ private void PositionCentered()

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(
displayArea,
AppWindow.Size,
(int)this.GetDpiForWindow());
displayArea,
windowSize,
windowDpi);

if (rect is not null)
{
Expand Down Expand Up @@ -678,6 +695,8 @@ private static DisplayArea GetScreen(HWND currentHwnd, MonitorBehavior target)

public void Receive(ShowWindowMessage message)
{
_isLoadedFromDock = false;

var settings = App.Current.Services.GetService<SettingsModel>()!;

// Start session tracking
Expand All @@ -690,6 +709,13 @@ public void Receive(ShowWindowMessage 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.
RestoreWindowPosition();

ShowHwnd(HWND.Null, message.PosPixels, message.Anchor);
}

Expand All @@ -698,6 +724,7 @@ public void Receive(HideWindowMessage message)
// This might come in off the UI thread. Make sure to hop back.
DispatcherQueue.TryEnqueue(() =>
{
_isLoadedFromDock = false;
EndSession("Hide");
HideWindow();
});
Expand Down Expand Up @@ -860,13 +887,16 @@ private void Uncloak()
internal void MainWindow_Closed(object sender, WindowEventArgs args)
{
var serviceProvider = App.Current.Services;

UpdateWindowPositionInMemory();

var settings = serviceProvider.GetService<SettingsModel>();
if (settings is not null)
{
// a quick sanity check, so we don't overwrite correct values
if (_currentWindowPosition.IsSizeValid)
// and don't save size changes from dock
if (_currentWindowPosition.IsSizeValid &&
!_isLoadedFromDock)
{
settings.LastWindowPosition = _currentWindowPosition;
SettingsModel.SaveSettings(settings);
Expand Down Expand Up @@ -960,7 +990,11 @@ internal void MainWindow_Activated(object sender, WindowActivatedEventArgs args)
if (args.WindowActivationState == WindowActivationState.Deactivated)
{
// Save the current window position before hiding the window
UpdateWindowPositionInMemory();
// but not when opened from dock — preserve the pre-dock size.
if (!_isLoadedFromDock)
{
UpdateWindowPositionInMemory();
}

// If there's a debugger attached...
if (System.Diagnostics.Debugger.IsAttached)
Expand Down
Loading