CmdPal: Update Extension SDK classes to raise property change notifications only on change (#44547)

## Summary of the Pull Request

This PR introduces a new method  
`bool SetProperty<T>(ref T field, T value, [CallerMemberName] string?
propertyName = null)`
on `BaseObservable`.

The method updates the backing field only when the new value differs
from the current one and raises property change notifications only in
that case.

#### Summary

- Adds `SetProperty<T>` to `BaseObservable` to guard property change
notifications behind an actual value change.
- SDK API is not affected.
- Prevents unnecessary notifications from leaving the extension
boundary.
- Reduces redundant updates and saves a few round-trips between COM
objects and view models.
- Improves overall efficiency without changing observable behavior for
consumers.

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

- [ ] Closes: #xxx
<!-- - [ ] 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:
Jiří Polášek
2026-01-08 17:42:12 +01:00
committed by GitHub
parent a2cd47f36c
commit be90b587da
24 changed files with 119 additions and 532 deletions

View File

@@ -2,6 +2,7 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Runtime.CompilerServices;
using Windows.Foundation;
namespace Microsoft.CommandPalette.Extensions.Toolkit;
@@ -14,7 +15,7 @@ public partial class BaseObservable : INotifyPropChanged
{
public event TypedEventHandler<object, IPropChangedEventArgs>? PropChanged;
protected void OnPropertyChanged(string propertyName)
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
try
{
@@ -22,10 +23,37 @@ public partial class BaseObservable : INotifyPropChanged
// this can crash as we try to invoke the handlers from that process.
// However, just catching it seems to still raise the event on the
// new host?
PropChanged?.Invoke(this, new PropChangedEventArgs(propertyName));
PropChanged?.Invoke(this, new PropChangedEventArgs(propertyName!));
}
catch
{
}
}
/// <summary>
/// Sets the backing field to the specified value and raises a property changed
/// notification if the value is different from the current one.
/// </summary>
/// <typeparam name="T">The type of the property.</typeparam>
/// <param name="field">A reference to the backing field for the property.</param>
/// <param name="value">The new value to assign to the property.</param>
/// <param name="propertyName">
/// The name of the property. This is optional and is usually supplied
/// automatically by the <see cref="CallerMemberNameAttribute"/>.
/// </param>
/// <returns>
/// <see langword="true"/> if the field was updated and a property changed
/// notification was raised; otherwise, <see langword="false"/>.
/// </returns>
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value))
{
return false;
}
field = value;
OnPropertyChanged(propertyName!);
return true;
}
}

View File

@@ -6,31 +6,11 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class Command : BaseObservable, ICommand
{
public virtual string Name
{
get;
set
{
field = value;
OnPropertyChanged(nameof(Name));
}
}
= string.Empty;
public virtual string Name { get; set => SetProperty(ref field, value); } = string.Empty;
public virtual string Id { get; set; } = string.Empty;
public virtual IconInfo Icon
{
get;
set
{
field = value;
OnPropertyChanged(nameof(Icon));
}
}
= new();
public virtual IconInfo Icon { get; set => SetProperty(ref field, value); } = new();
IIconInfo ICommand.Icon => Icon;
}

View File

@@ -6,9 +6,9 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class CommandContextItem : CommandItem, ICommandContextItem
{
public virtual bool IsCritical { get; set; }
public virtual bool IsCritical { get; set => SetProperty(ref field, value); }
public virtual KeyChord RequestedShortcut { get; set; }
public virtual KeyChord RequestedShortcut { get; set => SetProperty(ref field, value); }
public CommandContextItem(ICommand command)
: base(command)

View File

@@ -19,44 +19,36 @@ public partial class CommandItem : BaseObservable, ICommandItem, IExtendedAttrib
private DataPackage? _dataPackage;
private DataPackageView? _dataPackageView;
public virtual IIconInfo? Icon
{
get => field;
set
{
field = value;
OnPropertyChanged(nameof(Icon));
}
}
public virtual IIconInfo? Icon { get; set => SetProperty(ref field, value); }
public virtual string Title
{
get => !string.IsNullOrEmpty(_title) ? _title : _command?.Name ?? string.Empty;
set
{
var oldTitle = Title;
_title = value;
OnPropertyChanged(nameof(Title));
if (Title != oldTitle)
{
OnPropertyChanged();
}
}
}
public virtual string Subtitle
{
get;
set
{
field = value;
OnPropertyChanged(nameof(Subtitle));
}
}
= string.Empty;
public virtual string Subtitle { get; set => SetProperty(ref field, value); } = string.Empty;
public virtual ICommand? Command
{
get => _command;
set
{
if (EqualityComparer<ICommand?>.Default.Equals(value, _command))
{
return;
}
var oldTitle = Title;
if (_commandListener is not null)
{
_commandListener.Detach();
@@ -71,8 +63,8 @@ public partial class CommandItem : BaseObservable, ICommandItem, IExtendedAttrib
value.PropChanged += _commandListener.OnEvent;
}
OnPropertyChanged(nameof(Command));
if (string.IsNullOrEmpty(_title))
OnPropertyChanged();
if (string.IsNullOrEmpty(_title) && oldTitle != Title)
{
OnPropertyChanged(nameof(Title));
}
@@ -88,17 +80,7 @@ public partial class CommandItem : BaseObservable, ICommandItem, IExtendedAttrib
}
}
public virtual IContextItem[] MoreCommands
{
get;
set
{
field = value;
OnPropertyChanged(nameof(MoreCommands));
}
}
= [];
public virtual IContextItem[] MoreCommands { get; set => SetProperty(ref field, value); } = [];
public DataPackage? DataPackage
{

View File

@@ -6,9 +6,9 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class CommandResult : ICommandResult
{
public ICommandResultArgs? Args { get; private set; }
public ICommandResultArgs? Args { get; private init; }
public CommandResultKind Kind { get; private set; } = CommandResultKind.Dismiss;
public CommandResultKind Kind { get; private init; } = CommandResultKind.Dismiss;
public static CommandResult Dismiss()
{

View File

@@ -10,17 +10,9 @@ public abstract partial class ContentPage : Page, IContentPage
{
public event TypedEventHandler<object, IItemsChangedEventArgs>? ItemsChanged;
public virtual IDetails? Details
{
get => field;
set
{
field = value;
OnPropertyChanged(nameof(Details));
}
}
public virtual IDetails? Details { get; set => SetProperty(ref field, value); }
public virtual IContextItem[] Commands { get; set; } = [];
public virtual IContextItem[] Commands { get; set => SetProperty(ref field, value); } = [];
public abstract IContent[] GetContent();

View File

@@ -7,65 +7,15 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class Details : BaseObservable, IDetails, IExtendedAttributesProvider
{
public virtual IIconInfo HeroImage
{
get => field;
set
{
field = value;
OnPropertyChanged(nameof(HeroImage));
}
}
public virtual IIconInfo HeroImage { get; set => SetProperty(ref field, value); } = new IconInfo();
= new IconInfo();
public virtual string Title { get; set => SetProperty(ref field, value); } = string.Empty;
public virtual string Title
{
get;
set
{
field = value;
OnPropertyChanged(nameof(Title));
}
}
public virtual string Body { get; set => SetProperty(ref field, value); } = string.Empty;
= string.Empty;
public virtual IDetailsElement[] Metadata { get; set => SetProperty(ref field, value); } = [];
public virtual string Body
{
get;
set
{
field = value;
OnPropertyChanged(nameof(Body));
}
}
= string.Empty;
public virtual IDetailsElement[] Metadata
{
get;
set
{
field = value;
OnPropertyChanged(nameof(Metadata));
}
}
= [];
public virtual ContentSize Size
{
get;
set
{
field = value;
OnPropertyChanged(nameof(Size));
}
}
= ContentSize.Small;
public virtual ContentSize Size { get; set => SetProperty(ref field, value); } = ContentSize.Small;
public IDictionary<string, object>? GetProperties() => new ValueSet()
{

View File

@@ -6,39 +6,9 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class Filter : BaseObservable, IFilter
{
public virtual IIconInfo Icon
{
get => field;
set
{
field = value;
OnPropertyChanged(nameof(Icon));
}
}
public virtual IIconInfo Icon { get; set => SetProperty(ref field, value); } = new IconInfo();
= new IconInfo();
public virtual string Id { get; set => SetProperty(ref field, value); } = string.Empty;
public virtual string Id
{
get;
set
{
field = value;
OnPropertyChanged(nameof(Id));
}
}
= string.Empty;
public virtual string Name
{
get;
set
{
field = value;
OnPropertyChanged(nameof(Name));
}
}
= string.Empty;
public virtual string Name { get; set => SetProperty(ref field, value); } = string.Empty;
}

View File

@@ -6,17 +6,7 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public abstract partial class Filters : BaseObservable, IFilters
{
public string CurrentFilterId
{
get => field;
set
{
field = value;
OnPropertyChanged(nameof(CurrentFilterId));
}
}
= string.Empty;
public string CurrentFilterId { get; set => SetProperty(ref field, value); } = string.Empty;
// This method should be overridden in derived classes to provide the actual filters.
public abstract IFilterItem[] GetFilters();

View File

@@ -6,41 +6,11 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class FormContent : BaseObservable, IFormContent
{
public virtual string DataJson
{
get;
set
{
field = value;
OnPropertyChanged(nameof(DataJson));
}
}
public virtual string DataJson { get; set => SetProperty(ref field, value); } = string.Empty;
= string.Empty;
public virtual string StateJson { get; set => SetProperty(ref field, value); } = string.Empty;
public virtual string StateJson
{
get;
set
{
field = value;
OnPropertyChanged(nameof(StateJson));
}
}
= string.Empty;
public virtual string TemplateJson
{
get;
set
{
field = value;
OnPropertyChanged(nameof(TemplateJson));
}
}
= string.Empty;
public virtual string TemplateJson { get; set => SetProperty(ref field, value); } = string.Empty;
public virtual ICommandResult SubmitForm(string inputs, string data) => SubmitForm(inputs);

View File

@@ -6,27 +6,7 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class GalleryGridLayout : BaseObservable, IGalleryGridLayout
{
public virtual bool ShowTitle
{
get => field;
set
{
field = value;
OnPropertyChanged(nameof(ShowTitle));
}
}
public virtual bool ShowTitle { get; set => SetProperty(ref field, value); } = true;
= true;
public virtual bool ShowSubtitle
{
get => field;
set
{
field = value;
OnPropertyChanged(nameof(ShowSubtitle));
}
}
= true;
public virtual bool ShowSubtitle { get; set => SetProperty(ref field, value); } = true;
}

View File

@@ -2,7 +2,6 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics.CodeAnalysis;
using Windows.Storage.Streams;
namespace Microsoft.CommandPalette.Extensions.Toolkit;

View File

@@ -6,51 +6,13 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class ListItem : CommandItem, IListItem
{
private ITag[] _tags = [];
private IDetails? _details;
public virtual ITag[] Tags { get; set => SetProperty(ref field, value); } = [];
private string _section = string.Empty;
private string _textToSuggest = string.Empty;
public virtual IDetails? Details { get; set => SetProperty(ref field, value); }
public virtual ITag[] Tags
{
get => _tags;
set
{
_tags = value;
OnPropertyChanged(nameof(Tags));
}
}
public virtual string Section { get; set => SetProperty(ref field, value); } = string.Empty;
public virtual IDetails? Details
{
get => _details;
set
{
_details = value;
OnPropertyChanged(nameof(Details));
}
}
public virtual string Section
{
get => _section;
set
{
_section = value;
OnPropertyChanged(nameof(Section));
}
}
public virtual string TextToSuggest
{
get => _textToSuggest;
set
{
_textToSuggest = value;
OnPropertyChanged(nameof(TextToSuggest));
}
}
public virtual string TextToSuggest { get; set => SetProperty(ref field, value); } = string.Empty;
public ListItem(ICommand command)
: base(command)

View File

@@ -8,85 +8,23 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class ListPage : Page, IListPage
{
private string _placeholderText = string.Empty;
private string _searchText = string.Empty;
private bool _showDetails;
private bool _hasMore;
private IFilters? _filters;
private IGridProperties? _gridProperties;
private ICommandItem? _emptyContent;
public event TypedEventHandler<object, IItemsChangedEventArgs>? ItemsChanged;
public virtual string PlaceholderText
{
get => _placeholderText;
set
{
_placeholderText = value;
OnPropertyChanged(nameof(PlaceholderText));
}
}
private string _searchText = string.Empty;
public virtual string SearchText
{
get => _searchText;
set
{
_searchText = value;
OnPropertyChanged(nameof(SearchText));
}
}
public virtual string PlaceholderText { get; set => SetProperty(ref field, value); } = string.Empty;
public virtual bool ShowDetails
{
get => _showDetails;
set
{
_showDetails = value;
OnPropertyChanged(nameof(ShowDetails));
}
}
public virtual string SearchText { get => _searchText; set => SetProperty(ref _searchText, value); }
public virtual bool HasMoreItems
{
get => _hasMore;
set
{
_hasMore = value;
OnPropertyChanged(nameof(HasMoreItems));
}
}
public virtual bool ShowDetails { get; set => SetProperty(ref field, value); }
public virtual IFilters? Filters
{
get => _filters;
set
{
_filters = value;
OnPropertyChanged(nameof(Filters));
}
}
public virtual bool HasMoreItems { get; set => SetProperty(ref field, value); }
public virtual IGridProperties? GridProperties
{
get => _gridProperties;
set
{
_gridProperties = value;
OnPropertyChanged(nameof(GridProperties));
}
}
public virtual IFilters? Filters { get; set => SetProperty(ref field, value); }
public virtual ICommandItem? EmptyContent
{
get => _emptyContent;
set
{
_emptyContent = value;
OnPropertyChanged(nameof(EmptyContent));
}
}
public virtual IGridProperties? GridProperties { get; set => SetProperty(ref field, value); }
public virtual ICommandItem? EmptyContent { get; set => SetProperty(ref field, value); }
public virtual IListItem[] GetItems() => [];

View File

@@ -6,17 +6,7 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class MarkdownContent : BaseObservable, IMarkdownContent
{
public virtual string Body
{
get;
set
{
field = value;
OnPropertyChanged(nameof(Body));
}
}
= string.Empty;
public virtual string Body { get; set => SetProperty(ref field, value); } = string.Empty;
public MarkdownContent()
{

View File

@@ -6,15 +6,5 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class MediumGridLayout : BaseObservable, IMediumGridLayout
{
public virtual bool ShowTitle
{
get => field;
set
{
field = value;
OnPropertyChanged(nameof(ShowTitle));
}
}
= true;
public virtual bool ShowTitle { get; set => SetProperty(ref field, value); } = true;
}

View File

@@ -6,37 +6,9 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class Page : Command, IPage
{
private bool _loading;
private string _title = string.Empty;
private OptionalColor _accentColor;
public virtual bool IsLoading { get; set => SetProperty(ref field, value); }
public virtual bool IsLoading
{
get => _loading;
set
{
_loading = value;
OnPropertyChanged(nameof(IsLoading));
}
}
public virtual string Title { get; set => SetProperty(ref field, value); } = string.Empty;
public virtual string Title
{
get => _title;
set
{
_title = value;
OnPropertyChanged(nameof(Title));
}
}
public virtual OptionalColor AccentColor
{
get => _accentColor;
set
{
_accentColor = value;
OnPropertyChanged(nameof(AccentColor));
}
}
public virtual OptionalColor AccentColor { get; set => SetProperty(ref field, value); }
}

View File

@@ -6,27 +6,7 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class ProgressState : BaseObservable, IProgressState
{
private bool _isIndeterminate;
public virtual bool IsIndeterminate { get; set => SetProperty(ref field, value); }
private uint _progressPercent;
public virtual bool IsIndeterminate
{
get => _isIndeterminate;
set
{
_isIndeterminate = value;
OnPropertyChanged(nameof(IsIndeterminate));
}
}
public virtual uint ProgressPercent
{
get => _progressPercent;
set
{
_progressPercent = value;
OnPropertyChanged(nameof(ProgressPercent));
}
}
public virtual uint ProgressPercent { get; set => SetProperty(ref field, value); }
}

View File

@@ -2,8 +2,6 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Windows.Foundation;
namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class PropChangedEventArgs : IPropChangedEventArgs

View File

@@ -12,11 +12,6 @@ public sealed partial class Section : IEnumerable<IListItem>
public string SectionTitle { get; set; } = string.Empty;
private Separator CreateSectionListItem()
{
return new Separator(SectionTitle);
}
public Section(string sectionName, IListItem[] items)
{
SectionTitle = sectionName;
@@ -33,6 +28,11 @@ public sealed partial class Section : IEnumerable<IListItem>
{
}
private Separator CreateSectionListItem()
{
return new Separator(SectionTitle);
}
public IEnumerator<IListItem> GetEnumerator() => Items.ToList().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

View File

@@ -4,15 +4,8 @@
namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class Separator : IListItem, ISeparatorContextItem, ISeparatorFilterItem
public partial class Separator : BaseObservable, IListItem, ISeparatorContextItem, ISeparatorFilterItem
{
public Separator(string? title = "")
: base()
{
Section = title ?? string.Empty;
Command = null;
}
public IDetails? Details => null;
public string? Section { get; private set; }
@@ -21,7 +14,7 @@ public partial class Separator : IListItem, ISeparatorContextItem, ISeparatorFil
public string? TextToSuggest => null;
public ICommand? Command { get; private set; }
public ICommand? Command => null;
public IIconInfo? Icon => null;
@@ -32,12 +25,19 @@ public partial class Separator : IListItem, ISeparatorContextItem, ISeparatorFil
public string? Title
{
get => Section;
set => Section = value;
set
{
if (Section != value)
{
Section = value;
OnPropertyChanged();
OnPropertyChanged(Section);
}
}
}
public event Windows.Foundation.TypedEventHandler<object, IPropChangedEventArgs>? PropChanged
public Separator(string? title = "")
{
add { }
remove { }
Section = title ?? string.Empty;
}
}

View File

@@ -6,37 +6,9 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class StatusMessage : BaseObservable, IStatusMessage
{
public virtual string Message
{
get;
set
{
field = value;
OnPropertyChanged(nameof(Message));
}
}
public virtual string Message { get; set => SetProperty(ref field, value); } = string.Empty;
= string.Empty;
public virtual MessageState State { get; set => SetProperty(ref field, value); } = MessageState.Info;
public virtual MessageState State
{
get;
set
{
field = value;
OnPropertyChanged(nameof(State));
}
}
= MessageState.Info;
public virtual IProgressState? Progress
{
get;
set
{
field = value;
OnPropertyChanged(nameof(Progress));
}
}
public virtual IProgressState? Progress { get; set => SetProperty(ref field, value); }
}

View File

@@ -6,63 +6,15 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class Tag : BaseObservable, ITag
{
private OptionalColor _foreground;
private OptionalColor _background;
private string _text = string.Empty;
public virtual OptionalColor Foreground { get; set => SetProperty(ref field, value); }
public virtual OptionalColor Foreground
{
get => _foreground;
set
{
_foreground = value;
OnPropertyChanged(nameof(Foreground));
}
}
public virtual OptionalColor Background { get; set => SetProperty(ref field, value); }
public virtual OptionalColor Background
{
get => _background;
set
{
_background = value;
OnPropertyChanged(nameof(Background));
}
}
public virtual IIconInfo Icon { get; set => SetProperty(ref field, value); } = new IconInfo();
public virtual IIconInfo Icon
{
get;
set
{
field = value;
OnPropertyChanged(nameof(Icon));
}
}
public virtual string Text { get; set => SetProperty(ref field, value); } = string.Empty;
= new IconInfo();
public virtual string Text
{
get => _text;
set
{
_text = value;
OnPropertyChanged(nameof(Text));
}
}
public virtual string ToolTip
{
get;
set
{
field = value;
OnPropertyChanged(nameof(ToolTip));
}
}
= string.Empty;
public virtual string ToolTip { get; set => SetProperty(ref field, value); } = string.Empty;
public Tag()
{
@@ -70,6 +22,6 @@ public partial class Tag : BaseObservable, ITag
public Tag(string text)
{
_text = text;
Text = text;
}
}

View File

@@ -8,19 +8,11 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit;
public partial class TreeContent : BaseObservable, ITreeContent
{
public event TypedEventHandler<object, IItemsChangedEventArgs>? ItemsChanged;
public IContent[] Children { get; set; } = [];
public virtual IContent? RootContent
{
get;
set
{
field = value;
OnPropertyChanged(nameof(RootContent));
}
}
public event TypedEventHandler<object, IItemsChangedEventArgs>? ItemsChanged;
public virtual IContent? RootContent { get; set => SetProperty(ref field, value); }
public virtual IContent[] GetChildren() => Children;