mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 09:46:54 +02:00
CmdPal: Debounce SelectedItem updates in CommandBarViewModel (#45782)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request This PR adds DispatcherQueueTimer-based debounce to SelectedItem updates when receiving UpdateCommandBarMessage, preventing rapid consecutive changes and prevents blinking when items change to fast (e.g. during search). That's right - Command Palette is too fast! <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #45776 <!-- - [ ] Closes: #yyy (add separate lines for additional resolved issues) --> - [ ] **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
This commit is contained in:
@@ -2,31 +2,54 @@
|
|||||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
using System.ComponentModel;
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using CommunityToolkit.Mvvm.Messaging;
|
using CommunityToolkit.Mvvm.Messaging;
|
||||||
|
using CommunityToolkit.WinUI;
|
||||||
using Microsoft.CmdPal.UI.ViewModels.Messages;
|
using Microsoft.CmdPal.UI.ViewModels.Messages;
|
||||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||||
using Windows.System;
|
using Windows.System;
|
||||||
|
using DispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue;
|
||||||
|
using DispatcherQueueTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;
|
||||||
|
|
||||||
namespace Microsoft.CmdPal.UI.ViewModels;
|
namespace Microsoft.CmdPal.UI.ViewModels;
|
||||||
|
|
||||||
public partial class CommandBarViewModel : ObservableObject,
|
public sealed partial class CommandBarViewModel : ObservableObject,
|
||||||
IRecipient<UpdateCommandBarMessage>
|
IRecipient<UpdateCommandBarMessage>
|
||||||
{
|
{
|
||||||
|
private readonly DispatcherQueueTimer _debounceTimer;
|
||||||
|
|
||||||
|
private volatile ICommandBarContext? _pendingSelectedItem;
|
||||||
|
|
||||||
public ICommandBarContext? SelectedItem
|
public ICommandBarContext? SelectedItem
|
||||||
{
|
{
|
||||||
get => field;
|
get;
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (field != null)
|
// TODO: verify if we can safely return early
|
||||||
|
// if (ReferenceEquals(field, value))
|
||||||
|
// {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
if (field is not null)
|
||||||
{
|
{
|
||||||
field.PropertyChanged -= SelectedItemPropertyChanged;
|
field.PropertyChanged -= SelectedItemPropertyChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
field = value;
|
field = value;
|
||||||
SetSelectedItem(value);
|
|
||||||
|
|
||||||
OnPropertyChanged(nameof(SelectedItem));
|
if (field is not null)
|
||||||
|
{
|
||||||
|
PrimaryCommand = field.PrimaryCommand;
|
||||||
|
field.PropertyChanged += SelectedItemPropertyChanged;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PrimaryCommand = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateContextItems();
|
||||||
|
OnPropertyChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,6 +57,8 @@ public partial class CommandBarViewModel : ObservableObject,
|
|||||||
[NotifyPropertyChangedFor(nameof(HasPrimaryCommand))]
|
[NotifyPropertyChangedFor(nameof(HasPrimaryCommand))]
|
||||||
public partial CommandItemViewModel? PrimaryCommand { get; set; }
|
public partial CommandItemViewModel? PrimaryCommand { get; set; }
|
||||||
|
|
||||||
|
// TODO: PrimaryCommand.ShouldBeVisible is not observed, if it changes the bar won't refresh;
|
||||||
|
// but at this moment CommandItemViewModel won't raise INPC for ShouldBeVisible anyway.
|
||||||
public bool HasPrimaryCommand => PrimaryCommand is not null && PrimaryCommand.ShouldBeVisible;
|
public bool HasPrimaryCommand => PrimaryCommand is not null && PrimaryCommand.ShouldBeVisible;
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
@@ -50,29 +75,31 @@ public partial class CommandBarViewModel : ObservableObject,
|
|||||||
|
|
||||||
public CommandBarViewModel()
|
public CommandBarViewModel()
|
||||||
{
|
{
|
||||||
|
var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
|
||||||
|
if (dispatcherQueue is null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("DispatcherQueue is not available for the current thread.");
|
||||||
|
}
|
||||||
|
|
||||||
|
_debounceTimer = dispatcherQueue.CreateTimer();
|
||||||
WeakReferenceMessenger.Default.Register<UpdateCommandBarMessage>(this);
|
WeakReferenceMessenger.Default.Register<UpdateCommandBarMessage>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Receive(UpdateCommandBarMessage message) => SelectedItem = message.ViewModel;
|
public void Receive(UpdateCommandBarMessage message)
|
||||||
|
|
||||||
private void SetSelectedItem(ICommandBarContext? value)
|
|
||||||
{
|
{
|
||||||
if (value is not null)
|
_pendingSelectedItem = message.ViewModel;
|
||||||
{
|
|
||||||
PrimaryCommand = value.PrimaryCommand;
|
|
||||||
value.PropertyChanged += SelectedItemPropertyChanged;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (SelectedItem is not null)
|
|
||||||
{
|
|
||||||
SelectedItem.PropertyChanged -= SelectedItemPropertyChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
PrimaryCommand = null;
|
// immediate: false is intentional — the timer tick always fires on the
|
||||||
}
|
// dispatcher queue thread, which guarantees ApplyPendingSelectedItem
|
||||||
|
// runs on the UI thread even if Receive is called from a background
|
||||||
|
// thread. Using immediate: true would invoke the delegate synchronously
|
||||||
|
// on the calling thread, bypassing the dispatcher.
|
||||||
|
_debounceTimer.Debounce(ApplyPendingSelectedItem, TimeSpan.FromMilliseconds(50));
|
||||||
|
}
|
||||||
|
|
||||||
UpdateContextItems();
|
private void ApplyPendingSelectedItem()
|
||||||
|
{
|
||||||
|
SelectedItem = _pendingSelectedItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SelectedItemPropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
|
private void SelectedItemPropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
|
||||||
|
|||||||
Reference in New Issue
Block a user