Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Profile::TrayIcon) Add profile to the tray icon menu #605

Merged
merged 15 commits into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.de.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Sie möchten etwas verbessern oder eine neue Sprache hinzufügen? Übersetzungen
- Portuguese (Brazilian) translation [#258](https://github.com/Belphemur/SoundSwitch/pull/258) [@aleczk](https://github.com/aleczk)
- Awesome Logo [#278](https://github.com/Belphemur/SoundSwitch/pull/278) [@linadesteem](https://github.com/linadesteem)
- Icons [Pastel SVG icon set](https://codefisher.org/pastel-svg/), by Michael Buckley ([CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/))
- Icons [Font Awesome](https://fontawesome.com/), Creative Commons Attribution 4.0 International license: [License](https://fontawesome.com/license)

### 🤝 JetBrains ![JetBrain Tooling](https://i.imgur.com/SN2qAuL.png "JetBrain Tooling")

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Improve an existing or add another language? Translations are online editable [r
- Awesome Logo [#278](https://github.com/Belphemur/SoundSwitch/pull/278) [@linadesteem](https://github.com/linadesteem)
- Icons [Pastel SVG icon set](https://codefisher.org/pastel-svg/), by Michael Buckley ([CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/))
- Discovered and reported a security vulnerability with the updater and its code signature checker [#415](https://github.com/Belphemur/SoundSwitch/issues/415) [@JarLob](https://github.com/JarLob)

- Free Icons from [Font Awesome](https://fontawesome.com/), Creative Commons Attribution 4.0 International license: [License](https://fontawesome.com/license)
### 🤝 JetBrains ![JetBrain Tooling](https://i.imgur.com/SN2qAuL.png "JetBrain Tooling")

Thanks for their Open-Source licence to their amazing IDEs and addons like [ReSharper](https://www.jetbrains.com/resharper) for Visual Studio.
Expand Down
10 changes: 6 additions & 4 deletions SoundSwitch/Framework/Profile/ProfileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ private void RegisterTriggers(Profile profile, bool onInit = false)
}

SwitchAudio(profile);
}, () => { _profilesByUwpApp.Add(trigger.WindowName.ToLower(), (profile, trigger)); });
}, () => { _profilesByUwpApp.Add(trigger.WindowName.ToLower(), (profile, trigger)); },
() => {});
}
}

Expand All @@ -92,7 +93,8 @@ private void UnRegisterTriggers(Profile profile)
() => { _profilesByWindowName.Remove(trigger.WindowName.ToLower()); },
() => { _profileByApplication.Remove(trigger.ApplicationPath.ToLower()); },
() => { _steamProfile = null; }, () => { },
() => { _profilesByUwpApp.Remove(trigger.WindowName.ToLower()); });
() => { _profilesByUwpApp.Remove(trigger.WindowName.ToLower()); },
() => {});
}
}

Expand Down Expand Up @@ -292,7 +294,7 @@ private void SwitchAudio(Profile profile, uint processId)
}


private void SwitchAudio(Profile profile)
public void SwitchAudio(Profile profile)
{
_notificationManager.NotifyProfileChanged(profile, null);
foreach (var device in profile.Devices)
Expand Down Expand Up @@ -425,7 +427,7 @@ private Result<string, VoidSuccess> ValidateProfile(Profile profile)
}

return null;
});
}, () => null);
if (error != null)
{
return error;
Expand Down
59 changes: 12 additions & 47 deletions SoundSwitch/Framework/Profile/Trigger/TriggerEnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,29 @@ public static class TriggerEnumExtensions
/// <summary>
/// Switch on the given enum
/// </summary>
/// <param name="enum"></param>
/// <param name="hotkey"></param>
/// <param name="window"></param>
/// <param name="process"></param>
/// <param name="steam"></param>
/// <param name="startup"></param>
/// <param name="uwpApp"></param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static void Switch(this TriggerFactory.Enum @enum, Action hotkey, Action window, Action process, Action steam, Action startup, Action uwpApp)
public static void Switch(this TriggerFactory.Enum @enum, Action hotkey, Action window, Action process, Action steam, Action startup, Action uwpApp, Action trayMenu)
{
switch (@enum)
{
case TriggerFactory.Enum.HotKey:
hotkey();
break;
case TriggerFactory.Enum.Window:
window();
break;
case TriggerFactory.Enum.Process:
process();
break;
case TriggerFactory.Enum.Steam:
steam();
break;
case TriggerFactory.Enum.Startup:
startup();
break;
case TriggerFactory.Enum.UwpApp:
uwpApp();
break;
default:
throw new ArgumentOutOfRangeException(nameof(@enum), @enum, null);
}
Match(@enum, () => hotkey, () => window, () => process, () => steam, () => startup, () => uwpApp, () => trayMenu)();
}

/// <summary>
/// Match on the enum
/// </summary>
/// <param name="enum"></param>
/// <param name="hotkey"></param>
/// <param name="window"></param>
/// <param name="process"></param>
/// <param name="steam"></param>
/// <param name="startup"></param>
/// <param name="uwpApp"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T Match<T>(this TriggerFactory.Enum @enum, Func<T> hotkey, Func<T> window, Func<T> process, Func<T> steam, Func<T> startup, Func<T> uwpApp)
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static T Match<T>(this TriggerFactory.Enum @enum, Func<T> hotkey, Func<T> window, Func<T> process, Func<T> steam, Func<T> startup, Func<T> uwpApp, Func<T> trayMenu)
{
return @enum switch
{
TriggerFactory.Enum.HotKey => hotkey(),
TriggerFactory.Enum.Window => window(),
TriggerFactory.Enum.Process => process(),
TriggerFactory.Enum.Steam => steam(),
TriggerFactory.Enum.Startup => startup(),
TriggerFactory.Enum.UwpApp => uwpApp(),
_ => throw new ArgumentOutOfRangeException(nameof(@enum), @enum, null)
TriggerFactory.Enum.HotKey => hotkey(),
TriggerFactory.Enum.Window => window(),
TriggerFactory.Enum.Process => process(),
TriggerFactory.Enum.Steam => steam(),
TriggerFactory.Enum.Startup => startup(),
TriggerFactory.Enum.UwpApp => uwpApp(),
TriggerFactory.Enum.TrayMenu => trayMenu(),
_ => throw new ArgumentOutOfRangeException(nameof(@enum), @enum, null)
};
}
}
Expand Down
91 changes: 49 additions & 42 deletions SoundSwitch/Framework/Profile/Trigger/TriggerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Windows.Forms;
using SoundSwitch.Framework.Factory;
using SoundSwitch.Framework.Factory;
using SoundSwitch.Localization;
using SoundSwitch.Util;

namespace SoundSwitch.Framework.Profile.Trigger
{
Expand All @@ -15,7 +12,8 @@ public enum Enum
Process,
Steam,
Startup,
UwpApp
UwpApp,
TrayMenu
}

private static readonly IEnumImplList<Enum, ITriggerDefinition> Impl =
Expand All @@ -26,7 +24,8 @@ public enum Enum
new WindowTrigger(),
new SteamBigPictureTrigger(),
new Startup(),
new UwpApp()
new UwpApp(),
new TrayMenu()
};

public TriggerFactory() : base(Impl)
Expand Down Expand Up @@ -66,68 +65,76 @@ public override string ToString()
return Label;
}

public virtual TriggerFactory.Enum TypeEnum { get; }
public virtual string Label { get; }
public virtual int MaxOccurence { get; } = -1;
public virtual int MaxGlobalOccurence { get; } = -1;
public abstract string Description { get; }
public virtual bool CanRestoreDevices { get; } = false;
public virtual bool AlwaysDefaultAndRestoreDevice { get; } = false;
public virtual TriggerFactory.Enum TypeEnum { get; }
public virtual string Label { get; }
public virtual int MaxOccurence => -1;
public virtual int MaxGlobalOccurence => -1;
public abstract string Description { get; }
public virtual bool CanRestoreDevices => false;
public virtual bool AlwaysDefaultAndRestoreDevice => false;
}

public class HotKeyTrigger : BaseTrigger
{
public override TriggerFactory.Enum TypeEnum { get; } = TriggerFactory.Enum.HotKey;
public override string Label => SettingsStrings.hotkeys;
public override string Description { get; } = SettingsStrings.profile_trigger_hotkey_desc;
public override int MaxOccurence { get; } = 1;
public override TriggerFactory.Enum TypeEnum => TriggerFactory.Enum.HotKey;
public override string Label => SettingsStrings.hotkeys;
public override string Description { get; } = SettingsStrings.profile_trigger_hotkey_desc;
public override int MaxOccurence => 1;
}

public class WindowTrigger : BaseTrigger
{
public override TriggerFactory.Enum TypeEnum { get; } = TriggerFactory.Enum.Window;
public override string Label => SettingsStrings.profile_trigger_window;
public override string Description { get; } = SettingsStrings.profile_trigger_window_desc;
public override bool CanRestoreDevices { get; } = true;
public override TriggerFactory.Enum TypeEnum => TriggerFactory.Enum.Window;
public override string Label => SettingsStrings.profile_trigger_window;
public override string Description { get; } = SettingsStrings.profile_trigger_window_desc;
public override bool CanRestoreDevices => true;
}

public class ProcessTrigger : BaseTrigger
{
public override TriggerFactory.Enum TypeEnum { get; } = TriggerFactory.Enum.Process;
public override string Label => SettingsStrings.profile_trigger_process;
public override string Description { get; } = SettingsStrings.profile_trigger_process_desc;
public override bool CanRestoreDevices { get; } = true;
public override TriggerFactory.Enum TypeEnum => TriggerFactory.Enum.Process;
public override string Label => SettingsStrings.profile_trigger_process;
public override string Description { get; } = SettingsStrings.profile_trigger_process_desc;
public override bool CanRestoreDevices => true;
}

public class SteamBigPictureTrigger : BaseTrigger
{
public override TriggerFactory.Enum TypeEnum { get; } = TriggerFactory.Enum.Steam;
public override string Label => SettingsStrings.profile_trigger_steam;

public override int MaxOccurence { get; } = 1;
public override int MaxGlobalOccurence { get; } = 1;
public override string Description { get; } = SettingsStrings.profile_trigger_steam_desc;
public override bool CanRestoreDevices { get; } = true;
public override bool AlwaysDefaultAndRestoreDevice { get; } = true;
public override TriggerFactory.Enum TypeEnum => TriggerFactory.Enum.Steam;
public override string Label => SettingsStrings.profile_trigger_steam;

public override int MaxOccurence => 1;
public override int MaxGlobalOccurence => 1;
public override string Description { get; } = SettingsStrings.profile_trigger_steam_desc;
public override bool CanRestoreDevices => true;
public override bool AlwaysDefaultAndRestoreDevice => true;
}

public class Startup : BaseTrigger
{
public override TriggerFactory.Enum TypeEnum { get; } = TriggerFactory.Enum.Startup;
public override string Label => SettingsStrings.profile_trigger_startup;
public override TriggerFactory.Enum TypeEnum => TriggerFactory.Enum.Startup;
public override string Label => SettingsStrings.profile_trigger_startup;

public override int MaxOccurence { get; } = 1;
public override int MaxGlobalOccurence { get; } = 1;
public override int MaxOccurence => 1;
public override int MaxGlobalOccurence => 1;

public override string Description { get; } = SettingsStrings.profile_trigger_startup_desc;
}

public class UwpApp : BaseTrigger
{
public override TriggerFactory.Enum TypeEnum { get; } = TriggerFactory.Enum.UwpApp;
public override string Label => SettingsStrings.profile_trigger_uwp;
public override bool CanRestoreDevices { get; } = true;
public override string Description => SettingsStrings.profile_trigger_uwp_desc;
public override bool AlwaysDefaultAndRestoreDevice { get; } = true;
public override TriggerFactory.Enum TypeEnum => TriggerFactory.Enum.UwpApp;
public override string Label => SettingsStrings.profile_trigger_uwp;
public override bool CanRestoreDevices => true;
public override string Description => SettingsStrings.profile_trigger_uwp_desc;
public override bool AlwaysDefaultAndRestoreDevice => true;
}

public class TrayMenu : BaseTrigger
{
public override string Description => SettingsStrings.profile_trigger_trayMenu_desc;
public override TriggerFactory.Enum TypeEnum => TriggerFactory.Enum.TrayMenu;
public override string Label => SettingsStrings.profile_trigger_trayMenu;
public override int MaxOccurence => 1;
}
}
30 changes: 30 additions & 0 deletions SoundSwitch/Framework/Profile/UI/ProfileToolStripMenuItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/********************************************************************
* Copyright (C) 2015 Jeroen Pelgrims
* Copyright (C) 2015-2017 Antoine Aflalo
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
********************************************************************/

using System;
using System.Drawing;
using System.Windows.Forms;

namespace SoundSwitch.Framework.Profile.UI
{
internal class ProfileToolStripMenuItem : ToolStripMenuItem
{

public ProfileToolStripMenuItem(Profile profile, Image image, Action<Profile> onClick)
: base(profile.Name, image, (_, _) => onClick(profile))
{
}
}
}
64 changes: 64 additions & 0 deletions SoundSwitch/Framework/Profile/UI/ProfileTrayIconBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using SoundSwitch.Common.Framework.Icon;
using SoundSwitch.Framework.Profile.Trigger;
using SoundSwitch.Model;

namespace SoundSwitch.Framework.Profile.UI
{
public class ProfileTrayIconBuilder
{
private ProfileManager ProfileManager => AppModel.Instance.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);
}

private ProfileToolStripMenuItem BuildMenuItem(Profile profile)
{
Image image = null;
try
{
var appTrigger = profile.Triggers.FirstOrDefault(trigger => trigger.ApplicationPath != null);
if (appTrigger != null)
{
image = IconExtractor.Extract(appTrigger.ApplicationPath, 0, false).ToBitmap();
}
}
catch (Exception)
{
// ignored
}

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

try
{
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));
}
}
}
18 changes: 18 additions & 0 deletions SoundSwitch/Localization/SettingsStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading