Repository: microsoft/terminal
File: src/cascadia/TerminalApp/TerminalPage.cpp (C++)

Code before:
    }

    winrt::fire_and_forget TerminalPage::_onTabDroppedOutside(winrt::Windows::Foundation::IInspectable sender,
                                                              winrt::Microsoft::UI::Xaml::Controls::TabViewTabDroppedOutsideEventArgs e)
    {
        // Get the curremt pointer point from the corewindow
        auto pointerPoint{ CoreWindow::GetForCurrentThread().PointerPosition() };
        pointerPoint;

        // This is called when a tab FROM OUR WINDOW was dropped outside the
        // tabview. We already know which tab was being dragged. We'll just
        // invoke a moveTab action with the target window being -1. That will
        // force the creation of a new window.

        if (!_stashedDraggedTab)
        {
            co_return;
        }

        // must do the work of adding/removing tabs on the UI thread.
        auto weakThis{ get_weak() };
        co_await wil::resume_foreground(Dispatcher(), CoreDispatcherPriority::Normal);
        if (const auto& page{ weakThis.get() })
        {
            // `this` is safe to use in here.
            auto startupActions = _stashedDraggedTab->BuildStartupActions(true);
            _DetachTabFromWindow(_stashedDraggedTab);

            // We need to convert the pointer point to a point that we can use
            // to position the new window. We'll use the drag offset from before
            // so that the tab in the new window is positioned so that it's
            // basically still directly under the cursor.
            til::point adjusted = til::point{ til::math::rounding, pointerPoint } - _dragOffset;

            // -1 is the magic number for "new window"
            // 0 as the tab index, because we don't care. It's making a new window. It'll be the only tab.
            _MoveContent(startupActions, winrt::hstring{ L"-1" }, 0, adjusted.to_winrt_point());
            // _RemoveTab will make sure to null out the _stashedDraggedTab
            _RemoveTab(*_stashedDraggedTab);
        }
    }
}


Review comment (suggestion):
```suggestion
            const auto startupActions = _stashedDraggedTab->BuildStartupActions(true);
```

Code after:
            // No windowId? Bail.
            co_return;
        }
        const uint64_t src{ winrt::unbox_value<uint64_t>(windowIdObj) };

        // Figure out where in the tab strip we're dropping this tab. Add that
        // index to the request. This is largely taken from the WinUI sample
        // app.

        // We need to be on OUR UI thread to figure out where we dropped
        auto weakThis{ get_weak() };
        co_await wil::resume_foreground(Dispatcher());
        if (const auto& page{ weakThis.get() })
        {
            // First we need to get the position in the List to drop to
            auto index = -1;

            // Determine which items in the list our pointer is between.
            for (auto i = 0u; i < _tabView.TabItems().Size(); i++)
            {
                if (const auto& item{ _tabView.ContainerFromIndex(i).try_as<winrt::MUX::Controls::TabViewItem>() })
                {
                    const auto posX{ e.GetPosition(item).X }; // The point of the drop, relative to the tab
                    const auto itemWidth{ item.ActualWidth() }; // The right of the tab
                    // If the drag point is on the left half of the tab, then insert here.
                    if (posX < itemWidth / 2)
                    {
                        index = i;
                        break;
                    }
                }
            }

            // `this` is safe to use
            const auto request = winrt::make_self<RequestReceiveContentArgs>(src, _WindowProperties.WindowId(), index);

            // This will go up to the monarch, who will then dispatch the request
            // back down to the source TerminalPage, who will then perform a
            // RequestMoveContent to move their tab to us.
            _RequestReceiveContentHandlers(*this, *request);
        }
    }

    // Method Description:
    // - This is called on the drag/drop SOURCE TerminalPage, when the monarch has
    //   requested that we send our tab to another window. We'll need to
    //   serialize the tab, and send it to the monarch, who will then send it to
    //   the destination window.
    // - Fortunately, sending the tab is basically just a MoveTab action, so we
    //   can largely reuse that.
    winrt::fire_and_forget TerminalPage::SendContentToOther(winrt::TerminalApp::RequestReceiveContentArgs args)