Skip to content

Commit

Permalink
Encapsulate dispatching ShortcutActions in it's own class (#3658)
Browse files Browse the repository at this point in the history
## Summary of the Pull Request

Moves all the code responsible for dispatching an `ActionAndArgs` to it's own class, `ShortcutActionDispatch`. Now, the `AppKeyBindings` just uses the single instance of a `ShortcutActionDispatch` that the `TerminalPage` owns to dispatch events, without the need to re-attach the event handlers every time we reload the settings.

## References

This is something I originally did as a part of #2046.

I need this now for #607.

It's also a part of work for #3475

## PR Checklist
* [x] This is a bullet point within #3475
* [x] I work here
* [ ] Tests added/passed
* [n/a] Requires documentation to be updated

## Detailed Description of the Pull Request / Additional comments

With this change, we'll be able to have other things dispatch `ShortcutAction`s easily, by constructing an `ActionAndArgs` and just passing it straight to the `ShortcutActionDispatch`.

## Validation Steps Performed

Ran the Terminal, tried out some keybindings, namely <kbd>Ctrl+c</kbd> for copy when there is a selection, or send `^C` when there isn't. That still works. 

Reloading settings also still works. 

-----------------------------------------------
* Move action handling to it's own class separate from AKB. This is the first checkbox in #3475

(cherry picked from commit 696726b)

* clean up doc comments
  • Loading branch information
zadjii-msft authored Nov 27, 2019
1 parent 7bcb060 commit 111b88c
Show file tree
Hide file tree
Showing 9 changed files with 427 additions and 327 deletions.
182 changes: 3 additions & 179 deletions src/cascadia/TerminalApp/AppKeyBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,190 +70,14 @@ namespace winrt::TerminalApp::implementation
if (keyIter != _keyShortcuts.end())
{
const auto actionAndArgs = keyIter->second;
return _DoAction(actionAndArgs);
return _dispatch.DoAction(actionAndArgs);
}
return false;
}

bool AppKeyBindings::_DoAction(ActionAndArgs actionAndArgs)
void AppKeyBindings::SetDispatch(const winrt::TerminalApp::ShortcutActionDispatch& dispatch)
{
const auto& action = actionAndArgs.Action();
const auto& args = actionAndArgs.Args();
auto eventArgs = args ? winrt::make_self<ActionEventArgs>(args) :
winrt::make_self<ActionEventArgs>();

switch (action)
{
case ShortcutAction::CopyText:
{
_CopyTextHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::CopyTextWithoutNewlines:
{
_CopyTextHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::PasteText:
{
_PasteTextHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::OpenNewTabDropdown:
{
_OpenNewTabDropdownHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::DuplicateTab:
{
_DuplicateTabHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::OpenSettings:
{
_OpenSettingsHandlers(*this, *eventArgs);
break;
}

case ShortcutAction::NewTab:
case ShortcutAction::NewTabProfile0:
case ShortcutAction::NewTabProfile1:
case ShortcutAction::NewTabProfile2:
case ShortcutAction::NewTabProfile3:
case ShortcutAction::NewTabProfile4:
case ShortcutAction::NewTabProfile5:
case ShortcutAction::NewTabProfile6:
case ShortcutAction::NewTabProfile7:
case ShortcutAction::NewTabProfile8:
{
_NewTabHandlers(*this, *eventArgs);
break;
}

case ShortcutAction::NewWindow:
{
_NewWindowHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::CloseWindow:
{
_CloseWindowHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::CloseTab:
{
_CloseTabHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::ClosePane:
{
_ClosePaneHandlers(*this, *eventArgs);
break;
}

case ShortcutAction::ScrollUp:
{
_ScrollUpHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::ScrollDown:
{
_ScrollDownHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::ScrollUpPage:
{
_ScrollUpPageHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::ScrollDownPage:
{
_ScrollDownPageHandlers(*this, *eventArgs);
break;
}

case ShortcutAction::NextTab:
{
_NextTabHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::PrevTab:
{
_PrevTabHandlers(*this, *eventArgs);
break;
}

case ShortcutAction::SplitVertical:
{
_SplitVerticalHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::SplitHorizontal:
{
_SplitHorizontalHandlers(*this, *eventArgs);
break;
}

case ShortcutAction::SwitchToTab:
case ShortcutAction::SwitchToTab0:
case ShortcutAction::SwitchToTab1:
case ShortcutAction::SwitchToTab2:
case ShortcutAction::SwitchToTab3:
case ShortcutAction::SwitchToTab4:
case ShortcutAction::SwitchToTab5:
case ShortcutAction::SwitchToTab6:
case ShortcutAction::SwitchToTab7:
case ShortcutAction::SwitchToTab8:
{
_SwitchToTabHandlers(*this, *eventArgs);
break;
}

case ShortcutAction::ResizePane:
case ShortcutAction::ResizePaneLeft:
case ShortcutAction::ResizePaneRight:
case ShortcutAction::ResizePaneUp:
case ShortcutAction::ResizePaneDown:
{
_ResizePaneHandlers(*this, *eventArgs);
break;
}

case ShortcutAction::MoveFocus:
case ShortcutAction::MoveFocusLeft:
case ShortcutAction::MoveFocusRight:
case ShortcutAction::MoveFocusUp:
case ShortcutAction::MoveFocusDown:
{
_MoveFocusHandlers(*this, *eventArgs);
break;
}

case ShortcutAction::IncreaseFontSize:
{
_AdjustFontSizeHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::DecreaseFontSize:
{
_AdjustFontSizeHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::ResetFontSize:
{
auto eventArgs = winrt::make_self<ActionEventArgs>();
_ResetFontSizeHandlers(*this, *eventArgs);
return eventArgs->Handled();
}
case ShortcutAction::ToggleFullscreen:
{
_ToggleFullscreenHandlers(*this, *eventArgs);
break;
}
default:
return false;
}
return eventArgs->Handled();
_dispatch = dispatch;
}

// Method Description:
Expand Down
35 changes: 5 additions & 30 deletions src/cascadia/TerminalApp/AppKeyBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "AppKeyBindings.g.h"
#include "ActionArgs.h"
#include "ShortcutActionDispatch.h"
#include "..\inc\cppwinrt_utils.h"

// fwdecl unittest classes
Expand Down Expand Up @@ -54,36 +55,12 @@ namespace winrt::TerminalApp::implementation
void LayerJson(const Json::Value& json);
Json::Value ToJson();

// clang-format off
TYPED_EVENT(CopyText, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(PasteText, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(OpenNewTabDropdown,TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(DuplicateTab, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(NewTab, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(NewWindow, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(CloseWindow, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(CloseTab, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(ClosePane, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(SwitchToTab, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(NextTab, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(PrevTab, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(SplitVertical, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(SplitHorizontal, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(AdjustFontSize, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(ResetFontSize, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(ScrollUp, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(ScrollDown, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(ScrollUpPage, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(ScrollDownPage, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(OpenSettings, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(ResizePane, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(MoveFocus, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(ToggleFullscreen, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
// clang-format on
void SetDispatch(const winrt::TerminalApp::ShortcutActionDispatch& dispatch);

private:
std::unordered_map<winrt::Microsoft::Terminal::Settings::KeyChord, TerminalApp::ActionAndArgs, KeyChordHash, KeyChordEquality> _keyShortcuts;
bool _DoAction(ActionAndArgs actionAndArgs);

winrt::TerminalApp::ShortcutActionDispatch _dispatch{ nullptr };

friend class TerminalAppLocalTests::SettingsTests;
friend class TerminalAppLocalTests::KeyBindingsTests;
Expand All @@ -92,7 +69,5 @@ namespace winrt::TerminalApp::implementation

namespace winrt::TerminalApp::factory_implementation
{
struct AppKeyBindings : AppKeyBindingsT<AppKeyBindings, implementation::AppKeyBindings>
{
};
BASIC_FACTORY(AppKeyBindings);
}
92 changes: 2 additions & 90 deletions src/cascadia/TerminalApp/AppKeyBindings.idl
Original file line number Diff line number Diff line change
@@ -1,75 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import "../ActionArgs.idl";
import "../ShortcutActionDispatch.idl";

namespace TerminalApp
{
// TODO: GH#1069 - Many of these shortcut actions are "legacy" now that we
// have support for arbitrary args (#1142). We should remove them, and our
// legacy deserializers.
enum ShortcutAction
{
Invalid = 0,
CopyText,
CopyTextWithoutNewlines,
PasteText,
OpenNewTabDropdown,
DuplicateTab,
NewTab,
NewTabProfile0, // Legacy
NewTabProfile1, // Legacy
NewTabProfile2, // Legacy
NewTabProfile3, // Legacy
NewTabProfile4, // Legacy
NewTabProfile5, // Legacy
NewTabProfile6, // Legacy
NewTabProfile7, // Legacy
NewTabProfile8, // Legacy
NewWindow,
CloseWindow,
CloseTab,
ClosePane,
NextTab,
PrevTab,
SplitVertical,
SplitHorizontal,
SwitchToTab,
SwitchToTab0, // Legacy
SwitchToTab1, // Legacy
SwitchToTab2, // Legacy
SwitchToTab3, // Legacy
SwitchToTab4, // Legacy
SwitchToTab5, // Legacy
SwitchToTab6, // Legacy
SwitchToTab7, // Legacy
SwitchToTab8, // Legacy
IncreaseFontSize,
DecreaseFontSize,
ResetFontSize,
ScrollUp,
ScrollDown,
ScrollUpPage,
ScrollDownPage,
ResizePane,
ResizePaneLeft, // Legacy
ResizePaneRight, // Legacy
ResizePaneUp, // Legacy
ResizePaneDown, // Legacy
MoveFocus,
MoveFocusLeft, // Legacy
MoveFocusRight, // Legacy
MoveFocusUp, // Legacy
MoveFocusDown, // Legacy
ToggleFullscreen,
OpenSettings
};

[default_interface] runtimeclass ActionAndArgs {
ActionAndArgs();
IActionArgs Args;
ShortcutAction Action;
};

[default_interface] runtimeclass AppKeyBindings : Microsoft.Terminal.Settings.IKeyBindings
{
AppKeyBindings();
Expand All @@ -80,29 +15,6 @@ namespace TerminalApp
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;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> NewTab;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> OpenNewTabDropdown;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> DuplicateTab;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> NewWindow;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> CloseWindow;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> CloseTab;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> ClosePane;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> SwitchToTab;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> NextTab;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> PrevTab;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> SplitVertical;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> SplitHorizontal;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> AdjustFontSize;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> ResetFontSize;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> ScrollUp;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> ScrollDown;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> ScrollUpPage;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> ScrollDownPage;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> OpenSettings;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> ResizePane;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> MoveFocus;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> ToggleFullscreen;
void SetDispatch(ShortcutActionDispatch dispatch);
}
}
Loading

0 comments on commit 111b88c

Please sign in to comment.