Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsEditor/Actions.cpp (C++)

Code before:
    }

    void KeyBindingViewModel::AttemptAcceptChanges()
    {
        AttemptAcceptChanges(_ProposedKeys);
    }

    void KeyBindingViewModel::AttemptAcceptChanges(hstring newKeyChordText)
    {
        auto args{ make_self<ModifyKeyBindingEventArgs>(_Keys, _Keys, _CurrentAction, unbox_value<hstring>(_ProposedAction)) };

        // Key Chord Text
        try
        {
            // Attempt to convert the provided key chord text
            const auto newKeyChord{ KeyChordSerialization::FromString(newKeyChordText) };
            args->NewKeys(newKeyChord);
        }
        catch (hresult_invalid_argument)
        {
            // Converting the text into a key chord failed
            // TODO GH #6900:
            //  This is tricky. I still haven't found a way to reference the
            //  key chord text box. It's hidden behind the data template.
            //  Ideally, some kind of notification would alert the user, but
            //  to make it look nice, we need it to somehow target the text box.
            //  Alternatively, we want a full key chord editor/listener.
            //  If we implement that, we won't need this validation or error message.
        }

        _ModifyKeyBindingRequestedHandlers(*this, *args);
    }

    Actions::Actions()
    {
        InitializeComponent();
    }

    Automation::Peers::AutomationPeer Actions::OnCreateAutomationPeer()
    {
        _AutomationPeerAttached = true;
        for (const auto& kbdVM : _KeyBindingList)
        {
            // To create a more accessible experience, we want the "edit" buttons to _always_
            // appear when a screen reader is attached. This ensures that the edit buttons are
            // accessible via the UIA tree.
            get_self<KeyBindingViewModel>(kbdVM)->IsAutomationPeerAttached(_AutomationPeerAttached);
        }
        return nullptr;
    }


Review comment (suggestion):
note: this is handled in `Actions::_ViewModelModifyKeyBindingHandler`

Code after:
            ProposedKeys(KeyChordText());
            ProposedAction(box_value(CurrentAction()));
        }
    }

    void KeyBindingViewModel::AttemptAcceptChanges()
    {
        AttemptAcceptChanges(_ProposedKeys);
    }

    void KeyBindingViewModel::AttemptAcceptChanges(hstring newKeyChordText)
    {
        try
        {
            // empty string --> don't accept changes
            if (newKeyChordText.empty())
            {
                return;
            }

            // ModifyKeyBindingEventArgs
            const auto args{ make_self<ModifyKeyBindingEventArgs>(_Keys, // OldKeys
                                                                  KeyChordSerialization::FromString(newKeyChordText), // NewKeys: Attempt to convert the provided key chord text
                                                                  _IsNewlyAdded ? hstring{} : _CurrentAction, // OldAction
                                                                  unbox_value<hstring>(_ProposedAction)) }; //
            _ModifyKeyBindingRequestedHandlers(*this, *args);
        }
        catch (hresult_invalid_argument)
        {
            // Converting the text into a key chord failed.
            // Don't accept the changes.
            // TODO GH #6900:
            //  This is tricky. I still haven't found a way to reference the
            //  key chord text box. It's hidden behind the data template.
            //  Ideally, some kind of notification would alert the user, but
            //  to make it look nice, we need it to somehow target the text box.
            //  Alternatively, we want a full key chord editor/listener.
            //  If we implement that, we won't need this validation or error message.
        }
    }

    void KeyBindingViewModel::CancelChanges()
    {
        if (_IsNewlyAdded)
        {
            _DeleteNewlyAddedKeyBindingHandlers(*this, nullptr);
        }
        else
        {
            ToggleEditMode();
        }