Repository: microsoft/terminal (language: cpp)

Issue:
[1.14] `ShowWindow(SW_MINIMIZE)` doesn't work anymore
From bug bash. 

1. put in file: 
```pwsh
$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)
```

2. Run `.\hide.ps1`

expect:
* See a dterm sequence for hiding the window

actual:
* Terminal still visible.


Discussion / hints:
I was the murderer

![image](https://user-images.githubusercontent.com/18356694/167620964-4a614678-d3b1-48a5-9130-628b9127f235.png)

69b77cae83f8b73dfcc71f99eb75c3db8a1572a9


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.
* ✔️ 22fb15f0a - definitely works, no flashing. 
* ❔ 90d6fe792b969ad1913088087a1fda555ef0bb9d
* ❔ 419551e39cb943eb0d067bfcc047b7ba8521eeee
* ❔ 5b59cf87232495312f829015631ae509f5268052
* ✔️ 6bdc8c928524111809798d9ef155b6cb87060f33
* ⚠️ 69b77cae83f8b73dfcc71f99eb75c3db8a1572a9 - 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 26d67d9c0af0a05b0e65aa76537006862d39535b, which was #12526.
  * ✔️ 479c6c9f08cf273768e64f9f7fbc74a2c231b815 was my branch where I thought I merged these all and it worked.  That's basically a merge of (5923cf3, 23b6fee1f0b1722a9f081e52b8f47eea45a0f709, 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 69b77cae83f8b73dfcc71f99eb75c3db8a1572a9 broke. Interesting. 
* ❔ d54e6d13ce606e42c03d1b4a485066b22eb33aa8
* ❔ 9aa987f6d581c7af7e7c19013310fca478abd34e
* ❔ f70c53db84664d911aeace245aa34d63b4619b38
* ❔ 46b88e189bc0b39c95556f9c1335fd7c4310a52b
* ❌ 3b9710a4a - 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`
Hmm. It's the combo of 22fb15f0a69f70a1b9115567a41b984273835f74 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 bbae2355c) 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)`](https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-showwindow), 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.
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. 

* [x] ❌ 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.
> This doesn't work with DefTerm until the window is minimized and restored once. :|



Fix patch:
diff --git a/src/cascadia/TerminalConnection/ConptyConnection.cpp b/src/cascadia/TerminalConnection/ConptyConnection.cpp
index 3e816e2c39c..1d590367984 100644
--- a/src/cascadia/TerminalConnection/ConptyConnection.cpp
+++ b/src/cascadia/TerminalConnection/ConptyConnection.cpp
@@ -340,6 +340,11 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
 
             THROW_IF_FAILED(ConptyResizePseudoConsole(_hPC.get(), dimensions));
             THROW_IF_FAILED(ConptyReparentPseudoConsole(_hPC.get(), reinterpret_cast<HWND>(_initialParentHwnd)));
+
+            if (_initialVisibility)
+            {
+                THROW_IF_FAILED(ConptyShowHidePseudoConsole(_hPC.get(), _initialVisibility));
+            }
         }
 
         _startTime = std::chrono::high_resolution_clock::now();


Test patch: