microsoft / terminal

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

Pseudoconsole hosting window position and size do not match the Terminal window #13525

Open PhMajerus opened 2 years ago

PhMajerus commented 2 years ago

Windows Terminal version

1.15.1863.0 (preview)

Windows build number

10.0.22621.232

Other Software

All Win32 CUI apps that use GUI windows (incl. popups and dialogs) with the console as the parent.

Steps to reproduce

So ConPTY now has pseudoconsole hosting windows for Win32 CUI apps to be able to get a handle using GetConsoleWindow and support some of the scenarios expected from a Windows console host.

A common scenario is for a CUI app to use some GUI elements such as messageboxes and dialogs, using the console HWND as the parent. Messageboxes as well as dialogs with the DS_CENTER flag automatically get centered on the monitor containing the parent window. Many apps will go further and try to center their dialogs over their parent window. This is important on multi-monitors system to have the new window visibly related to its parent instead of in the center of the primary/main display, making it easier to find or even to notice. It also improves productivity by minimizing the pointer movements required to reach related UI.

See for example the following code:

#include <Windows.h>

int main()
{
    HWND hwndParent = GetConsoleWindow();
    MessageBox(hwndParent, TEXT("This dialog should be centered on the screen where the console is."), TEXT("Demo dialog from CUI app"), MB_OK);
}

Currently, this works when using the legacy console as its window is properly positioned and sized, but always get displayed on the primary/main monitor when using the Terminal.

When using Terminal, the pseudoconsole hosting window is always located at 0,0 and sized 16,16.

This can be tested with the following code:

#include <Windows.h>
#include <stdio.h>

int main()
{
    HWND hwndParent = GetConsoleWindow();

    RECT rc;
    GetWindowRect(hwndParent, &rc);
    printf("coordinates %d,%d,%d,%d\r\n", rc.left, rc.top, rc.right, rc.bottom);
}

This makes screen-centered messageboxes and dialogs centered in the primary/main display instead of on the monitor where the Terminal is. Even worse, some apps trying to center their dialogs over their parent window do not handle desktop boundaries and will clip their dialogs by centering them at 8,8, making it difficult even to move them into view since their title bars are offscreen (unless user has negative coordinates monitors as part of their desktop).

Expected Behavior

Pseudoconsole hosting windows should be moved and sized to always match the Terminal window, so that apps relying on GetWindowRect get valid values. Note even the dialog manager and the MessageBox function are using those values to center popups and dialogs on the same display.

Actual Behavior

CUI apps relying on pseudoconsole hosting windows positions and sizes will not display messageboxes and dialogs in their intended location, and sometimes even partially offscreen.

Lo0oG commented 2 years ago

The following powershell script will also demonstrate the issue when you have multiple monitors. The message box will always be displayed on the primary monitor with Terminal whereas the legacy console will show the message box on the same window as the console.

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms");[System.Windows.Forms.Messagebox]::Show("Test")

carlos-zamora commented 2 years ago

Sorry for the radio silence here. Finally got a chance to talk about it as a team. This is something we definitely need a solution for, but we need to write a big spec for it considering that Windows Terminal supports panes and tabs (so that complicates things). Tagging up appropriately and throwing it in the "up next" milestone.