CmdPal: Fix updating primary command and context menu and app icons (#42155)

## Summary of the Pull Request

This PR fixes three issues in one go:
- Restores missing icons in app context menus.
- Fixes propagation of changes from a command item to the context menu
item for the primary action.
- Ensures the context menus stay in sync when underlying command items
change.

Details:
- Correctly propagates updates of name, icon, and subtitle from a
command item to its primary command
(`CommandItemViewModel._defaultCommandContextItemViewModel`).
- Correctly propagate updates of command's name to title
(`CommandItem.ctor`).
- Fixes icon loading for application items: `AppCommand` no longer loads
an app icon by default but instead relies on the caller to provide one
(since `AppListItem` also handles icon loading).
- Adds a generic fallback icon for apps when an icon cannot be loaded.
- Updates bindings on context menu items to `OneWay`, ensuring the UI
properly reflects item changes.
- Adds a sample that showcases dynamically updated commands (with cats
and dolphins!) to _Samples → List Page Sample Command_.

⚠️ Toolkit changes:
- `CommandItem` won't capture assigned Command's name as its `Title`.
This will allow it to propagate future changes to `Command.Name`.

Pictures? Moving ones!


https://github.com/user-attachments/assets/1a482394-d222-4f7c-9922-bb67d47dc566

<img width="864" height="538" alt="image"
src="https://github.com/user-attachments/assets/12f07b3e-f41c-4c40-a4e5-315f40676c52"
/>


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

- [x] Closes: #40946
- [x] Related: #40991 
- [ ] **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
2025-10-06 16:45:10 +02:00
committed by GitHub
parent 26ec8c6bd5
commit 466a94eb40
7 changed files with 187 additions and 82 deletions

View File

@@ -2,8 +2,9 @@
// 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;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using Microsoft.CommandPalette.Extensions;
using Microsoft.CommandPalette.Extensions.Toolkit;
using Windows.Foundation;
@@ -181,6 +182,15 @@ internal sealed partial class SampleListPage : ListPage
{
Title = "I also have properties",
},
new ListItem(new EverChangingCommand("Cat", "🐈‍⬛", "🐈"))
{
Title = "And I have a commands with changing name and icon",
MoreCommands = [
new CommandContextItem(new EverChangingCommand("Water", "🐬", "🐳", "🐟", "🦈")),
new CommandContextItem(new EverChangingCommand("Faces", "😁", "🥺", "😍")),
new CommandContextItem(new EverChangingCommand("Hearts", "♥️", "💚", "💜", "🧡", "💛", "💙")),
],
}
];
}
@@ -229,4 +239,47 @@ internal sealed partial class SampleListPage : ListPage
{ "hmm?", null },
};
}
internal sealed partial class EverChangingCommand : InvokableCommand, IDisposable
{
private readonly string[] _icons;
private readonly Timer _timer;
private readonly string _name;
private int _currentIndex;
public EverChangingCommand(string name, params string[] icons)
{
_icons = icons ?? throw new ArgumentNullException(nameof(icons));
if (_icons.Length == 0)
{
throw new ArgumentException("Icons array cannot be empty", nameof(icons));
}
_name = name;
Name = $"{_name} {DateTimeOffset.UtcNow:hh:mm:ss}";
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));
}
private void OnTimerElapsed(object state)
{
var nextIndex = (_currentIndex + 1) % _icons.Length;
if (nextIndex == _currentIndex && _icons.Length > 1)
{
nextIndex = (_currentIndex + 1) % _icons.Length;
}
_currentIndex = nextIndex;
Name = $"{_name} {DateTimeOffset.UtcNow:hh:mm:ss}";
Icon = new IconInfo(_icons[_currentIndex]);
}
public void Dispose()
{
_timer?.Dispose();
}
}
}