Update command list when a command is added or a name is changed (#19771)

When a new command is added or an existing command's name is changed,
make sure we update the top-level command list so that it appears there
when the user navigates back to the actions page

## Detailed Description of the Pull Request / Additional comments
Added a new `_CommandListDirty` flag to the `ActionsViewModel`. When a
new command is added or a command's name is changed the flag is set.
When navigating to the top-level actions page, we regenerate the
`_CommandList` if the flag is set.

## Validation Steps Performed
Adding a new command or updating a command's name is now reflected
correctly when navigating back to the actions page.
This commit is contained in:
PankajBhojwani
2026-02-27 14:59:30 -08:00
committed by GitHub
parent 1414abc843
commit b6f041c4a4
4 changed files with 46 additions and 6 deletions

View File

@@ -23,6 +23,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
{
const auto args = e.Parameter().as<Editor::NavigateToPageArgs>();
_ViewModel = args.ViewModel().as<Editor::ActionsViewModel>();
_ViewModel.ReSortCommandList();
auto vmImpl = get_self<ActionsViewModel>(_ViewModel);
vmImpl->MarkAsVisited();
_layoutUpdatedRevoker = LayoutUpdated(winrt::auto_revoke, [this](auto /*s*/, auto /*e*/) {

View File

@@ -126,13 +126,12 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
void CommandViewModel::Name(const winrt::hstring& newName)
{
_command.Name(newName);
if (newName.empty())
if (_command.Name() != newName)
{
// if the name was cleared, refresh the DisplayName
_command.Name(newName);
_NotifyChanges(L"DisplayName", L"DisplayNameAndKeyChordAutomationPropName");
_cachedDisplayName.clear();
}
_cachedDisplayName.clear();
}
winrt::hstring CommandViewModel::DisplayNameAndKeyChordAutomationPropName()
@@ -322,7 +321,10 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
{
weak->_ReplaceCommandWithUserCopy(false);
}
weak->_NotifyChanges(L"DisplayName");
if (!weak->_command.HasName())
{
weak->_NotifyChanges(L"DisplayName");
}
}
});
}
@@ -359,7 +361,10 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
_RegisterActionArgsVMEvents(*actionArgsVM);
actionArgsVM->Initialize();
ActionArgsVM(*actionArgsVM);
_NotifyChanges(L"DisplayName");
if (!_command.HasName())
{
_NotifyChanges(L"DisplayName");
}
}
ArgWrapper::ArgWrapper(const Model::ArgDescriptor& descriptor, const Windows::Foundation::IInspectable& value) :
@@ -1241,6 +1246,25 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
CurrentPage(ActionsSubPage::Edit);
}
void ActionsViewModel::ReSortCommandList()
{
if (_CommandListDirty)
{
std::vector<Editor::CommandViewModel> commandList;
commandList.reserve(_CommandList.Size());
for (const auto& cmd : _CommandList)
{
commandList.push_back(cmd);
}
std::sort(commandList.begin(), commandList.end(), [](const Editor::CommandViewModel& lhs, const Editor::CommandViewModel& rhs) {
return lhs.DisplayName() < rhs.DisplayName();
});
_CommandList = single_threaded_observable_vector(std::move(commandList));
_NotifyChanges(L"CommandList");
_CommandListDirty = false;
}
}
void ActionsViewModel::CurrentCommand(const Editor::CommandViewModel& newCommand)
{
_CurrentCommand = newCommand;
@@ -1411,5 +1435,17 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
cmdVM->DeleteRequested({ this, &ActionsViewModel::_CmdVMDeleteRequestedHandler });
cmdVM->PropagateColorSchemeRequested({ this, &ActionsViewModel::_CmdVMPropagateColorSchemeRequestedHandler });
cmdVM->PropagateColorSchemeNamesRequested({ this, &ActionsViewModel::_CmdVMPropagateColorSchemeNamesRequestedHandler });
cmdVM->PropertyChanged([weakThis{ get_weak() }](const IInspectable& sender, const Windows::UI::Xaml::Data::PropertyChangedEventArgs& args) {
if (const auto self{ weakThis.get() })
{
const auto senderVM{ sender.as<Editor::CommandViewModel>() };
const auto propertyName{ args.PropertyName() };
if (propertyName == L"DisplayName")
{
// when a command's name changes, note that we need to re-sort the command list when we navigate back to the actions page
self->_CommandListDirty = true;
}
}
});
}
}

View File

@@ -258,6 +258,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
bool DisplayBadge() const noexcept;
void AddNewCommand();
void ReSortCommandList();
void CurrentCommand(const Editor::CommandViewModel& newCommand);
Editor::CommandViewModel CurrentCommand();
@@ -279,6 +280,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
Model::CascadiaSettings _Settings;
Windows::Foundation::Collections::IMap<Model::ShortcutAction, winrt::hstring> _AvailableActionsAndNamesMap;
Windows::Foundation::Collections::IMap<winrt::hstring, Model::ShortcutAction> _NameToActionMap;
bool _CommandListDirty{ false };
void _MakeCommandVMsHelper();
void _RegisterCmdVMEvents(com_ptr<implementation::CommandViewModel>& cmdVM);

View File

@@ -166,6 +166,7 @@ namespace Microsoft.Terminal.Settings.Editor
void UpdateSettings(Microsoft.Terminal.Settings.Model.CascadiaSettings settings);
void AddNewCommand();
void ReSortCommandList();
ActionsSubPage CurrentPage;
Boolean DisplayBadge { get; };