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

Code before:
// Method Description:
// - Asynchronously get the window layout from the current page. This is
//   done async because we need to switch between the ui thread and the calling
//   thread.
// - NB: The peasant calling this must not be running on the UI thread, otherwise
//   they will crash since they just call .get on the async operation.
// Arguments:
// - <none>
// Return Value:
// - The window layout as a json string.
winrt::Windows::Foundation::IAsyncOperation<winrt::hstring> AppHost::_GetWindowLayoutAsync()
{
    winrt::apartment_context peasant_thread;

    winrt::hstring layoutJson = L"";
    // Use the main thread since we are accessing controls.
    co_await wil::resume_foreground(_windowLogic.GetRoot().Dispatcher());
    try
    {
        const auto pos = _GetWindowLaunchPosition();
        layoutJson = _windowLogic.GetWindowLayoutJson(pos);
    }
    CATCH_LOG()

    // go back to give the result to the peasant.
    co_await peasant_thread;

    co_return layoutJson;
}

void AppHost::_HandleSummon(const winrt::Windows::Foundation::IInspectable& /*sender*/,
                            const Remoting::SummonWindowBehavior& args)
{
    _window->SummonWindow(args);

    if (args != nullptr && args.MoveToCurrentDesktop())
    {
        if (_desktopManager)
        {
            // First thing - make sure that we're not on the current desktop. If
            // we are, then don't call MoveWindowToDesktop. This is to mitigate
            // MSFT:33035972
            BOOL onCurrentDesktop{ false };
            if (SUCCEEDED(_desktopManager->IsWindowOnCurrentVirtualDesktop(_window->GetHandle(), &onCurrentDesktop)) && onCurrentDesktop)
            {
                // If we succeeded, and the window was on the current desktop, then do nothing.
            }
            else
            {
                // Here, we either failed to check if the window is on the
                // current desktop, or it wasn't on that desktop. In both those

Review comment (performance):
This does worry me - we're now returning the results _on the UI[^1] thread_. I wanna make sure that the callers don't do anything too expensive on the UI thread. 

[^1]: The window's UI thread, not the _main_ main thread

Code after:
// Method Description:
// - Asynchronously get the window layout from the current page. This is
//   done async because we need to switch between the ui thread and the calling
//   thread.
// - NB: The peasant calling this must not be running on the UI thread, otherwise
//   they will crash since they just call .get on the async operation.
// Arguments:
// - <none>
// Return Value:
// - The window layout as a json string.
winrt::Windows::Foundation::IAsyncOperation<winrt::hstring> AppHost::_GetWindowLayoutAsync()
{
    winrt::hstring layoutJson = L"";
    // Use the main thread since we are accessing controls.
    co_await wil::resume_foreground(_windowLogic.GetRoot().Dispatcher());
    try
    {
        const auto pos = _GetWindowLaunchPosition();
        layoutJson = _windowLogic.GetWindowLayoutJson(pos);
    }
    CATCH_LOG()

    co_return layoutJson;
}

void AppHost::_HandleSummon(const winrt::Windows::Foundation::IInspectable& /*sender*/,
                            const Remoting::SummonWindowBehavior& args)
{
    _window->SummonWindow(args);

    if (args != nullptr && args.MoveToCurrentDesktop())
    {
        if (_desktopManager)
        {
            // First thing - make sure that we're not on the current desktop. If
            // we are, then don't call MoveWindowToDesktop. This is to mitigate
            // MSFT:33035972
            BOOL onCurrentDesktop{ false };
            if (SUCCEEDED(_desktopManager->IsWindowOnCurrentVirtualDesktop(_window->GetHandle(), &onCurrentDesktop)) && onCurrentDesktop)
            {
                // If we succeeded, and the window was on the current desktop, then do nothing.
            }
            else
            {
                // Here, we either failed to check if the window is on the
                // current desktop, or it wasn't on that desktop. In both those
                // cases, just move the window.

                GUID currentlyActiveDesktop{ 0 };
                if (VirtualDesktopUtils::GetCurrentVirtualDesktopId(&currentlyActiveDesktop))
                {