Repository: microsoft/terminal
File: src/cascadia/TerminalControl/ControlCore.cpp (C++)

Code before:
        // Get our current size in rows/cols, and hook them up to
        // this connection too.
        {
            const auto vp = _terminal->GetViewport();
            const auto width = vp.Width();
            const auto height = vp.Height();

            newConnection.Resize(height, width);
        }
        // Window owner too.
        if (auto conpty{ newConnection.try_as<TerminalConnection::ConptyConnection>() })
        {
            conpty.ReparentWindow(_owningHwnd);
        }
        const bool replacing = _connection != nullptr;
        if (replacing)
        {
            _connection.Close();
        }
        _connection = newConnection;
        // This event is explicitly revoked in the destructor: does not need weak_ref
        _connectionOutputEventRevoker = _connection.TerminalOutput(winrt::auto_revoke, { this, &ControlCore::_connectionOutputHandler });
        if (replacing)
        {
            _connection.Start();
        }
    }

    bool ControlCore::Initialize(const float actualWidth,
                                 const float actualHeight,
                                 const float compositionScale)
    {
        assert(_settings);

        _panelWidth = actualWidth;
        _panelHeight = actualHeight;
        _compositionScale = compositionScale;

        { // scope for terminalLock
            auto terminalLock = _terminal->LockForWriting();

            if (_initializedTerminal)
            {
                return false;
            }

            const auto windowWidth = actualWidth * compositionScale;
            const auto windowHeight = actualHeight * compositionScale;

            if (windowWidth == 0 || windowHeight == 0)
            {

Review comment (suggestion):
(A theoretical concern:) If the new connection is already started, this will re-start it. Either, we should replace `if (replacing)` with `if (_connection.State() != Started)`, or make sure that all implementations of `Start()` are robust against calling it multiple times. I think the former is better. If you did that, you can replace the other `if (replacing)` above with a simpler `if (_connection)` as well... I think. 🤔

Code after:

            // Get our current size in rows/cols, and hook them up to
            // this connection too.
            {
                const auto vp = _terminal->GetViewport();
                const auto width = vp.Width();
                const auto height = vp.Height();

                newConnection.Resize(height, width);
            }
            // Window owner too.
            if (auto conpty{ newConnection.try_as<TerminalConnection::ConptyConnection>() })
            {
                conpty.ReparentWindow(_owningHwnd);
            }

            // This event is explicitly revoked in the destructor: does not need weak_ref
            _connectionOutputEventRevoker = _connection.TerminalOutput(winrt::auto_revoke, { this, &ControlCore::_connectionOutputHandler });
        }

        // Fire off a connection state changed notification, to let our hosting
        // app know that we're in a different state now.
        if (oldState != ConnectionState())
        { // rely on the null handling again
            // send the notification
            _ConnectionStateChangedHandlers(*this, nullptr);
        }
    }

    bool ControlCore::Initialize(const float actualWidth,
                                 const float actualHeight,
                                 const float compositionScale)
    {
        assert(_settings);

        _panelWidth = actualWidth;
        _panelHeight = actualHeight;
        _compositionScale = compositionScale;

        { // scope for terminalLock
            auto terminalLock = _terminal->LockForWriting();

            if (_initializedTerminal)
            {
                return false;
            }

            const auto windowWidth = actualWidth * compositionScale;
            const auto windowHeight = actualHeight * compositionScale;

            if (windowWidth == 0 || windowHeight == 0)