Skip to content

Commit

Permalink
Adding UI shortcut generation to new keybinding mappings. Resolving m…
Browse files Browse the repository at this point in the history
  • Loading branch information
timheuer committed May 21, 2019
1 parent f0acd8b commit 3a75c74
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 15 deletions.
76 changes: 61 additions & 15 deletions src/cascadia/TerminalApp/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,28 +215,47 @@ namespace winrt::TerminalApp::implementation
void App::_CreateNewTabFlyout()
{
auto newTabFlyout = Controls::MenuFlyout{};
auto _keyBindings = _settings->GetKeybindings();

for (int profileIndex = 0; profileIndex < _settings->GetProfiles().size(); profileIndex++)
{
const auto& profile = _settings->GetProfiles()[profileIndex];
auto profileMenuItem = Controls::MenuFlyoutItem{};

// add the keyboard shortcuts for the first 10 profiles
if (profileIndex < 10)
// add the keyboard shortcuts for the first 9 profiles
if (profileIndex < 9)
{
auto profileShortcut = Windows::UI::Xaml::Input::KeyboardAccelerator{};
profileShortcut.Modifiers(Windows::System::VirtualKeyModifiers::Control | Windows::System::VirtualKeyModifiers::Shift);
// enum value for ShortcutAction::NewTabProfileX starts at 3; 0==3
auto profileKeyChord = _keyBindings.GetKeyBinding(static_cast<ShortcutAction>(profileIndex + 3));

if (profileIndex == 9)
{
// the last tab is actually 0 in the shortcut, so make sure to handle that
profileShortcut.Key(Windows::System::VirtualKey::Number0);
}
else
// make sure we find one to display
if (profileKeyChord)
{
// VK_Number1 starts at 48
profileShortcut.Key(static_cast<Windows::System::VirtualKey>(49 + profileIndex));
}
profileMenuItem.KeyboardAccelerators().Append(profileShortcut);
// work around https://github.com/microsoft/microsoft-ui-xaml/issues/708 in case of VK_OEM_COMMA
if (profileKeyChord.Vkey() != VK_OEM_COMMA)
{
// use the XAML shortcut to give us the automatic capabilities
auto profileShortcut = Windows::UI::Xaml::Input::KeyboardAccelerator{};

// TODO: Modify this when https://github.com/microsoft/terminal/issues/877 is resolved
profileShortcut.Key(static_cast<Windows::System::VirtualKey>(profileKeyChord.Vkey()));

// inspect the modifiers fromt he KeyChord and set the flags int he XAML value
auto modifiers = AppKeyBindings::ConvertVKModifiers(profileKeyChord.Modifiers());

// add the modifiers to the shortcut
profileShortcut.Modifiers(modifiers);

// add to the menu
profileMenuItem.KeyboardAccelerators().Append(profileShortcut);
}
else // we've got a comma, so need to just use the alternate method
{
// extract the modifier and key to a nice format
auto overrideString = AppKeyBindings::FormatOverrideShortcutText(profileKeyChord.Modifiers());
profileMenuItem.KeyboardAcceleratorTextOverride(overrideString + L" ,");
}
}
}

auto profileName = profile.GetName();
Expand Down Expand Up @@ -275,7 +294,34 @@ namespace winrt::TerminalApp::implementation

// Using alternate method here than VirtualKey due to https://github.com/microsoft/microsoft-ui-xaml/issues/708
// this should really use KeyboardAccelerator API bug above bug in framework prevents it, using override
settingsItem.KeyboardAcceleratorTextOverride(L"Ctrl + ,");
auto settingsKeyChord = _keyBindings.GetKeyBinding(ShortcutAction::OpenSettings);
if (settingsKeyChord)
{
// work around https://github.com/microsoft/microsoft-ui-xaml/issues/708 in case of VK_OEM_COMMA
if (settingsKeyChord.Vkey() != VK_OEM_COMMA)
{
// use the XAML shortcut to give us the automatic capabilities
auto settingsShortcut = Windows::UI::Xaml::Input::KeyboardAccelerator{};

// TODO: Modify this when https://github.com/microsoft/terminal/issues/877 is resolved
settingsShortcut.Key(static_cast<Windows::System::VirtualKey>(settingsKeyChord.Vkey()));

// inspect the modifiers fromt he KeyChord and set the flags int he XAML value
auto modifiers = AppKeyBindings::ConvertVKModifiers(settingsKeyChord.Modifiers());

// add the modifiers to the shortcut
settingsShortcut.Modifiers(modifiers);

// add to the menu
settingsItem.KeyboardAccelerators().Append(settingsShortcut);
}
else // we've got a comma, so need to just use the alternate method
{
// extract the modifier and key to a nice format
auto overrideString = AppKeyBindings::FormatOverrideShortcutText(settingsKeyChord.Modifiers());
settingsItem.KeyboardAcceleratorTextOverride(overrideString + L" ,");
}
}

// Create the feedback button.
auto feedbackFlyout = Controls::MenuFlyoutItem{};
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ namespace winrt::TerminalApp::implementation
std::vector<std::shared_ptr<Tab>> _tabs;

std::unique_ptr<::TerminalApp::CascadiaSettings> _settings;
std::unique_ptr<TerminalApp::AppKeyBindings> _keyBindings;

bool _loadedInitialSettings;

Expand Down
50 changes: 50 additions & 0 deletions src/cascadia/TerminalApp/AppKeyBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ static const std::map<std::wstring_view, ShortcutAction> commandNames {
{ SWITCHTOTAB6_KEY, ShortcutAction::SwitchToTab6 },
{ SWITCHTOTAB7_KEY, ShortcutAction::SwitchToTab7 },
{ SWITCHTOTAB8_KEY, ShortcutAction::SwitchToTab8 },
{ OPENSETTINGS_KEY, ShortcutAction::OpenSettings },
};

namespace winrt::TerminalApp::implementation
Expand All @@ -92,6 +93,15 @@ namespace winrt::TerminalApp::implementation
_keyShortcuts[chord] = action;
}

Microsoft::Terminal::Settings::KeyChord AppKeyBindings::GetKeyBinding(TerminalApp::ShortcutAction const& action)
{
for (auto& kv : _keyShortcuts)
{
if (kv.second == action) return kv.first;
}
return { nullptr };
}

bool AppKeyBindings::TryKeyChord(const Settings::KeyChord& kc)
{
const auto keyIter = _keyShortcuts.find(kc);
Expand Down Expand Up @@ -344,4 +354,44 @@ namespace winrt::TerminalApp::implementation

return bindingsArray;
}
Windows::System::VirtualKeyModifiers AppKeyBindings::ConvertVKModifiers(Settings::KeyModifiers modifiers)
{
Windows::System::VirtualKeyModifiers keyModifiers = Windows::System::VirtualKeyModifiers::None;

if (WI_IsFlagSet(modifiers, Settings::KeyModifiers::Ctrl))
{
keyModifiers |= Windows::System::VirtualKeyModifiers::Control;
}
if (WI_IsFlagSet(modifiers, Settings::KeyModifiers::Shift))
{
keyModifiers |= Windows::System::VirtualKeyModifiers::Shift;
}
if (WI_IsFlagSet(modifiers, Settings::KeyModifiers::Alt))
{
// note: Menu is the Alt VK_MENU
keyModifiers |= Windows::System::VirtualKeyModifiers::Menu;
}

return keyModifiers;
}

winrt::hstring AppKeyBindings::FormatOverrideShortcutText(Settings::KeyModifiers modifiers)
{
std::wstring buffer{ L"" };

if (WI_IsFlagSet(modifiers, Settings::KeyModifiers::Ctrl))
{
buffer += L"Ctrl+";
}
if (WI_IsFlagSet(modifiers, Settings::KeyModifiers::Shift))
{
buffer += L"Shift+";
}
if (WI_IsFlagSet(modifiers, Settings::KeyModifiers::Alt))
{
buffer += L"Alt+";
}

return winrt::hstring{ buffer };
}
}
3 changes: 3 additions & 0 deletions src/cascadia/TerminalApp/AppKeyBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ namespace winrt::TerminalApp::implementation

static TerminalApp::AppKeyBindings FromJson(Windows::Data::Json::JsonArray const& json);
Windows::Data::Json::JsonArray ToJson();
static Windows::System::VirtualKeyModifiers ConvertVKModifiers(winrt::Microsoft::Terminal::Settings::KeyModifiers modifiers);
static winrt::hstring FormatOverrideShortcutText(winrt::Microsoft::Terminal::Settings::KeyModifiers modifiers);

bool TryKeyChord(winrt::Microsoft::Terminal::Settings::KeyChord const& kc);
void SetKeyBinding(TerminalApp::ShortcutAction const& action, winrt::Microsoft::Terminal::Settings::KeyChord const& chord);
Microsoft::Terminal::Settings::KeyChord GetKeyBinding(TerminalApp::ShortcutAction const& action);

DECLARE_EVENT(CopyText, _CopyTextHandlers, TerminalApp::CopyTextEventArgs);
DECLARE_EVENT(PasteText, _PasteTextHandlers, TerminalApp::PasteTextEventArgs);
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/AppKeyBindings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace TerminalApp
static AppKeyBindings FromJson(Windows.Data.Json.JsonArray json);

void SetKeyBinding(ShortcutAction action, Microsoft.Terminal.Settings.KeyChord chord);
Microsoft.Terminal.Settings.KeyChord GetKeyBinding(ShortcutAction action);

event CopyTextEventArgs CopyText;
event PasteTextEventArgs PasteText;
Expand Down

0 comments on commit 3a75c74

Please sign in to comment.