mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
## Summary of the Pull Request Changes the timing of when `ContentPage` registers to messages from the Toolkit Messenger so it happens only when navigated to, mirroring the unregister on navigation from. Also unregisters from all messages when unloaded. Proactively unregisters the Settings window from all messages on close instead of relying on the GC’s nondeterministic cleanup. Since the Settings window is newly created each time, old instances can still react to messages even after their time is over, merely waiting for GC to collect them. Co-authored-by: zadjii-msft <migrie@microsoft.com> ## PR Checklist - [x] Closes: #40846 - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed
89 lines
3.0 KiB
C#
89 lines
3.0 KiB
C#
// 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.
|
|
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
using Microsoft.CmdPal.Core.ViewModels;
|
|
using Microsoft.CmdPal.Core.ViewModels.Messages;
|
|
using Microsoft.UI.Dispatching;
|
|
using Microsoft.UI.Xaml;
|
|
using Microsoft.UI.Xaml.Controls;
|
|
using Microsoft.UI.Xaml.Navigation;
|
|
|
|
namespace Microsoft.CmdPal.UI;
|
|
|
|
/// <summary>
|
|
/// An empty page that can be used on its own or navigated to within a Frame.
|
|
/// </summary>
|
|
public sealed partial class ContentPage : Page,
|
|
IRecipient<ActivateSelectedListItemMessage>,
|
|
IRecipient<ActivateSecondaryCommandMessage>
|
|
{
|
|
private readonly DispatcherQueue _queue = DispatcherQueue.GetForCurrentThread();
|
|
|
|
public ContentPageViewModel? ViewModel
|
|
{
|
|
get => (ContentPageViewModel?)GetValue(ViewModelProperty);
|
|
set => SetValue(ViewModelProperty, value);
|
|
}
|
|
|
|
// Using a DependencyProperty as the backing store for ViewModel. This enables animation, styling, binding, etc...
|
|
public static readonly DependencyProperty ViewModelProperty =
|
|
DependencyProperty.Register(nameof(ViewModel), typeof(ContentPageViewModel), typeof(ContentPage), new PropertyMetadata(null));
|
|
|
|
public ContentPage()
|
|
{
|
|
this.InitializeComponent();
|
|
this.Unloaded += OnUnloaded;
|
|
}
|
|
|
|
private void OnUnloaded(object sender, RoutedEventArgs e)
|
|
{
|
|
// Unhook from everything to ensure nothing can reach us
|
|
// between this point and our complete and utter destruction.
|
|
WeakReferenceMessenger.Default.UnregisterAll(this);
|
|
}
|
|
|
|
protected override void OnNavigatedTo(NavigationEventArgs e)
|
|
{
|
|
if (e.Parameter is ContentPageViewModel vm)
|
|
{
|
|
ViewModel = vm;
|
|
}
|
|
|
|
if (!WeakReferenceMessenger.Default.IsRegistered<ActivateSelectedListItemMessage>(this))
|
|
{
|
|
WeakReferenceMessenger.Default.Register<ActivateSelectedListItemMessage>(this);
|
|
}
|
|
|
|
if (!WeakReferenceMessenger.Default.IsRegistered<ActivateSecondaryCommandMessage>(this))
|
|
{
|
|
WeakReferenceMessenger.Default.Register<ActivateSecondaryCommandMessage>(this);
|
|
}
|
|
|
|
base.OnNavigatedTo(e);
|
|
}
|
|
|
|
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
|
|
{
|
|
base.OnNavigatingFrom(e);
|
|
WeakReferenceMessenger.Default.Unregister<ActivateSelectedListItemMessage>(this);
|
|
WeakReferenceMessenger.Default.Unregister<ActivateSecondaryCommandMessage>(this);
|
|
|
|
// Clean-up event listeners
|
|
ViewModel = null;
|
|
}
|
|
|
|
// this comes in on Enter keypresses in the SearchBox
|
|
public void Receive(ActivateSelectedListItemMessage message)
|
|
{
|
|
ViewModel?.InvokePrimaryCommandCommand?.Execute(ViewModel);
|
|
}
|
|
|
|
// this comes in on Ctrl+Enter keypresses in the SearchBox
|
|
public void Receive(ActivateSecondaryCommandMessage message)
|
|
{
|
|
ViewModel?.InvokeSecondaryCommandCommand?.Execute(ViewModel);
|
|
}
|
|
}
|