From 0c2ae7015fc581fcc384f9359a817351911bea6e Mon Sep 17 00:00:00 2001 From: Leon Liang <57155886+leonMSFT@users.noreply.github.com> Date: Wed, 20 Nov 2019 07:45:43 -0800 Subject: [PATCH] Fixing increase + decrease font size (#3629) ## Summary of the Pull Request Fixes #3604 where Increase/Decrease font size bindings were not working. ## PR Checklist * [x] Closes #3604 * [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA ## 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 --- .../KeyBindingsTests.cpp | 31 +++++++++++++++++-- src/cascadia/TerminalApp/ActionArgs.h | 14 +++++++++ .../AppKeyBindingsSerialization.cpp | 21 +++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/cascadia/LocalTests_TerminalApp/KeyBindingsTests.cpp b/src/cascadia/LocalTests_TerminalApp/KeyBindingsTests.cpp index ad1bae275..5c0dfd606 100644 --- a/src/cascadia/LocalTests_TerminalApp/KeyBindingsTests.cpp +++ b/src/cascadia/LocalTests_TerminalApp/KeyBindingsTests.cpp @@ -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('=') }; + auto actionAndArgs = GetActionAndArgs(*appKeyBindings, kc); + VERIFY_ARE_EQUAL(ShortcutAction::IncreaseFontSize, actionAndArgs.Action()); + const auto& realArgs = actionAndArgs.Args().try_as(); + 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('-') }; + auto actionAndArgs = GetActionAndArgs(*appKeyBindings, kc); + VERIFY_ARE_EQUAL(ShortcutAction::DecreaseFontSize, actionAndArgs.Action()); + const auto& realArgs = actionAndArgs.Args().try_as(); + VERIFY_IS_NOT_NULL(realArgs); + // Verify the args have the expected value + VERIFY_ARE_EQUAL(-1, realArgs.Delta()); + } } } diff --git a/src/cascadia/TerminalApp/ActionArgs.h b/src/cascadia/TerminalApp/ActionArgs.h index 0a1972d5c..0da4d6776 100644 --- a/src/cascadia/TerminalApp/ActionArgs.h +++ b/src/cascadia/TerminalApp/ActionArgs.h @@ -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(); + if (auto jsonDelta{ json[JsonKey(AdjustFontSizeDelta)] }) + { + args->_Delta = jsonDelta.asInt(); + } + return *args; + } }; } diff --git a/src/cascadia/TerminalApp/AppKeyBindingsSerialization.cpp b/src/cascadia/TerminalApp/AppKeyBindingsSerialization.cpp index c621a9d3b..cd9287c88 100644 --- a/src/cascadia/TerminalApp/AppKeyBindingsSerialization.cpp +++ b/src/cascadia/TerminalApp/AppKeyBindingsSerialization.cpp @@ -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 LegacyParseAdjustFontSizeArgs(int delta) +{ + auto pfn = [delta](const Json::Value & /*value*/) -> IActionArgs { + auto args = winrt::make_self(); + args->Delta(delta); + return *args; + }; + return pfn; +} + // This is a map of ShortcutAction->function. 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