Repository: microsoft/terminal
File: src/cascadia/WindowsTerminal/WindowEmperor.cpp (C++)

Code before:
safe_void_coroutine WindowEmperor::_dispatchCommandlineCurrentDesktop(winrt::TerminalApp::CommandlineArgs args)
{
    std::weak_ptr<AppHost> mostRecentWeak;

    if (winrt::guid currentDesktop; VirtualDesktopUtils::GetCurrentVirtualDesktopId(reinterpret_cast<GUID*>(&currentDesktop)))
    {
        int64_t max = INT64_MIN;
        for (const auto& w : _windows)
        {
            const auto lastActivatedTime = w->GetLastActivatedTime();
            const auto desktopId = co_await w->GetVirtualDesktopId();
            if (desktopId == currentDesktop && lastActivatedTime > max)
            {
                max = lastActivatedTime;
                mostRecentWeak = w;
            }
        }
    }

    // GetVirtualDesktopId(), as current implemented, should always return on the main thread.
    _assertIsMainThread();

    const auto mostRecent = mostRecentWeak.lock();
    auto window = mostRecent.get();

    if (!window)
    {
        window = _mostRecentWindow();
    }

    if (window)
    {
        window->DispatchCommandline(std::move(args));
    }
    else
    {
        CreateNewWindow(winrt::TerminalApp::WindowRequestedArgs{ 0, std::move(args) });
    }
}

bool WindowEmperor::_summonWindow(const SummonWindowSelectionArgs& args) const
{
    _assertIsMainThread();

    AppHost* window = nullptr;

    if (args.WindowID)
    {
        for (const auto& w : _windows)
        {
            if (w->Logic().WindowProperties().WindowId() == args.WindowID)

Review comment (bug):
@DHowett The fix is that this if-condition is gone. If the `currentDesktop` was acquired we _must_ yield a `window = nullptr` if no window exists on the current desktop. This if-condition however will assign the most recent window.

Code after:
safe_void_coroutine WindowEmperor::_dispatchCommandlineCurrentDesktop(winrt::TerminalApp::CommandlineArgs args)
{
    std::shared_ptr<AppHost> mostRecent;
    AppHost* window = nullptr;

    if (winrt::guid currentDesktop; VirtualDesktopUtils::GetCurrentVirtualDesktopId(reinterpret_cast<GUID*>(&currentDesktop)))
    {
        int64_t max = INT64_MIN;
        for (const auto& w : _windows)
        {
            const auto lastActivatedTime = w->GetLastActivatedTime();
            const auto desktopId = co_await w->GetVirtualDesktopId();
            if (desktopId == currentDesktop && lastActivatedTime > max)
            {
                max = lastActivatedTime;
                mostRecent = w;
            }
        }

        // GetVirtualDesktopId(), as current implemented, should always return on the main thread.
        _assertIsMainThread();
        window = mostRecent.get();
    }
    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();
    }

    if (window)
    {
        window->DispatchCommandline(std::move(args));
    }
    else
    {
        CreateNewWindow(winrt::TerminalApp::WindowRequestedArgs{ 0, std::move(args) });
    }
}

bool WindowEmperor::_summonWindow(const SummonWindowSelectionArgs& args) const
{
    _assertIsMainThread();

    AppHost* window = nullptr;

    if (args.WindowID)
    {
        for (const auto& w : _windows)
        {
            if (w->Logic().WindowProperties().WindowId() == args.WindowID)