Skip to content

Commit 4a43fa5

Browse files
committed
feat(Profile::Trigger::TrayIcon): Add tray icon as trigger
The profile will be displayed in the toolstrip when left-clicking on the SoundSwitch tray icon. See #492
1 parent 4b75a25 commit 4a43fa5

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

SoundSwitch/Framework/Profile/UI/ProfileTrayIconBuilder.cs

+12-14
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,19 @@ namespace SoundSwitch.Framework.Profile.UI
1111
{
1212
public class ProfileTrayIconBuilder
1313
{
14-
private readonly IAudioDeviceLister _audioDeviceLister;
15-
private readonly ProfileManager _profileManager;
14+
private ProfileManager ProfileManager => AppModel.Instance.ProfileManager;
1615

17-
public ProfileTrayIconBuilder(IAudioDeviceLister audioDeviceLister, ProfileManager profileManager)
18-
{
19-
_audioDeviceLister = audioDeviceLister;
20-
_profileManager = profileManager;
21-
}
16+
private IAudioDeviceLister AudioDeviceLister => AppModel.Instance.ActiveAudioDeviceLister;
2217

2318
/// <summary>
2419
/// Get the menu items for profile that needs to be shown in the menu
2520
/// </summary>
2621
/// <returns></returns>
2722
public IEnumerable<ToolStripMenuItem> GetMenuItems()
2823
{
29-
return _profileManager.Profiles
30-
.Where(profile => profile.Triggers.Any(trigger => trigger.Type == TriggerFactory.Enum.TrayMenu))
31-
.Select(BuildMenuItem);
24+
return ProfileManager.Profiles
25+
.Where(profile => profile.Triggers.Any(trigger => trigger.Type == TriggerFactory.Enum.TrayMenu))
26+
.Select(BuildMenuItem);
3227
}
3328

3429
private ProfileToolStripMenuItem BuildMenuItem(Profile profile)
@@ -47,20 +42,23 @@ private ProfileToolStripMenuItem BuildMenuItem(Profile profile)
4742
// ignored
4843
}
4944

50-
if (image == null)
45+
foreach (var wrapper in profile.Devices)
5146
{
47+
if (image != null)
48+
break;
49+
5250
try
5351
{
54-
var playback = _audioDeviceLister.PlaybackDevices.FirstOrDefault(info => info.Equals(profile.Playback));
55-
image = playback?.SmallIcon.ToBitmap();
52+
var device = AudioDeviceLister.PlaybackDevices.FirstOrDefault(info => info.Equals(wrapper.DeviceInfo));
53+
image = device?.SmallIcon.ToBitmap();
5654
}
5755
catch (Exception)
5856
{
5957
// ignored
6058
}
6159
}
6260

63-
return new ProfileToolStripMenuItem(profile, image, profileClicked => _profileManager.SwitchAudio(profileClicked));
61+
return new ProfileToolStripMenuItem(profile, image, profileClicked => ProfileManager.SwitchAudio(profileClicked));
6462
}
6563
}
6664
}

SoundSwitch/UI/Component/TrayIcon.cs

+20-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Diagnostics;
1818
using System.Drawing;
1919
using System.IO;
20+
using System.Linq;
2021
using System.Reflection;
2122
using System.Threading;
2223
using System.Windows.Forms;
@@ -25,6 +26,7 @@
2526
using SoundSwitch.Common.Framework.Audio.Device;
2627
using SoundSwitch.Framework;
2728
using SoundSwitch.Framework.Configuration;
29+
using SoundSwitch.Framework.Profile.UI;
2830
using SoundSwitch.Framework.TrayIcon.Icon;
2931
using SoundSwitch.Framework.TrayIcon.TooltipInfoManager;
3032
using SoundSwitch.Framework.Updater;
@@ -52,19 +54,20 @@ public sealed class TrayIcon : IDisposable
5254
private static readonly Icon SoundSwitchLogoIcon = Resources.Switch_SoundWave;
5355
private static readonly Icon ResourceDiscord = Resources.DiscordIcon;
5456

55-
private readonly ContextMenuStrip _selectionMenu = new ContextMenuStrip();
56-
private readonly ContextMenuStrip _settingsMenu = new ContextMenuStrip();
57+
private readonly ContextMenuStrip _selectionMenu = new();
58+
private readonly ContextMenuStrip _settingsMenu = new();
5759

5860
private readonly SynchronizationContext _context =
5961
SynchronizationContext.Current ?? new SynchronizationContext();
6062

61-
public NotifyIcon NotifyIcon { get; } = new NotifyIcon
63+
public NotifyIcon NotifyIcon { get; } = new()
6264
{
6365
Visible = true,
6466
Text = Application.ProductName
6567
};
6668

6769
private readonly TooltipInfoManager _tooltipInfoManager;
70+
private readonly ProfileTrayIconBuilder _profileTrayIconBuilder;
6871

6972
private readonly ToolStripMenuItem _updateMenuItem;
7073
private TimerForm _animationTimer;
@@ -74,6 +77,7 @@ public TrayIcon()
7477
{
7578
UpdateIcon();
7679
_tooltipInfoManager = new TooltipInfoManager(NotifyIcon);
80+
_profileTrayIconBuilder = new ProfileTrayIconBuilder();
7781

7882
_updateMenuItem = new ToolStripMenuItem(AppConfigs.Configuration.UpdateMode == UpdateMode.Never ? TrayIconStrings.updateDisabled : TrayIconStrings.noUpdate, RessourceUpdateBitmap, OnUpdateClick)
7983
{
@@ -161,9 +165,7 @@ private void PopulateSettingsMenu()
161165
var applicationDirectory = Path.GetDirectoryName(ApplicationPath.Executable);
162166
Debug.Assert(applicationDirectory != null, "applicationDirectory != null");
163167
var readmeHtml = Path.Combine(applicationDirectory, "Readme.html");
164-
_settingsMenu.Items.Add(
165-
Application.ProductName + ' ' + AssemblyUtils.GetReleaseState() + " (" + Application.ProductVersion +
166-
")", SoundSwitchLogoIcon.ToBitmap());
168+
_settingsMenu.Items.Add(Application.ProductName + ' ' + AssemblyUtils.GetReleaseState() + " (" + Application.ProductVersion + ")", SoundSwitchLogoIcon.ToBitmap());
167169
_settingsMenu.Items.Add("-");
168170
_settingsMenu.Items.Add(TrayIconStrings.playbackDevices, RessourcePlaybackDevicesBitmap,
169171
(sender, e) => { Process.Start(new ProcessStartInfo("control", "mmsys.cpl sounds")); });
@@ -294,16 +296,27 @@ public void ShowSettings()
294296
/// </summary>
295297
public void UpdateDeviceSelectionList()
296298
{
299+
_selectionMenu.Items.Clear();
297300
var playbackDevices = AppModel.Instance.AvailablePlaybackDevices;
298301
var recordingDevices = AppModel.Instance.AvailableRecordingDevices;
302+
var profiles = _profileTrayIconBuilder.GetMenuItems().ToArray();
303+
304+
305+
if (profiles.Length > 0)
306+
{
307+
var profileMenu = new ContextMenuStrip();
308+
var profileMenuItem = new ToolStripMenuItem(SettingsStrings.profile_tab) {DropDown = profileMenu};
309+
profileMenu.Items.AddRange(profiles);
310+
_selectionMenu.Items.Add(profileMenuItem);
311+
_selectionMenu.Items.Add(new ToolStripSeparator());
312+
}
299313
if (playbackDevices.Count < 0 &&
300314
recordingDevices.Count < 0)
301315
{
302316
Log.Information("Device list empty");
303317
return;
304318
}
305319

306-
_selectionMenu.Items.Clear();
307320
Log.Information("Set tray icon menu devices");
308321
foreach (var item in playbackDevices)
309322
{

0 commit comments

Comments
 (0)