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

Teach terminal to hide mouse while typing #8629

Merged
8 commits merged into from
Jan 21, 2021
2 changes: 2 additions & 0 deletions .github/actions/spell-check/expect/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ GETLBTEXT
getline
GETMINMAXINFO
GETMOUSEINFO
GETMOUSEVANISH
GETNUMBEROFFONTS
GETNUMBEROFINPUTEVENTS
GETOBJECT
Expand Down Expand Up @@ -2846,3 +2847,4 @@ zsh
zu
zxcvbnm
zy

36 changes: 36 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ namespace winrt::TerminalApp::implementation
_layoutUpdatedRevoker = _tabContent.LayoutUpdated(winrt::auto_revoke, { this, &TerminalPage::_OnFirstLayout });

_isAlwaysOnTop = _settings.GlobalSettings().AlwaysOnTop();

// Setup mouse vanish attributes
SystemParametersInfoW(SPI_GETMOUSEVANISH, 0, &_shouldMouseVanish, false);

// Store cursor, so we can restore it, e.g., after mouse vanishing
// (we'll need to adapt this logic once we make cursor context aware)
_defaultPointerCursor = CoreWindow::GetForCurrentThread().PointerCursor();
DHowett marked this conversation as resolved.
Show resolved Hide resolved
}

// Method Description:
Expand Down Expand Up @@ -1260,6 +1267,9 @@ namespace winrt::TerminalApp::implementation
// Add an event handler for when the terminal wants to set a progress indicator on the taskbar
term.SetTaskbarProgress({ this, &TerminalPage::_SetTaskbarProgressHandler });

term.HidePointerCursor({ this, &TerminalPage::_HidePointerCursorHandler });
term.RestorePointerCursor({ this, &TerminalPage::_RestorePointerCursorHandler });

// Bind Tab events to the TermControl and the Tab's Pane
hostingTab.Initialize(term);

Expand Down Expand Up @@ -3014,6 +3024,32 @@ namespace winrt::TerminalApp::implementation
return text;
}

// Method Description:
// - Hides cursor if required
// Return Value:
// - <none>
void TerminalPage::_HidePointerCursorHandler(const IInspectable& /*sender*/, const IInspectable& /*eventArgs*/)
{
if (_shouldMouseVanish && !_isMouseHidden)
{
CoreWindow::GetForCurrentThread().PointerCursor(nullptr);
_isMouseHidden = true;
}
}

// Method Description:
// - Restores cursor if required
// Return Value:
// - <none>
void TerminalPage::_RestorePointerCursorHandler(const IInspectable& /*sender*/, const IInspectable& /*eventArgs*/)
{
if (_isMouseHidden)
{
CoreWindow::GetForCurrentThread().PointerCursor(_defaultPointerCursor);
_isMouseHidden = false;
}
}

// -------------------------------- WinRT Events ---------------------------------
// Winrt events need a method for adding a callback to the event and removing the callback.
// These macros will define them both for you.
Expand Down
6 changes: 6 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ namespace winrt::TerminalApp::implementation

void _TryMoveTab(const uint32_t currentTabIndex, const int32_t suggestedNewTabIndex);

bool _shouldMouseVanish{ false };
bool _isMouseHidden{ false };
Windows::UI::Core::CoreCursor _defaultPointerCursor{ nullptr };
void _HidePointerCursorHandler(const IInspectable& sender, const IInspectable& eventArgs);
void _RestorePointerCursorHandler(const IInspectable& sender, const IInspectable& eventArgs);

#pragma region ActionHandlers
// These are all defined in AppActionHandlers.cpp
void _HandleOpenNewTabDropdown(const IInspectable& sender, const Microsoft::Terminal::Settings::Model::ActionEventArgs& args);
Expand Down
12 changes: 12 additions & 0 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
return;
}

_HidePointerCursorHandlers(*this, nullptr);

const auto ch = e.Character();
const auto scanCode = gsl::narrow_cast<WORD>(e.KeyStatus().ScanCode);
auto modifiers = _GetPressedModifierKeys();
Expand Down Expand Up @@ -1213,6 +1215,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
return;
}

_RestorePointerCursorHandlers(*this, nullptr);

_CapturePointer(sender, args);

const auto ptr = args.Pointer();
Expand Down Expand Up @@ -1342,6 +1346,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
return;
}

_RestorePointerCursorHandlers(*this, nullptr);

const auto ptr = args.Pointer();
const auto point = args.GetCurrentPoint(*this);
const auto cursorPosition = point.Position();
Expand Down Expand Up @@ -1549,6 +1555,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
return;
}

_RestorePointerCursorHandlers(*this, nullptr);

const auto point = args.GetCurrentPoint(*this);
const auto props = point.Properties();
const TerminalInput::MouseButtonState state{ props.IsLeftButtonPressed(), props.IsMiddleButtonPressed(), props.IsRightButtonPressed() };
Expand Down Expand Up @@ -1980,6 +1988,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
return;
}

_RestorePointerCursorHandlers(*this, nullptr);

_focused = false;

if (_uiaEngine.get())
Expand Down Expand Up @@ -2572,6 +2582,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
{
if (!_closing.exchange(true))
{
_RestorePointerCursorHandlers(*this, nullptr);
Copy link
Member

Choose a reason for hiding this comment

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

hmm.. will this restore the cursor to pointer if a terminal closes while a different one is the one that hid it? The sleep 5; exit scenario, but you go type in a different terminal tab while it's waiting to beef it

Copy link
Contributor Author

@Don-Vito Don-Vito Jan 20, 2021

Choose a reason for hiding this comment

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

Once again this user who wants to destroy terminal? 😄
The restore is managed on the TerminalPage level - so we won't need to remember who hid it.
If I don't miss anything, in the worst case we will get mouse restored.. while typing.. and then it will be hidden again.

Copy link
Member

Choose a reason for hiding this comment

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

Let's leave it like this. This will be okay for now  😄

Copy link
Member

Choose a reason for hiding this comment

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

user who wants to destroy terminal

You'd be surprised! Look for posts by vefatica here 😉 and there's also one Microsoft person who keeps reporting bugs in multibyte character handling because he apparently just runs a stress testing program that emits random code units and then he actively resizes the window while it's doing that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You'd be surprised! Look for posts by vefatica here 😉 and there's also one Microsoft person who keeps reporting bugs in multibyte character handling because he apparently just runs a stress testing program that emits random code units and then he actively resizes the window while it's doing that

After few months following the tracker I am not surprised with anything as the creativity is limitless 😊 And I am a big fan of vefatica quite for a while - even before he found the handle leak.


// Stop accepting new output and state changes before we disconnect everything.
_connection.TerminalOutput(_connectionOutputEventToken);
_connectionStateChangedRevoker.revoke();
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 @@ -180,6 +180,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
TYPED_EVENT(ConnectionStateChanged, TerminalControl::TermControl, IInspectable);
TYPED_EVENT(Initialized, TerminalControl::TermControl, Windows::UI::Xaml::RoutedEventArgs);
TYPED_EVENT(TabColorChanged, IInspectable, IInspectable);
TYPED_EVENT(HidePointerCursor, IInspectable, IInspectable);
TYPED_EVENT(RestorePointerCursor, IInspectable, IInspectable);
// clang-format on

private:
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 @@ -77,6 +77,8 @@ namespace Microsoft.Terminal.TerminalControl
event Windows.Foundation.TypedEventHandler<TermControl, Object> SetTaskbarProgress;
event Windows.Foundation.TypedEventHandler<TermControl, NoticeEventArgs> RaiseNotice;
event Windows.Foundation.TypedEventHandler<Object, Object> WarningBell;
event Windows.Foundation.TypedEventHandler<Object, Object> HidePointerCursor;
event Windows.Foundation.TypedEventHandler<Object, Object> RestorePointerCursor;

event Windows.Foundation.TypedEventHandler<TermControl, Windows.UI.Xaml.RoutedEventArgs> Initialized;
// This is an event handler forwarder for the underlying connection.
Expand Down