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

Code before:

    void TerminalPage::_DetachTabFromWindow(const winrt::com_ptr<TabBase>& tab)
    {
        if (const auto terminalTab = tab.try_as<TerminalTab>())
        {
            // Detach the root pane, which will act like the whole tab got detached.
            if (const auto rootPane = terminalTab->GetRootPane())
            {
                _DetachPaneFromWindow(rootPane);
            }
        }
    }

    // Method Description:
    // - Serialize these actions to json, and raise them as a RequestMoveContent
    //   event. Our Window will raise that to the window manager / monarch, who
    //   will dispatch this blob of json back to the window that should handle
    //   this.
    // - `actions` will be emptied into a winrt IVector as a part of this method
    //   and should be expected to be empty after this call.
    void TerminalPage::_MoveContent(std::vector<Settings::Model::ActionAndArgs>&& actions,
                                    const winrt::hstring& windowName,
                                    const uint32_t tabIndex,
                                    const std::optional<til::point>& dragPoint)
    {
        const auto winRtActions{ winrt::single_threaded_vector<ActionAndArgs>(std::move(actions)) };
        const auto str{ ActionAndArgs::Serialize(winRtActions) };
        const auto request = winrt::make_self<RequestMoveContentArgs>(windowName,
                                                                      str,
                                                                      tabIndex);
        if (dragPoint.has_value())
        {
            request->WindowPosition(dragPoint->to_winrt_point());
        }
        _RequestMoveContentHandlers(*this, *request);
    }

    bool TerminalPage::_MoveTab(winrt::com_ptr<TerminalTab> tab, MoveTabArgs args)
    {
        if (!tab)
        {
            return false;
        }

        // If there was a windowId in the action, try to move it to the
        // specified window instead of moving it in our tab row.
        const auto windowId{ args.Window() };
        if (!windowId.empty())
        {
            // if the windowId is the same as our name, do nothing
            if (windowId == WindowProperties().WindowName() ||

Review comment (bug):
Do we have to construct the peer every time? Is it just a getter and cheap to call? Should we cache it? (I think this could be figured out by checking if the pointer that's returned is always the same. In that case it's probably a cheap getter.)

You could consider abstracting the code away like so:
```cpp
void TerminalPage::_raiseActionCompletedEventforUIA(const std::wstring_view& activityId, const std::wstring_view& displayString) {
    using namespace ::winrt::Windows::UI::Xaml::Automation::Peers;
    if (auto autoPeer = FrameworkElementAutomationPeer::FromElement(*this))
    {
        autoPeer.RaiseNotificationEvent(
            AutomationNotificationKind::ActionCompleted,
            AutomationNotificationProcessing::ImportantMostRecent,
            displayString,
            activityId);
    }
}
```

So that the remaining code becomes:
```cpp
_raiseActionCompletedEventforUIA(L"TerminalPageMovePaneToNewTab", RS_(L"TerminalPage_PaneMovedAnnouncement_NewTab"));
```

(etc.)

Code after:
                return false;
            }
            auto pane = focusedTab->DetachPane();
            targetTab->AttachPane(pane);
            _SetFocusedTab(*targetTab);

            if (auto autoPeer = Automation::Peers::FrameworkElementAutomationPeer::FromElement(*this))
            {
                const auto tabTitle = targetTab->Title();
                autoPeer.RaiseNotificationEvent(Automation::Peers::AutomationNotificationKind::ActionCompleted,
                                                Automation::Peers::AutomationNotificationProcessing::ImportantMostRecent,
                                                fmt::format(std::wstring_view{ RS_(L"TerminalPage_PaneMovedAnnouncement_ExistingTab") }, tabTitle),
                                                L"TerminalPageMovePaneToExistingTab" /* unique name for this notification category */);
            }
        }
        else
        {
            auto pane = focusedTab->DetachPane();
            _CreateNewTabFromPane(pane);
            if (auto autoPeer = Automation::Peers::FrameworkElementAutomationPeer::FromElement(*this))
            {
                autoPeer.RaiseNotificationEvent(Automation::Peers::AutomationNotificationKind::ActionCompleted,
                                                Automation::Peers::AutomationNotificationProcessing::ImportantMostRecent,
                                                RS_(L"TerminalPage_PaneMovedAnnouncement_NewTab"),
                                                L"TerminalPageMovePaneToNewTab" /* unique name for this notification category */);
            }
        }

        return true;
    }

    // Detach a tree of panes from this terminal. Helper used for moving panes
    // and tabs to other windows.
    void TerminalPage::_DetachPaneFromWindow(std::shared_ptr<Pane> pane)
    {
        pane->WalkTree([&](auto p) {
            if (const auto& control{ p->GetTerminalControl() })
            {
                _manager.Detach(control);
            }
        });
    }

    void TerminalPage::_DetachTabFromWindow(const winrt::com_ptr<TabBase>& tab)
    {
        if (const auto terminalTab = tab.try_as<TerminalTab>())
        {
            // Detach the root pane, which will act like the whole tab got detached.
            if (const auto rootPane = terminalTab->GetRootPane())
            {
                _DetachPaneFromWindow(rootPane);