From 56aa9acfb405a36988584a46420f2793594b8f7a Mon Sep 17 00:00:00 2001 From: Michael Jolley Date: Thu, 21 Aug 2025 06:09:00 -0500 Subject: [PATCH] CmdPal: Ensuring alias changes are propagated to related TopLevelViewModels (#40970) Closes #39709 - Only updating aliases when the alias has changed - When an alias is used that is already in use, remove the alias from the previous TopLevelViewModel - Don't crash if the previous TopLevelViewModel doesn't exist (e.g. it was uninstalled) --- .../AliasManager.cs | 15 ++++++++++++++- .../TopLevelViewModel.cs | 12 +++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/AliasManager.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/AliasManager.cs index 642e5ad4a9..72a295c83f 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/AliasManager.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/AliasManager.cs @@ -97,14 +97,27 @@ public partial class AliasManager : ObservableObject } } - // Look for the old alias, and remove it List toRemove = []; foreach (var kv in _aliases) { + // Look for the old aliases for the command, and remove it if (kv.Value.CommandId == commandId) { toRemove.Add(kv.Value); } + + // Look for the alias belonging to another command, and remove it + if (newAlias is not null && kv.Value.Alias == newAlias.Alias) + { + toRemove.Add(kv.Value); + + // Remove alias from other TopLevelViewModels it may be assigned to + var topLevelCommand = _topLevelCommandManager.LookupCommand(kv.Value.CommandId); + if (topLevelCommand is not null) + { + topLevelCommand.AliasText = string.Empty; + } + } } foreach (var alias in toRemove) diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelViewModel.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelViewModel.cs index e73f5b09ba..e9b3f1bcca 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelViewModel.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelViewModel.cs @@ -110,6 +110,8 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem get => Alias?.Alias ?? string.Empty; set { + var previousAlias = Alias?.Alias ?? string.Empty; + if (string.IsNullOrEmpty(value)) { Alias = null; @@ -126,9 +128,13 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem } } - HandleChangeAlias(); - OnPropertyChanged(nameof(AliasText)); - OnPropertyChanged(nameof(IsDirectAlias)); + // Only call HandleChangeAlias if there was an actual change. + if (previousAlias != Alias?.Alias) + { + HandleChangeAlias(); + OnPropertyChanged(nameof(AliasText)); + OnPropertyChanged(nameof(IsDirectAlias)); + } } }