Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-add keybindings to new tab dropdown #3618

Merged
merged 3 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/cascadia/TerminalApp/ActionArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ namespace winrt::TerminalApp::implementation
static constexpr std::string_view TrimWhitespaceKey{ "trimWhitespace" };

public:
bool Equals(const IActionArgs& other)
{
auto otherAsUs = other.try_as<CopyTextArgs>();
if (otherAsUs)
{
return otherAsUs->_TrimWhitespace == _TrimWhitespace;
}
return false;
};
static winrt::TerminalApp::IActionArgs FromJson(const Json::Value& json)
{
// LOAD BEARING: Not using make_self here _will_ break you in the future!
Expand All @@ -61,6 +70,15 @@ namespace winrt::TerminalApp::implementation
static constexpr std::string_view ProfileIndexKey{ "index" };

public:
bool Equals(const IActionArgs& other)
{
auto otherAsUs = other.try_as<NewTabArgs>();
if (otherAsUs)
{
return otherAsUs->_ProfileIndex == _ProfileIndex;
}
return false;
};
static winrt::TerminalApp::IActionArgs FromJson(const Json::Value& json)
{
// LOAD BEARING: Not using make_self here _will_ break you in the future!
Expand All @@ -81,6 +99,15 @@ namespace winrt::TerminalApp::implementation
static constexpr std::string_view TabIndexKey{ "index" };

public:
bool Equals(const IActionArgs& other)
{
auto otherAsUs = other.try_as<SwitchToTabArgs>();
if (otherAsUs)
{
return otherAsUs->_TabIndex == _TabIndex;
}
return false;
};
static winrt::TerminalApp::IActionArgs FromJson(const Json::Value& json)
{
// LOAD BEARING: Not using make_self here _will_ break you in the future!
Expand Down Expand Up @@ -136,6 +163,15 @@ namespace winrt::TerminalApp::implementation
static constexpr std::string_view DirectionKey{ "direction" };

public:
bool Equals(const IActionArgs& other)
{
auto otherAsUs = other.try_as<ResizePaneArgs>();
if (otherAsUs)
{
return otherAsUs->_Direction == _Direction;
}
return false;
};
static winrt::TerminalApp::IActionArgs FromJson(const Json::Value& json)
{
// LOAD BEARING: Not using make_self here _will_ break you in the future!
Expand All @@ -156,6 +192,15 @@ namespace winrt::TerminalApp::implementation
static constexpr std::string_view DirectionKey{ "direction" };

public:
bool Equals(const IActionArgs& other)
{
auto otherAsUs = other.try_as<MoveFocusArgs>();
if (otherAsUs)
{
return otherAsUs->_Direction == _Direction;
}
return false;
};
static winrt::TerminalApp::IActionArgs FromJson(const Json::Value& json)
{
// LOAD BEARING: Not using make_self here _will_ break you in the future!
Expand All @@ -172,6 +217,17 @@ namespace winrt::TerminalApp::implementation
{
AdjustFontSizeArgs() = default;
GETSET_PROPERTY(int32_t, Delta, 0);

public:
bool Equals(const IActionArgs& other)
{
auto otherAsUs = other.try_as<AdjustFontSizeArgs>();
if (otherAsUs)
{
return otherAsUs->_Delta == _Delta;
}
return false;
};
};
}

Expand Down
7 changes: 4 additions & 3 deletions src/cascadia/TerminalApp/ActionArgs.idl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

namespace TerminalApp
{
// An empty interface must specify an explicit [uuid] to ensure uniqueness.
// We also manually have to specify a "version" attribute to make the compiler happy.
[uuid("191C2BDE-1A60-4BAB-9765-D850F0EF2CAC")][version(1)] interface IActionArgs{};
interface IActionArgs
{
Boolean Equals(IActionArgs other);
};

interface IActionEventArgs
{
Expand Down
24 changes: 23 additions & 1 deletion src/cascadia/TerminalApp/AppKeyBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

using namespace winrt::Microsoft::Terminal;
using namespace winrt::TerminalApp;
using namespace winrt::Microsoft::Terminal::Settings;

namespace winrt::TerminalApp::implementation
{
Expand All @@ -29,7 +30,7 @@ namespace winrt::TerminalApp::implementation
_keyShortcuts.erase(chord);
}

Microsoft::Terminal::Settings::KeyChord AppKeyBindings::GetKeyBinding(TerminalApp::ShortcutAction const& action)
KeyChord AppKeyBindings::GetKeyBindingForAction(TerminalApp::ShortcutAction const& action)
{
for (auto& kv : _keyShortcuts)
{
Expand All @@ -40,6 +41,27 @@ namespace winrt::TerminalApp::implementation
}
return { nullptr };
}
// Method Description:
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
// - 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 AppKeyBindings::GetKeyBindingForActionWithArgs(TerminalApp::ActionAndArgs const& actionAndArgs)
{
for (auto& kv : _keyShortcuts)
{
if (kv.second.Action() == actionAndArgs.Action() &&
kv.second.Args().Equals(actionAndArgs.Args()))
{
return kv.first;
}
}
return { nullptr };
}

bool AppKeyBindings::TryKeyChord(const Settings::KeyChord& kc)
{
Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/TerminalApp/AppKeyBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ namespace winrt::TerminalApp::implementation
void SetKeyBinding(TerminalApp::ActionAndArgs const& actionAndArgs,
winrt::Microsoft::Terminal::Settings::KeyChord const& chord);
void ClearKeyBinding(winrt::Microsoft::Terminal::Settings::KeyChord const& chord);
Microsoft::Terminal::Settings::KeyChord GetKeyBinding(TerminalApp::ShortcutAction const& action);
Microsoft::Terminal::Settings::KeyChord GetKeyBindingForAction(TerminalApp::ShortcutAction const& action);
Microsoft::Terminal::Settings::KeyChord GetKeyBindingForActionWithArgs(TerminalApp::ActionAndArgs const& actionAndArgs);

static Windows::System::VirtualKeyModifiers ConvertVKModifiers(winrt::Microsoft::Terminal::Settings::KeyModifiers modifiers);

Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/TerminalApp/AppKeyBindings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ namespace TerminalApp
void SetKeyBinding(ActionAndArgs actionAndArgs, Microsoft.Terminal.Settings.KeyChord chord);
void ClearKeyBinding(Microsoft.Terminal.Settings.KeyChord chord);

Microsoft.Terminal.Settings.KeyChord GetKeyBinding(ShortcutAction action);
Microsoft.Terminal.Settings.KeyChord GetKeyBindingForAction(ShortcutAction action);
Microsoft.Terminal.Settings.KeyChord GetKeyBindingForActionWithArgs(ActionAndArgs actionAndArgs);

event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> CopyText;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> PasteText;
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/AppKeyBindingsSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ Json::Value winrt::TerminalApp::implementation::AppKeyBindings::ToJson()
const auto searchedForName = actionName.first;
const auto searchedForAction = actionName.second;

if (const auto chord{ GetKeyBinding(searchedForAction) })
if (const auto chord{ GetKeyBindingForAction(searchedForAction) })
{
if (const auto serialization{ _ShortcutAsJsonObject(chord, searchedForName) })
{
Expand Down
19 changes: 17 additions & 2 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "pch.h"
#include "TerminalPage.h"
#include "ActionAndArgs.h"
#include "Utils.h"

#include <LibraryResources.h>
Expand Down Expand Up @@ -253,7 +254,21 @@ namespace winrt::TerminalApp::implementation
{
// enum value for ShortcutAction::NewTabProfileX; 0==NewTabProfile0
const auto action = static_cast<ShortcutAction>(profileIndex + static_cast<int>(ShortcutAction::NewTabProfile0));
auto profileKeyChord = keyBindings.GetKeyBinding(action);
// First, attepmt to search for the keybinding for the simple
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
// NewTabProfile0-9 ShortcutActions.
auto profileKeyChord = keyBindings.GetKeyBindingForAction(action);
if (!profileKeyChord)
{
// If NewTabProfileN didn't have a binding, look for a
// keychord that is bound to the equivalent
// NewTab(ProfileIndex=N) action
auto actionAndArgs = winrt::make_self<winrt::TerminalApp::implementation::ActionAndArgs>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

omg the expense

actionAndArgs->Action(ShortcutAction::NewTab);
auto newTabArgs = winrt::make_self<winrt::TerminalApp::implementation::NewTabArgs>();
newTabArgs->ProfileIndex(profileIndex);
actionAndArgs->Args(*newTabArgs);
profileKeyChord = keyBindings.GetKeyBindingForActionWithArgs(*actionAndArgs);
}

// make sure we find one to display
if (profileKeyChord)
Expand Down Expand Up @@ -306,7 +321,7 @@ namespace winrt::TerminalApp::implementation
settingsItem.Click({ this, &TerminalPage::_SettingsButtonOnClick });
newTabFlyout.Items().Append(settingsItem);

auto settingsKeyChord = keyBindings.GetKeyBinding(ShortcutAction::OpenSettings);
auto settingsKeyChord = keyBindings.GetKeyBindingForAction(ShortcutAction::OpenSettings);
if (settingsKeyChord)
{
_SetAcceleratorForMenuItem(settingsItem, settingsKeyChord);
Expand Down