Repository: microsoft/terminal (language: cpp)

Issue:
Suggestions pop up of the Japanese MS IME got pushed down after multiple commands
### Windows Terminal version

1.15.2282.0

### Windows build number

10.0.19044.1889

### Other Software

_No response_

### Steps to reproduce

- Entering multiple commands or just press Enter multiple times.
- Japanese MS IME's suggestions pop up got pushed down gradually.


### Expected Behavior

this doesn't happen on v1.13.10983.0
![terminal-1 13](https://user-images.githubusercontent.com/76022206/185293805-4758f650-f19c-4620-9bfc-145e1092dd1d.gif)

what's being typed is shown in the input area
![image](https://user-images.githubusercontent.com/76022206/185297843-05b1638e-1aa9-452f-8506-cd7d00d5d962.png)


### Actual Behavior

This happens on v1.14.1432.0 and above.

v1.15.2282.0
![terminal-1 15](https://user-images.githubusercontent.com/76022206/185293342-c0f0f26c-e434-4d45-8ec2-9de548cf436a.gif)

v1.14.1432.0
![terminal-1 14](https://user-images.githubusercontent.com/76022206/185293296-acb49d08-d9d2-423b-bbe8-ff84bf669f80.gif)

also, what's being typed isn't shown in the input area
![image](https://user-images.githubusercontent.com/76022206/185297901-899803e7-2bc5-400a-b63c-70a674c8d736.png)



Discussion / hints:
oh no, we must have lost a DIP<->Pixel conversion somewhere in #13677 or #13678. 
We may also (or instead) have mixed up a viewport -> buffer conversion.
I'm bisecting. :)

```
git bisect start v1.14.1432.0 29e97b618 src/cascadia
Bisecting: 111 revisions left to test after this (roughly 7 steps)
```
I may not be able to finish bisecting just yet. Here's the current bisect log:

```
# bad: [39c71d642179403d52b04a03de7014820ab85657] Debounce window state changes caused by the PTY (#13147)
# good: [29e97b61823297b414c41fc70725569a2acf32cf] Update readme to account for minversion bump (#12332)
git bisect start 'v1.14.1432.0' '29e97b618' 'src/cascadia'
# good: [73f02024d29cf558e42633f93e707631c306bf6f] AtlasEngine: Fix grayscale blending shader (#12734)
git bisect good 73f02024d29cf558e42633f93e707631c306bf6f
# good: [30383ccc2210e3e7d62323421748c6eedf4b5fdd] Revert "Add support for rich code navigation in GitHub (#12855)" (#12909)
git bisect good 30383ccc2210e3e7d62323421748c6eedf4b5fdd
```
This is weird, but bisect pointed to f4e0d9f2bd1f5da3a6c378453b90320fb6b1c542 (#13024)

It _did_ remove a copy of `Terminal::GetCursorPosition` that returned the position adjusted by the viewport offset from TerminalApi.cpp.

We may need to introduce something like that again -- or propagate knowledge of the viewport offset up to `ControlCore`?

(/cc @j4james)
Oh my god, I think we were just getting unimaginably lucky the whole time?

```
src\cascadia\TerminalCore\TerminalApi.cpp
53:til::point Terminal::GetCursorPosition()

src\cascadia\TerminalCore\terminalrenderdata.cpp
41:COORD Terminal::GetCursorPosition() const noexcept
```

There were two, and one of them was pulled when we were const-qualified.

James' PR removed the first one.

They had different return values...!
Sorry about that. I'll try and have a look on the weekend if someone else hasn't fixed it by then.
![supa-hot-loop-omg](https://user-images.githubusercontent.com/18356694/185605818-c19575bd-2d1e-4ecc-aebe-52d299dcb7a5.gif)



Fix patch:
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);
 


Test patch: