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

Code before:
                if (auto page{ weakThis.get() })
                {
                    page->_LaunchSettings(e);
                }
            });

            auto newTabImpl = winrt::make_self<SettingsTab>(sui);

            // Add the new tab to the list of our tabs.
            _tabs.Append(*newTabImpl);
            _mruTabs.Append(*newTabImpl);

            newTabImpl->SetDispatch(*_actionDispatch);
            newTabImpl->SetActionMap(_settings.ActionMap());

            // Give the tab its index in the _tabs vector so it can manage its own SwitchToTab command.
            _UpdateTabIndices();

            // Don't capture a strong ref to the tab. If the tab is removed as this
            // is called, we don't really care anymore about handling the event.
            auto weakTab = make_weak(newTabImpl);

            auto tabViewItem = newTabImpl->TabViewItem();
            _tabView.TabItems().Append(tabViewItem);

            tabViewItem.PointerPressed({ this, &TerminalPage::_OnTabClick });

            // When the tab requests close, try to close it (prompt for approval, if required)
            newTabImpl->CloseRequested([weakTab, weakThis{ get_weak() }](auto&& /*s*/, auto&& /*e*/) {
                auto page{ weakThis.get() };
                auto tab{ weakTab.get() };

                if (page && tab)
                {
                    page->_HandleCloseTabRequested(*tab);
                }
            });

            // When the tab is closed, remove it from our list of tabs.
            newTabImpl->Closed([tabViewItem, weakThis{ get_weak() }](auto&& /*s*/, auto&& /*e*/) {
                if (auto page{ weakThis.get() })
                {
                    page->_settingsTab = nullptr;
                    page->_RemoveOnCloseRoutine(tabViewItem, page);
                }
            });

            _settingsTab = *newTabImpl;

            // This kicks off TabView::SelectionChanged, in response to which
            // we'll attach the terminal's Xaml control to the Xaml root.

Review comment (suggestion):
Not sure if there is any way currently for a defterm invocation to open up a tree of panes, but for future-proofing purposes we call `WalkTree` here

Code after:
        if (!Dispatcher().HasThreadAccess())
        {
            til::latch latch{ 1 };
            auto finalVal = S_OK;

            Dispatcher().RunAsync(CoreDispatcherPriority::Normal, [&]() {
                finalVal = _OnNewConnection(connection);
                latch.count_down();
            });

            latch.wait();
            return finalVal;
        }

        try
        {
            NewTerminalArgs newTerminalArgs;
            newTerminalArgs.Commandline(connection.Commandline());
            // GH #12370: We absolutely cannot allow a defterm connection to
            // auto-elevate. Defterm doesn't work for elevated scenarios in the
            // first place. If we try accepting the connection, the spawning an
            // elevated version of the Terminal with that profile... that's a
            // recipe for disaster. We won't ever open up a tab in this window.
            newTerminalArgs.Elevate(false);
            const auto newPane = _MakePane(newTerminalArgs, false, connection);
            newPane->WalkTree([](auto pane) {
                pane->FinalizeConfigurationGivenDefault();
            });
            _CreateNewTabFromPane(newPane);

            // Request a summon of this window to the foreground
            _SummonWindowRequestedHandlers(*this, nullptr);

            const IInspectable unused{ nullptr };
            _SetAsDefaultDismissHandler(unused, unused);
            return S_OK;
        }
        CATCH_RETURN()
    }

    // Method Description:
    // - Creates a settings UI tab and focuses it. If there's already a settings UI tab open,
    //   just focus the existing one.
    // Arguments:
    // - <none>
    // Return Value:
    // - <none>
    void TerminalPage::OpenSettingsUI()
    {
        // If we're holding the settings tab's switch command, don't create a new one, switch to the existing one.
        if (!_settingsTab)