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

Code before:
        auto weakThis{ get_weak() };

        co_await wil::resume_foreground(_tabView.Dispatcher());

        if (auto page{ weakThis.get() })
        {
            // Make sure the tab was not removed
            uint32_t tabIndex{};
            if (_tabs.IndexOf(tab, tabIndex))
            {
                _tabView.SelectedItem(tab.TabViewItem());
            }
        }
    }

    // Method Description:
    // - Disables read-only mode on pane if the user wishes to close it and read-only mode is enabled.
    // Arguments:
    // - pane: the pane that is about to be closed.
    // Return Value:
    // - bool indicating whether the (read-only) pane can be closed.
    winrt::Windows::Foundation::IAsyncOperation<bool> TerminalPage::_PaneConfirmCloseReadOnly(std::shared_ptr<Pane> pane)
    {
        if (const auto control{ pane->GetTerminalControl() })
        {
            if (control.ReadOnly())
            {
                auto warningResult = co_await _ShowCloseReadOnlyDialog();

                // If the user didn't explicitly click on close tab - leave
                if (warningResult != ContentDialogResult::Primary)
                {
                    co_return false;
                }
                control.ToggleReadOnly();
            }
        }
        co_return true;
    }

    // Method Description:
    // - Removes the pane from the tab it belongs to.
    // Arguments:
    // - pane: the pane to close.
    void TerminalPage::_HandleClosePaneRequested(std::shared_ptr<Pane> pane)
    {
        // Build the list of actions to recreate the closed pane,
        // BuildStartupActions returns the "first" pane and the rest of
        // its actions are assuming that first pane has been created first.
        // This doesn't handle refocusing anything in particular, the
        // result will be that the last pane created is focused. In the

Review comment (refactor):
This used to be a `pane->ContainsReadOnly` call, replaced now with checking if the actual control in the pane is read-only. 

Technically, you can select a _parent_ pane. So you could close a parent pane (and close all the leaves within it). I think with this change, we've lost the ability to check if _any_ leaves in a pane are read-only. 

A simple fix might be to just add an `else (pane->ContainsReadOnly())` after the `if (const auto control` block, and repeat the walking we had before in that block. A little duplicated, but 

Code after:
        auto weakThis{ get_weak() };

        co_await wil::resume_foreground(_tabView.Dispatcher());

        if (auto page{ weakThis.get() })
        {
            // Make sure the tab was not removed
            uint32_t tabIndex{};
            if (_tabs.IndexOf(tab, tabIndex))
            {
                _tabView.SelectedItem(tab.TabViewItem());
            }
        }
    }

    // Method Description:
    // - Disables read-only mode on pane if the user wishes to close it and read-only mode is enabled.
    // Arguments:
    // - pane: the pane that is about to be closed.
    // Return Value:
    // - bool indicating whether the (read-only) pane can be closed.
    winrt::Windows::Foundation::IAsyncOperation<bool> TerminalPage::_PaneConfirmCloseReadOnly(std::shared_ptr<Pane> pane)
    {
        if (pane->ContainsReadOnly())
        {
            auto warningResult = co_await _ShowCloseReadOnlyDialog();

            // If the user didn't explicitly click on close tab - leave
            if (warningResult != ContentDialogResult::Primary)
            {
                co_return false;
            }

            // Clean read-only mode to prevent additional prompt if closing the pane triggers closing of a hosting tab
            pane->WalkTree([](auto p) {
                if (const auto control{ p->GetTerminalControl() })
                {
                    if (control.ReadOnly())
                    {
                        control.ToggleReadOnly();
                    }
                }
            });
        }
        co_return true;
    }

    // Method Description:
    // - Removes the pane from the tab it belongs to.
    // Arguments:
    // - pane: the pane to close.