Allow creating new windows on another virtual desktop (#19458)

Whoops. Closes #18652

<DHowett> 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.

(cherry picked from commit 5ae95d7df0ee3050ad36b98cd8f3f8d55005f0d2)
Service-Card-Id: PVTI_lADOAF3p4s4BBcTlzgf7tqY
Service-Version: 1.24
This commit is contained in:
Leonard Hecker 2025-10-21 21:08:57 +02:00 committed by Dustin L. Howett
parent d25b84d4c1
commit b38489b67b

View File

@ -648,7 +648,8 @@ void WindowEmperor::_dispatchCommandlineCommon(winrt::array_view<const winrt::hs
// This is an implementation-detail of _dispatchCommandline().
safe_void_coroutine WindowEmperor::_dispatchCommandlineCurrentDesktop(winrt::TerminalApp::CommandlineArgs args)
{
std::weak_ptr<AppHost> mostRecentWeak;
std::shared_ptr<AppHost> mostRecent;
AppHost* window = nullptr;
if (winrt::guid currentDesktop; VirtualDesktopUtils::GetCurrentVirtualDesktopId(reinterpret_cast<GUID*>(&currentDesktop)))
{
@ -660,19 +661,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();
}