CmdPal: Ensuring alias changes are propagated to related TopLevelViewModels (#40970)
Some checks failed
Spell checking / Check Spelling (push) Has been cancelled
Spell checking / Report (Push) (push) Has been cancelled
Spell checking / Report (PR) (push) Has been cancelled
Spell checking / Update PR (push) Has been cancelled

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)
This commit is contained in:
Michael Jolley 2025-08-21 06:09:00 -05:00 committed by GitHub
parent da572c6c40
commit 56aa9acfb4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 4 deletions

View File

@ -97,14 +97,27 @@ public partial class AliasManager : ObservableObject
} }
} }
// Look for the old alias, and remove it
List<CommandAlias> toRemove = []; List<CommandAlias> toRemove = [];
foreach (var kv in _aliases) foreach (var kv in _aliases)
{ {
// Look for the old aliases for the command, and remove it
if (kv.Value.CommandId == commandId) if (kv.Value.CommandId == commandId)
{ {
toRemove.Add(kv.Value); 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) foreach (var alias in toRemove)

View File

@ -110,6 +110,8 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem
get => Alias?.Alias ?? string.Empty; get => Alias?.Alias ?? string.Empty;
set set
{ {
var previousAlias = Alias?.Alias ?? string.Empty;
if (string.IsNullOrEmpty(value)) if (string.IsNullOrEmpty(value))
{ {
Alias = null; Alias = null;
@ -126,11 +128,15 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem
} }
} }
// Only call HandleChangeAlias if there was an actual change.
if (previousAlias != Alias?.Alias)
{
HandleChangeAlias(); HandleChangeAlias();
OnPropertyChanged(nameof(AliasText)); OnPropertyChanged(nameof(AliasText));
OnPropertyChanged(nameof(IsDirectAlias)); OnPropertyChanged(nameof(IsDirectAlias));
} }
} }
}
public bool IsDirectAlias public bool IsDirectAlias
{ {