From 4b735748de5549c522e30bc5d6c137c915f32d85 Mon Sep 17 00:00:00 2001 From: radj307 Date: Fri, 10 Nov 2023 15:57:50 -0500 Subject: [PATCH] hide notif immediately if there's nothing to display - Also added SelectedDeviceChanged event to AudioDeviceSelector --- .../AudioDeviceSelector.cs | 6 +++++ .../AudioSessionMultiSelector.cs | 4 +-- .../EnumExtensions.cs | 20 ++++++++++++++- VolumeControl/DeviceListNotification.xaml.cs | 18 ++++++++++--- VolumeControl/SessionListNotification.xaml.cs | 25 ++++++++++++++++--- ...wFlagsVM.cs => NotificationViewFlagsVM.cs} | 0 6 files changed, 62 insertions(+), 11 deletions(-) rename VolumeControl/ViewModels/{ListNotificationViewFlagsVM.cs => NotificationViewFlagsVM.cs} (100%) diff --git a/VolumeControl.CoreAudio/AudioDeviceSelector.cs b/VolumeControl.CoreAudio/AudioDeviceSelector.cs index 24ea7faaa..6e3881856 100644 --- a/VolumeControl.CoreAudio/AudioDeviceSelector.cs +++ b/VolumeControl.CoreAudio/AudioDeviceSelector.cs @@ -68,6 +68,7 @@ public AudioDevice? Selected NotifyPropertyChanged(); NotifyPropertyChanged(nameof(SelectedIndex)); + NotifySelectedDeviceChanged(_selected); } } private AudioDevice? _selected; @@ -105,6 +106,11 @@ public bool LockSelection /// public event PropertyChangedEventHandler? PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") => PropertyChanged?.Invoke(this, new(propertyName)); + /// + /// Occurs when the selected device is changed for any reason. + /// + public event EventHandler? SelectedDeviceChanged; + private void NotifySelectedDeviceChanged(AudioDevice? selectedDevice) => SelectedDeviceChanged?.Invoke(this, selectedDevice); #endregion Events #region Methods diff --git a/VolumeControl.CoreAudio/AudioSessionMultiSelector.cs b/VolumeControl.CoreAudio/AudioSessionMultiSelector.cs index 8485a92df..767b2165c 100644 --- a/VolumeControl.CoreAudio/AudioSessionMultiSelector.cs +++ b/VolumeControl.CoreAudio/AudioSessionMultiSelector.cs @@ -114,7 +114,7 @@ public int CurrentIndex get => _currentIndex; set { - if (LockCurrentIndex) return; + if (LockCurrentIndex || value == _currentIndex) return; _currentIndex = value; @@ -143,7 +143,7 @@ public AudioSession? CurrentSession get => CurrentIndex == -1 ? null : Sessions[CurrentIndex]; set { - if (LockCurrentIndex) return; + if (LockCurrentIndex || value == CurrentSession) return; if (value == null) { diff --git a/VolumeControl.TypeExtensions/EnumExtensions.cs b/VolumeControl.TypeExtensions/EnumExtensions.cs index b1293370b..f2e203193 100644 --- a/VolumeControl.TypeExtensions/EnumExtensions.cs +++ b/VolumeControl.TypeExtensions/EnumExtensions.cs @@ -37,7 +37,7 @@ public static bool EqualsAny(this T e, IEnumerable other) where T : struct /// True when is equal to at least one value from public static bool HasAnyFlag(this T e, IEnumerable other) where T : struct, Enum { - foreach (T? o in other) + foreach (T o in other) { if (e.HasFlag(o)) return true; @@ -50,6 +50,24 @@ public static bool HasAnyFlag(this T e, IEnumerable other) where T : struc /// public static bool HasAnyFlag(this T e, params T[] other) where T : struct, Enum => e.HasAnyFlag(other.AsEnumerable()); /// + /// Checks if the enum value has all of the specified set. + /// + /// The enum type. + /// (implicit) The enum value to check. + /// The flags to check. + /// when the enum value has all of the set; otherwise, . + public static bool HasAllFlags(this T e, IEnumerable flags) where T : struct, Enum + { + foreach (T flag in flags) + { + if (!e.HasFlag(flag)) + return false; + } + return true; + } + /// + public static bool HasAllFlags(this T e, params T[] flags) where T : struct, Enum => e.HasAllFlags((IEnumerable)flags); + /// /// Returns the enum value with the specified changed. /// /// The type of enum being operated on. diff --git a/VolumeControl/DeviceListNotification.xaml.cs b/VolumeControl/DeviceListNotification.xaml.cs index 2067cde6a..aeac6812a 100644 --- a/VolumeControl/DeviceListNotification.xaml.cs +++ b/VolumeControl/DeviceListNotification.xaml.cs @@ -10,6 +10,7 @@ using VolumeControl.Log; using VolumeControl.SDK; using VolumeControl.SDK.Internal; +using VolumeControl.TypeExtensions; using VolumeControl.ViewModels; using VolumeControl.WPF; @@ -55,6 +56,7 @@ public DeviceListNotification() Settings.DeviceListNotificationConfig.PropertyChanged += this.DeviceListNotificationConfig_PropertyChanged; VCSettings.DeviceConfigVM.FlagsVM.StateChanged += this.FlagsVM_StateChanged; + VCSettings.AudioAPI.AudioDeviceSelector.SelectedDeviceChanged += this.AudioDeviceSelector_SelectedDeviceChanged; // bind to the show event VCEvents.ShowDeviceListNotification += this.VCEvents_ShowDeviceListNotification; @@ -106,7 +108,7 @@ private bool IsFadingOut _fadingIn = false; } } - private bool IsHiddenByViewMode => VM.FlagsVM.State == ENotificationViewMode.Nothing; + private bool IsHiddenByViewMode => VM.FlagsVM.State == ENotificationViewMode.Nothing || (VM.FlagsVM.State.HasAllFlags(ENotificationViewMode.ControlBar, ENotificationViewMode.SelectedItemOnly) && VCSettings.AudioAPI.SelectedDevice == null); #endregion Properties #region Methods @@ -510,13 +512,21 @@ private void Window_Closing(object sender, CancelEventArgs e) #region FlagsVM private void FlagsVM_StateChanged(object? sender, (ENotificationViewMode NewState, ENotificationViewMode ChangedFlags) e) { - if (e.NewState == ENotificationViewMode.Nothing) - { + if (IsHiddenByViewMode) HideNowNoFadeOut(); - } } #endregion FlagsVM + #region AudioDeviceSelector + private void AudioDeviceSelector_SelectedDeviceChanged(object? sender, CoreAudio.AudioDevice? e) + { + if (e != null) return; + + if (IsHiddenByViewMode) + HideNowNoFadeOut(); + } + #endregion AudioDeviceSelector + #endregion EventHandlers } } diff --git a/VolumeControl/SessionListNotification.xaml.cs b/VolumeControl/SessionListNotification.xaml.cs index 887fa0094..69f833a8c 100644 --- a/VolumeControl/SessionListNotification.xaml.cs +++ b/VolumeControl/SessionListNotification.xaml.cs @@ -10,6 +10,7 @@ using VolumeControl.Helpers; using VolumeControl.Log; using VolumeControl.SDK.Internal; +using VolumeControl.TypeExtensions; using VolumeControl.ViewModels; using VolumeControl.WPF; @@ -55,6 +56,9 @@ public SessionListNotification() Settings.SessionListNotificationConfig.PropertyChanged += this.SessionListNotificationConfig_PropertyChanged; VCSettings.SessionConfigVM.FlagsVM.StateChanged += this.FlagsVM_StateChanged; + VCSettings.AudioAPI.AudioSessionMultiSelector.CurrentSessionChanged += this.AudioSessionMultiSelector_CurrentSessionChanged; + VCSettings.AudioAPI.AudioSessionMultiSelector.SessionSelected += this.AudioSessionMultiSelector_SessionSelectedOrDeselected; + VCSettings.AudioAPI.AudioSessionMultiSelector.SessionDeselected += this.AudioSessionMultiSelector_SessionSelectedOrDeselected; // bind to the show event VCEvents.ShowSessionListNotification += this.VCEvents_ShowSessionListNotification; @@ -106,7 +110,7 @@ private bool IsFadingOut _fadingIn = false; } } - private bool IsHiddenByViewMode => VM.FlagsVM.State == ENotificationViewMode.Nothing; + private bool IsHiddenByViewMode => VM.FlagsVM.State == ENotificationViewMode.Nothing || (VM.FlagsVM.State.HasAllFlags(ENotificationViewMode.ControlBar, ENotificationViewMode.SelectedItemOnly) && VCSettings.AudioAPI.CurrentSession == null && VCSettings.AudioAPI.SelectedSessions.Count == 0); #endregion Properties #region Methods @@ -506,13 +510,26 @@ private void Window_Closing(object sender, CancelEventArgs e) #region FlagsVM private void FlagsVM_StateChanged(object? sender, (ENotificationViewMode NewState, ENotificationViewMode ChangedFlags) e) { - if (e.NewState == ENotificationViewMode.Nothing) - { + if (IsHiddenByViewMode) HideNowNoFadeOut(); - } } #endregion FlagsVM + #region AudioSessionMultiSelector + private void AudioSessionMultiSelector_CurrentSessionChanged(object? sender, CoreAudio.AudioSession? e) + { + if (e != null) return; + + if (IsHiddenByViewMode) + HideNowNoFadeOut(); + } + private void AudioSessionMultiSelector_SessionSelectedOrDeselected(object? sender, CoreAudio.AudioSession e) + { + if (IsHiddenByViewMode) + HideNowNoFadeOut(); + } + #endregion AudioSessionMultiSelector + #endregion EventHandlers } } diff --git a/VolumeControl/ViewModels/ListNotificationViewFlagsVM.cs b/VolumeControl/ViewModels/NotificationViewFlagsVM.cs similarity index 100% rename from VolumeControl/ViewModels/ListNotificationViewFlagsVM.cs rename to VolumeControl/ViewModels/NotificationViewFlagsVM.cs