From a4791cc4935f67fe8bb7c3325d00ce0469b51f04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pol=C3=A1=C5=A1ek?= Date: Tue, 28 Oct 2025 17:22:09 +0100 Subject: [PATCH] CmdPal: Ensure CommandItemViewModel reacts to changes of replaced Command (#42982) ## Summary of the Pull Request This PR resolves the issue with CommandItemViewModel's subscription to changes in the associated Command when it gets replaced by another Command. The current implementation removes the handler from the old command but fails to attach a new one. ## Change log one-liner ## PR Checklist - [x] Closes: #42981 - [ ] **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 --- .../CommandItemViewModel.cs | 1 + .../Pages/SampleListPage.cs | 38 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/CommandItemViewModel.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/CommandItemViewModel.cs index b1a977f0de..e0d2c38262 100644 --- a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/CommandItemViewModel.cs +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/CommandItemViewModel.cs @@ -306,6 +306,7 @@ public partial class CommandItemViewModel : ExtensionObjectViewModel, ICommandBa Command.PropertyChanged -= Command_PropertyChanged; Command = new(model.Command, PageContext); Command.InitializeProperties(); + Command.PropertyChanged += Command_PropertyChanged; // Extensions based on Command Palette SDK < 0.3 CommandItem class won't notify when Title changes because Command // or Command.Name change. This is a workaround to ensure that the Title is always up-to-date for extensions with old SDK. diff --git a/src/modules/cmdpal/ext/SamplePagesExtension/Pages/SampleListPage.cs b/src/modules/cmdpal/ext/SamplePagesExtension/Pages/SampleListPage.cs index 95f5eb84c0..2464724d50 100644 --- a/src/modules/cmdpal/ext/SamplePagesExtension/Pages/SampleListPage.cs +++ b/src/modules/cmdpal/ext/SamplePagesExtension/Pages/SampleListPage.cs @@ -190,7 +190,11 @@ internal sealed partial class SampleListPage : ListPage new CommandContextItem(new EverChangingCommand("Faces", "๐Ÿ˜", "๐Ÿฅบ", "๐Ÿ˜")), new CommandContextItem(new EverChangingCommand("Hearts", "โ™ฅ๏ธ", "๐Ÿ’š", "๐Ÿ’œ", "๐Ÿงก", "๐Ÿ’›", "๐Ÿ’™")), ], - } + }, + new ListItemChangingCommandInTime() + { + Title = "I'm a list item that changes entire command in time", + }, ]; } @@ -248,6 +252,11 @@ internal sealed partial class SampleListPage : ListPage private int _currentIndex; public EverChangingCommand(string name, params string[] icons) + : this(name, TimeSpan.FromSeconds(5), icons) + { + } + + public EverChangingCommand(string name, TimeSpan interval, params string[] icons) { _icons = icons ?? throw new ArgumentNullException(nameof(icons)); if (_icons.Length == 0) @@ -260,7 +269,7 @@ internal sealed partial class SampleListPage : ListPage Icon = new IconInfo(_icons[_currentIndex]); // Start timer to change icon and name every 5 seconds - _timer = new Timer(OnTimerElapsed, null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5)); + _timer = new Timer(OnTimerElapsed, null, interval, interval); } private void OnTimerElapsed(object state) @@ -282,4 +291,29 @@ internal sealed partial class SampleListPage : ListPage _timer?.Dispose(); } } + + internal sealed partial class ListItemChangingCommandInTime : ListItem + { + private readonly EverChangingCommand[] _commands = + [ + new("Water", TimeSpan.FromSeconds(2), "๐Ÿฌ", "๐Ÿณ", "๐ŸŸ", "๐Ÿฆˆ"), + new("Faces", TimeSpan.FromSeconds(2), "๐Ÿ˜", "๐Ÿฅบ", "๐Ÿ˜"), + new("Hearts", TimeSpan.FromSeconds(2), "โ™ฅ๏ธ", "๐Ÿ’š", "๐Ÿ’œ", "๐Ÿงก", "๐Ÿ’›", "๐Ÿ’™"), + ]; + + private int _state; + + public ListItemChangingCommandInTime() + { + Subtitle = "I change my command every 10 seconds, and the command changes it's icon every 2 seconds"; + var timer = new Timer(OnTimerElapsed, null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10)); + this.Command = _commands[0]; + } + + private void OnTimerElapsed(object state) + { + _state = (_state + 1) % _commands.Length; + this.Command = _commands[_state]; + } + } }