Skip to content

Commit

Permalink
Teach terminal to hide mouse while typing (#8629)
Browse files Browse the repository at this point in the history
## PR Checklist
* [x] Closes #5699
* [x] CLA signed. 
* [ ] Tests added/passed
* [ ] Documentation updated. 
* [ ] Schema updated.
* [ ] I've discussed this with core contributors already. 

## Detailed Description of the Pull Request / Additional comments
* Use SPI_GETMOUSEVANISH setting
* Hide upon CharacterReceived
* Unhide upon PointerMoved, PointerMoved, MouseWheel, LoseFocus
  • Loading branch information
Don-Vito authored Jan 21, 2021
1 parent 3b53c69 commit 9c4950c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
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();
}

// 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);

// 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

0 comments on commit 9c4950c

Please sign in to comment.