Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable changing the bell sound #11511

Merged
16 commits merged into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/cascadia/TerminalApp/Pane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1131,8 +1131,21 @@ void Pane::_ControlWarningBellHandler(const winrt::Windows::Foundation::IInspect
if (WI_IsFlagSet(_profile.BellStyle(), winrt::Microsoft::Terminal::Settings::Model::BellStyle::Audible))
{
// Audible is set, play the sound
const auto soundAlias = reinterpret_cast<LPCTSTR>(SND_ALIAS_SYSTEMHAND);
PlaySound(soundAlias, NULL, SND_ALIAS_ID | SND_ASYNC | SND_SENTRY);
auto sounds{ _profile.BellSound() };
if (sounds && sounds.Size() > 0)
{
winrt::Windows::Foundation::Uri uri{ sounds.GetAt(rand() % sounds.Size()) };
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
auto source{ winrt::Windows::Media::Core::MediaSource::CreateFromUri(uri) };
auto item{ winrt::Windows::Media::Playback::MediaPlaybackItem(source) };
p.Source(item);
p.Play();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the URI points to a video file or stream?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

omfg it's great.

Say you had a file that was no.mp4. If you set the path to "bellSound": "%~%\\Downloads\\memes\\no.mp4", then it'll just play the entirety of the audio of the mp4 file. It's great. If you emit another bell in the middle, then it'll just start it over.

There's a lot of possibilities here.

}
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))
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/Pane.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ class Pane : public std::enable_shared_from_this<Pane>

bool _zoomed{ false };

winrt::Windows::Media::Playback::MediaPlayer p{};
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved

bool _IsLeaf() const noexcept;
bool _HasFocusedChild() const noexcept;
void _SetupChildCloseHandlers();
Expand Down
5 changes: 4 additions & 1 deletion src/cascadia/TerminalApp/pch.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
// pch.h
Expand Down Expand Up @@ -45,6 +45,9 @@
#include <winrt/Windows.UI.Xaml.Markup.h>
#include <winrt/Windows.UI.Xaml.Media.h>
#include <winrt/Windows.UI.Xaml.Media.Animation.h>
#include <winrt/Windows.Media.h>
#include <winrt/Windows.Media.Core.h>
#include <winrt/Windows.Media.Playback.h>

#include <winrt/Microsoft.Toolkit.Win32.UI.XamlHost.h>
#include <winrt/Microsoft.UI.Xaml.Controls.h>
Expand Down
15 changes: 12 additions & 3 deletions src/cascadia/TerminalSettingsModel/JsonUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,16 @@ namespace Microsoft::Terminal::Settings::Model::JsonUtils
val.reserve(json.size());

ConversionTrait<T> trait;
for (const auto& element : json)
if (json.isArray())
{
for (const auto& element : json)
{
val.push_back(trait.FromJson(element));
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
}
}
else
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to self: this is the place I should just check for null and skip

{
val.push_back(trait.FromJson(element));
val.push_back(trait.FromJson(json));
}

return val;
Expand All @@ -318,7 +325,9 @@ namespace Microsoft::Terminal::Settings::Model::JsonUtils
bool CanConvert(const Json::Value& json) const
{
ConversionTrait<T> trait;
return json.isArray() && std::all_of(json.begin(), json.end(), [trait](const auto& json) mutable -> bool { return trait.CanConvert(json); });
// If there's only one element provided, then see if we can convert
// that single element into a length-1 array
return (json.isArray() && std::all_of(json.begin(), json.end(), [trait](const auto& json) mutable -> bool { return trait.CanConvert(json); })) || trait.CanConvert(json);
}

Json::Value ToJson(const std::vector<T>& val)
Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/TerminalSettingsModel/Profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static constexpr std::string_view AntialiasingModeKey{ "antialiasingMode" };
static constexpr std::string_view TabColorKey{ "tabColor" };
static constexpr std::string_view BellStyleKey{ "bellStyle" };
static constexpr std::string_view UnfocusedAppearanceKey{ "unfocusedAppearance" };
static constexpr std::string_view BellSoundKey{ "bellSound" };

Profile::Profile(guid guid) noexcept :
_Guid(guid)
Expand Down Expand Up @@ -134,6 +135,7 @@ winrt::com_ptr<Profile> Profile::CopySettings() const
profile->_SnapOnInput = _SnapOnInput;
profile->_AltGrAliasing = _AltGrAliasing;
profile->_BellStyle = _BellStyle;
profile->_BellSound = _BellSound;
profile->_ConnectionType = _ConnectionType;
profile->_Origin = _Origin;
profile->_FontInfo = *fontInfo;
Expand Down Expand Up @@ -221,6 +223,7 @@ void Profile::LayerJson(const Json::Value& json)
JsonUtils::GetValueForKey(json, AntialiasingModeKey, _AntialiasingMode);
JsonUtils::GetValueForKey(json, TabColorKey, _TabColor);
JsonUtils::GetValueForKey(json, BellStyleKey, _BellStyle);
JsonUtils::GetValueForKey(json, BellSoundKey, _BellSound);

if (json.isMember(JsonKey(UnfocusedAppearanceKey)))
{
Expand Down Expand Up @@ -374,6 +377,7 @@ Json::Value Profile::ToJson() const
JsonUtils::SetValueForKey(json, AntialiasingModeKey, _AntialiasingMode);
JsonUtils::SetValueForKey(json, TabColorKey, _TabColor);
JsonUtils::SetValueForKey(json, BellStyleKey, _BellStyle);
JsonUtils::SetValueForKey(json, BellSoundKey, _BellSound);

// Font settings
const auto fontInfoImpl = winrt::get_self<FontConfig>(_FontInfo);
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/Profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

INHERITABLE_SETTING(Model::Profile, Model::IAppearanceConfig, UnfocusedAppearance, nullptr);

INHERITABLE_SETTING(Model::Profile, Windows::Foundation::Collections::IVector<winrt::hstring>, BellSound);

private:
Model::IAppearanceConfig _DefaultAppearance{ winrt::make<AppearanceConfig>(weak_ref<Model::Profile>(*this)) };
Model::FontConfig _FontInfo{ winrt::make<FontConfig>(weak_ref<Model::Profile>(*this)) };
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/Profile.idl
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,7 @@ namespace Microsoft.Terminal.Settings.Model
INHERITABLE_PROFILE_SETTING(Boolean, SnapOnInput);
INHERITABLE_PROFILE_SETTING(Boolean, AltGrAliasing);
INHERITABLE_PROFILE_SETTING(BellStyle, BellStyle);

INHERITABLE_PROFILE_SETTING(Windows.Foundation.Collections.IVector<String>, BellSound);
}
}