Repository: microsoft/terminal (language: cpp)

Issue:
Ghost/dummy window appears in the task bar (DesktopWindowXamlSource)
<!-- 
🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨

I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING:
1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement.
2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement.
3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number).
4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement.
5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement.

All good? Then proceed!
-->

<!--
This bug tracker is monitored by Windows Terminal development team and other technical folks.

**Important: When reporting BSODs or security issues, DO NOT attach memory dumps, logs, or traces to Github issues**.
Instead, send dumps/traces to secure@microsoft.com, referencing this GitHub issue.

If this is an application crash, please also provide a Feedback Hub submission link so we can find your diagnostic data on the backend. Use the category "Apps > Windows Terminal (Preview)" and choose "Share My Feedback" after submission to get the link.

Please use this form and describe your issue, concisely but precisely, with as much detail as possible.

-->

# Environment

```none
Windows build number: [run `[Environment]::OSVersion` for powershell, or `ver` for cmd]
 Win32NT             10.0.19041.0 Microsoft Windows NT 10.0.19041.0

Windows Terminal version (if applicable):
Windows Terminal Preview
Version: 1.1.1611.0

Any other software?
```

# Steps to reproduce
1. Start Windows Terminal on PowerShell
2. Start wsl

<!-- A description of how to trigger this bug. -->

# Expected behavior
Only one mini window should show on the task bar

<!-- A description of what you're expecting, possibly containing screenshots or reference material. -->

# Actual behavior
Two mini windows are shown in the task bar:
<img width="336" alt="2020-06-14 (1)" src="https://user-images.githubusercontent.com/2091582/84608486-91405b80-ae67-11ea-90eb-b66bcd042143.png">

<!-- What's actually happening? -->
The second window shown is the CoreWindow, this is a known issue with Xaml Islands version 1.0.
The workaround is to hide the core window hWnd by calling `::ShowWindow(hWnd, SW_HIDE)`



Discussion / hints:
I don’t believe this happens every time; the extra window usually goes away after it’s been parented. Do you know what causes it to stick around?

When hosting an island, we only have two windows: the terminal Win32 window, and the “interop HWND” we get from DWXS. Since the interop HWND is the one we parent to the terminal window, I’m worried that hiding it will hide our XAML content. 

I’m hoping you know a way to get the best of both worlds :)
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for **4 days**. It will be closed if no further activity occurs **within 3 days of this comment**.
Hey @ocalvo, any idea if the root xaml islands bug was solved in Windows 11? I haven't seen this in quite some time now, so maybe it got fixed on your side? 

I suppose even in Windows 10, we should be `ShowWindow(hWnd, SW_HIDE)`'ing the `_interopWindowHandle` around here: https://github.com/microsoft/terminal/blob/main/src/cascadia/WindowsTerminal/IslandWindow.cpp#L311, yea? Will that really not hide our XAML content?


I think W11 shipped with the DWXS window hidden by default!

We should do the hide still -- and yeah, it shouldn't hide our XAML content.
Easy enough to test out, thanks for the confirmation!
Here is my solution for that and hope to give you some help:

```
        // Prevent showing the dummy/empty/ghost DesktopWindowXamlSource window
        // in the task bar.
        // Reference: https://github.com/microsoft/terminal/issues/6507
        // Reference: https://github.com/microsoft/microsoft-ui-xaml/issues/6490
        // Fixes: https://github.com/M2Team/NanaZip/issues/225
        // For also fixing the window with empty content due to CoreWindow is
        // not exist issue, create a host window without message loop is a
        // better workaround.
        g_CoreWindowHostWindowHandle = ::CreateWindowExW(
            0,
            L"Mile.Xaml.ContentWindow",
            L"Mile.Xaml.CoreWindowHostWindow",
            0,
            CW_USEDEFAULT,
            0,
            CW_USEDEFAULT,
            0,
            nullptr,
            nullptr,
            nullptr,
            winrt::get_abi(winrt::Grid()));
        winrt::check_pointer(g_CoreWindowHostWindowHandle);
```

Note: This workaround should be used as soon as possible after first calling `WindowsXamlManager::InitializeForCurrentThread()`, and only need to use once. (Of course, should before any other XAML Islands content hosting windows.)

Reference: https://github.com/ProjectMile/Mile.Xaml/blob/c5c7af247f7182b94e3e64ae467b8bd823df5a1c/Mile.Xaml/Mile.Xaml.cpp#L519-L540

Kenji Mouri
I'm also experiencing this on launch at startup or just via opening the app normally from the start menu or taskbar.

Windows Terminal 1.18.902.0
Windows 10 22H2 19045.2728

Fix patch:
diff --git a/src/cascadia/TerminalApp/App.cpp b/src/cascadia/TerminalApp/App.cpp
index b4019d95227..0a73c8bb41c 100644
--- a/src/cascadia/TerminalApp/App.cpp
+++ b/src/cascadia/TerminalApp/App.cpp
@@ -4,6 +4,7 @@
 #include "pch.h"
 #include "App.h"
 #include "App.g.cpp"
+#include <CoreWindow.h>
 
 using namespace winrt;
 using namespace winrt::Windows::ApplicationModel::Activation;
@@ -32,6 +33,27 @@ namespace winrt::TerminalApp::implementation
         if (!dispatcherQueue)
         {
             _windowsXamlManager = xaml::Hosting::WindowsXamlManager::InitializeForCurrentThread();
+
+            // As of Process Model v3, terminal windows are all created on their
+            // own threads, but we still initiate XAML for the App on the main
+            // thread. Thing is, just initializing XAML creates a CoreWindow for
+            // us. On Windows 10, that CoreWindow will show up as a visible
+            // window on the taskbar, unless we hide it manually. So, go get it
+            // and do the SW_HIDE thing on it.
+            if (const auto& coreWindow{ winrt::Windows::UI::Core::CoreWindow::GetForCurrentThread() })
+            {
+                if (const auto& interop{ coreWindow.try_as<ICoreWindowInterop>() })
+                {
+                    HWND coreHandle{ 0 };
+                    interop->get_WindowHandle(&coreHandle);
+                    if (coreHandle)
+                    {
+                        // This prevents an empty "DesktopWindowXamlSource" from
+                        // appearing on the taskbar
+                        ShowWindow(coreHandle, SW_HIDE);
+                    }
+                }
+            }
         }
         else
         {
diff --git a/src/cascadia/WindowsTerminal/IslandWindow.cpp b/src/cascadia/WindowsTerminal/IslandWindow.cpp
index 7e4c5ff8aee..17a240f8fa5 100644
--- a/src/cascadia/WindowsTerminal/IslandWindow.cpp
+++ b/src/cascadia/WindowsTerminal/IslandWindow.cpp
@@ -307,6 +307,11 @@ void IslandWindow::Initialize()
     // stash the child interop handle so we can resize it when the main hwnd is resized
     interop->get_WindowHandle(&_interopWindowHandle);
 
+    // Immediately hide our XAML island hwnd. On earlier versions of Windows,
+    // this HWND could sometimes appear as an actual window in the taskbar
+    // without this!
+    ShowWindow(_interopWindowHandle, SW_HIDE);
+
     _rootGrid = winrt::Windows::UI::Xaml::Controls::Grid();
     _source.Content(_rootGrid);
 
diff --git a/src/cascadia/WindowsTerminal/IslandWindow.h b/src/cascadia/WindowsTerminal/IslandWindow.h
index 3673ce8f5b0..7a00528ae59 100644
--- a/src/cascadia/WindowsTerminal/IslandWindow.h
+++ b/src/cascadia/WindowsTerminal/IslandWindow.h
@@ -94,8 +94,8 @@ class IslandWindow :
 
     HWND _interopWindowHandle;
 
-    winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource _source;
-    winrt::Windows::UI::Xaml::Controls::Grid _rootGrid;
+    winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource _source; // nulled in ctor
+    winrt::Windows::UI::Xaml::Controls::Grid _rootGrid; // nulled in ctor
     wil::com_ptr<ITaskbarList3> _taskbar;
 
     std::function<void(const HWND, const til::rect&)> _pfnCreateCallback;


Test patch: