terminal/src/cascadia/TerminalSettingsModel/KeyMapping.cpp
Carlos Zamora 90452664ff
Implement CascadiaSettings::Copy() (#7877)
## Summary of the Pull Request
This implements the `Copy` function for `CascadiaSettings`. Copy performs a deep copy of a `CascadiaSettings` object. This is needed for data binding in the Terminal Settings Editor.

The `Copy` function was basically implemented in every settings model object. This was mostly just repetitive work.

## References
#7667 - TSM
#1564 - Settings UI

## PR Checklist
* [X] Tests added/passed
2020-10-16 15:14:11 -07:00

125 lines
4.3 KiB
C++

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "pch.h"
#include "KeyMapping.h"
#include "KeyChordSerialization.h"
#include "ActionAndArgs.h"
#include "KeyMapping.g.cpp"
using namespace winrt::Microsoft::Terminal::Settings::Model;
using namespace winrt::Microsoft::Terminal::TerminalControl;
namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
winrt::com_ptr<KeyMapping> KeyMapping::Copy() const
{
auto keymap{ winrt::make_self<KeyMapping>() };
std::for_each(_keyShortcuts.begin(), _keyShortcuts.end(), [keymap](auto& kv) {
const auto actionAndArgsImpl{ winrt::get_self<ActionAndArgs>(kv.second) };
keymap->_keyShortcuts[kv.first] = *actionAndArgsImpl->Copy();
});
return keymap;
}
Microsoft::Terminal::Settings::Model::ActionAndArgs KeyMapping::TryLookup(KeyChord const& chord) const
{
const auto result = _keyShortcuts.find(chord);
if (result != _keyShortcuts.end())
{
return result->second;
}
return nullptr;
}
uint64_t KeyMapping::Size() const
{
return _keyShortcuts.size();
}
void KeyMapping::SetKeyBinding(const Microsoft::Terminal::Settings::Model::ActionAndArgs& actionAndArgs,
const KeyChord& chord)
{
_keyShortcuts[chord] = actionAndArgs;
}
// Method Description:
// - Remove the action that's bound to a particular KeyChord.
// Arguments:
// - chord: the keystroke to remove the action for.
// Return Value:
// - <none>
void KeyMapping::ClearKeyBinding(const KeyChord& chord)
{
_keyShortcuts.erase(chord);
}
KeyChord KeyMapping::GetKeyBindingForAction(Microsoft::Terminal::Settings::Model::ShortcutAction const& action)
{
for (auto& kv : _keyShortcuts)
{
if (kv.second.Action() == action)
{
return kv.first;
}
}
return { nullptr };
}
// Method Description:
// - Lookup the keychord bound to a particular combination of ShortcutAction
// and IActionArgs. This enables searching no only for the binding of a
// particular ShortcutAction, but also a particular set of values for
// arguments to that action.
// Arguments:
// - actionAndArgs: The ActionAndArgs to lookup the keybinding for.
// Return Value:
// - The bound keychord, if this ActionAndArgs is bound to a key, otherwise nullptr.
KeyChord KeyMapping::GetKeyBindingForActionWithArgs(Microsoft::Terminal::Settings::Model::ActionAndArgs const& actionAndArgs)
{
if (actionAndArgs == nullptr)
{
return { nullptr };
}
for (auto& kv : _keyShortcuts)
{
const auto action = kv.second.Action();
const auto args = kv.second.Args();
const auto actionMatched = action == actionAndArgs.Action();
const auto argsMatched = args ? args.Equals(actionAndArgs.Args()) : args == actionAndArgs.Args();
if (actionMatched && argsMatched)
{
return kv.first;
}
}
return { nullptr };
}
// Method Description:
// - Takes the KeyModifier flags from Terminal and maps them to the WinRT types which are used by XAML
// Return Value:
// - a Windows::System::VirtualKeyModifiers object with the flags of which modifiers used.
Windows::System::VirtualKeyModifiers KeyMapping::ConvertVKModifiers(KeyModifiers modifiers)
{
Windows::System::VirtualKeyModifiers keyModifiers = Windows::System::VirtualKeyModifiers::None;
if (WI_IsFlagSet(modifiers, KeyModifiers::Ctrl))
{
keyModifiers |= Windows::System::VirtualKeyModifiers::Control;
}
if (WI_IsFlagSet(modifiers, KeyModifiers::Shift))
{
keyModifiers |= Windows::System::VirtualKeyModifiers::Shift;
}
if (WI_IsFlagSet(modifiers, KeyModifiers::Alt))
{
// note: Menu is the Alt VK_MENU
keyModifiers |= Windows::System::VirtualKeyModifiers::Menu;
}
return keyModifiers;
}
}