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

Code before:
    // Return Value:
    // - true if we should be in "always on top" mode
    bool TerminalPage::AlwaysOnTop() const
    {
        return _isAlwaysOnTop;
    }

    HRESULT TerminalPage::_OnNewConnection(winrt::Microsoft::Terminal::TerminalConnection::ITerminalConnection connection)
    {
        // We need to be on the UI thread in order for _OpenNewTab to run successfully.
        // HasThreadAccess will return true if we're currently on a UI thread and false otherwise.
        // When we're on a COM thread, we'll need to dispatch the calls to the UI thread
        // and wait on it hence the locking mechanism.
        if (Dispatcher().HasThreadAccess())
        {
            try
            {
                NewTerminalArgs newTerminalArgs{};
                // TODO GH#XXXXX: When we pass the actual commandline (or originating application), the
                // settings model can choose the right settings based on command matching, or synthesize
                // a profile from the registry/link settings (TODO GH#9458).
                // TODO GH#9458: Get and pass the LNK/EXE filenames.
                newTerminalArgs.Commandline(L"default-terminal-invocation-placeholder");
                const auto profile{ _settings.GetProfileForArgs(newTerminalArgs) };
                const auto settings{ TerminalSettings::CreateWithProfile(_settings, profile, *_bindings) };

                _CreateNewTabWithProfileAndSettings(profile, settings, connection);

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

            return S_OK;
        }
        else
        {
            til::latch latch{ 1 };
            HRESULT finalVal = S_OK;

            Dispatcher().RunAsync(CoreDispatcherPriority::Normal, [&]() {
                // Re-running ourselves under the dispatcher will cause us to take the first branch above.
                finalVal = _OnNewConnection(connection);

                latch.count_down();
            });

            latch.wait();
            return finalVal;
        }
    }

Review comment (suggestion):
I feel worst about this. It was either "use the new commandline detection in `GetProfileForArgs`" or "copy the feature flag detection code over to TerminalPage as well" 😦  (since it needs to work properly for all types of invocation.)

Code after:
    // Return Value:
    // - true if we should be in "always on top" mode
    bool TerminalPage::AlwaysOnTop() const
    {
        return _isAlwaysOnTop;
    }

    HRESULT TerminalPage::_OnNewConnection(winrt::Microsoft::Terminal::TerminalConnection::ITerminalConnection connection)
    {
        // We need to be on the UI thread in order for _OpenNewTab to run successfully.
        // HasThreadAccess will return true if we're currently on a UI thread and false otherwise.
        // When we're on a COM thread, we'll need to dispatch the calls to the UI thread
        // and wait on it hence the locking mechanism.
        if (Dispatcher().HasThreadAccess())
        {
            try
            {
                NewTerminalArgs newTerminalArgs{};
                // TODO GH#10952: When we pass the actual commandline (or originating application), the
                // settings model can choose the right settings based on command matching, or synthesize
                // a profile from the registry/link settings (TODO GH#9458).
                // TODO GH#9458: Get and pass the LNK/EXE filenames.
                // Passing in a commandline forces GetProfileForArgs to use Base Layer instead of Default Profile;
                // in the future, it can make a better decision based on the value we pull out of the process handle.
                // TODO GH#5047: When we hang on to the N.T.A., try not to spawn "default... .exe" :)
                newTerminalArgs.Commandline(L"default-terminal-invocation-placeholder");
                const auto profile{ _settings.GetProfileForArgs(newTerminalArgs) };
                const auto settings{ TerminalSettings::CreateWithProfile(_settings, profile, *_bindings) };

                _CreateNewTabWithProfileAndSettings(profile, settings, connection);

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

            return S_OK;
        }
        else
        {
            til::latch latch{ 1 };
            HRESULT finalVal = S_OK;

            Dispatcher().RunAsync(CoreDispatcherPriority::Normal, [&]() {
                // Re-running ourselves under the dispatcher will cause us to take the first branch above.
                finalVal = _OnNewConnection(connection);

                latch.count_down();
            });

            latch.wait();