[CmdPal] Single settings window (#38069)

## Summary of the Pull Request

Make settings window single.

<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist

- [x] **Closes:** https://github.com/zadjii-msft/PowerToys/issues/581

## Validation Steps Performed

Manual test.
- Open settings windows multiple times
- Activate minimized settings window
This commit is contained in:
Davide Giacometti
2025-03-21 20:58:47 +01:00
committed by GitHub
parent 4d7691a56f
commit 7368458a72
4 changed files with 26 additions and 2 deletions

View File

@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.CmdPal.UI.ViewModels.Messages;
public record SettingsWindowClosedMessage
{
}

View File

@@ -35,6 +35,7 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page,
IRecipient<ClearSearchMessage>,
IRecipient<HandleCommandResultMessage>,
IRecipient<LaunchUriMessage>,
IRecipient<SettingsWindowClosedMessage>,
INotifyPropertyChanged
{
private readonly DispatcherQueue _queue = DispatcherQueue.GetForCurrentThread();
@@ -50,6 +51,7 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page,
private readonly Lock _invokeLock = new();
private Task? _handleInvokeTask;
private SettingsWindow? _settingsWindow;
public ShellViewModel ViewModel { get; private set; } = App.Current.Services.GetService<ShellViewModel>()!;
@@ -65,6 +67,7 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page,
WeakReferenceMessenger.Default.Register<HandleCommandResultMessage>(this);
WeakReferenceMessenger.Default.Register<OpenSettingsMessage>(this);
WeakReferenceMessenger.Default.Register<HotkeySummonMessage>(this);
WeakReferenceMessenger.Default.Register<SettingsWindowClosedMessage>(this);
WeakReferenceMessenger.Default.Register<ShowDetailsMessage>(this);
WeakReferenceMessenger.Default.Register<HideDetailsMessage>(this);
@@ -396,8 +399,12 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page,
// Also hide our details pane about here, if we had one
HideDetails();
var settingsWindow = new SettingsWindow();
settingsWindow.Activate();
if (_settingsWindow == null)
{
_settingsWindow = new SettingsWindow();
}
_settingsWindow.Activate();
WeakReferenceMessenger.Default.Send<UpdateCommandBarMessage>(new(null));
});
@@ -458,6 +465,8 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page,
_ = DispatcherQueue.TryEnqueue(() => SummonOnUiThread(message));
}
public void Receive(SettingsWindowClosedMessage message) => _settingsWindow = null;
private void SummonOnUiThread(HotkeySummonMessage message)
{
var settings = App.Current.Services.GetService<SettingsModel>()!;

View File

@@ -9,6 +9,7 @@
xmlns:ui="using:CommunityToolkit.WinUI"
Title="SettingsWindow"
Activated="Window_Activated"
Closed="Window_Closed"
mc:Ignorable="d">
<Window.SystemBackdrop>
<MicaBackdrop />

View File

@@ -105,6 +105,11 @@ public sealed partial class SettingsWindow : Window,
WeakReferenceMessenger.Default.Send<WindowActivatedEventArgs>(args);
}
private void Window_Closed(object sender, WindowEventArgs args)
{
WeakReferenceMessenger.Default.Send<SettingsWindowClosedMessage>();
}
public void Receive(QuitMessage message)
{
// This might come in on a background thread