Skip to content

Commit a163a09

Browse files
boost(notification): Merged Custom Sound Notification into Sound Notification (#1352)
Improve the custom sound feature. --------- Co-authored-by: Antoine Aflalo <[email protected]>
1 parent b7e3954 commit a163a09

24 files changed

+396
-590
lines changed

README.de.md

+2-7
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ Wählen Sie zuerst, wie im vorigen Abschnitt beschrieben, Ihre gewünschten Ger
3333

3434
## _Umschalten_ Benachrichtigung
3535

36-
SoundSwitch bietet fünf verschiedenen Arten der Benachrichtigung, wenn ein Gerät gewechselt wird:
36+
SoundSwitch bietet vier verschiedenen Arten der Benachrichtigung, wenn ein Gerät gewechselt wird:
3737

38-
- #### 🎟 Banner
38+
- #### 🎟 Banner Benachrichtigung
3939
Verwendet ein Anzeigeelement, welches über allem liegt (always-on-top), nützlich für die Verwendung in Spielen. Dies ist die Standardart der Benachrichtigung.
4040

4141
- #### 🗨 Windows Benachrichtigung
@@ -44,11 +44,6 @@ Verwendet die Standard Windows Benachrichtigung. Im Falle von Windows 7, ist die
4444
- #### 🎵 Akustische Benachrichtigung
4545
Diese Benachrichtigung ist ein Ton, der über das aktuell aktive Augabegerät abgespielt wird. Auf diese Weise teilt das neue Gerät ihnen mit, dass es ausgewählt ist.
4646

47-
- #### 🎶 Angepasste Akustische Benachrichtigung
48-
Genau wie bei der akustischen Benachrichtigung, aber mit einem Sound, welcher selbst definiert werden kann.
49-
50-
Wenn Sie zur stillen Windows Benachrichtigung zurückkehren möchten, öffnen Sie hierzu den Dateiauswahl-Dialog und drücken Abbrechen. Dadurch wird der eingestellte Sound entfernt.
51-
5247
## Profile
5348

5449
Mithilfe von Profilen ist es möglich zu einem bestimmten Gerät zu wechseln, wenn eine Bedingung eintritt. Profile können für die folgenden Zwecke definiert werden:

README.md

+2-7
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ First set up your devices you want to cycle through, using _right click_ on the
3838

3939
## _Switched_ Notification
4040

41-
SoundSwitch provides five types of notification when a device is changed:
41+
SoundSwitch provides four types of notification when a device is changed:
4242

43-
- #### 🎟 Banner
43+
- #### 🎟 Banner Notification
4444
Uses a custom always-on-top frame, useful for in-game usage. This is the recommended default display style.
4545

4646
- #### 🗨 Windows Notification
@@ -49,11 +49,6 @@ Uses the balloon tip of Windows. In the case of Windows 7, it's the little ballo
4949
- #### 🎵 Sound Notification
5050
This notification is a sound played on the switched device. This way when you are switching devices, the new device will 'chime' to tell you it's selected.
5151

52-
- #### 🎶 Customized Sound Notification
53-
The same as a Sound Notification but you can specify the sound which is played.
54-
55-
If you want to return to a silent Toast Notification, open the file selector, and just press Cancel. Doing that will remove the set sound.
56-
5752
## Profiles
5853

5954
Using profiles, it is possible to switch to a specific device when a condition occurs. Profiles can be defined for the following purposes:

SoundSwitch/Framework/Banner/BannerForm.cs

+9-10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public partial class BannerForm : Form
3535
private BannerData _currentData;
3636
private CancellationTokenSource _cancellationTokenSource = new();
3737

38+
/// <summary>
39+
/// Get the Screen object
40+
/// </summary>
41+
private static Screen GetScreen()
42+
{
43+
return AppModel.Instance.NotifyUsingPrimaryScreen ? Screen.PrimaryScreen : Screen.FromPoint(Cursor.Position);
44+
}
45+
3846
/// <summary>
3947
/// Constructor for the <see cref="BannerForm"/> class
4048
/// </summary>
@@ -64,14 +72,6 @@ public BannerForm()
6472
// }
6573
// }
6674

67-
/// <summary>
68-
///
69-
/// </summary>
70-
private static Screen GetScreen()
71-
{
72-
return AppModel.Instance.NotifyUsingPrimaryScreen ? Screen.PrimaryScreen : Screen.FromPoint(Cursor.Position);
73-
}
74-
7575
/// <summary>
7676
/// Called internally to configure pass notification parameters
7777
/// </summary>
@@ -112,7 +112,7 @@ internal void SetData(BannerData data)
112112

113113
var screen = GetScreen();
114114

115-
Location = data.Position.GetScreenPosition(screen, Height, Width);
115+
Location = data.Position.GetScreenPosition(screen, Height, Width);
116116

117117
_timerHide.Enabled = true;
118118

@@ -154,7 +154,6 @@ protected override void Dispose(bool disposing)
154154
base.Dispose(disposing);
155155
}
156156

157-
158157
/// <summary>
159158
/// Event handler for the "hiding" timer.
160159
/// </summary>

SoundSwitch/Framework/Configuration/SoundSwitchConfiguration.cs

+6
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ public bool Migrate()
167167
NotificationSettings = NotificationTypeEnum.BannerNotification;
168168
}
169169

170+
if (NotificationSettings == NotificationTypeEnum.CustomNotification)
171+
{
172+
NotificationSettings = NotificationTypeEnum.SoundNotification;
173+
migrated = true;
174+
}
175+
170176
#pragma warning disable 612
171177
if (!MigratedFields.Contains(nameof(KeepSystrayIcon)))
172178
{

SoundSwitch/Framework/NotificationManager/Notification/INotification.cs

+18-24
Original file line numberDiff line numberDiff line change
@@ -28,52 +28,46 @@ public interface INotification : IEnumImpl<NotificationTypeEnum>
2828
/// </summary>
2929
INotificationConfiguration Configuration { get; set; }
3030

31-
/// <summary>
32-
/// Does this notification support showing an icon
33-
/// </summary>
34-
bool SupportIcon => false;
35-
3631
/// <summary>
3732
/// Notify the change of default audio device
3833
/// </summary>
3934
/// <param name="audioDevice"></param>
4035
void NotifyDefaultChanged(DeviceFullInfo audioDevice);
4136

4237
/// <summary>
43-
/// Called when the set sound changed
38+
/// Notify when a profile has changed
4439
/// </summary>
45-
/// <param name="newSound"></param>
46-
void OnSoundChanged(CachedSound newSound);
40+
/// <param name="profile"></param>
41+
/// <param name="icon"></param>
42+
/// <param name="processId"></param>
43+
void NotifyProfileChanged(Profile.Profile profile, [CanBeNull] Bitmap icon, uint? processId);
4744

4845
/// <summary>
49-
/// Does the notification support a Custom Sound
46+
/// Notify about the mute state having changed
5047
/// </summary>
51-
/// <returns></returns>
52-
NotificationCustomSoundEnum SupportCustomSound();
48+
void NotifyMuteChanged(string microphoneName, bool newMuteState);
5349

5450
/// <summary>
55-
/// Does the notification need a Custom Sound set to work
51+
/// Does this notification support showing an icon
5652
/// </summary>
57-
/// <returns></returns>
58-
bool NeedCustomSound();
53+
bool SupportIcon => false;
5954

6055
/// <summary>
61-
/// Is this notification available
56+
/// Called when the set sound changed
6257
/// </summary>
63-
/// <returns></returns>
64-
bool IsAvailable();
58+
/// <param name="newSound"></param>
59+
void OnSoundChanged(CachedSound newSound);
6560

6661
/// <summary>
67-
/// Notify when a profile has changed
62+
/// Does the notification support a Custom Sound
6863
/// </summary>
69-
/// <param name="profile"></param>
70-
/// <param name="icon"></param>
71-
/// <param name="processId"></param>
72-
void NotifyProfileChanged(Profile.Profile profile, [CanBeNull] Bitmap icon, uint? processId);
64+
/// <returns></returns>
65+
bool SupportCustomSound();
7366

7467
/// <summary>
75-
/// Notify about the mute state having changed
68+
/// Is this notification available
7669
/// </summary>
77-
void NotifyMuteChanged(string microphoneName, bool newMuteState);
70+
/// <returns></returns>
71+
bool IsAvailable();
7872
}
7973
}

SoundSwitch/Framework/NotificationManager/Notification/NotificationBanner.cs

+36-42
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,48 @@
2020
using SoundSwitch.Common.Framework.Audio.Device;
2121
using SoundSwitch.Framework.Audio;
2222
using SoundSwitch.Framework.Banner;
23+
using SoundSwitch.Framework.Banner.Position;
2324
using SoundSwitch.Framework.NotificationManager.Notification.Configuration;
2425
using SoundSwitch.Localization;
26+
using SoundSwitch.Model;
2527
using SoundSwitch.Properties;
2628

2729
namespace SoundSwitch.Framework.NotificationManager.Notification
2830
{
29-
public class NotificationBanner : INotification
31+
internal class NotificationBanner : INotification
3032
{
3133
public NotificationTypeEnum TypeEnum => NotificationTypeEnum.BannerNotification;
3234
public string Label => SettingsStrings.notificationOptionBanner;
3335

3436
public INotificationConfiguration Configuration { get; set; }
35-
3637
private readonly BannerManager _bannerManager = new();
3738
private readonly BannerPositionFactory _bannerPositionFactory = new();
3839

39-
public bool SupportIcon => true;
40+
private IPosition BannerPosition => _bannerPositionFactory.Get(Configuration.BannerPosition);
41+
42+
public void NotifyDefaultChanged(DeviceFullInfo audioDevice)
43+
{
44+
var toastData = new BannerData
45+
{
46+
Image = audioDevice.LargeIcon.ToBitmap(),
47+
Text = audioDevice.NameClean,
48+
Position = BannerPosition
49+
};
50+
if (CustomSoundCheck(audioDevice))
51+
{
52+
toastData.SoundFile = Configuration.CustomSound;
53+
toastData.CurrentDeviceId = audioDevice.Id;
54+
}
55+
56+
toastData.Title = audioDevice.Type switch
57+
{
58+
DataFlow.Render => SettingsStrings.tooltipOnHoverOptionPlaybackDevice,
59+
DataFlow.Capture => SettingsStrings.tooltipOnHoverOptionRecordingDevice,
60+
_ => throw new ArgumentOutOfRangeException(nameof(audioDevice.Type), audioDevice.Type, null)
61+
};
62+
63+
_bannerManager.ShowNotification(toastData);
64+
}
4065

4166
public void NotifyProfileChanged(Profile.Profile profile, Bitmap icon, uint? processId)
4267
{
@@ -46,7 +71,7 @@ public void NotifyProfileChanged(Profile.Profile profile, Bitmap icon, uint? pro
4671
Image = icon,
4772
Title = string.Format(SettingsStrings.profile_notification_text, profile.Name),
4873
Text = string.Join("\n", profile.Devices.Select(wrapper => wrapper.DeviceInfo.NameClean).Distinct()),
49-
Position = _bannerPositionFactory.Get(Configuration.BannerPosition)
74+
Position = BannerPosition
5075
};
5176
_bannerManager.ShowNotification(bannerData);
5277
}
@@ -62,51 +87,20 @@ public void NotifyMuteChanged(string microphoneName, bool newMuteState)
6287
Priority = 2,
6388
Image = icon,
6489
Title = title,
65-
Position = _bannerPositionFactory.Get(Configuration.BannerPosition)
90+
Position = BannerPosition
6691
};
6792
_bannerManager.ShowNotification(bannerData);
6893
}
6994

70-
public void NotifyDefaultChanged(DeviceFullInfo audioDevice)
71-
{
72-
var toastData = new BannerData
73-
{
74-
Image = audioDevice.LargeIcon.ToBitmap(),
75-
Text = audioDevice.NameClean,
76-
Position = _bannerPositionFactory.Get(Configuration.BannerPosition)
77-
};
78-
if (audioDevice.Type == DataFlow.Render && Configuration.CustomSound != null && File.Exists(Configuration.CustomSound.FilePath))
79-
{
80-
toastData.SoundFile = Configuration.CustomSound;
81-
toastData.CurrentDeviceId = audioDevice.Id;
82-
}
83-
84-
toastData.Title = audioDevice.Type switch
85-
{
86-
DataFlow.Render => SettingsStrings.tooltipOnHoverOptionPlaybackDevice,
87-
DataFlow.Capture => SettingsStrings.tooltipOnHoverOptionRecordingDevice,
88-
_ => throw new ArgumentOutOfRangeException(nameof(audioDevice.Type), audioDevice.Type, null)
89-
};
90-
91-
_bannerManager.ShowNotification(toastData);
92-
}
95+
public bool SupportIcon => true;
9396

94-
public void OnSoundChanged(CachedSound newSound)
95-
{
96-
Configuration.CustomSound = newSound;
97-
}
97+
public void OnSoundChanged(CachedSound newSound) => Configuration.CustomSound = newSound;
9898

99-
public NotificationCustomSoundEnum SupportCustomSound() => NotificationCustomSoundEnum.Optional;
99+
public bool SupportCustomSound() => true;
100100

101-
public bool NeedCustomSound()
102-
{
103-
return false;
104-
}
101+
// Available in all Windows versions
102+
public bool IsAvailable() => true;
105103

106-
public bool IsAvailable()
107-
{
108-
// Available in all Windows versions
109-
return true;
110-
}
104+
public bool CustomSoundCheck(DeviceFullInfo audioDevice) => audioDevice.Type == DataFlow.Render && Configuration.CustomSound != null && File.Exists(Configuration.CustomSound.FilePath);
111105
}
112106
}

SoundSwitch/Framework/NotificationManager/Notification/NotificationCustom.cs

-87
This file was deleted.

0 commit comments

Comments
 (0)