Skip to content

Commit

Permalink
Fix #670: Terminal selects a char when bringing window to foreground (#…
Browse files Browse the repository at this point in the history
…856)

* Added focus tracking to TermControl to prevent clicks which refocus the terminal window from selecting text.

* Moved open brace to a new line per repo code style.

* Moved the TermControl's _MouseClickHandler's focus check into the Mouse specific block of code. This lets any touch and drag events scroll the terminal's contents.

Fixes #670.
  • Loading branch information
mblowey authored and DHowett committed May 20, 2019
1 parent a0ebd2e commit 9f4ad6d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,16 +415,16 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
_cursorTimer = std::make_optional(DispatcherTimer());
_cursorTimer.value().Interval(std::chrono::milliseconds(blinkTime));
_cursorTimer.value().Tick({ this, &TermControl::_BlinkCursor });

_controlRoot.GotFocus({ this, &TermControl::_GotFocusHandler });
_controlRoot.LostFocus({ this, &TermControl::_LostFocusHandler });
}
else
{
// The user has disabled cursor blinking
_cursorTimer = std::nullopt;
}

_controlRoot.GotFocus({ this, &TermControl::_GotFocusHandler });
_controlRoot.LostFocus({ this, &TermControl::_LostFocusHandler });

// Focus the control here. If we do it up above (in _Create_), then the
// focus won't actually get passed to us. I believe this is because
// we're not technically a part of the UI tree yet, so focusing us
Expand Down Expand Up @@ -549,6 +549,15 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation

if (ptr.PointerDeviceType() == Windows::Devices::Input::PointerDeviceType::Mouse)
{
// Ignore mouse events while the terminal does not have focus.
// This prevents the user from selecting and copying text if they
// click inside the current tab to refocus the terminal window.
if (!_focused)
{
args.Handled(true);
return;
}

const auto modifiers = args.KeyModifiers();
const auto altEnabled = WI_IsFlagSet(modifiers, VirtualKeyModifiers::Menu);
const auto shiftEnabled = WI_IsFlagSet(modifiers, VirtualKeyModifiers::Shift);
Expand Down Expand Up @@ -828,6 +837,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
void TermControl::_GotFocusHandler(Windows::Foundation::IInspectable const& /* sender */,
RoutedEventArgs const& /* args */)
{
_focused = true;

if (_cursorTimer.has_value())
_cursorTimer.value().Start();
}
Expand All @@ -838,6 +849,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
void TermControl::_LostFocusHandler(Windows::Foundation::IInspectable const& /* sender */,
RoutedEventArgs const& /* args */)
{
_focused = false;

if (_cursorTimer.has_value())
{
_cursorTimer.value().Stop();
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
std::unique_ptr<::Microsoft::Console::Render::DxEngine> _renderEngine;

Settings::IControlSettings _settings;
bool _focused;
bool _closing;

FontInfoDesired _desiredFont;
Expand Down

0 comments on commit 9f4ad6d

Please sign in to comment.