Skip to content

Commit b33ef84

Browse files
committed
fix(IconChanger): Fix issue where the icon wouldn't change when the default device is switched.
1 parent 3be7eae commit b33ef84

File tree

6 files changed

+36
-76
lines changed

6 files changed

+36
-76
lines changed

SoundSwitch/Framework/TrayIcon/Icon/Changer/AbstractIconChanger.cs

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
1-
using SoundSwitch.Common.Framework.Audio.Device;
1+
using NAudio.CoreAudioApi;
2+
using SoundSwitch.Audio.Manager;
3+
using SoundSwitch.Audio.Manager.Interop.Enum;
4+
using SoundSwitch.Common.Framework.Audio.Device;
25

36
namespace SoundSwitch.Framework.TrayIcon.Icon.Changer
47
{
58
public abstract class AbstractIconChanger : IIconChanger
69
{
710
public abstract IconChangerFactory.ActionEnum TypeEnum { get; }
811
public abstract string Label { get; }
9-
public abstract bool NeedsToChangeIcon(DeviceInfo deviceInfo);
10-
public abstract void ChangeIcon(UI.Component.TrayIcon trayIcon);
12+
13+
protected abstract DataFlow Flow { get; }
14+
15+
protected virtual bool NeedsToChangeIcon(DeviceInfo deviceInfo)
16+
{
17+
return deviceInfo.Type == Flow;
18+
}
19+
20+
public void ChangeIcon(UI.Component.TrayIcon trayIcon)
21+
{
22+
var audio = AudioSwitcher.Instance.GetDefaultAudioEndpoint((EDataFlow)Flow, ERole.eConsole);
23+
ChangeIcon(trayIcon, audio);
24+
}
1125

1226
public void ChangeIcon(UI.Component.TrayIcon trayIcon, DeviceFullInfo deviceInfo)
1327
{
14-
if (!NeedsToChangeIcon(deviceInfo)) return;
28+
if (deviceInfo == null)
29+
{
30+
return;
31+
}
32+
33+
if (!NeedsToChangeIcon(deviceInfo))
34+
{
35+
return;
36+
}
37+
38+
1539
trayIcon.ReplaceIcon(deviceInfo.SmallIcon);
1640
}
1741
}
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,19 @@
11
using NAudio.CoreAudioApi;
22
using SoundSwitch.Common.Framework.Audio.Device;
33
using SoundSwitch.Localization;
4-
using SoundSwitch.Properties;
54

65
namespace SoundSwitch.Framework.TrayIcon.Icon.Changer
76
{
87
public class AlwaysIconChanger : AbstractIconChanger
98
{
109
public override IconChangerFactory.ActionEnum TypeEnum => IconChangerFactory.ActionEnum.Always;
1110
public override string Label => TrayIconStrings.iconChanger_both;
12-
internal const int E_NOT_SET = unchecked((int)0x80070490);
13-
public override bool NeedsToChangeIcon(DeviceInfo deviceInfo)
11+
12+
protected override bool NeedsToChangeIcon(DeviceInfo deviceInfo)
1413
{
1514
return true;
1615
}
1716

18-
public override void ChangeIcon(UI.Component.TrayIcon trayIcon)
19-
{
20-
using var enumerator = new MMDeviceEnumerator();
21-
try
22-
{
23-
using var defaultAudio = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
24-
trayIcon.ReplaceIcon(new DeviceFullInfo(defaultAudio).SmallIcon);
25-
}
26-
catch (System.Runtime.InteropServices.COMException e)
27-
{
28-
// Only handle "Element Not Found"
29-
if (e.ErrorCode == E_NOT_SET)
30-
{
31-
// Set to app icon
32-
trayIcon.ReplaceIcon(Resources.Switch_SoundWave);
33-
}
34-
else
35-
{
36-
// Throw other ErrorCodes
37-
throw e;
38-
}
39-
}
40-
}
17+
protected override DataFlow Flow => DataFlow.Render;
4118
}
4219
}

SoundSwitch/Framework/TrayIcon/Icon/Changer/NeverIconIconChanger.cs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using SoundSwitch.Common.Framework.Audio.Device;
1+
using SoundSwitch.Common.Framework.Audio.Device;
32
using SoundSwitch.Localization;
43
using SoundSwitch.Properties;
54

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

13-
public bool NeedsToChangeIcon(DeviceInfo deviceInfo)
14-
{
15-
return false;
16-
}
17-
1812
public void ChangeIcon(UI.Component.TrayIcon trayIcon)
1913
{
2014
trayIcon.ReplaceIcon(Resources.Switch_SoundWave);
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
11
using NAudio.CoreAudioApi;
2-
using SoundSwitch.Audio.Manager;
3-
using SoundSwitch.Audio.Manager.Interop.Enum;
4-
using SoundSwitch.Common.Framework.Audio.Device;
52
using SoundSwitch.Localization;
6-
using SoundSwitch.Model;
73

84
namespace SoundSwitch.Framework.TrayIcon.Icon.Changer
95
{
106
public class PlaybackIconChanger : AbstractIconChanger
117
{
128
public override IconChangerFactory.ActionEnum TypeEnum => IconChangerFactory.ActionEnum.Playback;
13-
public override string Label => TrayIconStrings.iconChanger_playback;
14-
15-
public override bool NeedsToChangeIcon(DeviceInfo deviceInfo)
16-
{
17-
return deviceInfo.Type == DataFlow.Render;
18-
}
19-
20-
public override void ChangeIcon(UI.Component.TrayIcon trayIcon)
21-
{
22-
var defaultAudio = AudioSwitcher.Instance.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eConsole);
23-
if (defaultAudio != null)
24-
trayIcon.ReplaceIcon(defaultAudio.SmallIcon);
25-
}
9+
public override string Label => TrayIconStrings.iconChanger_playback;
10+
protected override DataFlow Flow => DataFlow.Render;
2611
}
2712
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
using NAudio.CoreAudioApi;
2-
using SoundSwitch.Audio.Manager;
3-
using SoundSwitch.Audio.Manager.Interop.Enum;
4-
using SoundSwitch.Common.Framework.Audio.Device;
52
using SoundSwitch.Localization;
63

74
namespace SoundSwitch.Framework.TrayIcon.Icon.Changer
85
{
96
public class RecordingIconChanger : AbstractIconChanger
107
{
118
public override IconChangerFactory.ActionEnum TypeEnum => IconChangerFactory.ActionEnum.Recording;
12-
public override string Label => TrayIconStrings.iconChanger_recording;
13-
14-
public override bool NeedsToChangeIcon(DeviceInfo deviceInfo)
15-
{
16-
return deviceInfo.Type == DataFlow.Capture;
17-
}
18-
19-
public override void ChangeIcon(UI.Component.TrayIcon trayIcon)
20-
{
21-
var defaultAudio = AudioSwitcher.Instance.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eConsole);
22-
if (defaultAudio != null)
23-
trayIcon.ReplaceIcon(defaultAudio.SmallIcon);
24-
}
9+
public override string Label => TrayIconStrings.iconChanger_recording;
10+
protected override DataFlow Flow => DataFlow.Capture;
2511
}
2612
}

SoundSwitch/Framework/TrayIcon/Icon/IIconChanger.cs

-6
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ namespace SoundSwitch.Framework.TrayIcon.Icon
55
{
66
public interface IIconChanger : IEnumImpl<IconChangerFactory.ActionEnum>
77
{
8-
/// <summary>
9-
/// Should the icon change
10-
/// </summary>
11-
/// <param name="deviceInfo"></param>
12-
/// <returns></returns>
13-
bool NeedsToChangeIcon(DeviceInfo deviceInfo);
148

159
/// <summary>
1610
/// Change the icon to the current default device

0 commit comments

Comments
 (0)