Skip to content

Commit

Permalink
fix(IconChanger): Fix issue where the icon wouldn't change when the d…
Browse files Browse the repository at this point in the history
…efault device is switched.
  • Loading branch information
Belphemur committed Sep 29, 2021
1 parent 3be7eae commit b33ef84
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 76 deletions.
32 changes: 28 additions & 4 deletions SoundSwitch/Framework/TrayIcon/Icon/Changer/AbstractIconChanger.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
using SoundSwitch.Common.Framework.Audio.Device;
using NAudio.CoreAudioApi;
using SoundSwitch.Audio.Manager;
using SoundSwitch.Audio.Manager.Interop.Enum;
using SoundSwitch.Common.Framework.Audio.Device;

namespace SoundSwitch.Framework.TrayIcon.Icon.Changer
{
public abstract class AbstractIconChanger : IIconChanger
{
public abstract IconChangerFactory.ActionEnum TypeEnum { get; }
public abstract string Label { get; }
public abstract bool NeedsToChangeIcon(DeviceInfo deviceInfo);
public abstract void ChangeIcon(UI.Component.TrayIcon trayIcon);

protected abstract DataFlow Flow { get; }

protected virtual bool NeedsToChangeIcon(DeviceInfo deviceInfo)
{
return deviceInfo.Type == Flow;
}

public void ChangeIcon(UI.Component.TrayIcon trayIcon)
{
var audio = AudioSwitcher.Instance.GetDefaultAudioEndpoint((EDataFlow)Flow, ERole.eConsole);
ChangeIcon(trayIcon, audio);
}

public void ChangeIcon(UI.Component.TrayIcon trayIcon, DeviceFullInfo deviceInfo)
{
if (!NeedsToChangeIcon(deviceInfo)) return;
if (deviceInfo == null)
{
return;
}

if (!NeedsToChangeIcon(deviceInfo))
{
return;
}


trayIcon.ReplaceIcon(deviceInfo.SmallIcon);
}
}
Expand Down
29 changes: 3 additions & 26 deletions SoundSwitch/Framework/TrayIcon/Icon/Changer/AlwaysIconChanger.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,19 @@
using NAudio.CoreAudioApi;
using SoundSwitch.Common.Framework.Audio.Device;
using SoundSwitch.Localization;
using SoundSwitch.Properties;

namespace SoundSwitch.Framework.TrayIcon.Icon.Changer
{
public class AlwaysIconChanger : AbstractIconChanger
{
public override IconChangerFactory.ActionEnum TypeEnum => IconChangerFactory.ActionEnum.Always;
public override string Label => TrayIconStrings.iconChanger_both;
internal const int E_NOT_SET = unchecked((int)0x80070490);
public override bool NeedsToChangeIcon(DeviceInfo deviceInfo)

protected override bool NeedsToChangeIcon(DeviceInfo deviceInfo)
{
return true;
}

public override void ChangeIcon(UI.Component.TrayIcon trayIcon)
{
using var enumerator = new MMDeviceEnumerator();
try
{
using var defaultAudio = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
trayIcon.ReplaceIcon(new DeviceFullInfo(defaultAudio).SmallIcon);
}
catch (System.Runtime.InteropServices.COMException e)
{
// Only handle "Element Not Found"
if (e.ErrorCode == E_NOT_SET)
{
// Set to app icon
trayIcon.ReplaceIcon(Resources.Switch_SoundWave);
}
else
{
// Throw other ErrorCodes
throw e;
}
}
}
protected override DataFlow Flow => DataFlow.Render;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using SoundSwitch.Common.Framework.Audio.Device;
using SoundSwitch.Common.Framework.Audio.Device;
using SoundSwitch.Localization;
using SoundSwitch.Properties;

Expand All @@ -10,11 +9,6 @@ public class NeverIconIconChanger : IIconChanger
public IconChangerFactory.ActionEnum TypeEnum => IconChangerFactory.ActionEnum.Never;
public string Label => TrayIconStrings.iconChanger_none;

public bool NeedsToChangeIcon(DeviceInfo deviceInfo)
{
return false;
}

public void ChangeIcon(UI.Component.TrayIcon trayIcon)
{
trayIcon.ReplaceIcon(Resources.Switch_SoundWave);
Expand Down
19 changes: 2 additions & 17 deletions SoundSwitch/Framework/TrayIcon/Icon/Changer/PlaybackIconChanger.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
using NAudio.CoreAudioApi;
using SoundSwitch.Audio.Manager;
using SoundSwitch.Audio.Manager.Interop.Enum;
using SoundSwitch.Common.Framework.Audio.Device;
using SoundSwitch.Localization;
using SoundSwitch.Model;

namespace SoundSwitch.Framework.TrayIcon.Icon.Changer
{
public class PlaybackIconChanger : AbstractIconChanger
{
public override IconChangerFactory.ActionEnum TypeEnum => IconChangerFactory.ActionEnum.Playback;
public override string Label => TrayIconStrings.iconChanger_playback;

public override bool NeedsToChangeIcon(DeviceInfo deviceInfo)
{
return deviceInfo.Type == DataFlow.Render;
}

public override void ChangeIcon(UI.Component.TrayIcon trayIcon)
{
var defaultAudio = AudioSwitcher.Instance.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eConsole);
if (defaultAudio != null)
trayIcon.ReplaceIcon(defaultAudio.SmallIcon);
}
public override string Label => TrayIconStrings.iconChanger_playback;
protected override DataFlow Flow => DataFlow.Render;
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
using NAudio.CoreAudioApi;
using SoundSwitch.Audio.Manager;
using SoundSwitch.Audio.Manager.Interop.Enum;
using SoundSwitch.Common.Framework.Audio.Device;
using SoundSwitch.Localization;

namespace SoundSwitch.Framework.TrayIcon.Icon.Changer
{
public class RecordingIconChanger : AbstractIconChanger
{
public override IconChangerFactory.ActionEnum TypeEnum => IconChangerFactory.ActionEnum.Recording;
public override string Label => TrayIconStrings.iconChanger_recording;

public override bool NeedsToChangeIcon(DeviceInfo deviceInfo)
{
return deviceInfo.Type == DataFlow.Capture;
}

public override void ChangeIcon(UI.Component.TrayIcon trayIcon)
{
var defaultAudio = AudioSwitcher.Instance.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eConsole);
if (defaultAudio != null)
trayIcon.ReplaceIcon(defaultAudio.SmallIcon);
}
public override string Label => TrayIconStrings.iconChanger_recording;
protected override DataFlow Flow => DataFlow.Capture;
}
}
6 changes: 0 additions & 6 deletions SoundSwitch/Framework/TrayIcon/Icon/IIconChanger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ namespace SoundSwitch.Framework.TrayIcon.Icon
{
public interface IIconChanger : IEnumImpl<IconChangerFactory.ActionEnum>
{
/// <summary>
/// Should the icon change
/// </summary>
/// <param name="deviceInfo"></param>
/// <returns></returns>
bool NeedsToChangeIcon(DeviceInfo deviceInfo);

/// <summary>
/// Change the icon to the current default device
Expand Down

0 comments on commit b33ef84

Please sign in to comment.