Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyboard shortcuts to increase and decrease font size #2700

Merged
merged 5 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/ActionArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
#include "SwitchToTabArgs.g.cpp"
#include "ResizePaneArgs.g.cpp"
#include "MoveFocusArgs.g.cpp"
#include "AdjustFontSizeArgs.g.cpp"
6 changes: 6 additions & 0 deletions src/cascadia/TerminalApp/ActionArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "SwitchToTabArgs.g.h"
#include "ResizePaneArgs.g.h"
#include "MoveFocusArgs.g.h"
#include "AdjustFontSizeArgs.g.h"

#include "../../cascadia/inc/cppwinrt_utils.h"

Expand Down Expand Up @@ -61,6 +62,11 @@ namespace winrt::TerminalApp::implementation
GETSET_PROPERTY(TerminalApp::Direction, Direction, TerminalApp::Direction::Left);
};

struct AdjustFontSizeArgs : public AdjustFontSizeArgsT<AdjustFontSizeArgs>
{
AdjustFontSizeArgs() = default;
GETSET_PROPERTY(int32_t, Delta, 0);
};
}

namespace winrt::TerminalApp::factory_implementation
Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/TerminalApp/ActionArgs.idl
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ namespace TerminalApp
Direction Direction { get; };
};

[default_interface] runtimeclass AdjustFontSizeArgs : IActionArgs
{
Int32 Delta { get; };
};
}
10 changes: 10 additions & 0 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,14 @@ namespace winrt::TerminalApp::implementation
}
}

void TerminalPage::_HandleAdjustFontSize(const IInspectable& /*sender*/,
const TerminalApp::ActionEventArgs& args)
{
if (const auto& realArgs = args.ActionArgs().try_as<TerminalApp::AdjustFontSizeArgs>())
{
const auto termControl = _GetFocusedControl();
termControl.AdjustFontSize(realArgs.Delta());
args.Handled(true);
}
}
}
16 changes: 16 additions & 0 deletions src/cascadia/TerminalApp/AppKeyBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,22 @@ namespace winrt::TerminalApp::implementation
_MoveFocusHandlers(*this, *eventArgs);
return eventArgs->Handled();
}
case ShortcutAction::IncreaseFontSize:
{
auto args = winrt::make_self<AdjustFontSizeArgs>();
args->Delta(1);
auto eventArgs = winrt::make_self<ActionEventArgs>(*args);
_AdjustFontSizeHandlers(*this, *eventArgs);
return eventArgs->Handled();
}
case ShortcutAction::DecreaseFontSize:
{
auto args = winrt::make_self<AdjustFontSizeArgs>();
args->Delta(-1);
auto eventArgs = winrt::make_self<ActionEventArgs>(*args);
_AdjustFontSizeHandlers(*this, *eventArgs);
return eventArgs->Handled();
}
default:
return false;
}
Expand Down
3 changes: 1 addition & 2 deletions src/cascadia/TerminalApp/AppKeyBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ namespace winrt::TerminalApp::implementation
TYPED_EVENT(PrevTab, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(SplitVertical, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(SplitHorizontal, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(IncreaseFontSize, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(DecreaseFontSize, TerminalApp::AppKeyBindings, TerminalApp::ActionEventArgs);
TYPED_EVENT(AdjustFontSize, 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);
Expand Down
3 changes: 1 addition & 2 deletions src/cascadia/TerminalApp/AppKeyBindings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ namespace TerminalApp
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> IncreaseFontSize;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> DecreaseFontSize;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> AdjustFontSize;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> ScrollUp;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> ScrollDown;
event Windows.Foundation.TypedEventHandler<AppKeyBindings, ActionEventArgs> ScrollUpPage;
Expand Down
7 changes: 7 additions & 0 deletions src/cascadia/TerminalApp/CascadiaSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,13 @@ void CascadiaSettings::_CreateDefaultKeybindings()
keyBindings.SetKeyBinding(ShortcutAction::SwitchToTab8,
KeyChord{ KeyModifiers::Alt | KeyModifiers::Ctrl,
static_cast<int>('9') });

keyBindings.SetKeyBinding(ShortcutAction::IncreaseFontSize,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only reservation is that as of #2515, the default keybindings aren't set here anymore. They should instead be included in src\cascadia\TerminalApp\defaults.json

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting conundrum here @zadjii-msft. To adjust text size / zoom using the mouse is ctrl + scroll. It would therefore, make sense if zoom via the keyboard was also ctrl + +/-.

What would also make THAT even more consistent with mouse scroll actions was if also ctrl + shift + +/- adjusted blur, but I may be pushing my luck here ... or am I? 😉

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, it looks like there's no VT encoding for Ctrl-+, so it's free to be bound! Problem, though: Ctrl+- emits ^_.
We also do need to be considerate of the fact that + is Shift+-...
Perhaps we should follow suit with other zoomable things and make Ctrl+= the "zoom in" binding?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Ctrl+Shift+- emits ^_ in putty and conhost, but Ctrl+- only emits ^_ in putty. Perhaps the right behavior is that Ctrl alone doesn't do anything and is free to be bound for both?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, like this then?

Key Chord Keys + Mouse Action
ctrl + =/- ctrl + mouse scroll up/down Zoom in/out
ctrl+shift + +/_ ctrl + shift + mouse scroll up/down Blur more/less

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the bindings to defaults.json as

{ "command": "decreaseFontSize", "keys": ["ctrl+-"] },
{ "command": "increaseFontSize", "keys": ["ctrl+="] }

If a different default is decided I'll be happy to change it.

KeyChord{ KeyModifiers::Ctrl,
VK_OEM_PLUS });
keyBindings.SetKeyBinding(ShortcutAction::DecreaseFontSize,
KeyChord{ KeyModifiers::Ctrl,
VK_OEM_MINUS });
}

// Method Description:
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ namespace winrt::TerminalApp::implementation
bindings.ResizePane({ this, &TerminalPage::_HandleResizePane });
bindings.MoveFocus({ this, &TerminalPage::_HandleMoveFocus });
bindings.CopyText({ this, &TerminalPage::_HandleCopyText });
bindings.AdjustFontSize({ this, &TerminalPage::_HandleAdjustFontSize });
}

// Method Description:
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/TerminalPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ namespace winrt::TerminalApp::implementation
void _HandleResizePane(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleMoveFocus(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleCopyText(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleAdjustFontSize(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
#pragma endregion
};
}
Expand Down
11 changes: 10 additions & 1 deletion src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,10 +981,19 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
void TermControl::_MouseZoomHandler(const double mouseDelta)
{
const auto fontDelta = mouseDelta < 0 ? -1 : 1;
AdjustFontSize(fontDelta);
}

// Method Description:
// - Adjust the font size of the terminal control.
// Arguments:
// - fontSizeDelta: The amount to increase or decrease the font size by.
void TermControl::AdjustFontSize(int fontSizeDelta)
{
try
{
// Make sure we have a non-zero font size
const auto newSize = std::max(gsl::narrow<short>(_desiredFont.GetEngineSize().Y + fontDelta), static_cast<short>(1));
const auto newSize = std::max(gsl::narrow<short>(_desiredFont.GetEngineSize().Y + fontSizeDelta), static_cast<short>(1));
const auto* fontFace = _settings.FontFace().c_str();
_actualFont = { fontFace, 0, 10, { 0, newSize }, CP_UTF8, false };
_desiredFont = { _actualFont };
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
int GetScrollOffset();
int GetViewHeight() const;

void AdjustFontSize(int fontSizeDelta);

void SwapChainChanged();
~TermControl();

Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/TermControl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ namespace Microsoft.Terminal.TerminalControl
Int32 GetScrollOffset();
Int32 GetViewHeight();
event ScrollPositionChangedEventArgs ScrollPositionChanged;

void AdjustFontSize(Int32 fontSizeDelta);
}
}