Skip to content

Commit c2f64d8

Browse files
committed
boost(Profile::Icon): Always have an icon for the profile banner notification. Default to the first device of the profile when it's not linked to an application.
Fixes #1109
1 parent 68180bf commit c2f64d8

File tree

7 files changed

+73
-23
lines changed

7 files changed

+73
-23
lines changed

SoundSwitch/Framework/NotificationManager/Notification/INotification.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* GNU General Public License for more details.
1313
********************************************************************/
1414

15+
using System.Drawing;
16+
using JetBrains.Annotations;
1517
using SoundSwitch.Common.Framework.Audio.Device;
1618
using SoundSwitch.Framework.Audio;
1719
using SoundSwitch.Framework.Factory;
@@ -26,6 +28,11 @@ public interface INotification : IEnumImpl<NotificationTypeEnum>
2628
/// </summary>
2729
INotificationConfiguration Configuration { get; set; }
2830

31+
/// <summary>
32+
/// Does this notification support showing an icon
33+
/// </summary>
34+
bool SupportIcon => false;
35+
2936
/// <summary>
3037
/// Notify the change of default audio device
3138
/// </summary>
@@ -60,8 +67,9 @@ public interface INotification : IEnumImpl<NotificationTypeEnum>
6067
/// Notify when a profile has changed
6168
/// </summary>
6269
/// <param name="profile"></param>
70+
/// <param name="icon"></param>
6371
/// <param name="processId"></param>
64-
void NotifyProfileChanged(Profile.Profile profile, uint? processId);
72+
void NotifyProfileChanged(Profile.Profile profile, [CanBeNull] Bitmap icon, uint? processId);
6573

6674
/// <summary>
6775
/// Notify about the mute state having changed

SoundSwitch/Framework/NotificationManager/Notification/NotificationBanner.cs

+4-17
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
********************************************************************/
1414

1515
using System;
16-
using System.Diagnostics;
16+
using System.Drawing;
1717
using System.IO;
1818
using System.Linq;
1919
using NAudio.CoreAudioApi;
2020
using SoundSwitch.Common.Framework.Audio.Device;
21-
using SoundSwitch.Common.Framework.Icon;
2221
using SoundSwitch.Framework.Audio;
2322
using SoundSwitch.Framework.Banner;
2423
using SoundSwitch.Framework.NotificationManager.Notification.Configuration;
@@ -36,22 +35,10 @@ public class NotificationBanner : INotification
3635

3736
private readonly BannerManager _bannerManager = new();
3837

39-
public void NotifyProfileChanged(Profile.Profile profile, uint? processId)
40-
{
41-
var icon = Resources.default_profile_image;
42-
if (processId.HasValue)
43-
{
44-
try
45-
{
46-
var process = Process.GetProcessById((int)processId.Value);
47-
icon = IconExtractor.Extract(process.MainModule?.FileName, 0, true).ToBitmap();
48-
}
49-
catch (Exception)
50-
{
51-
// ignored
52-
}
53-
}
38+
public bool SupportIcon => true;
5439

40+
public void NotifyProfileChanged(Profile.Profile profile, Bitmap icon, uint? processId)
41+
{
5542
var bannerData = new BannerData
5643
{
5744
Priority = 1,

SoundSwitch/Framework/NotificationManager/Notification/NotificationCustom.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
********************************************************************/
1414

1515
using System;
16+
using System.Drawing;
1617
using System.Threading;
1718
using NAudio.CoreAudioApi;
1819
using SoundSwitch.Audio.Manager;
@@ -62,7 +63,7 @@ public bool IsAvailable()
6263
return true;
6364
}
6465

65-
public void NotifyProfileChanged(Profile.Profile profile, uint? processId)
66+
public void NotifyProfileChanged(Profile.Profile profile, Bitmap icon, uint? processId)
6667
{
6768
if (profile.Playback == null)
6869
return;

SoundSwitch/Framework/NotificationManager/Notification/NotificationNone.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* GNU General Public License for more details.
1313
********************************************************************/
1414

15+
using System.Drawing;
1516
using SoundSwitch.Common.Framework.Audio.Device;
1617
using SoundSwitch.Framework.Audio;
1718
using SoundSwitch.Framework.NotificationManager.Notification.Configuration;
@@ -46,7 +47,7 @@ public bool IsAvailable()
4647
return true;
4748
}
4849

49-
public void NotifyProfileChanged(Profile.Profile profile, uint? processId)
50+
public void NotifyProfileChanged(Profile.Profile profile, Bitmap icon, uint? processId)
5051
{
5152
}
5253

SoundSwitch/Framework/NotificationManager/Notification/NotificationSound.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
********************************************************************/
1414

1515
using System;
16+
using System.Drawing;
1617
using System.IO;
1718
using System.Threading;
1819
using NAudio.CoreAudioApi;
@@ -62,7 +63,7 @@ public bool IsAvailable()
6263
return true;
6364
}
6465

65-
public void NotifyProfileChanged(Profile.Profile profile, uint? processId)
66+
public void NotifyProfileChanged(Profile.Profile profile, Bitmap icon, uint? processId)
6667
{
6768
if (profile.Playback == null)
6869
return;

SoundSwitch/Framework/NotificationManager/Notification/NotificationWindows.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
********************************************************************/
1414

1515
using System;
16+
using System.Drawing;
1617
using System.Linq;
1718
using System.Windows.Forms;
1819
using NAudio.CoreAudioApi;
@@ -65,7 +66,7 @@ public bool IsAvailable()
6566
return true;
6667
}
6768

68-
public void NotifyProfileChanged(Profile.Profile profile, uint? processId)
69+
public void NotifyProfileChanged(Profile.Profile profile, Bitmap icon, uint? processId)
6970
{
7071
var title = string.Format(SettingsStrings.profile_notification_text, profile.Name);
7172
var text = string.Join("\n", profile.Devices.Select(wrapper => wrapper.DeviceInfo.NameClean));

SoundSwitch/Framework/NotificationManager/NotificationManager.cs

+52-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@
1212
* GNU General Public License for more details.
1313
********************************************************************/
1414

15+
using System;
16+
using System.Diagnostics;
17+
using System.Drawing;
18+
using System.Linq;
1519
using System.Windows.Forms;
20+
using NAudio.CoreAudioApi;
1621
using Serilog;
22+
using SoundSwitch.Common.Framework.Audio.Device;
23+
using SoundSwitch.Common.Framework.Icon;
1724
using SoundSwitch.Framework.Audio;
1825
using SoundSwitch.Framework.NotificationManager.Notification;
1926
using SoundSwitch.Framework.NotificationManager.Notification.Configuration;
@@ -90,6 +97,15 @@ private void ModelOnDefaultDeviceChanged(object sender, DeviceDefaultChangedEven
9097
_lastDeviceId = deviceDefaultChangedEvent.DeviceId;
9198
}
9299

100+
private DeviceFullInfo? CheckDeviceAvailable(DeviceInfo deviceInfo)
101+
{
102+
return deviceInfo.Type switch
103+
{
104+
DataFlow.Capture => _model.AvailableRecordingDevices.FirstOrDefault(info => info.Equals(deviceInfo)),
105+
_ => _model.AvailablePlaybackDevices.FirstOrDefault(info => info.Equals(deviceInfo))
106+
};
107+
}
108+
93109
/// <summary>
94110
/// Notify on Profile changed
95111
/// </summary>
@@ -100,7 +116,42 @@ public void NotifyProfileChanged(Profile.Profile profile, uint? processId)
100116
return;
101117
}
102118

103-
_notification.NotifyProfileChanged(profile, processId);
119+
var icon = GetIcon(profile, processId);
120+
121+
_notification.NotifyProfileChanged(profile, icon, processId);
122+
}
123+
124+
private Bitmap GetIcon(Profile.Profile profile, uint? processId)
125+
{
126+
if (!_notification.SupportIcon)
127+
{
128+
return null;
129+
}
130+
131+
Bitmap icon = null;
132+
if (processId.HasValue)
133+
{
134+
try
135+
{
136+
var process = Process.GetProcessById((int)processId.Value);
137+
icon = IconExtractor.Extract(process.MainModule?.FileName, 0, true).ToBitmap();
138+
}
139+
catch (Exception)
140+
{
141+
// ignored
142+
}
143+
}
144+
145+
if (icon == null)
146+
{
147+
var device = profile.Devices.Select(wrapper => CheckDeviceAvailable(wrapper.DeviceInfo)).FirstOrDefault(info => info != null);
148+
if (device != null)
149+
{
150+
icon = device.LargeIcon.ToBitmap();
151+
}
152+
}
153+
154+
return icon ?? Resources.default_profile_image;
104155
}
105156

106157
public void NotifyMuteChanged(string microphoneName, bool newMuteState)

0 commit comments

Comments
 (0)