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

Code before:
        {
            co_return;
        }
        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);
            _MoveContent(startupActions, winrt::hstring{ fmt::format(L"{}", args.TargetWindow()) }, args.TabIndex());
            // _RemoveTab will make sure to null out the _stashedDraggedTab
            _RemoveTab(*_stashedDraggedTab);
        }
    }

    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

Review comment (suggestion):
```suggestion
        // Get the current pointer point from the corewindow
```

Code after:
    {
        // Get the PID and make sure it is the same as ours.
        if (const auto& pidObj{ e.DataView().Properties().TryLookup(L"pid") })
        {
            const auto pid{ winrt::unbox_value_or<uint32_t>(pidObj, 0u) };
            if (pid != GetCurrentProcessId())
            {
                // The PID doesn't match ours. We can't handle this drop.
                co_return;
            }
        }
        else
        {
            // No PID? We can't handle this drop. Bail.
            co_return;
        }

        const auto& windowIdObj{ e.DataView().Properties().TryLookup(L"windowId") };
        if (windowIdObj == nullptr)
        {
            // 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;
                    }
                }