terminal/src/cascadia/TerminalApp/ActionArgs.h
Leon Liang 99a28f9e9e
Convert Tab to a WinRT type (#4350)
## Summary of the Pull Request
This PR will make the existing `Tab` class into a WinRT type. This will allow any XAML to simply bind to the `ObservableVector` of Tabs. 

This PR will be followed up with a future PR to change our TabView to use the ObservableVector, which will in turn eliminate the need for maintaining two vectors of Tabs. (We currently maintain `_tabs` in `TerminalPage` and we also maintain `TabView().TabViewItems()` at the same time as described here: #2740)

## References
#3922 

## PR Checklist
* [x] CLA signed.
* [x] Tests added/passed

## Detailed Description of the Pull Request / Additional comments
I've currently only exposed a Tab's Title and IconPath to keep things simple. I foresee XAML elements that bind to Tabs to only really need these two properties for displaying.

I've also converted `TerminalPage`'s `std::vector<std::shared_ptr> _tabs` into a `IObservableVector<winrt::TerminalPage::Tab> _tabs` just so that future PRs will have the ground set for binding to this vector of tabs.

## Validation Steps Performed
Played around with Tabs and Panes and all sorts of combinations of keybindings for interacting with tabs and dragging and whatnot, it all seemed fine! Tab Tests also all pass.
2020-02-04 21:51:11 +00:00

355 lines
13 KiB
C++

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#pragma once
// HEY YOU: When adding ActionArgs types, make sure to add the corresponding
// *.g.cpp to ActionArgs.cpp!
#include "ActionEventArgs.g.h"
#include "NewTerminalArgs.g.h"
#include "CopyTextArgs.g.h"
#include "NewTabArgs.g.h"
#include "SwitchToTabArgs.g.h"
#include "ResizePaneArgs.g.h"
#include "MoveFocusArgs.g.h"
#include "AdjustFontSizeArgs.g.h"
#include "SplitPaneArgs.g.h"
#include "../../cascadia/inc/cppwinrt_utils.h"
#include "Utils.h"
// Notes on defining ActionArgs and ActionEventArgs:
// * All properties specific to an action should be defined as an ActionArgs
// class that implements IActionArgs
// * ActionEventArgs holds a single IActionArgs. For events that don't need
// additional args, this can be nullptr.
namespace winrt::TerminalApp::implementation
{
struct ActionEventArgs : public ActionEventArgsT<ActionEventArgs>
{
ActionEventArgs() = default;
ActionEventArgs(const TerminalApp::IActionArgs& args) :
_ActionArgs{ args } {};
GETSET_PROPERTY(IActionArgs, ActionArgs, nullptr);
GETSET_PROPERTY(bool, Handled, false);
};
struct NewTerminalArgs : public NewTerminalArgsT<NewTerminalArgs>
{
NewTerminalArgs() = default;
GETSET_PROPERTY(winrt::hstring, Commandline, L"");
GETSET_PROPERTY(winrt::hstring, StartingDirectory, L"");
GETSET_PROPERTY(winrt::hstring, TabTitle, L"");
GETSET_PROPERTY(Windows::Foundation::IReference<int32_t>, ProfileIndex, nullptr);
GETSET_PROPERTY(winrt::hstring, Profile, L"");
static constexpr std::string_view CommandlineKey{ "commandline" };
static constexpr std::string_view StartingDirectoryKey{ "startingDirectory" };
static constexpr std::string_view TabTitleKey{ "tabTitle" };
static constexpr std::string_view ProfileIndexKey{ "index" };
static constexpr std::string_view ProfileKey{ "profile" };
public:
bool Equals(const winrt::TerminalApp::NewTerminalArgs& other)
{
return other.Commandline() == _Commandline &&
other.StartingDirectory() == _StartingDirectory &&
other.TabTitle() == _TabTitle &&
other.ProfileIndex() == _ProfileIndex &&
other.Profile() == _Profile;
};
static winrt::TerminalApp::NewTerminalArgs FromJson(const Json::Value& json)
{
// LOAD BEARING: Not using make_self here _will_ break you in the future!
auto args = winrt::make_self<NewTerminalArgs>();
if (auto commandline{ json[JsonKey(CommandlineKey)] })
{
args->_Commandline = winrt::to_hstring(commandline.asString());
}
if (auto startingDirectory{ json[JsonKey(StartingDirectoryKey)] })
{
args->_StartingDirectory = winrt::to_hstring(startingDirectory.asString());
}
if (auto tabTitle{ json[JsonKey(TabTitleKey)] })
{
args->_TabTitle = winrt::to_hstring(tabTitle.asString());
}
if (auto index{ json[JsonKey(ProfileIndexKey)] })
{
args->_ProfileIndex = index.asInt();
}
if (auto profile{ json[JsonKey(ProfileKey)] })
{
args->_Profile = winrt::to_hstring(profile.asString());
}
return *args;
}
};
struct CopyTextArgs : public CopyTextArgsT<CopyTextArgs>
{
CopyTextArgs() = default;
GETSET_PROPERTY(bool, TrimWhitespace, true);
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!
auto args = winrt::make_self<CopyTextArgs>();
if (auto trimWhitespace{ json[JsonKey(TrimWhitespaceKey)] })
{
args->_TrimWhitespace = trimWhitespace.asBool();
}
return *args;
}
};
struct NewTabArgs : public NewTabArgsT<NewTabArgs>
{
NewTabArgs() = default;
GETSET_PROPERTY(winrt::TerminalApp::NewTerminalArgs, TerminalArgs, nullptr);
public:
bool Equals(const IActionArgs& other)
{
auto otherAsUs = other.try_as<NewTabArgs>();
if (otherAsUs)
{
return otherAsUs->_TerminalArgs.Equals(_TerminalArgs);
}
return false;
};
static winrt::TerminalApp::IActionArgs FromJson(const Json::Value& json)
{
// LOAD BEARING: Not using make_self here _will_ break you in the future!
auto args = winrt::make_self<NewTabArgs>();
args->_TerminalArgs = NewTerminalArgs::FromJson(json);
return *args;
}
};
struct SwitchToTabArgs : public SwitchToTabArgsT<SwitchToTabArgs>
{
SwitchToTabArgs() = default;
GETSET_PROPERTY(uint32_t, TabIndex, 0);
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!
auto args = winrt::make_self<SwitchToTabArgs>();
if (auto tabIndex{ json[JsonKey(TabIndexKey)] })
{
args->_TabIndex = tabIndex.asUInt();
}
return *args;
}
};
// Possible Direction values
// TODO:GH#2550/#3475 - move these to a centralized deserializing place
static constexpr std::string_view LeftString{ "left" };
static constexpr std::string_view RightString{ "right" };
static constexpr std::string_view UpString{ "up" };
static constexpr std::string_view DownString{ "down" };
// Function Description:
// - Helper function for parsing a Direction from a string
// Arguments:
// - directionString: the string to attempt to parse
// Return Value:
// - The encoded Direction value, or Direction::None if it was an invalid string
static TerminalApp::Direction ParseDirection(const std::string& directionString)
{
if (directionString == LeftString)
{
return TerminalApp::Direction::Left;
}
else if (directionString == RightString)
{
return TerminalApp::Direction::Right;
}
else if (directionString == UpString)
{
return TerminalApp::Direction::Up;
}
else if (directionString == DownString)
{
return TerminalApp::Direction::Down;
}
// default behavior for invalid data
return TerminalApp::Direction::None;
};
struct ResizePaneArgs : public ResizePaneArgsT<ResizePaneArgs>
{
ResizePaneArgs() = default;
GETSET_PROPERTY(TerminalApp::Direction, Direction, TerminalApp::Direction::None);
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!
auto args = winrt::make_self<ResizePaneArgs>();
if (auto directionString{ json[JsonKey(DirectionKey)] })
{
args->_Direction = ParseDirection(directionString.asString());
}
return *args;
}
};
struct MoveFocusArgs : public MoveFocusArgsT<MoveFocusArgs>
{
MoveFocusArgs() = default;
GETSET_PROPERTY(TerminalApp::Direction, Direction, TerminalApp::Direction::None);
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!
auto args = winrt::make_self<MoveFocusArgs>();
if (auto directionString{ json[JsonKey(DirectionKey)] })
{
args->_Direction = ParseDirection(directionString.asString());
}
return *args;
}
};
struct AdjustFontSizeArgs : public AdjustFontSizeArgsT<AdjustFontSizeArgs>
{
AdjustFontSizeArgs() = default;
GETSET_PROPERTY(int32_t, Delta, 0);
static constexpr std::string_view AdjustFontSizeDelta{ "delta" };
public:
bool Equals(const IActionArgs& other)
{
auto otherAsUs = other.try_as<AdjustFontSizeArgs>();
if (otherAsUs)
{
return otherAsUs->_Delta == _Delta;
}
return false;
};
static winrt::TerminalApp::IActionArgs FromJson(const Json::Value& json)
{
// LOAD BEARING: Not using make_self here _will_ break you in the future!
auto args = winrt::make_self<AdjustFontSizeArgs>();
if (auto jsonDelta{ json[JsonKey(AdjustFontSizeDelta)] })
{
args->_Delta = jsonDelta.asInt();
}
return *args;
}
};
// Possible SplitState values
// TODO:GH#2550/#3475 - move these to a centralized deserializing place
static constexpr std::string_view VerticalKey{ "vertical" };
static constexpr std::string_view HorizontalKey{ "horizontal" };
static constexpr std::string_view AutomaticKey{ "auto" };
static TerminalApp::SplitState ParseSplitState(const std::string& stateString)
{
if (stateString == VerticalKey)
{
return TerminalApp::SplitState::Vertical;
}
else if (stateString == HorizontalKey)
{
return TerminalApp::SplitState::Horizontal;
}
else if (stateString == AutomaticKey)
{
return TerminalApp::SplitState::Automatic;
}
// default behavior for invalid data
return TerminalApp::SplitState::None;
};
struct SplitPaneArgs : public SplitPaneArgsT<SplitPaneArgs>
{
SplitPaneArgs() = default;
GETSET_PROPERTY(winrt::TerminalApp::SplitState, SplitStyle, winrt::TerminalApp::SplitState::None);
GETSET_PROPERTY(winrt::TerminalApp::NewTerminalArgs, TerminalArgs, nullptr);
static constexpr std::string_view SplitKey{ "split" };
public:
bool Equals(const IActionArgs& other)
{
auto otherAsUs = other.try_as<SplitPaneArgs>();
if (otherAsUs)
{
return otherAsUs->_SplitStyle == _SplitStyle &&
otherAsUs->_TerminalArgs == _TerminalArgs;
}
return false;
};
static winrt::TerminalApp::IActionArgs FromJson(const Json::Value& json)
{
// LOAD BEARING: Not using make_self here _will_ break you in the future!
auto args = winrt::make_self<SplitPaneArgs>();
args->_TerminalArgs = NewTerminalArgs::FromJson(json);
if (auto jsonStyle{ json[JsonKey(SplitKey)] })
{
args->_SplitStyle = ParseSplitState(jsonStyle.asString());
}
return *args;
}
};
}
namespace winrt::TerminalApp::factory_implementation
{
BASIC_FACTORY(ActionEventArgs);
BASIC_FACTORY(NewTerminalArgs);
}