terminal/src/cascadia/TerminalApp/ShortcutActionDispatch.cpp
Mike Griese c09472347c
Add X Macro for fun and for profit (#9667)
**Summary of the Pull Request**

This PR adds an X Macro for defining our ShortcutActions. This means that you can add the action in one place, and have the macro synthesize all sorts of boilerplate for you!

From the `AllShortcutActions.h` file:

> For a clearer explanation of how this file should be used, see:
> https://en.wikipedia.org/wiki/X_Macro
>
> Include this file to be able to quickly define some code in the exact same
> way for _every single shortcut action_. To use:
>
> 1. Include this file
> 2. Define the ON_ALL_ACTIONS macro with what you want each action to show up
>    as. Ex:
>
>    #define ON_ALL_ACTIONS(action) void action##Handler();
>
> 3. Then, use the ALL_SHORTCUT_ACTIONS macro to get the ON_ALL_ACTIONS marcro
>    repeated once for every ShortcutAction
>
> This is used in KeyMapping.idl, ShortcutAction.*, TerminalPage.*, etc. to
> reduce the number of places where we must copy-paste boiler-plate code for
> each action. This is _NOT_ something that should be used when any individual
> case should be customized.

**PR Checklist**
* [x] Scratches an itch
* [x] I work here
* [x] Tests passed
* [n/a] Requires documentation to be updated

**Detailed Description of the Pull Request / Additional comments**

Originally I had this blocked as a follow up to #9662. However, I've grown tired after a month of merging main into this branch, and I'm just shipping it separately. It will inevitably conflict with anyone who has actions in flight currently.

**Validation Steps Performed**
The code still builds exactly as before!
2021-03-31 16:38:25 +00:00

54 lines
1.7 KiB
C++

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "pch.h"
#include "ShortcutActionDispatch.h"
#include "ShortcutActionDispatch.g.cpp"
using namespace winrt::Microsoft::Terminal;
using namespace winrt::Microsoft::Terminal::Settings::Model;
using namespace winrt::TerminalApp;
#define ACTION_CASE(action) \
case ShortcutAction::action: \
{ \
_##action##Handlers(*this, eventArgs); \
break; \
}
namespace winrt::TerminalApp::implementation
{
// Method Description:
// - Dispatch the appropriate event for the given ActionAndArgs. Constructs
// an ActionEventArgs to hold the IActionArgs payload for the event, and
// calls the matching handlers for that event.
// Arguments:
// - actionAndArgs: the ShortcutAction and associated args to raise an event for.
// Return Value:
// - true if we handled the event was handled, else false.
bool ShortcutActionDispatch::DoAction(const ActionAndArgs& actionAndArgs)
{
if (!actionAndArgs)
{
return false;
}
const auto& action = actionAndArgs.Action();
const auto& args = actionAndArgs.Args();
auto eventArgs = args ? ActionEventArgs{ args } :
ActionEventArgs{};
switch (action)
{
#define ON_ALL_ACTIONS(id) ACTION_CASE(id);
ALL_SHORTCUT_ACTIONS
#undef ON_ALL_ACTIONS
default:
return false;
}
return eventArgs.Handled();
}
}