Skip to content
Open
Changes from 2 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
64 changes: 58 additions & 6 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,11 +709,14 @@ public void Receive(ShowWindowMessage message)

internal void Receive(ShowPaletteAtMessage message)
{
_isLoadedFromDock = true;
ShowHwnd(HWND.Null, message.PosPixels, message.Anchor);
}

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

// This might come in off the UI thread. Make sure to hop back.
DispatcherQueue.TryEnqueue(() =>
{
Expand All @@ -718,6 +740,14 @@ public void Receive(DismissMessage message)
// This might come in off the UI thread. Make sure to hop back.
DispatcherQueue.TryEnqueue(() =>
{
// This is a bit of a hack, but the purpose is to resize
// the window according to the current settings
// (in case they changed while the window was opened from the dock)
// Without this the window will reposition itself correctly but we'll
// get a flash as it resizes it's content to the previous size.
var display = GetScreen(_hwnd, MonitorBehavior.ToLast);
PositionCentered(display);

EndSession("Dismiss");
HideWindow();
});
Expand All @@ -728,6 +758,18 @@ public void Receive(DismissMessage message)
public void Receive(NavigateToPageMessage message)
{
_sessionPagesVisited++;

// If we are displayed via the dock, we need to resize
// ourself to ensure any resizing done doesn't affect
// other dock windows.
if (_isLoadedFromDock)
{
DispatcherQueue.TryEnqueue(() =>
{
var display = GetScreen(_hwnd, MonitorBehavior.ToLast);
PositionCentered(display);
});
}
}

public void Receive(NavigationDepthMessage message)
Expand Down Expand Up @@ -860,13 +902,19 @@ private void Uncloak()
internal void MainWindow_Closed(object sender, WindowEventArgs args)
{
var serviceProvider = App.Current.Services;
UpdateWindowPositionInMemory();

if (!_isLoadedFromDock)
{
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 +1008,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