Skip to content

Commit

Permalink
hide notif immediately if there's nothing to display
Browse files Browse the repository at this point in the history
- Also added SelectedDeviceChanged event to AudioDeviceSelector
  • Loading branch information
radj307 committed Nov 10, 2023
1 parent 34062e8 commit 4b73574
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
6 changes: 6 additions & 0 deletions VolumeControl.CoreAudio/AudioDeviceSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public AudioDevice? Selected

NotifyPropertyChanged();
NotifyPropertyChanged(nameof(SelectedIndex));
NotifySelectedDeviceChanged(_selected);
}
}
private AudioDevice? _selected;
Expand Down Expand Up @@ -105,6 +106,11 @@ public bool LockSelection
/// <inheritdoc/>
public event PropertyChangedEventHandler? PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") => PropertyChanged?.Invoke(this, new(propertyName));
/// <summary>
/// Occurs when the selected device is changed for any reason.
/// </summary>
public event EventHandler<AudioDevice?>? SelectedDeviceChanged;
private void NotifySelectedDeviceChanged(AudioDevice? selectedDevice) => SelectedDeviceChanged?.Invoke(this, selectedDevice);
#endregion Events

#region Methods
Expand Down
4 changes: 2 additions & 2 deletions VolumeControl.CoreAudio/AudioSessionMultiSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public int CurrentIndex
get => _currentIndex;
set
{
if (LockCurrentIndex) return;
if (LockCurrentIndex || value == _currentIndex) return;

_currentIndex = value;

Expand Down Expand Up @@ -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)
{
Expand Down
20 changes: 19 additions & 1 deletion VolumeControl.TypeExtensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static bool EqualsAny<T>(this T e, IEnumerable<T> other) where T : struct
/// <returns>True when <paramref name="e"/> is equal to at least one value from <paramref name="other"/></returns>
public static bool HasAnyFlag<T>(this T e, IEnumerable<T> other) where T : struct, Enum
{
foreach (T? o in other)
foreach (T o in other)
{
if (e.HasFlag(o))
return true;
Expand All @@ -50,6 +50,24 @@ public static bool HasAnyFlag<T>(this T e, IEnumerable<T> other) where T : struc
/// <inheritdoc cref="EqualsAny{T}(T, IEnumerable{T})"/>
public static bool HasAnyFlag<T>(this T e, params T[] other) where T : struct, Enum => e.HasAnyFlag(other.AsEnumerable());
/// <summary>
/// Checks if the enum value has all of the specified <paramref name="flags"/> set.
/// </summary>
/// <typeparam name="T">The enum type.</typeparam>
/// <param name="e">(implicit) The enum value to check.</param>
/// <param name="flags">The flags to check.</param>
/// <returns><see langword="true"/> when the enum value has all of the <paramref name="flags"/> set; otherwise, <see langword="false"/>.</returns>
public static bool HasAllFlags<T>(this T e, IEnumerable<T> flags) where T : struct, Enum
{
foreach (T flag in flags)
{
if (!e.HasFlag(flag))
return false;
}
return true;
}
/// <inheritdoc cref="HasAllFlags{T}(T, IEnumerable{T})"/>
public static bool HasAllFlags<T>(this T e, params T[] flags) where T : struct, Enum => e.HasAllFlags((IEnumerable<T>)flags);
/// <summary>
/// Returns the enum value with the specified <paramref name="flag"/> changed.
/// </summary>
/// <typeparam name="T">The type of enum being operated on.</typeparam>
Expand Down
18 changes: 14 additions & 4 deletions VolumeControl/DeviceListNotification.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using VolumeControl.Log;
using VolumeControl.SDK;
using VolumeControl.SDK.Internal;
using VolumeControl.TypeExtensions;
using VolumeControl.ViewModels;
using VolumeControl.WPF;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
25 changes: 21 additions & 4 deletions VolumeControl/SessionListNotification.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using VolumeControl.Helpers;
using VolumeControl.Log;
using VolumeControl.SDK.Internal;
using VolumeControl.TypeExtensions;
using VolumeControl.ViewModels;
using VolumeControl.WPF;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

0 comments on commit 4b73574

Please sign in to comment.