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

Code before:
            _UnZoomIfNeeded();

            if (const auto pane{ terminalTab->GetActivePane() })
            {
                if (co_await _PaneConfirmCloseReadOnly(pane))
                {
                    _HandleClosePaneRequested(pane);
                }
            }
        }
        else if (auto index{ _GetFocusedTabIndex() })
        {
            const auto tab{ _tabs.GetAt(*index) };
            if (tab.try_as<TerminalApp::SettingsTab>())
            {
                _HandleCloseTabRequested(tab);
            }
        }
    }

    // Method Description:
    // - Close all other panes except the pane that is currently focused.
    // Arguments:
    // - weakTab: weak reference to the tab that the pane belongs to.
    // - paneIds: collection of the IDs of the panes that are marked for removal.
    winrt::fire_and_forget TerminalPage::_CloseUnfocusedPanes(weak_ref<TerminalTab> weakTab, std::vector<uint32_t> paneIds)
    {
        if (auto strongTab{ weakTab.get() })
        {
            // Close all unfocused panes one by one
            while (!paneIds.empty())
            {
                const auto id = paneIds.back();
                paneIds.pop_back();

                if (const auto pane{ strongTab->GetRootPane()->FindPane(id) })
                {
                    if (co_await _PaneConfirmCloseReadOnly(pane))
                    {
                        pane->ClosedByParent([ids{ std::move(paneIds) }, weakThis{ get_weak() }, weakTab]() {
                            if (auto strongThis{ weakThis.get() })
                            {
                                strongThis->_CloseUnfocusedPanes(weakTab, std::move(ids));
                            }
                        });

                        // Close the pane which will eventually trigger the closed by parent event
                        _HandleClosePaneRequested(pane);
                        break;
                    }
                }

Review comment (refactor):
```suggestion
    winrt::fire_and_forget TerminalPage::_ClosePanes(weak_ref<TerminalTab> weakTab, std::vector<uint32_t> paneIds)
```
nit: this function is abstracted enough that we're not even checking for the active pane ID. That's good! Might as well rename the function to just `_ClosePanes` then (and update the function comment too)

Code after:
    // - Close the currently focused pane. If the pane is the last pane in the
    //   tab, the tab will also be closed. This will happen when we handle the
    //   tab's Closed event.
    winrt::fire_and_forget TerminalPage::_CloseFocusedPane()
    {
        if (const auto terminalTab{ _GetFocusedTabImpl() })
        {
            _UnZoomIfNeeded();

            if (const auto pane{ terminalTab->GetActivePane() })
            {
                if (co_await _PaneConfirmCloseReadOnly(pane))
                {
                    _HandleClosePaneRequested(pane);
                }
            }
        }
        else if (auto index{ _GetFocusedTabIndex() })
        {
            const auto tab{ _tabs.GetAt(*index) };
            if (tab.try_as<TerminalApp::SettingsTab>())
            {
                _HandleCloseTabRequested(tab);
            }
        }
    }

    // Method Description:
    // - Close all panes with the given IDs sequentially.
    // Arguments:
    // - weakTab: weak reference to the tab that the pane belongs to.
    // - paneIds: collection of the IDs of the panes that are marked for removal.
    void TerminalPage::_ClosePanes(weak_ref<TerminalTab> weakTab, std::vector<uint32_t> paneIds)
    {
        if (auto strongTab{ weakTab.get() })
        {
            // Close all unfocused panes one by one
            while (!paneIds.empty())
            {
                const auto id = paneIds.back();
                paneIds.pop_back();

                if (const auto pane{ strongTab->GetRootPane()->FindPane(id) })
                {
                    pane->ClosedByParent([ids{ std::move(paneIds) }, weakThis{ get_weak() }, weakTab]() {
                        if (auto strongThis{ weakThis.get() })
                        {
                            strongThis->_ClosePanes(weakTab, std::move(ids));
                        }
                    });