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]; + } + } }