From 19c3615963c1ed123227321bbdc8a2e556763564 Mon Sep 17 00:00:00 2001 From: Dustin Howett Date: Fri, 19 Aug 2022 17:10:26 -0500 Subject: [PATCH] [1.14] Use the viewport-relative cursor pos for CCore.CursorPosition In #13024, we removed `Terminal::GetCursorPosition` from TerminalCore. This has been widely regarded as a good move. Now, you might rightly be wondering: why didn't compilation immediately fail? Well. It turns out that there were _two_ copies of `GetCursorPosition`. One for `const Terminal`, and one for `Terminal`. This is important. `Terminal::GetCursorPosition()` returned the cursor position relative to the viewport. `Terminal::GetCursorPosition() const`, however, returns the cursor position in absolute. We removed the non-`const` one. Fortunately, thanks to the lookup rules for `const`-qualified members, this didn't matter. Code that called `GetCursorPosition()` still called `GetCursorPosition()`, and everything was fine. Except that part about the relative coordinates. That was not fine. The TSF control is the _only_ consumer of `ControlCore.CursorPosition`, and that was the _only_ consumer of relative-`GetCursorPosition()`. This commit restores equilibrium by introducing a new `GetViewportRelativeCursorPosition()` member to `Terminal` and switching over the only consumer of relative cursor position to use it. Closes #13769. --- src/cascadia/TerminalControl/ControlCore.cpp | 2 +- src/cascadia/TerminalCore/Terminal.cpp | 9 +++++++++ src/cascadia/TerminalCore/Terminal.hpp | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/cascadia/TerminalControl/ControlCore.cpp b/src/cascadia/TerminalControl/ControlCore.cpp index 218146b9ea3..1c832194c49 100644 --- a/src/cascadia/TerminalControl/ControlCore.cpp +++ b/src/cascadia/TerminalControl/ControlCore.cpp @@ -1419,7 +1419,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation } auto lock = _terminal->LockForReading(); - return til::point{ _terminal->GetCursorPosition() }.to_core_point(); + return _terminal->GetViewportRelativeCursorPosition().to_core_point(); } // This one's really pushing the boundary of what counts as "encapsulation". diff --git a/src/cascadia/TerminalCore/Terminal.cpp b/src/cascadia/TerminalCore/Terminal.cpp index 66b3e77bca5..bb8ba14161b 100644 --- a/src/cascadia/TerminalCore/Terminal.cpp +++ b/src/cascadia/TerminalCore/Terminal.cpp @@ -1462,3 +1462,12 @@ void Terminal::_updateUrlDetection() ClearPatternTree(); } } + +// Method Description: +// - Returns the position of the cursor relative to the active viewport +til::point Terminal::GetViewportRelativeCursorPosition() const noexcept +{ + const til::point absoluteCursorPosition{ GetCursorPosition() }; + const auto viewport{ _GetMutableViewport() }; + return absoluteCursorPosition - til::point{ viewport.Origin() }; +} diff --git a/src/cascadia/TerminalCore/Terminal.hpp b/src/cascadia/TerminalCore/Terminal.hpp index b88f79b653c..4db779ae1b9 100644 --- a/src/cascadia/TerminalCore/Terminal.hpp +++ b/src/cascadia/TerminalCore/Terminal.hpp @@ -84,6 +84,8 @@ class Microsoft::Terminal::Core::Terminal final : bool IsXtermBracketedPasteModeEnabled() const; std::wstring_view GetWorkingDirectory(); + til::point GetViewportRelativeCursorPosition() const noexcept; + // Write comes from the PTY and goes to our parser to be stored in the output buffer void Write(std::wstring_view stringView);