From abeac1b1355d6ddd23ebdda9c1e4ddab6fcaac14 Mon Sep 17 00:00:00 2001 From: aarushi singh <110608667+aarushisingh04@users.noreply.github.com> Date: Sat, 16 May 2026 00:21:01 +0530 Subject: [PATCH] Use PlaySoundW for profile bell sounds (#20031) We believed that this would fix an issue on Windows 10, where the volume mixer would forget Windows Terminal after every relaunch. It turns out that it does not. Still, this code is much more concise and doesn't require yet another WinRT object. Story of our lives. Refs #17733 --- .github/actions/spelling/expect/expect.txt | 2 + .../TerminalApp/TerminalPaneContent.cpp | 50 +++---------------- .../TerminalApp/TerminalPaneContent.h | 5 -- 3 files changed, 8 insertions(+), 49 deletions(-) diff --git a/.github/actions/spelling/expect/expect.txt b/.github/actions/spelling/expect/expect.txt index 3d3eff382f..56f0bb4211 100644 --- a/.github/actions/spelling/expect/expect.txt +++ b/.github/actions/spelling/expect/expect.txt @@ -1075,6 +1075,7 @@ NOCONTEXTHELP NOCOPYBITS nodiscard NODUP +NODEFAULT noexcepts NOFONT NOHIDDENTEXT @@ -1561,6 +1562,7 @@ SMARTQUOTE SMTO snapcx snapcy +SND snk SOLIDBOX Solutiondir diff --git a/src/cascadia/TerminalApp/TerminalPaneContent.cpp b/src/cascadia/TerminalApp/TerminalPaneContent.cpp index a14e0c326f..5e02da1eb7 100644 --- a/src/cascadia/TerminalApp/TerminalPaneContent.cpp +++ b/src/cascadia/TerminalApp/TerminalPaneContent.cpp @@ -71,18 +71,6 @@ namespace winrt::TerminalApp::implementation _removeControlEvents(); _control.Close(); - - // Clear out our media player callbacks, and stop any playing media. This - // will prevent the callback from being triggered after we've closed, and - // also make sure that our sound stops when we're closed. - if (_bellPlayer) - { - _bellPlayer.Pause(); - _bellPlayer.Source(nullptr); - _bellPlayer.Close(); - _bellPlayer = nullptr; - _bellPlayerCreated = false; - } } winrt::hstring TerminalPaneContent::Icon() const @@ -275,14 +263,15 @@ namespace winrt::TerminalApp::implementation auto sounds{ _profile.BellSound() }; if (sounds && sounds.Size() > 0) { - winrt::hstring soundPath{ sounds.GetAt(rand() % sounds.Size()).Resolved() }; - winrt::Windows::Foundation::Uri uri{ soundPath }; - _playBellSound(uri); + // Sound paths are resolved and validated by CascadiaSettings + // before we reach this point. + auto soundPath{ sounds.GetAt(rand() % sounds.Size()).Resolved() }; + PlaySoundW(soundPath.c_str(), nullptr, SND_FILENAME | SND_ASYNC | SND_SENTRY | SND_NODEFAULT); } else { - const auto soundAlias = reinterpret_cast(SND_ALIAS_SYSTEMHAND); - PlaySound(soundAlias, NULL, SND_ALIAS_ID | SND_ASYNC | SND_SENTRY); + const auto soundAlias = reinterpret_cast(SND_ALIAS_SYSTEMHAND); + PlaySoundW(soundAlias, nullptr, SND_ALIAS_ID | SND_ASYNC | SND_SENTRY); } } @@ -300,33 +289,6 @@ namespace winrt::TerminalApp::implementation } } - safe_void_coroutine TerminalPaneContent::_playBellSound(winrt::Windows::Foundation::Uri uri) - { - auto weakThis{ get_weak() }; - co_await wil::resume_foreground(_control.Dispatcher()); - if (auto pane{ weakThis.get() }) - { - if (!_bellPlayerCreated) - { - // The MediaPlayer might not exist on Windows N SKU. - try - { - _bellPlayerCreated = true; - _bellPlayer = winrt::Windows::Media::Playback::MediaPlayer(); - // GH#12258: The media keys (like play/pause) should have no effect on our bell sound. - _bellPlayer.CommandManager().IsEnabled(false); - } - CATCH_LOG(); - } - if (_bellPlayer) - { - const auto source{ winrt::Windows::Media::Core::MediaSource::CreateFromUri(uri) }; - const auto item{ winrt::Windows::Media::Playback::MediaPlaybackItem(source) }; - _bellPlayer.Source(item); - _bellPlayer.Play(); - } - } - } void TerminalPaneContent::_closeTerminalRequestedHandler(const winrt::Windows::Foundation::IInspectable& /*sender*/, const winrt::Windows::Foundation::IInspectable& /*args*/) { diff --git a/src/cascadia/TerminalApp/TerminalPaneContent.h b/src/cascadia/TerminalApp/TerminalPaneContent.h index 0388aae935..f6277d73f7 100644 --- a/src/cascadia/TerminalApp/TerminalPaneContent.h +++ b/src/cascadia/TerminalApp/TerminalPaneContent.h @@ -76,9 +76,6 @@ namespace winrt::TerminalApp::implementation std::shared_ptr _cache{}; bool _isDefTermSession{ false }; - winrt::Windows::Media::Playback::MediaPlayer _bellPlayer{ nullptr }; - bool _bellPlayerCreated{ false }; - struct ControlEventTokens { winrt::Microsoft::Terminal::Control::TermControl::ConnectionStateChanged_revoker _ConnectionStateChanged; @@ -96,8 +93,6 @@ namespace winrt::TerminalApp::implementation void _setupControlEvents(); void _removeControlEvents(); - safe_void_coroutine _playBellSound(winrt::Windows::Foundation::Uri uri); - safe_void_coroutine _controlConnectionStateChangedHandler(const winrt::Windows::Foundation::IInspectable& sender, const winrt::Windows::Foundation::IInspectable& /*args*/); void _controlWarningBellHandler(const winrt::Windows::Foundation::IInspectable& sender, const winrt::Windows::Foundation::IInspectable& e);