Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.14] ShowWindow(SW_MINIMIZE) doesn't work anymore #13066

Closed
zadjii-msft opened this issue May 9, 2022 · 5 comments · Fixed by #13118 or #13129
Closed

[1.14] ShowWindow(SW_MINIMIZE) doesn't work anymore #13066

zadjii-msft opened this issue May 9, 2022 · 5 comments · Fixed by #13118 or #13129
Labels
Area-Windowing Window frame, quake mode, tearout Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Tag-Fix Doesn't match tag requirements Priority-0 Bugs that we consider release-blocking/recall-class (P0) Product-Conpty For console issues specifically related to conpty Product-Terminal The new Windows Terminal. Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release. Severity-Blocking We won't ship a release like this! No-siree.

Comments

@zadjii-msft
Copy link
Member

From bug bash.

  1. put in file:
$Source=@"
using System;
using System.Runtime.InteropServices;
public class Native {
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetConsoleWindow();
}
"@
Add-Type -TypeDefinition $Source
[Native]::ShowWindow([Native]::GetConsoleWindow(), 6)
  1. Run .\hide.ps1

expect:

  • See a dterm sequence for hiding the window

actual:

  • Terminal still visible.
@zadjii-msft zadjii-msft added Product-Conpty For console issues specifically related to conpty Issue-Bug It either shouldn't be doing this or needs an investigation. Product-Terminal The new Windows Terminal. Priority-0 Bugs that we consider release-blocking/recall-class (P0) Area-Windowing Window frame, quake mode, tearout labels May 9, 2022
@ghost ghost added the Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting label May 9, 2022
@zadjii-msft zadjii-msft added this to the Terminal v1.14 milestone May 9, 2022
@zadjii-msft zadjii-msft removed the Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting label May 9, 2022
@zadjii-msft
Copy link
Member Author

I was the murderer

image

69b77ca

That's a 1 line fix, but fixing that causes the window to flicker uncontrollably. Somewhere there's a mismatch between the Terminal HWND visibility and the ConPTY one, some flipped boolean somewhere.

@zadjii-msft zadjii-msft added the Severity-Blocking We won't ship a release like this! No-siree. label May 10, 2022
@zadjii-msft
Copy link
Member Author

zadjii-msft commented May 16, 2022

  • ✔️ 22fb15f - definitely works, no flashing.
  • 90d6fe7
  • 419551e
  • 5b59cf8
  • ✔️ 6bdc8c9
  • ⚠️ 69b77ca - the bad merge. Presumably, anything after here doesn't work, but maybe with the merge fixed they would?
    • Even with the fix to the merge, this causes the flashing. Everything after here would as well.
    • This was a merge with 26d67d9, which was Enable the Terminal to tell ConPTY who the owner is #12526.
    • ✔️ 479c6c9 was my branch where I thought I merged these all and it worked. That's basically a merge of (5923cf3, 23b6fee, and 5923cf3, the last of which was NOT the whole focus/foreground implementation, merely a prototype. Shiz. )
      • This commit DOES work. So somewhere between there and 69b77ca broke. Interesting.
  • d54e6d1
  • 9aa987f
  • f70c53d
  • 46b88e1
  • 3b9710a - didn't work, but neither does the test script so clearly I never tested that build. That's after the bad merge, so yea that doesn't work,
  • main

@zadjii-msft
Copy link
Member Author

zadjii-msft commented May 17, 2022

Hmm. It's the combo of 22fb15f with #12526 that causes this. That initial show/hide to sync the terminal with the conpty, that's what's busted somehow.

Merely commenting those lines fixes this. (see bbae235) HOWEVER, it breaks doing a [Native]::ShowWindow([Native]::GetConsoleWindow(), 0) (0==SW_HIDE) immediately in a fresh window. Trick here: the Terminal starts with WS_VISIBLE | !WS_MINIMIZE. The Conpty starts with !WS_VISIBLE | !WS_MINIMIZE. Somewhere early in startup, we get a WM_SHOWWINDOW(false, 0), and that then bounces with the terminal, because that results in the pty telling the terminal to hide.

  • ❔ I need to validate that [Native]::ShowWindow([Native]::GetConsoleWindow(), 0) did work between 2fb15f and 69b77ca
  • ✔️ Does removing the ConptyReparentPseudoConsole call instead fix this? Is the window getting hidden as a result of being reparented? I've determined that the window is 100% of the time created by ::ConsoleInputThreadProcWin32 in windowio.cpp, and that creates it without the owner hwnd.
    • It did, but the plot thickens.

@zadjii-msft
Copy link
Member Author

zadjii-msft commented May 17, 2022

The plot thickens. Remove the call to reparent, but leave the initial state sync in. Things seem to work. HOWEVER, if you minimize the Terminal, then restore it, the terminal doesn't get focus. I bet that the pty window is becoming WS_VISIBLE, which means it's now a valid target to become the foreground window, even though its dimensions are 0x0 and not drawn anywhere. Interesting.

  • ❌ try WS_EX_NOACTIVATE
  • try the cloaking thing again.

lmao

However, you can display the system menu by right-clicking or by typing ALT+SPACE.

Yep you can sure do that.

@ghost ghost added the In-PR This issue has a related PR label May 17, 2022
@ghost ghost added the Needs-Tag-Fix Doesn't match tag requirements label May 18, 2022
DHowett pushed a commit that referenced this issue May 18, 2022
A bad merge, that actually revealed a horrible bug.

There was a secret conflict between the code in #12526 and #12515. 69b77ca was a bad merge that hid just how bad the issue was. Fixing the one line `nullptr`->`this` in `InteractivityFactory` resulted in a window that would flash uncontrollably, as it minimized and restored itself in a loop. Great. 

This can seemingly be fixed by making sure that the conpty window is initially created with the owner already set, rather than relying on a `SetParent` call in post. This does pose some complications for the #1256 future we're approaching. However, this is a blocking bug _now_, and we can figure out the tearout/`SetParent` thing in post. 

* fixes #13066.
* Tested with the script in that issue.
* Window doesn't flash uncontrollably.
* `gci | ogv` still works right
* I work here.
* Opening a new tab doesn't spontaneously cause the window to minimize
* Restoring from minimized doesn't yeet focus to an invisible window
* Opening a new tab doesn't yeet focus to an invisible window
* There _is_ a viable way to call `GetAncestor` s.t. it returns the Terminal's hwnd in Terminal, and the console's in Conhost


The `SW_SHOWNOACTIVATE` change is also quite load bearing. With just `SW_NORMAL`, the pseudo window (which is invisible!) gets activated whenever the terminal window is restored from minimized. That's BAD.


There's actually more to this as well. 


Calling `SetParent` on a window that is `WS_VISIBLE` will cause the OS to hide the window, make it a _child_ window, then call `SW_SHOW` on the window to re-show it. `SW_SHOW`, however, will cause the OS to also set that window as the _foreground_ window, which would result in the pty's hwnd stealing the foreground away from the owning terminal window. That's bad.

`SetWindowLongPtr` seems to do the job of changing who the window owner is, without all the other side effects of reparenting the window. 

Without `SetParent`, however, the pty HWND is no longer a descendant of the Terminal HWND, so that means `GA_ROOT` can no longer be used to find the owner's hwnd. For even more insanity, without `WS_POPUP`, none of the values of `GetAncestor` will actually get the terminal HWND. So, now we also need `WS_POPUP` on the pty hwnd. To get at the Terminal hwnd, you'll need

```c++
GetAncestor(GetConsoleWindow(), GA_ROOTOWNER)
```
@ghost ghost added Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release. and removed In-PR This issue has a related PR labels May 18, 2022
@zadjii-msft zadjii-msft reopened this May 19, 2022
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label May 19, 2022
@zadjii-msft
Copy link
Member Author

This doesn't work with DefTerm until the window is minimized and restored once. :|

zadjii-msft added a commit that referenced this issue May 19, 2022
Well this one feels dumb.

Make sure to also initially set the visibility of ConPTY windows created for DefTerm connections.

* [x] Closes #13066 for real.
* [x] tested manually.
@ghost ghost added In-PR This issue has a related PR and removed Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release. labels May 19, 2022
@ghost ghost removed the In-PR This issue has a related PR label May 19, 2022
DHowett pushed a commit that referenced this issue May 19, 2022
Well this one feels dumb.

Make sure to also initially set the visibility of ConPTY windows created for DefTerm connections.

* [x] Closes #13066 for real.
* [x] tested manually.
@ghost ghost added Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release. Needs-Tag-Fix Doesn't match tag requirements labels May 19, 2022
DHowett pushed a commit that referenced this issue May 19, 2022
A bad merge, that actually revealed a horrible bug.

There was a secret conflict between the code in #12526 and #12515. 69b77ca was a bad merge that hid just how bad the issue was. Fixing the one line `nullptr`->`this` in `InteractivityFactory` resulted in a window that would flash uncontrollably, as it minimized and restored itself in a loop. Great.

This can seemingly be fixed by making sure that the conpty window is initially created with the owner already set, rather than relying on a `SetParent` call in post. This does pose some complications for the #1256 future we're approaching. However, this is a blocking bug _now_, and we can figure out the tearout/`SetParent` thing in post.

* fixes #13066.
* Tested with the script in that issue.
* Window doesn't flash uncontrollably.
* `gci | ogv` still works right
* I work here.
* Opening a new tab doesn't spontaneously cause the window to minimize
* Restoring from minimized doesn't yeet focus to an invisible window
* Opening a new tab doesn't yeet focus to an invisible window
* There _is_ a viable way to call `GetAncestor` s.t. it returns the Terminal's hwnd in Terminal, and the console's in Conhost

The `SW_SHOWNOACTIVATE` change is also quite load bearing. With just `SW_NORMAL`, the pseudo window (which is invisible!) gets activated whenever the terminal window is restored from minimized. That's BAD.

There's actually more to this as well.

Calling `SetParent` on a window that is `WS_VISIBLE` will cause the OS to hide the window, make it a _child_ window, then call `SW_SHOW` on the window to re-show it. `SW_SHOW`, however, will cause the OS to also set that window as the _foreground_ window, which would result in the pty's hwnd stealing the foreground away from the owning terminal window. That's bad.

`SetWindowLongPtr` seems to do the job of changing who the window owner is, without all the other side effects of reparenting the window.

Without `SetParent`, however, the pty HWND is no longer a descendant of the Terminal HWND, so that means `GA_ROOT` can no longer be used to find the owner's hwnd. For even more insanity, without `WS_POPUP`, none of the values of `GetAncestor` will actually get the terminal HWND. So, now we also need `WS_POPUP` on the pty hwnd. To get at the Terminal hwnd, you'll need

```c++
GetAncestor(GetConsoleWindow(), GA_ROOTOWNER)
```

(cherry picked from commit 77215d9)
Service-Card-Id: 82170678
Service-Version: 1.14
DHowett pushed a commit that referenced this issue May 19, 2022
Well this one feels dumb.

Make sure to also initially set the visibility of ConPTY windows created for DefTerm connections.

* [x] Closes #13066 for real.
* [x] tested manually.

(cherry picked from commit d82af93)
Service-Card-Id: 82146739
Service-Version: 1.14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Windowing Window frame, quake mode, tearout Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Tag-Fix Doesn't match tag requirements Priority-0 Bugs that we consider release-blocking/recall-class (P0) Product-Conpty For console issues specifically related to conpty Product-Terminal The new Windows Terminal. Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release. Severity-Blocking We won't ship a release like this! No-siree.
Projects
None yet
1 participant