Skip to content

Commit 59922d6

Browse files
committed
feat(DeviceMenu): Auto-hide after inactivity to not stay on user screen
1 parent e0a6a1a commit 59922d6

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

SoundSwitch/UI/Forms/DeviceSelectorMenu.Designer.cs

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SoundSwitch/UI/Forms/DeviceSelectorMenu.cs

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
4+
using System.Threading;
35
using System.Windows.Forms;
46
using SoundSwitch.UI.Forms.Components;
7+
using SoundSwitch.Util.Timer;
58

69
namespace SoundSwitch.UI.Forms
710
{
811
public partial class DeviceSelectorMenu : Form
912
{
1013
private readonly Dictionary<string, IconMenuItem.DataContainer> _currentPayloads = new();
14+
private readonly DebounceDispatcher _debounce = new();
15+
private bool _hiding = false;
16+
private readonly MethodInvoker _deleteMethod;
1117
protected override bool ShowWithoutActivation => true;
1218

1319
/// <summary>
@@ -19,7 +25,7 @@ protected override CreateParams CreateParams
1925
get
2026
{
2127
CreateParams p = base.CreateParams;
22-
// p.ExStyle |= 0x08000000; // WS_EX_NOACTIVATE
28+
// p.ExStyle |= 0x08000000; // WS_EX_NOACTIVATE
2329
p.ExStyle |= 0x00000008; // WS_EX_TOPMOST
2430
return p;
2531
}
@@ -28,10 +34,16 @@ protected override CreateParams CreateParams
2834
public DeviceSelectorMenu()
2935
{
3036
InitializeComponent();
37+
ResetOpacity();
38+
_deleteMethod = Delete;
3139
}
3240

3341
public void SetData(IEnumerable<IconMenuItem.DataContainer> payloads)
3442
{
43+
_hiding = false;
44+
_debounce.Debounce<object>(TimeSpan.FromMilliseconds(1500), _ => BeginInvoke(_deleteMethod));
45+
ResetOpacity();
46+
3547
var payloadsArray = payloads.ToArray();
3648
var newPayloads = payloadsArray.ToDictionary(container => container.Id);
3749
var toRemove = _currentPayloads.Keys.Except(newPayloads.Keys);
@@ -79,6 +91,27 @@ public void SetData(IEnumerable<IconMenuItem.DataContainer> payloads)
7991
SetLocationToCursor();
8092
}
8193

94+
private void Delete()
95+
{
96+
_hiding = true;
97+
while (Opacity > 0.0)
98+
{
99+
Thread.Sleep(50);
100+
101+
if (!_hiding)
102+
break;
103+
Opacity -= 0.05;
104+
}
105+
106+
Hide();
107+
Dispose();
108+
}
109+
110+
private void ResetOpacity()
111+
{
112+
Opacity = 0.7D;
113+
}
114+
82115
private void SetLocationToCursor()
83116
{
84117
SetDesktopLocation(Cursor.Position.X, Cursor.Position.Y);

0 commit comments

Comments
 (0)