Skip to content

Commit 8205809

Browse files
committed
fix(TrayIcon::Menu): Fix issue with the menu not showing up when it should.
Fixes #635
1 parent 4e2b51f commit 8205809

File tree

4 files changed

+28
-45
lines changed

4 files changed

+28
-45
lines changed

SoundSwitch/Framework/Audio/Lister/CachedAudioDeviceLister.cs

+4-32
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,14 @@ namespace SoundSwitch.Framework.Audio.Lister
2727
public class CachedAudioDeviceLister : IAudioDeviceLister
2828
{
2929
/// <inheritdoc />
30-
public IReadOnlyCollection<DeviceFullInfo> PlaybackDevices
31-
{
32-
get
33-
{
34-
lock (_lockPlayback)
35-
{
36-
return _playbackList;
37-
}
38-
}
39-
}
30+
public IReadOnlyCollection<DeviceFullInfo> PlaybackDevices { get; private set; } = new DeviceFullInfo[0];
4031

4132
/// <inheritdoc />
42-
public IReadOnlyCollection<DeviceFullInfo> RecordingDevices
43-
{
44-
get
45-
{
46-
lock (_lockRecording)
47-
{
48-
return _recordingList;
49-
}
50-
}
51-
}
33+
public IReadOnlyCollection<DeviceFullInfo> RecordingDevices { get; private set; } = new DeviceFullInfo[0];
5234

5335
private readonly DeviceState _state;
5436
private readonly DebounceDispatcher _dispatcher = new();
5537
private readonly object _lockRefresh = new();
56-
private readonly object _lockRecording = new();
57-
private readonly object _lockPlayback = new();
58-
private IReadOnlyCollection<DeviceFullInfo> _playbackList = new DeviceFullInfo[0];
59-
private IReadOnlyCollection<DeviceFullInfo> _recordingList = new DeviceFullInfo[0];
6038

6139

6240
public CachedAudioDeviceLister(DeviceState state)
@@ -108,15 +86,9 @@ public void Refresh()
10886
}
10987
}
11088

111-
lock (_lockPlayback)
112-
{
113-
_playbackList = playbackDevices.Values.ToArray();
114-
}
89+
PlaybackDevices = playbackDevices.Values.ToArray();
90+
RecordingDevices = recordingDevices.Values.ToArray();
11591

116-
lock (_lockRecording)
117-
{
118-
_recordingList = recordingDevices.Values.ToArray();
119-
}
12092

12193
Log.Information("[{@State}] Refreshed all devices. {@Recording}/rec, {@Playback}/play", _state, recordingDevices.Count, playbackDevices.Count);
12294
}

SoundSwitch/Framework/Logger/Configuration/LoggerConfigurator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static void ConfigureLogger()
2020
.Enrich.WithExceptionDetails()
2121
.Enrich.WithCaller()
2222
#if DEBUG
23-
.WriteTo.Console(theme: Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme.Code)
23+
.WriteTo.Console(LogEventLevel.Verbose, "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message} (at {Caller}){NewLine}{Exception}", theme: Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme.Code)
2424
#endif
2525
.WriteTo.File(Path.Combine(ApplicationPath.Logs, "soundswitch.log"),
2626
rollingInterval: RollingInterval.Day, retainedFileCountLimit: 3,

SoundSwitch/UI/Component/TrayIcon.cs

+21-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
using System.Windows.Forms;
2424
using NAudio.CoreAudioApi;
2525
using Serilog;
26+
using SoundSwitch.Audio.Manager;
27+
using SoundSwitch.Audio.Manager.Interop.Enum;
2628
using SoundSwitch.Common.Framework.Audio.Device;
2729
using SoundSwitch.Framework;
2830
using SoundSwitch.Framework.Configuration;
@@ -72,10 +74,12 @@ public sealed class TrayIcon : IDisposable
7274
private readonly ToolStripMenuItem _updateMenuItem;
7375
private TimerForm _animationTimer;
7476
private readonly UpdateDownloadForm _updateDownloadForm;
77+
private readonly MethodInfo? _showContextMenu;
7578

7679
public TrayIcon()
7780
{
7881
UpdateIcon();
82+
_showContextMenu = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
7983
_tooltipInfoManager = new TooltipInfoManager(NotifyIcon);
8084
_profileTrayIconBuilder = new ProfileTrayIconBuilder();
8185

@@ -89,10 +93,18 @@ public TrayIcon()
8993

9094
_selectionMenu.Items.Add(TrayIconStrings.noDevicesSelected, RessourceSettingsSmallBitmap, (sender, e) => ShowSettings());
9195

92-
NotifyIcon.MouseDoubleClick += (sender, args) => { AppModel.Instance.CycleActiveDevice(DataFlow.Render); };
96+
//NotifyIcon.MouseDoubleClick += (sender, args) => { AppModel.Instance.CycleActiveDevice(DataFlow.Render); };
9397

94-
NotifyIcon.MouseClick += (sender, e) =>
98+
NotifyIcon.MouseDown += (sender, e) =>
9599
{
100+
Log.Debug("Click on systray icon: {times} {button}", e.Clicks, e.Button);
101+
if (e.Clicks == 2)
102+
{
103+
NotifyIcon.ContextMenuStrip.Close();
104+
AppModel.Instance.CycleActiveDevice(DataFlow.Render);
105+
return;
106+
}
107+
96108
if (e.Button != MouseButtons.Left) return;
97109

98110
if (_updateMenuItem.Tag != null)
@@ -103,9 +115,7 @@ public TrayIcon()
103115

104116
UpdateDeviceSelectionList();
105117
NotifyIcon.ContextMenuStrip = _selectionMenu;
106-
var mi = typeof(NotifyIcon).GetMethod("ShowContextMenu",
107-
BindingFlags.Instance | BindingFlags.NonPublic);
108-
mi.Invoke(NotifyIcon, null);
118+
_showContextMenu.Invoke(NotifyIcon, null);
109119

110120
NotifyIcon.ContextMenuStrip = _settingsMenu;
111121
};
@@ -295,6 +305,7 @@ public void ShowSettings()
295305
/// </summary>
296306
public void UpdateDeviceSelectionList()
297307
{
308+
Log.Information("Set tray icon menu devices");
298309
_selectionMenu.Items.Clear();
299310
var playbackDevices = AppModel.Instance.AvailablePlaybackDevices;
300311
var recordingDevices = AppModel.Instance.AvailableRecordingDevices;
@@ -313,13 +324,15 @@ public void UpdateDeviceSelectionList()
313324
return;
314325
}
315326

316-
Log.Information("Set tray icon menu devices");
317-
_selectionMenu.Items.AddRange(playbackDevices.Select(info => new ToolStripDeviceItem(DeviceClicked, info)).ToArray());
327+
var defaultPlayback = AudioSwitcher.Instance.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eConsole);
328+
_selectionMenu.Items.AddRange(playbackDevices.Select(info => new ToolStripDeviceItem(DeviceClicked, info, info.Equals(defaultPlayback))).ToArray());
318329

319330
if (recordingDevices.Count > 0)
320331
{
332+
var defaultRecording = AudioSwitcher.Instance.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eConsole);
333+
321334
_selectionMenu.Items.Add(new ToolStripSeparator());
322-
_selectionMenu.Items.AddRange(recordingDevices.Select(info => new ToolStripDeviceItem(DeviceClicked, info)).ToArray());
335+
_selectionMenu.Items.AddRange(recordingDevices.Select(info => new ToolStripDeviceItem(DeviceClicked, info, info.Equals(defaultRecording))).ToArray());
323336
}
324337
}
325338

SoundSwitch/Util/ToolStripDeviceItem.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
using System;
1717
using System.Drawing;
1818
using System.Windows.Forms;
19-
using SoundSwitch.Audio.Manager;
20-
using SoundSwitch.Audio.Manager.Interop.Enum;
2119
using SoundSwitch.Common.Framework.Audio.Device;
2220
using SoundSwitch.Properties;
2321

@@ -27,11 +25,11 @@ internal class ToolStripDeviceItem : ToolStripMenuItem
2725
{
2826
private readonly bool _isDefault;
2927

30-
public ToolStripDeviceItem(EventHandler onClick, DeviceFullInfo audioDevice)
28+
public ToolStripDeviceItem(EventHandler onClick, DeviceFullInfo audioDevice, bool isDefault)
3129
: base(audioDevice.NameClean, null, onClick)
3230
{
3331
AudioDevice = audioDevice;
34-
_isDefault = AudioSwitcher.Instance.IsDefault(AudioDevice.Id, (EDataFlow) AudioDevice.Type, ERole.eConsole);
32+
_isDefault = isDefault;
3533
}
3634

3735
public override Image Image => _isDefault ? Resources.Check : null;

0 commit comments

Comments
 (0)