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

Code before:
    {
        return;
    }

    const auto newConnectionState = _control.ConnectionState();
    const auto previousConnectionState = std::exchange(_connectionState, newConnectionState);

    if (newConnectionState < ConnectionState::Closed)
    {
        // Pane doesn't care if the connection isn't entering a terminal state.
        return;
    }

    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.
        return;
    }

    if (_overrideCloseOnExit)
    {
        // We have been told to close upon process termination regardless of the value of
        // the profile's CloseOnExit, so go ahead and close
        Close();
    }

    if (_profile)
    {
        const auto mode = _profile.CloseOnExit();
        if ((mode == CloseOnExitMode::Always) ||
            (mode == CloseOnExitMode::Graceful && newConnectionState == ConnectionState::Closed))
        {
            Close();
        }
    }
}

winrt::fire_and_forget Pane::_playBellSound(winrt::Windows::Foundation::Uri uri)
{
    auto weakThis{ weak_from_this() };

    co_await wil::resume_foreground(_root.Dispatcher());
    if (auto pane{ weakThis.lock() })
    {
        // BODGY
        // GH#12258: We learned that if you leave the MediaPlayer open, and
        // press the media keys (like play/pause), then the OS will _replay the
        // bell_. So we have to re-create the MediaPlayer each time we want to
        // play the bell, to make sure a subsequent play doesn't come through

Review comment (suggestion):
~couldn't we just like, add another `((mode == CloseOnExitMode::GracefulIfLaunchedByTerminal) ||`~

Nah okay yea I get why we can't do that. We only want to enforce that for defterm panes.

Code after:
    {
        return;
    }

    const auto newConnectionState = _control.ConnectionState();
    const auto previousConnectionState = std::exchange(_connectionState, newConnectionState);

    if (newConnectionState < ConnectionState::Closed)
    {
        // Pane doesn't care if the connection isn't entering a terminal state.
        return;
    }

    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.
        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();
        }
    }
}

winrt::fire_and_forget Pane::_playBellSound(winrt::Windows::Foundation::Uri uri)
{
    auto weakThis{ weak_from_this() };

    co_await wil::resume_foreground(_root.Dispatcher());
    if (auto pane{ weakThis.lock() })
    {
        // BODGY
        // GH#12258: We learned that if you leave the MediaPlayer open, and
        // press the media keys (like play/pause), then the OS will _replay the
        // bell_. So we have to re-create the MediaPlayer each time we want to