Skip to content

Commit 7849b00

Browse files
authored
Fix CoreWindow being destroyed after handoff (#19298)
As per: #19280 (comment) ## Validation Steps Performed * Launch wtd via handoff (spawn cmd, etc.) * Shift+Click the tab bar + button to create a new window * Close the initial window * UI doesn't lock up ✅
1 parent 5899343 commit 7849b00

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/cascadia/WindowsTerminal/WindowEmperor.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,29 @@ void WindowEmperor::CreateNewWindow(winrt::TerminalApp::WindowRequestedArgs args
253253

254254
_windowCount += 1;
255255
_windows.emplace_back(std::move(host));
256+
257+
if (_windowCount == 1)
258+
{
259+
// The first CoreWindow is created implicitly by XAML and parented to the
260+
// first XAML island. We parent it to our initial window for 2 reasons:
261+
// * On Windows 10 the CoreWindow will show up as a visible window on the taskbar
262+
// due to a WinUI bug, and this will hide it, because our initial window is hidden.
263+
// * When we DestroyWindow() the island it will destroy the CoreWindow,
264+
// and it's not possible to recreate it. That's also a WinUI bug.
265+
//
266+
// Note that this must be done after the first window (= first island) is created.
267+
if (const auto coreWindow = winrt::Windows::UI::Core::CoreWindow::GetForCurrentThread())
268+
{
269+
if (const auto interop = coreWindow.try_as<ICoreWindowInterop>())
270+
{
271+
HWND coreHandle = nullptr;
272+
if (SUCCEEDED(interop->get_WindowHandle(&coreHandle)) && coreHandle)
273+
{
274+
SetParent(coreHandle, _window.get());
275+
}
276+
}
277+
}
278+
}
256279
}
257280

258281
AppHost* WindowEmperor::_mostRecentWindow() const noexcept
@@ -395,24 +418,6 @@ void WindowEmperor::HandleCommandlineArgs(int nCmdShow)
395418
LOG_IF_WIN32_BOOL_FALSE(SetCurrentDirectoryW(system32.c_str()));
396419
}
397420

398-
// The first CoreWindow is created implicitly by XAML and parented to the
399-
// first XAML island. We parent it to our initial window for 2 reasons:
400-
// * On Windows 10 the CoreWindow will show up as a visible window on the taskbar
401-
// due to a WinUI bug, and this will hide it, because our initial window is hidden.
402-
// * When we DestroyWindow() the island it will destroy the CoreWindow,
403-
// and it's not possible to recreate it. That's also a WinUI bug.
404-
if (const auto coreWindow = winrt::Windows::UI::Core::CoreWindow::GetForCurrentThread())
405-
{
406-
if (const auto interop = coreWindow.try_as<ICoreWindowInterop>())
407-
{
408-
HWND coreHandle = nullptr;
409-
if (SUCCEEDED(interop->get_WindowHandle(&coreHandle)) && coreHandle)
410-
{
411-
SetParent(coreHandle, _window.get());
412-
}
413-
}
414-
}
415-
416421
{
417422
TerminalConnection::ConptyConnection::NewConnection([this](TerminalConnection::ConptyConnection conn) {
418423
TerminalApp::CommandlineArgs args;

0 commit comments

Comments
 (0)