Skip to content

Commit

Permalink
boost(Profile::Icon): Always have an icon for the profile banner noti…
Browse files Browse the repository at this point in the history
…fication. Default to the first device of the profile when it's not linked to an application.

Fixes #1109
  • Loading branch information
Belphemur committed Feb 4, 2023
1 parent 68180bf commit c2f64d8
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* GNU General Public License for more details.
********************************************************************/

using System.Drawing;
using JetBrains.Annotations;
using SoundSwitch.Common.Framework.Audio.Device;
using SoundSwitch.Framework.Audio;
using SoundSwitch.Framework.Factory;
Expand All @@ -26,6 +28,11 @@ public interface INotification : IEnumImpl<NotificationTypeEnum>
/// </summary>
INotificationConfiguration Configuration { get; set; }

/// <summary>
/// Does this notification support showing an icon
/// </summary>
bool SupportIcon => false;

/// <summary>
/// Notify the change of default audio device
/// </summary>
Expand Down Expand Up @@ -60,8 +67,9 @@ public interface INotification : IEnumImpl<NotificationTypeEnum>
/// Notify when a profile has changed
/// </summary>
/// <param name="profile"></param>
/// <param name="icon"></param>
/// <param name="processId"></param>
void NotifyProfileChanged(Profile.Profile profile, uint? processId);
void NotifyProfileChanged(Profile.Profile profile, [CanBeNull] Bitmap icon, uint? processId);

/// <summary>
/// Notify about the mute state having changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
********************************************************************/

using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using NAudio.CoreAudioApi;
using SoundSwitch.Common.Framework.Audio.Device;
using SoundSwitch.Common.Framework.Icon;
using SoundSwitch.Framework.Audio;
using SoundSwitch.Framework.Banner;
using SoundSwitch.Framework.NotificationManager.Notification.Configuration;
Expand All @@ -36,22 +35,10 @@ public class NotificationBanner : INotification

private readonly BannerManager _bannerManager = new();

public void NotifyProfileChanged(Profile.Profile profile, uint? processId)
{
var icon = Resources.default_profile_image;
if (processId.HasValue)
{
try
{
var process = Process.GetProcessById((int)processId.Value);
icon = IconExtractor.Extract(process.MainModule?.FileName, 0, true).ToBitmap();
}
catch (Exception)
{
// ignored
}
}
public bool SupportIcon => true;

public void NotifyProfileChanged(Profile.Profile profile, Bitmap icon, uint? processId)
{
var bannerData = new BannerData
{
Priority = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
********************************************************************/

using System;
using System.Drawing;
using System.Threading;
using NAudio.CoreAudioApi;
using SoundSwitch.Audio.Manager;
Expand Down Expand Up @@ -62,7 +63,7 @@ public bool IsAvailable()
return true;
}

public void NotifyProfileChanged(Profile.Profile profile, uint? processId)
public void NotifyProfileChanged(Profile.Profile profile, Bitmap icon, uint? processId)
{
if (profile.Playback == null)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* GNU General Public License for more details.
********************************************************************/

using System.Drawing;
using SoundSwitch.Common.Framework.Audio.Device;
using SoundSwitch.Framework.Audio;
using SoundSwitch.Framework.NotificationManager.Notification.Configuration;
Expand Down Expand Up @@ -46,7 +47,7 @@ public bool IsAvailable()
return true;
}

public void NotifyProfileChanged(Profile.Profile profile, uint? processId)
public void NotifyProfileChanged(Profile.Profile profile, Bitmap icon, uint? processId)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
********************************************************************/

using System;
using System.Drawing;
using System.IO;
using System.Threading;
using NAudio.CoreAudioApi;
Expand Down Expand Up @@ -62,7 +63,7 @@ public bool IsAvailable()
return true;
}

public void NotifyProfileChanged(Profile.Profile profile, uint? processId)
public void NotifyProfileChanged(Profile.Profile profile, Bitmap icon, uint? processId)
{
if (profile.Playback == null)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
********************************************************************/

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using NAudio.CoreAudioApi;
Expand Down Expand Up @@ -65,7 +66,7 @@ public bool IsAvailable()
return true;
}

public void NotifyProfileChanged(Profile.Profile profile, uint? processId)
public void NotifyProfileChanged(Profile.Profile profile, Bitmap icon, uint? processId)
{
var title = string.Format(SettingsStrings.profile_notification_text, profile.Name);
var text = string.Join("\n", profile.Devices.Select(wrapper => wrapper.DeviceInfo.NameClean));
Expand Down
53 changes: 52 additions & 1 deletion SoundSwitch/Framework/NotificationManager/NotificationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
* GNU General Public License for more details.
********************************************************************/

using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using NAudio.CoreAudioApi;
using Serilog;
using SoundSwitch.Common.Framework.Audio.Device;
using SoundSwitch.Common.Framework.Icon;
using SoundSwitch.Framework.Audio;
using SoundSwitch.Framework.NotificationManager.Notification;
using SoundSwitch.Framework.NotificationManager.Notification.Configuration;
Expand Down Expand Up @@ -90,6 +97,15 @@ private void ModelOnDefaultDeviceChanged(object sender, DeviceDefaultChangedEven
_lastDeviceId = deviceDefaultChangedEvent.DeviceId;
}

private DeviceFullInfo? CheckDeviceAvailable(DeviceInfo deviceInfo)
{
return deviceInfo.Type switch
{
DataFlow.Capture => _model.AvailableRecordingDevices.FirstOrDefault(info => info.Equals(deviceInfo)),
_ => _model.AvailablePlaybackDevices.FirstOrDefault(info => info.Equals(deviceInfo))
};
}

/// <summary>
/// Notify on Profile changed
/// </summary>
Expand All @@ -100,7 +116,42 @@ public void NotifyProfileChanged(Profile.Profile profile, uint? processId)
return;
}

_notification.NotifyProfileChanged(profile, processId);
var icon = GetIcon(profile, processId);

_notification.NotifyProfileChanged(profile, icon, processId);
}

private Bitmap GetIcon(Profile.Profile profile, uint? processId)
{
if (!_notification.SupportIcon)
{
return null;
}

Bitmap icon = null;
if (processId.HasValue)
{
try
{
var process = Process.GetProcessById((int)processId.Value);
icon = IconExtractor.Extract(process.MainModule?.FileName, 0, true).ToBitmap();
}
catch (Exception)
{
// ignored
}
}

if (icon == null)
{
var device = profile.Devices.Select(wrapper => CheckDeviceAvailable(wrapper.DeviceInfo)).FirstOrDefault(info => info != null);
if (device != null)
{
icon = device.LargeIcon.ToBitmap();
}
}

return icon ?? Resources.default_profile_image;
}

public void NotifyMuteChanged(string microphoneName, bool newMuteState)
Expand Down

0 comments on commit c2f64d8

Please sign in to comment.