Skip to content

Commit

Permalink
feat(QuickMenu): Display a quick menu on cursor position when the use…
Browse files Browse the repository at this point in the history
…r use a HotKey.
  • Loading branch information
Belphemur committed Aug 25, 2021
1 parent 292f58e commit 8d83ad6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
15 changes: 11 additions & 4 deletions SoundSwitch.UI.Menu/Form/QuickMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public record MenuClickedEvent(IconMenuItem<T>.DataContainer Item);
private readonly DebounceDispatcher _debounce = new();
private bool _hiding = false;
private readonly MethodInvoker _hideDisposeMethod;
private readonly TimeSpan _menuTimeOut = TimeSpan.FromSeconds(2);

[Browsable(true)]
public event EventHandler<MenuClickedEvent> ItemClicked;
Expand Down Expand Up @@ -51,10 +52,8 @@ public QuickMenu()

public void SetData(IEnumerable<IconMenuItem<T>.DataContainer> payloads)
{
_hiding = false;
_debounce.Debounce<object>(TimeSpan.FromMilliseconds(1500), _ => BeginInvoke(_hideDisposeMethod));
ResetOpacity();

DebounceHiding();

var payloadsArray = payloads.ToArray();
var newPayloads = payloadsArray.ToDictionary(container => container.Id);
var toRemove = _currentPayloads.Keys.Except(newPayloads.Keys);
Expand Down Expand Up @@ -100,8 +99,16 @@ public void SetData(IEnumerable<IconMenuItem<T>.DataContainer> payloads)
}
}

private void DebounceHiding()
{
_hiding = false;
_debounce.Debounce<object>(_menuTimeOut, _ => BeginInvoke(_hideDisposeMethod));
ResetOpacity();
}

private void OnItemClicked(IconMenuItem<T> control)
{
DebounceHiding();
var dataContainer = control.CurrentDataContainer;
ItemClicked?.Invoke(control, new MenuClickedEvent(dataContainer));

Expand Down
22 changes: 0 additions & 22 deletions SoundSwitch/Framework/Banner/BannerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@
********************************************************************/

using System;
using System.Linq;
using SoundSwitch.Audio.Manager;
using SoundSwitch.Audio.Manager.Interop.Enum;
using SoundSwitch.Common.Framework.Audio.Device;
using SoundSwitch.Model;
using SoundSwitch.UI.Menu.Component;
using SoundSwitch.UI.Menu.Form;

namespace SoundSwitch.Framework.Banner
{
Expand All @@ -30,31 +23,16 @@ public class BannerManager
{
private static System.Threading.SynchronizationContext syncContext;
private static BannerForm banner;
private static QuickMenu<DeviceFullInfo> menu;

/// <summary>
/// Show a banner notification with the given data
/// </summary>
/// <param name="data"></param>
public void ShowNotification(BannerData data)
{
var defaultDevice = AudioSwitcher.Instance.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eConsole);

// Execute the banner in the context of the UI thread
syncContext.Post((d) =>
{
if (menu == null)
{
menu = new QuickMenu<DeviceFullInfo>();
menu.Disposed += (sender, args) => menu = null;
}

menu.SetData(AppModel.Instance.AvailablePlaybackDevices.Select(info => new IconMenuItem<DeviceFullInfo>.DataContainer(info.LargeIcon, info.NameClean, defaultDevice.Id == info.Id, info.Id, info)));
menu.ItemClicked += (sender, @event) =>
{
var payload = @event.Item.Payload;
AudioSwitcher.Instance.SwitchTo(payload.Id, ERole.eConsole);
};
if (banner == null)
{
banner = new BannerForm();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
using SoundSwitch.Audio.Manager;
using SoundSwitch.Audio.Manager.Interop.Enum;
using SoundSwitch.Common.Framework.Audio.Device;
using SoundSwitch.Framework.QuickMenu;
using SoundSwitch.Framework.QuickMenu.Model;
using SoundSwitch.Model;

namespace SoundSwitch.Framework.DeviceCyclerManager.DeviceCycler
Expand All @@ -35,11 +37,19 @@ public abstract class ADeviceCycler : IDeviceCycler
public bool CycleAudioDevice(DataFlow type)
{
var audioDevices = GetDevices(type).ToArray();

bool CycleDevice()
{
var nextDevice = GetNextDevice(audioDevices, type);
QuickMenuManager.Instance.DisplayMenu(audioDevices.Select(info => new DeviceDataContainer(info, info.Id == nextDevice.Id)), @event => SetActiveDevice(@event.Item.Payload));
return SetActiveDevice(nextDevice);
}

return audioDevices switch
{
{ Length: 0 } => throw new AppModel.NoDevicesException(),
{ Length: 1 } => false,
_ => SetActiveDevice(GetNextDevice(audioDevices, type))
_ => CycleDevice()
};
}

Expand Down

0 comments on commit 8d83ad6

Please sign in to comment.