Skip to content

Commit

Permalink
feat(Profile::Trigger::TrayIcon): Add tray icon as trigger
Browse files Browse the repository at this point in the history
The profile will be displayed in the toolstrip when left-clicking on the SoundSwitch tray icon.

See #492
  • Loading branch information
Belphemur committed Apr 17, 2021
1 parent 4b75a25 commit 4a43fa5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
26 changes: 12 additions & 14 deletions SoundSwitch/Framework/Profile/UI/ProfileTrayIconBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,19 @@ namespace SoundSwitch.Framework.Profile.UI
{
public class ProfileTrayIconBuilder
{
private readonly IAudioDeviceLister _audioDeviceLister;
private readonly ProfileManager _profileManager;
private ProfileManager ProfileManager => AppModel.Instance.ProfileManager;

public ProfileTrayIconBuilder(IAudioDeviceLister audioDeviceLister, ProfileManager profileManager)
{
_audioDeviceLister = audioDeviceLister;
_profileManager = profileManager;
}
private IAudioDeviceLister AudioDeviceLister => AppModel.Instance.ActiveAudioDeviceLister;

/// <summary>
/// Get the menu items for profile that needs to be shown in the menu
/// </summary>
/// <returns></returns>
public IEnumerable<ToolStripMenuItem> GetMenuItems()
{
return _profileManager.Profiles
.Where(profile => profile.Triggers.Any(trigger => trigger.Type == TriggerFactory.Enum.TrayMenu))
.Select(BuildMenuItem);
return ProfileManager.Profiles
.Where(profile => profile.Triggers.Any(trigger => trigger.Type == TriggerFactory.Enum.TrayMenu))
.Select(BuildMenuItem);
}

private ProfileToolStripMenuItem BuildMenuItem(Profile profile)
Expand All @@ -47,20 +42,23 @@ private ProfileToolStripMenuItem BuildMenuItem(Profile profile)
// ignored
}

if (image == null)
foreach (var wrapper in profile.Devices)
{
if (image != null)
break;

try
{
var playback = _audioDeviceLister.PlaybackDevices.FirstOrDefault(info => info.Equals(profile.Playback));
image = playback?.SmallIcon.ToBitmap();
var device = AudioDeviceLister.PlaybackDevices.FirstOrDefault(info => info.Equals(wrapper.DeviceInfo));
image = device?.SmallIcon.ToBitmap();
}
catch (Exception)
{
// ignored
}
}

return new ProfileToolStripMenuItem(profile, image, profileClicked => _profileManager.SwitchAudio(profileClicked));
return new ProfileToolStripMenuItem(profile, image, profileClicked => ProfileManager.SwitchAudio(profileClicked));
}
}
}
27 changes: 20 additions & 7 deletions SoundSwitch/UI/Component/TrayIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
Expand All @@ -25,6 +26,7 @@
using SoundSwitch.Common.Framework.Audio.Device;
using SoundSwitch.Framework;
using SoundSwitch.Framework.Configuration;
using SoundSwitch.Framework.Profile.UI;
using SoundSwitch.Framework.TrayIcon.Icon;
using SoundSwitch.Framework.TrayIcon.TooltipInfoManager;
using SoundSwitch.Framework.Updater;
Expand Down Expand Up @@ -52,19 +54,20 @@ public sealed class TrayIcon : IDisposable
private static readonly Icon SoundSwitchLogoIcon = Resources.Switch_SoundWave;
private static readonly Icon ResourceDiscord = Resources.DiscordIcon;

private readonly ContextMenuStrip _selectionMenu = new ContextMenuStrip();
private readonly ContextMenuStrip _settingsMenu = new ContextMenuStrip();
private readonly ContextMenuStrip _selectionMenu = new();
private readonly ContextMenuStrip _settingsMenu = new();

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

public NotifyIcon NotifyIcon { get; } = new NotifyIcon
public NotifyIcon NotifyIcon { get; } = new()
{
Visible = true,
Text = Application.ProductName
};

private readonly TooltipInfoManager _tooltipInfoManager;
private readonly ProfileTrayIconBuilder _profileTrayIconBuilder;

private readonly ToolStripMenuItem _updateMenuItem;
private TimerForm _animationTimer;
Expand All @@ -74,6 +77,7 @@ public TrayIcon()
{
UpdateIcon();
_tooltipInfoManager = new TooltipInfoManager(NotifyIcon);
_profileTrayIconBuilder = new ProfileTrayIconBuilder();

_updateMenuItem = new ToolStripMenuItem(AppConfigs.Configuration.UpdateMode == UpdateMode.Never ? TrayIconStrings.updateDisabled : TrayIconStrings.noUpdate, RessourceUpdateBitmap, OnUpdateClick)
{
Expand Down Expand Up @@ -161,9 +165,7 @@ private void PopulateSettingsMenu()
var applicationDirectory = Path.GetDirectoryName(ApplicationPath.Executable);
Debug.Assert(applicationDirectory != null, "applicationDirectory != null");
var readmeHtml = Path.Combine(applicationDirectory, "Readme.html");
_settingsMenu.Items.Add(
Application.ProductName + ' ' + AssemblyUtils.GetReleaseState() + " (" + Application.ProductVersion +
")", SoundSwitchLogoIcon.ToBitmap());
_settingsMenu.Items.Add(Application.ProductName + ' ' + AssemblyUtils.GetReleaseState() + " (" + Application.ProductVersion + ")", SoundSwitchLogoIcon.ToBitmap());
_settingsMenu.Items.Add("-");
_settingsMenu.Items.Add(TrayIconStrings.playbackDevices, RessourcePlaybackDevicesBitmap,
(sender, e) => { Process.Start(new ProcessStartInfo("control", "mmsys.cpl sounds")); });
Expand Down Expand Up @@ -294,16 +296,27 @@ public void ShowSettings()
/// </summary>
public void UpdateDeviceSelectionList()
{
_selectionMenu.Items.Clear();
var playbackDevices = AppModel.Instance.AvailablePlaybackDevices;
var recordingDevices = AppModel.Instance.AvailableRecordingDevices;
var profiles = _profileTrayIconBuilder.GetMenuItems().ToArray();


if (profiles.Length > 0)
{
var profileMenu = new ContextMenuStrip();
var profileMenuItem = new ToolStripMenuItem(SettingsStrings.profile_tab) {DropDown = profileMenu};
profileMenu.Items.AddRange(profiles);
_selectionMenu.Items.Add(profileMenuItem);
_selectionMenu.Items.Add(new ToolStripSeparator());
}
if (playbackDevices.Count < 0 &&
recordingDevices.Count < 0)
{
Log.Information("Device list empty");
return;
}

_selectionMenu.Items.Clear();
Log.Information("Set tray icon menu devices");
foreach (var item in playbackDevices)
{
Expand Down

0 comments on commit 4a43fa5

Please sign in to comment.