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

Code before:
        // parent will register its own event handler. That event handler will get
        // fired after this handler returns, and will properly cleanup state.

        if (previousConnectionState < ConnectionState::Connected && newConnectionState >= ConnectionState::Failed)
        {
            // A failure to complete the connection (before it has _connected_) is not covered by "closeOnExit".
            // This is to prevent a misconfiguration (closeOnExit: always, startingDirectory: garbage) resulting
            // in Terminal flashing open and immediately closed.
            co_return;
        }

        if (_profile)
        {
            if (_isDefTermSession && _profile.CloseOnExit() == CloseOnExitMode::Automatic)
            {
                // For 'automatic', we only care about the connection state if we were launched by Terminal
                // Since we were launched via defterm, ignore the connection state (i.e. we treat the
                // close on exit mode as 'always', see GH #13325 for discussion)
                Close();
            }

            const auto mode = _profile.CloseOnExit();
            if ((mode == CloseOnExitMode::Always) ||
                ((mode == CloseOnExitMode::Graceful || mode == CloseOnExitMode::Automatic) && newConnectionState == ConnectionState::Closed))
            {
                Close();
            }
        }
    }

    // Method Description:
    // - Plays a warning note when triggered by the BEL control character,
    //   using the sound configured for the "Critical Stop" system event.`
    //   This matches the behavior of the Windows Console host.
    // - Will also flash the taskbar if the bellStyle setting for this profile
    //   has the 'visual' flag set
    // Arguments:
    // - <unused>
    void TerminalPaneContent::_controlWarningBellHandler(const winrt::Windows::Foundation::IInspectable& /*sender*/,
                                                         const winrt::Windows::Foundation::IInspectable& /*eventArgs*/)
    {
        if (_profile)
        {
            // We don't want to do anything if nothing is set, so check for that first
            if (static_cast<int>(_profile.BellStyle()) != 0)
            {
                if (WI_IsFlagSet(_profile.BellStyle(), winrt::Microsoft::Terminal::Settings::Model::BellStyle::Audible))
                {
                    // Audible is set, play the sound
                    auto sounds{ _profile.BellSound() };
                    if (sounds && sounds.Size() > 0)

Review comment (suggestion):
For handoff sessions which transition to closed, we would previously call `Close()` twice. I hope that the new code is just as readable though.

Code after:

        if (previousConnectionState < ConnectionState::Connected && newConnectionState >= ConnectionState::Failed)
        {
            // A failure to complete the connection (before it has _connected_) is not covered by "closeOnExit".
            // This is to prevent a misconfiguration (closeOnExit: always, startingDirectory: garbage) resulting
            // in Terminal flashing open and immediately closed.
            co_return;
        }

        if (_profile)
        {
            const auto mode = _profile.CloseOnExit();

            if (
                // This one is obvious: If the user asked for "always" we do just that.
                (mode == CloseOnExitMode::Always) ||
                // Otherwise, and unless the user asked for the opposite of "always",
                // close the pane when the connection closed gracefully (not failed).
                (mode != CloseOnExitMode::Never && newConnectionState == ConnectionState::Closed) ||
                // However, defterm handoff can result in Windows Terminal randomly opening which may be annoying,
                // so by default we should at least always close the pane, even if the command failed.
                // See GH #13325 for discussion.
                (mode == CloseOnExitMode::Automatic && _isDefTermSession))
            {
                CloseRequested.raise(nullptr, nullptr);
            }
        }
    }

    // Method Description:
    // - Plays a warning note when triggered by the BEL control character,
    //   using the sound configured for the "Critical Stop" system event.`
    //   This matches the behavior of the Windows Console host.
    // - Will also flash the taskbar if the bellStyle setting for this profile
    //   has the 'visual' flag set
    // Arguments:
    // - <unused>
    void TerminalPaneContent::_controlWarningBellHandler(const winrt::Windows::Foundation::IInspectable& /*sender*/,
                                                         const winrt::Windows::Foundation::IInspectable& /*eventArgs*/)
    {
        if (_profile)
        {
            // We don't want to do anything if nothing is set, so check for that first
            if (static_cast<int>(_profile.BellStyle()) != 0)
            {
                if (WI_IsFlagSet(_profile.BellStyle(), winrt::Microsoft::Terminal::Settings::Model::BellStyle::Audible))
                {
                    // Audible is set, play the sound
                    auto sounds{ _profile.BellSound() };
                    if (sounds && sounds.Size() > 0)
                    {