From be90b587daef2b279d7ce181729f453c404a4dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pol=C3=A1=C5=A1ek?= Date: Thu, 8 Jan 2026 17:42:12 +0100 Subject: [PATCH] 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(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` 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. ## PR Checklist - [ ] Closes: #xxx - [ ] **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 ## Detailed Description of the Pull Request / Additional comments ## Validation Steps Performed --- .../BaseObservable.cs | 32 +++++++- .../Command.cs | 24 +----- .../CommandContextItem.cs | 4 +- .../CommandItem.cs | 52 ++++-------- .../CommandResult.cs | 4 +- .../ContentPage.cs | 12 +-- .../Details.cs | 60 ++------------ .../Filter.cs | 36 +-------- .../Filters.cs | 12 +-- .../FormContent.cs | 36 +-------- .../GalleryGridLayout.cs | 24 +----- .../IconData.cs | 1 - .../ListItem.cs | 46 +---------- .../ListPage.cs | 80 +++---------------- .../MarkdownContent.cs | 12 +-- .../MediumGridLayout.cs | 12 +-- .../Page.cs | 34 +------- .../ProgressState.cs | 24 +----- .../PropChangedEventArgs.cs | 2 - .../Section.cs | 10 +-- .../Separator.cs | 26 +++--- .../StatusMessage.cs | 34 +------- .../Tag.cs | 60 ++------------ .../TreeContent.cs | 14 +--- 24 files changed, 119 insertions(+), 532 deletions(-) diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/BaseObservable.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/BaseObservable.cs index 92303397dc..915abb89dc 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/BaseObservable.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/BaseObservable.cs @@ -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? 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 { } } + + /// + /// Sets the backing field to the specified value and raises a property changed + /// notification if the value is different from the current one. + /// + /// The type of the property. + /// A reference to the backing field for the property. + /// The new value to assign to the property. + /// + /// The name of the property. This is optional and is usually supplied + /// automatically by the . + /// + /// + /// if the field was updated and a property changed + /// notification was raised; otherwise, . + /// + protected bool SetProperty(ref T field, T value, [CallerMemberName] string? propertyName = null) + { + if (EqualityComparer.Default.Equals(field, value)) + { + return false; + } + + field = value; + OnPropertyChanged(propertyName!); + return true; + } } diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Command.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Command.cs index d7e8c4c796..1cd96252c8 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Command.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Command.cs @@ -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; } diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandContextItem.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandContextItem.cs index 467953381d..995ca4821d 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandContextItem.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandContextItem.cs @@ -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) diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandItem.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandItem.cs index 46c32765a9..1d80e1442e 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandItem.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandItem.cs @@ -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.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 { diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandResult.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandResult.cs index 4be5f5092b..8be877806d 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandResult.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandResult.cs @@ -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() { diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ContentPage.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ContentPage.cs index 1d2670d91d..9efe5940b4 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ContentPage.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ContentPage.cs @@ -10,17 +10,9 @@ public abstract partial class ContentPage : Page, IContentPage { public event TypedEventHandler? 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(); diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Details.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Details.cs index 43ca618994..c3af54fd17 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Details.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Details.cs @@ -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? GetProperties() => new ValueSet() { diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Filter.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Filter.cs index 961556e572..67285c209a 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Filter.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Filter.cs @@ -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; } diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Filters.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Filters.cs index 2379382d1e..01882d62e0 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Filters.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Filters.cs @@ -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(); diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FormContent.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FormContent.cs index 7a8d400a89..dbfa5c2f03 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FormContent.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FormContent.cs @@ -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); diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/GalleryGridLayout.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/GalleryGridLayout.cs index 0c696b8b16..c339d563e7 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/GalleryGridLayout.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/GalleryGridLayout.cs @@ -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; } diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/IconData.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/IconData.cs index 35fdf7b8e7..b36f7242ae 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/IconData.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/IconData.cs @@ -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; diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ListItem.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ListItem.cs index ffd20643aa..52cef6e99a 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ListItem.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ListItem.cs @@ -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) diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ListPage.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ListPage.cs index 16992613d8..9edaf798e8 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ListPage.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ListPage.cs @@ -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? 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() => []; diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/MarkdownContent.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/MarkdownContent.cs index b2de535d9a..73e503094a 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/MarkdownContent.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/MarkdownContent.cs @@ -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() { diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/MediumGridLayout.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/MediumGridLayout.cs index 31359866f9..18bef9465a 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/MediumGridLayout.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/MediumGridLayout.cs @@ -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; } diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Page.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Page.cs index 0dfd1d0bdd..6ac33a0c9e 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Page.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Page.cs @@ -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); } } diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ProgressState.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ProgressState.cs index 4cc7e5921b..8ed4d3e046 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ProgressState.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/ProgressState.cs @@ -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); } } diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/PropChangedEventArgs.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/PropChangedEventArgs.cs index aab02590f3..ae6a031354 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/PropChangedEventArgs.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/PropChangedEventArgs.cs @@ -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 diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Section.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Section.cs index 562bf0ad46..a077683172 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Section.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Section.cs @@ -12,11 +12,6 @@ public sealed partial class Section : IEnumerable 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 { } + private Separator CreateSectionListItem() + { + return new Separator(SectionTitle); + } + public IEnumerator GetEnumerator() => Items.ToList().GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Separator.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Separator.cs index 109771f162..259f17f34f 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Separator.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Separator.cs @@ -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? PropChanged + public Separator(string? title = "") { - add { } - remove { } + Section = title ?? string.Empty; } } diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/StatusMessage.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/StatusMessage.cs index 360cf3db5f..6e6d1f5bf2 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/StatusMessage.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/StatusMessage.cs @@ -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); } } diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Tag.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Tag.cs index 3a2d797f55..8f724a6330 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Tag.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Tag.cs @@ -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; } } diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/TreeContent.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/TreeContent.cs index dfb2b9b447..8a658df068 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/TreeContent.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/TreeContent.cs @@ -8,19 +8,11 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit; public partial class TreeContent : BaseObservable, ITreeContent { + public event TypedEventHandler? ItemsChanged; + public IContent[] Children { get; set; } = []; - public virtual IContent? RootContent - { - get; - set - { - field = value; - OnPropertyChanged(nameof(RootContent)); - } - } - - public event TypedEventHandler? ItemsChanged; + public virtual IContent? RootContent { get; set => SetProperty(ref field, value); } public virtual IContent[] GetChildren() => Children;