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

Code before:
        // This is to prevent a misconfiguration (closeOnExit: always, startingDirectory: garbage) resulting
        // in Terminal flashing open and immediately closed.
        return;
    }

    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{ shared_from_this() };

    co_await winrt::resume_foreground(_root.Dispatcher());
    if (auto pane{ weakThis.get() })
    {
        if (s_bellPlayer)
        {
            auto source{ winrt::Windows::Media::Core::MediaSource::CreateFromUri(uri) };
            auto item{ winrt::Windows::Media::Playback::MediaPlaybackItem(source) };
            s_bellPlayer.Source(item);
            s_bellPlayer.Play();
        }
    }
}

// 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 Pane::_ControlWarningBellHandler(const winrt::Windows::Foundation::IInspectable& /*sender*/,
                                      const winrt::Windows::Foundation::IInspectable& /*eventArgs*/)
{
    if (!_IsLeaf())
    {
        return;
    }
    if (_profile)
    {
        // We don't want to do anything if nothing is set, so check for that first

Review comment (bug):
I _suspect_ that if you set a nonexistent bell sound (or, perhaps, one causes a crash!) this will blast off into space.

We already have a "ValidateMediaPaths", though . . . hmm

Code after:
                    winrt::hstring soundPath{ wil::ExpandEnvironmentStringsW<std::wstring>(sounds.GetAt(rand() % sounds.Size()).c_str()) };
                    winrt::Windows::Foundation::Uri uri{ soundPath };
                    _playBellSound(uri);
                }
                else
                {
                    const auto soundAlias = reinterpret_cast<LPCTSTR>(SND_ALIAS_SYSTEMHAND);
                    PlaySound(soundAlias, NULL, SND_ALIAS_ID | SND_ASYNC | SND_SENTRY);
                }
            }

            if (WI_IsFlagSet(_profile.BellStyle(), winrt::Microsoft::Terminal::Settings::Model::BellStyle::Window))
            {
                _control.BellLightOn();
            }

            // raise the event with the bool value corresponding to the taskbar flag
            _PaneRaiseBellHandlers(nullptr, WI_IsFlagSet(_profile.BellStyle(), winrt::Microsoft::Terminal::Settings::Model::BellStyle::Taskbar));
        }
    }
}

// Event Description:
// - Called when our control gains focus. We'll use this to trigger our GotFocus
//   callback. The tab that's hosting us should have registered a callback which
//   can be used to mark us as active.
// Arguments:
// - <unused>
// Return Value:
// - <none>
void Pane::_ControlGotFocusHandler(winrt::Windows::Foundation::IInspectable const& sender,
                                   RoutedEventArgs const& /* args */)
{
    FocusState f = FocusState::Programmatic;
    if (const auto o = sender.try_as<winrt::Windows::UI::Xaml::Controls::Control>())
    {
        f = o.FocusState();
    }
    _GotFocusHandlers(shared_from_this(), f);
}

// Event Description:
// - Called when our control loses focus. We'll use this to trigger our LostFocus
//   callback. The tab that's hosting us should have registered a callback which
//   can be used to update its own internal focus state
void Pane::_ControlLostFocusHandler(winrt::Windows::Foundation::IInspectable const& /* sender */,
                                    RoutedEventArgs const& /* args */)
{
    _LostFocusHandlers(shared_from_this());
}