From 5ae95d7df0ee3050ad36b98cd8f3f8d55005f0d2 Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Tue, 21 Oct 2025 21:08:57 +0200 Subject: [PATCH] Allow creating new windows on another virtual desktop (#19458) Whoops. Closes #18652 I chatted with Leonard to figure out why I kept misunderstanding this PR. The key is that **this function should not always return an existing window.** It's supposed to find an existing window on the current virtual desktop, not literally any window anywhere. --- .../WindowsTerminal/WindowEmperor.cpp | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/cascadia/WindowsTerminal/WindowEmperor.cpp b/src/cascadia/WindowsTerminal/WindowEmperor.cpp index b597bab24b..962a4ac726 100644 --- a/src/cascadia/WindowsTerminal/WindowEmperor.cpp +++ b/src/cascadia/WindowsTerminal/WindowEmperor.cpp @@ -670,7 +670,8 @@ void WindowEmperor::_dispatchCommandlineCommon(winrt::array_view mostRecentWeak; + std::shared_ptr mostRecent; + AppHost* window = nullptr; if (winrt::guid currentDesktop; VirtualDesktopUtils::GetCurrentVirtualDesktopId(reinterpret_cast(¤tDesktop))) { @@ -682,19 +683,18 @@ safe_void_coroutine WindowEmperor::_dispatchCommandlineCurrentDesktop(winrt::Ter if (desktopId == currentDesktop && lastActivatedTime > max) { max = lastActivatedTime; - mostRecentWeak = w; + mostRecent = w; } } + + // GetVirtualDesktopId(), as current implemented, should always return on the main thread. + _assertIsMainThread(); + window = mostRecent.get(); } - - // GetVirtualDesktopId(), as current implemented, should always return on the main thread. - _assertIsMainThread(); - - const auto mostRecent = mostRecentWeak.lock(); - auto window = mostRecent.get(); - - if (!window) + else { + // If virtual desktops have never been used, and in turn Explorer never set them up, + // GetCurrentVirtualDesktopId will return false. In this case just use the current (only) desktop. window = _mostRecentWindow(); }