Skip to content

Commit a4791cc

Browse files
authored
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 <!-- Please review the items on the PR checklist before submitting--> ## 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 <!-- 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
1 parent 1a18944 commit a4791cc

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

‎src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/CommandItemViewModel.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ protected virtual void FetchProperty(string propertyName)
306306
Command.PropertyChanged -= Command_PropertyChanged;
307307
Command = new(model.Command, PageContext);
308308
Command.InitializeProperties();
309+
Command.PropertyChanged += Command_PropertyChanged;
309310

310311
// Extensions based on Command Palette SDK < 0.3 CommandItem class won't notify when Title changes because Command
311312
// or Command.Name change. This is a workaround to ensure that the Title is always up-to-date for extensions with old SDK.

‎src/modules/cmdpal/ext/SamplePagesExtension/Pages/SampleListPage.cs‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ public override IListItem[] GetItems()
190190
new CommandContextItem(new EverChangingCommand("Faces", "😁", "🥺", "😍")),
191191
new CommandContextItem(new EverChangingCommand("Hearts", "♥️", "💚", "💜", "🧡", "💛", "💙")),
192192
],
193-
}
193+
},
194+
new ListItemChangingCommandInTime()
195+
{
196+
Title = "I'm a list item that changes entire command in time",
197+
},
194198
];
195199
}
196200

@@ -248,6 +252,11 @@ internal sealed partial class EverChangingCommand : InvokableCommand, IDisposabl
248252
private int _currentIndex;
249253

250254
public EverChangingCommand(string name, params string[] icons)
255+
: this(name, TimeSpan.FromSeconds(5), icons)
256+
{
257+
}
258+
259+
public EverChangingCommand(string name, TimeSpan interval, params string[] icons)
251260
{
252261
_icons = icons ?? throw new ArgumentNullException(nameof(icons));
253262
if (_icons.Length == 0)
@@ -260,7 +269,7 @@ public EverChangingCommand(string name, params string[] icons)
260269
Icon = new IconInfo(_icons[_currentIndex]);
261270

262271
// Start timer to change icon and name every 5 seconds
263-
_timer = new Timer(OnTimerElapsed, null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));
272+
_timer = new Timer(OnTimerElapsed, null, interval, interval);
264273
}
265274

266275
private void OnTimerElapsed(object state)
@@ -282,4 +291,29 @@ public void Dispose()
282291
_timer?.Dispose();
283292
}
284293
}
294+
295+
internal sealed partial class ListItemChangingCommandInTime : ListItem
296+
{
297+
private readonly EverChangingCommand[] _commands =
298+
[
299+
new("Water", TimeSpan.FromSeconds(2), "🐬", "🐳", "🐟", "🦈"),
300+
new("Faces", TimeSpan.FromSeconds(2), "😁", "🥺", "😍"),
301+
new("Hearts", TimeSpan.FromSeconds(2), "♥️", "💚", "💜", "🧡", "💛", "💙"),
302+
];
303+
304+
private int _state;
305+
306+
public ListItemChangingCommandInTime()
307+
{
308+
Subtitle = "I change my command every 10 seconds, and the command changes it's icon every 2 seconds";
309+
var timer = new Timer(OnTimerElapsed, null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10));
310+
this.Command = _commands[0];
311+
}
312+
313+
private void OnTimerElapsed(object state)
314+
{
315+
_state = (_state + 1) % _commands.Length;
316+
this.Command = _commands[_state];
317+
}
318+
}
285319
}

0 commit comments

Comments
 (0)