microsoft/Terminal

Automatic scrolling of window when setting cursor position

Open

#14,774 创建于 2023年2月2日

在 GitHub 查看
 (5 评论) (2 反应) (0 负责人)C++ (3,212 fork)batch import
Area-OutputArea-ServerHelp WantedIssue-BugPriority-1Product-Conhost

仓库指标

Star
 (35,764 star)
PR 合并指标
 (平均合并 27天 19小时) (30 天内合并 24 个 PR)

描述

Windows Terminal version

1.16.230126001

Windows build number

10.0.19044.2486

Other Software

No response

Steps to reproduce

I mentioned this first in #14759.

You can reproduce the problem with this test program:

#include <windows.h>

int main()
{
  HANDLE con = GetStdHandle(STD_OUTPUT_HANDLE);
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  GetConsoleScreenBufferInfo(con, &csbi);

  // use scrollback of 100 lines
  COORD con_size = {
    csbi.srWindow.Right - csbi.srWindow.Left + 1,
    csbi.srWindow.Bottom - csbi.srWindow.Top + 1 + 100
  };
  SetConsoleScreenBufferSize(con, con_size);

  // write 50 lines so the position is not at the top of the scrollback
  for (int i = 0; i < 50; i++)
    WriteConsoleA(con, "line\n", 5, NULL, NULL);

  // stop at escape key
  HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
  INPUT_RECORD ir;
  DWORD didread;
  while (ReadConsoleInput(in, &ir, 1, &didread))
  {
    if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown)
    {
      if (ir.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
        break;

      WriteConsoleA(con, &ir.Event.KeyEvent.uChar.AsciiChar, 1, NULL, NULL);
      GetConsoleScreenBufferInfo(con, &csbi);
      // re-set the cursor position where it already is, this is done
      // so the cursor is visible at the new location after the character
      // was printed
      SetConsoleCursorPosition(con, csbi.dwCursorPosition);
      // now the console window is scrolled so that the cursor is at the bottom
      // of the window.
    }
  }

  return 0;
}

There is a difference of behavior when tried with either the conhost.exe of the current Win10, or the OpenConsole.exe of Microsoft.WindowsTerminal_Win10_1.16.10261.0_8wekyb3d8bbwe.msixbundle.

Between entering some keys, scroll the window up or down with the mouse.

Expected Behavior

Once you enter a key, and the cursor position is re-set, it will scroll the window to the cursor, but only if the cursor was not already in the visible part of the window. Which is exactly how it works in conhost.exe.

Actual Behavior

But with OpenConsole.exe 1.16.230126001, the window is always scrolled so that the cursor is at the bottom of the screen, even if it was already visible before.

贡献者指南