diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Messages/SettingsWindowClosedMessage.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Messages/SettingsWindowClosedMessage.cs new file mode 100644 index 0000000000..6627ab6192 --- /dev/null +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Messages/SettingsWindowClosedMessage.cs @@ -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 +{ +} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Pages/ShellPage.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Pages/ShellPage.xaml.cs index 193053b3d5..e40a3ae97f 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Pages/ShellPage.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Pages/ShellPage.xaml.cs @@ -35,6 +35,7 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page, IRecipient, IRecipient, IRecipient, + IRecipient, 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()!; @@ -65,6 +67,7 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page, WeakReferenceMessenger.Default.Register(this); WeakReferenceMessenger.Default.Register(this); WeakReferenceMessenger.Default.Register(this); + WeakReferenceMessenger.Default.Register(this); WeakReferenceMessenger.Default.Register(this); WeakReferenceMessenger.Default.Register(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(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()!; diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/SettingsWindow.xaml b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/SettingsWindow.xaml index 69d967a200..48fb4243e9 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/SettingsWindow.xaml +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/SettingsWindow.xaml @@ -9,6 +9,7 @@ xmlns:ui="using:CommunityToolkit.WinUI" Title="SettingsWindow" Activated="Window_Activated" + Closed="Window_Closed" mc:Ignorable="d"> diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/SettingsWindow.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/SettingsWindow.xaml.cs index 0386f4154f..98c6be09ea 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/SettingsWindow.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/SettingsWindow.xaml.cs @@ -105,6 +105,11 @@ public sealed partial class SettingsWindow : Window, WeakReferenceMessenger.Default.Send(args); } + private void Window_Closed(object sender, WindowEventArgs args) + { + WeakReferenceMessenger.Default.Send(); + } + public void Receive(QuitMessage message) { // This might come in on a background thread