microsoft / terminal

The new Windows Terminal and the original Windows console host, all in the same place!
MIT License
95.57k stars 8.31k forks source link

Windows Terminal Preview scroll bar doesn't respect the buffer size on Windows 11. #18148

Open BDisp opened 5 hours ago

BDisp commented 5 hours ago

We should continue this discussion in another issue, since there are over 20 people subscribed to this one. Do you mind filing an issue? It would be great if you could include reproduction steps.

BTW: I'm also confused why you mention the need to resize the buffer. The terminal resizes the buffer for you when the window size changes. There should be no need for you to call anything (nor use CSI t) during a maximize/restore. The alternate screen buffer always has the size of the window.

Originally posted by @lhecker in #5094

BDisp commented 5 hours ago

@lhecker thanks for the clarification. Yes it's true. Now the window buffer size always have the window size, but the scroll bar doesn't reflect that. When the buffer size is equal to the window size the scroll bar size should be zero and not allowing scrolling the buffer. If use my changed branch https://github.com/gui-cs/Terminal.Gui/pull/3768 the buffer size always reflects the window size but not the scroll bar.

Image

BDisp commented 5 hours ago

In the WT version 1.21 the change I made to my branch is working fine only using the \x1b[?1049h and \x1b[?1049l. which in the gui-cs are named as EscSeqUtils.CSI_SaveCursorAndActivateAltBufferNoBackscroll and EscSeqUtils.CSI_RestoreCursorAndRestoreAltBufferWithBackscroll, respectively.

lhecker commented 5 hours ago

I just remembered that we had another gui-cs issues reported on us a while ago: #17949 The summary is that gui-cs has various imperfections and bugs around the usage of the Console API. One of the bugs is that it calls SetConsoleScreenBufferSize with the information gained from GetConsoleScreenBufferInfoEx without realizing that this results in an off-by-one error.

Generally speaking: Do not ever resize the buffer because the window was resized. This is true without the alternate screen-buffer but it's especially true if you do use it.

I'll continue to investigate.

lhecker commented 4 hours ago

tl;dr: When you use SetConsoleActiveScreenBuffer you leave the alternate screen-buffer.

The issue is caused due to the usage of SetConsoleActiveScreenBuffer. That function used to have a pretty much undefined (random) behavior up until 1.21. Starting 1.22 it does what it's supposed to do: It switches the console screen buffer. But doing so means that you also leave the previously set alternate screen-buffer (ASB). This causes a scrollbar to show up because you aren't in the ASB anymore.

I would recommend removing all usage of the _screenBuffer member variable. While it's possible that I misunderstood your code, I don't think it should be used in the way you're doing it. Most functions can just use _outputHandle and _inputHandle directly. I don't think I understand the purpose of ReadFromConsoleOutput. Every time it gets called, it creates an entire, new text buffer (= very very expensive) and then reads from it. This will always return whitespace because there's nothing in that new text buffer. You should probably pass the _outputHandle there.

Let me know if I can help any other way.

BDisp commented 3 hours ago

Thank you very much. Removing the _screenBuffer variable and only using the _outputHandle variable in conjunction with the alternated screen solves the issue. I really appreciated your help. If you want please see my changed PR.