From 4bae9fb87ffe7b8d34c21964eaf13e4818f0bb7c Mon Sep 17 00:00:00 2001 From: Antoine Aflalo Date: Sat, 16 Jan 2021 17:20:09 -0500 Subject: [PATCH] Use Notification Manager in Profile manager See #539 --- .../NotificationManager.cs | 30 +++++-- .../Framework/Profile/ProfileManager.cs | 84 ++++++------------- 2 files changed, 46 insertions(+), 68 deletions(-) diff --git a/SoundSwitch/Framework/NotificationManager/NotificationManager.cs b/SoundSwitch/Framework/NotificationManager/NotificationManager.cs index 1c8c988197..637a007bfc 100644 --- a/SoundSwitch/Framework/NotificationManager/NotificationManager.cs +++ b/SoundSwitch/Framework/NotificationManager/NotificationManager.cs @@ -24,20 +24,20 @@ namespace SoundSwitch.Framework.NotificationManager { public class NotificationManager { - private readonly IAppModel _model; - private string _lastDeviceId; - private INotification _notification; + private readonly IAppModel _model; + private string _lastDeviceId; + private INotification _notification; private readonly NotificationFactory _notificationFactory; public NotificationManager(IAppModel model) { - _model = model; + _model = model; _notificationFactory = new NotificationFactory(); } public void Init() { - _model.DefaultDeviceChanged += ModelOnDefaultDeviceChanged; + _model.DefaultDeviceChanged += ModelOnDefaultDeviceChanged; _model.NotificationSettingsChanged += ModelOnNotificationSettingsChanged; SetNotification(_model.NotificationSettings); _model.CustomSoundChanged += ModelOnCustomSoundChanged; @@ -59,7 +59,7 @@ private void SetNotification(NotificationTypeEnum notificationTypeEnum) _notification = _notificationFactory.Get(notificationTypeEnum); _notification.Configuration = new NotificationConfiguration() { - Icon = AppModel.Instance.TrayIcon.NotifyIcon, + Icon = _model.TrayIcon.NotifyIcon, DefaultSound = Resources.NotificationSound }; try @@ -74,7 +74,7 @@ private void SetNotification(NotificationTypeEnum notificationTypeEnum) } MessageBox.Show(string.Format(SettingsStrings.audioFileNotFound, SettingsStrings.notificationOptionSound), - SettingsStrings.audioFileNotFoundCaption, MessageBoxButtons.OK, MessageBoxIcon.Error); + SettingsStrings.audioFileNotFoundCaption, MessageBoxButtons.OK, MessageBoxIcon.Error); _model.NotificationSettings = NotificationTypeEnum.SoundNotification; } } @@ -88,11 +88,23 @@ private void ModelOnDefaultDeviceChanged(object sender, DeviceDefaultChangedEven _lastDeviceId = deviceDefaultChangedEvent.DeviceId; } + /// + /// Notify on Profile changed + /// + public void NotifyProfileChanged(Profile.Profile profile, uint? processId) + { + if (!profile.NotifyOnActivation) + { + return; + } + _notification.NotifyProfileChanged(profile, processId); + } + ~NotificationManager() { - _model.DefaultDeviceChanged -= ModelOnDefaultDeviceChanged; + _model.DefaultDeviceChanged -= ModelOnDefaultDeviceChanged; _model.NotificationSettingsChanged -= ModelOnNotificationSettingsChanged; - _model.CustomSoundChanged -= ModelOnCustomSoundChanged; + _model.CustomSoundChanged -= ModelOnCustomSoundChanged; } } } \ No newline at end of file diff --git a/SoundSwitch/Framework/Profile/ProfileManager.cs b/SoundSwitch/Framework/Profile/ProfileManager.cs index 5f95c4c6a1..ca7c3dc8d0 100644 --- a/SoundSwitch/Framework/Profile/ProfileManager.cs +++ b/SoundSwitch/Framework/Profile/ProfileManager.cs @@ -4,7 +4,6 @@ using System.ComponentModel; using System.Diagnostics; using System.Linq; -using Markdig.Syntax.Inlines; using NAudio.CoreAudioApi; using RailSharp; using RailSharp.Internal.Result; @@ -13,16 +12,13 @@ using SoundSwitch.Audio.Manager.Interop.Com.User; using SoundSwitch.Audio.Manager.Interop.Enum; using SoundSwitch.Common.Framework.Audio.Device; -using SoundSwitch.Common.Framework.Icon; -using SoundSwitch.Framework.Banner; using SoundSwitch.Framework.Configuration; using SoundSwitch.Framework.Profile.Trigger; +using SoundSwitch.Framework.WinApi; +using SoundSwitch.Framework.WinApi.Keyboard; using SoundSwitch.Localization; using SoundSwitch.Model; -using SoundSwitch.Properties; using SoundSwitch.Util; -using HotKey = SoundSwitch.Framework.WinApi.Keyboard.HotKey; -using WindowsAPIAdapter = SoundSwitch.Framework.WinApi.WindowsAPIAdapter; namespace SoundSwitch.Framework.Profile { @@ -30,12 +26,12 @@ public class ProfileManager { public delegate void ShowError(string errorMessage, string errorTitle); - private readonly BannerManager _bannerManager = new(); - private readonly WindowMonitor _windowMonitor; - private readonly AudioSwitcher _audioSwitcher; - private readonly IAudioDeviceLister _activeDeviceLister; - private readonly ShowError _showError; - private readonly TriggerFactory _triggerFactory; + private readonly WindowMonitor _windowMonitor; + private readonly AudioSwitcher _audioSwitcher; + private readonly IAudioDeviceLister _activeDeviceLister; + private readonly ShowError _showError; + private readonly TriggerFactory _triggerFactory; + private readonly NotificationManager.NotificationManager _notificationManager; private Profile? _steamProfile; @@ -49,17 +45,19 @@ public class ProfileManager public IReadOnlyCollection Profiles => AppConfigs.Configuration.Profiles; - public ProfileManager(WindowMonitor windowMonitor, - AudioSwitcher audioSwitcher, - IAudioDeviceLister activeDeviceLister, - ShowError showError, - TriggerFactory triggerFactory) + public ProfileManager(WindowMonitor windowMonitor, + AudioSwitcher audioSwitcher, + IAudioDeviceLister activeDeviceLister, + ShowError showError, + TriggerFactory triggerFactory, + NotificationManager.NotificationManager notificationManager) { - _windowMonitor = windowMonitor; - _audioSwitcher = audioSwitcher; - _activeDeviceLister = activeDeviceLister; - _showError = showError; - _triggerFactory = triggerFactory; + _windowMonitor = windowMonitor; + _audioSwitcher = audioSwitcher; + _activeDeviceLister = activeDeviceLister; + _showError = showError; + _triggerFactory = triggerFactory; + _notificationManager = notificationManager; } private void RegisterTriggers(Profile profile, bool onInit = false) @@ -151,7 +149,7 @@ private void RegisterEvents() private bool HandleUwpApp(WindowMonitor.Event @event) { (Profile Profile, Trigger.Trigger Trigger) profileTuple; - + var windowNameLowerCase = @event.WindowName.ToLower(); profileTuple = _profilesByUwpApp.FirstOrDefault(pair => windowNameLowerCase.Contains(pair.Key)).Value; @@ -167,10 +165,9 @@ private bool HandleUwpApp(WindowMonitor.Event @event) private bool HandleWindowName(WindowMonitor.Event @event) { - (Profile Profile, Trigger.Trigger Trigger) profileTuple; - var windowNameLower = @event.WindowName.ToLower(); + var windowNameLower = @event.WindowName.ToLower(); - profileTuple = _profilesByWindowName.FirstOrDefault(pair => windowNameLower.Contains(pair.Key)).Value; + var profileTuple = _profilesByWindowName.FirstOrDefault(pair => windowNameLower.Contains(pair.Key)).Value; if (profileTuple != default) { SaveCurrentState(@event.Hwnd, profileTuple.Profile, profileTuple.Trigger); @@ -269,40 +266,9 @@ private bool HandleSteamBigPicture(WindowMonitor.Event @event) }; } - private void NotifyProfileIfNeeded(Profile profile, uint? processId) - { - if (!profile.NotifyOnActivation) - { - return; - } - - - var icon = Resources.default_profile_image; - if (processId.HasValue) - { - try - { - var process = Process.GetProcessById((int) processId.Value); - icon = IconExtractor.Extract(process.MainModule?.FileName, 0, true).ToBitmap(); - } - catch (Exception) - { - // ignored - } - } - - _bannerManager.ShowNotification(new BannerData - { - Priority = 1, - Image = icon, - Title = string.Format(SettingsStrings.profile_notification_text, profile.Name), - Text = string.Join("\n", profile.Devices.Select(wrapper => wrapper.DeviceInfo.NameClean)) - }); - } - private void SwitchAudio(Profile profile, uint processId) { - NotifyProfileIfNeeded(profile, processId); + _notificationManager.NotifyProfileChanged(profile, processId); foreach (var device in profile.Devices) { var deviceToUse = CheckDeviceAvailable(device.DeviceInfo); @@ -328,7 +294,7 @@ private void SwitchAudio(Profile profile, uint processId) private void SwitchAudio(Profile profile) { - NotifyProfileIfNeeded(profile, null); + _notificationManager.NotifyProfileChanged(profile, null); foreach (var device in profile.Devices) { var deviceToUse = CheckDeviceAvailable(device.DeviceInfo);