Fixing increase + decrease font size (#3629)

<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? -->
## Summary of the Pull Request
Fixes #3604 where Increase/Decrease font size bindings were not working.

<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist
* [x] Closes #3604
* [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA

<!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed
Increase and decrease font size works once again!
-------------------------------------

* adding FromJson to AdjustFontSizeArgs

* made a legacy function that just allows you to do 1/-1 delta for adjusting font size

* adding test case

* removing extra quotes

* comments lmao

* FORMATTING WHY
This commit is contained in:
Leon Liang 2019-11-20 07:45:43 -08:00 committed by Mike Griese
parent d1e9de7882
commit 0c2ae7015f
3 changed files with 64 additions and 2 deletions

View file

@ -196,7 +196,10 @@ namespace TerminalAppLocalTests
{ "command": "newTabProfile8", "keys": ["alt+shift+y"] },
{ "command": { "action": "copy", "madeUpBool": true }, "keys": ["ctrl+b"] },
{ "command": { "action": "copy" }, "keys": ["ctrl+shift+b"] }
{ "command": { "action": "copy" }, "keys": ["ctrl+shift+b"] },
{ "command": "decreaseFontSize", "keys": ["ctrl+-"] },
{ "command": "increaseFontSize", "keys": ["ctrl+="] }
])" };
@ -206,7 +209,7 @@ namespace TerminalAppLocalTests
VERIFY_IS_NOT_NULL(appKeyBindings);
VERIFY_ARE_EQUAL(0u, appKeyBindings->_keyShortcuts.size());
appKeyBindings->LayerJson(bindings0Json);
VERIFY_ARE_EQUAL(11u, appKeyBindings->_keyShortcuts.size());
VERIFY_ARE_EQUAL(13u, appKeyBindings->_keyShortcuts.size());
{
Log::Comment(NoThrowString().Format(
@ -336,6 +339,30 @@ namespace TerminalAppLocalTests
// Verify the args have the expected value
VERIFY_IS_FALSE(realArgs.TrimWhitespace());
}
{
Log::Comment(NoThrowString().Format(
L"Verify that `increaseFontSize` without args parses as AdjustFontSize(Delta=1)"));
KeyChord kc{ false, true, false, static_cast<int32_t>('=') };
auto actionAndArgs = GetActionAndArgs(*appKeyBindings, kc);
VERIFY_ARE_EQUAL(ShortcutAction::IncreaseFontSize, actionAndArgs.Action());
const auto& realArgs = actionAndArgs.Args().try_as<AdjustFontSizeArgs>();
VERIFY_IS_NOT_NULL(realArgs);
// Verify the args have the expected value
VERIFY_ARE_EQUAL(1, realArgs.Delta());
}
{
Log::Comment(NoThrowString().Format(
L"Verify that `decreaseFontSize` without args parses as AdjustFontSize(Delta=-1)"));
KeyChord kc{ false, true, false, static_cast<int32_t>('-') };
auto actionAndArgs = GetActionAndArgs(*appKeyBindings, kc);
VERIFY_ARE_EQUAL(ShortcutAction::DecreaseFontSize, actionAndArgs.Action());
const auto& realArgs = actionAndArgs.Args().try_as<AdjustFontSizeArgs>();
VERIFY_IS_NOT_NULL(realArgs);
// Verify the args have the expected value
VERIFY_ARE_EQUAL(-1, realArgs.Delta());
}
}
}

View file

@ -172,6 +172,20 @@ namespace winrt::TerminalApp::implementation
{
AdjustFontSizeArgs() = default;
GETSET_PROPERTY(int32_t, Delta, 0);
static constexpr std::string_view AdjustFontSizeDelta{ "delta" };
public:
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;
}
};
}

View file

@ -239,6 +239,24 @@ IActionArgs LegacyParseCopyTextWithoutNewlinesArgs(const Json::Value& /*json*/)
return *args;
};
// Function Description:
// - Used to generate a AdjustFontSizeArgs for IncreaseFontSize/DecreaseFontSize
// actions with a delta of 1/-1.
// - TODO: GH#1069 Remove this before 1.0, and force an upgrade to the new args.
// Arguments:
// - delta: the font size delta to create the parse function for.
// Return Value:
// - A function that can be used to "parse" json into an AdjustFontSizeArgs.
std::function<IActionArgs(const Json::Value&)> LegacyParseAdjustFontSizeArgs(int delta)
{
auto pfn = [delta](const Json::Value & /*value*/) -> IActionArgs {
auto args = winrt::make_self<winrt::TerminalApp::implementation::AdjustFontSizeArgs>();
args->Delta(delta);
return *args;
};
return pfn;
}
// This is a map of ShortcutAction->function<IActionArgs(Json::Value)>. It holds
// a set of deserializer functions that can be used to deserialize a IActionArgs
// from json. Each type of IActionArgs that can accept arbitrary args should be
@ -282,6 +300,9 @@ static const std::map<ShortcutAction, std::function<IActionArgs(const Json::Valu
{ ShortcutAction::MoveFocusUp, LegacyParseMoveFocusArgs(Direction::Up) },
{ ShortcutAction::MoveFocusDown, LegacyParseMoveFocusArgs(Direction::Down) },
{ ShortcutAction::DecreaseFontSize, LegacyParseAdjustFontSizeArgs(-1) },
{ ShortcutAction::IncreaseFontSize, LegacyParseAdjustFontSizeArgs(1) },
{ ShortcutAction::Invalid, nullptr },
};