-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeTrayIcon.cs
More file actions
65 lines (58 loc) · 2.1 KB
/
Copy pathNativeTrayIcon.cs
File metadata and controls
65 lines (58 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using Forms = System.Windows.Forms;
namespace SwitchifyPc.App;
public sealed class NativeTrayIcon : IDisposable
{
private readonly Forms.NotifyIcon notifyIcon;
private readonly Forms.ToolStripMenuItem statusItem;
private readonly Forms.ToolStripMenuItem disconnectItem;
public NativeTrayIcon(
Action showMainWindow,
Action showSettingsWindow,
Func<string> statusText,
Func<bool> canDisconnect,
Action disconnectDevices,
Action quit)
{
Forms.ContextMenuStrip menu = new();
menu.Items.Add("Show Switchify PC", null, (_, _) => showMainWindow());
menu.Items.Add("Open settings", null, (_, _) => showSettingsWindow());
menu.Items.Add(new Forms.ToolStripSeparator());
statusItem = new Forms.ToolStripMenuItem("Status unavailable")
{
Enabled = false
};
disconnectItem = new Forms.ToolStripMenuItem("Disconnect devices", null, (_, _) => disconnectDevices());
menu.Items.Add(statusItem);
menu.Items.Add(disconnectItem);
menu.Items.Add(new Forms.ToolStripSeparator());
menu.Items.Add("Quit", null, (_, _) => quit());
menu.Opening += (_, _) =>
{
statusItem.Text = statusText();
disconnectItem.Enabled = canDisconnect();
};
notifyIcon = new Forms.NotifyIcon
{
Icon = LoadAppIcon(),
Text = "Switchify PC",
ContextMenuStrip = menu,
Visible = true
};
notifyIcon.DoubleClick += (_, _) => showMainWindow();
}
public void Dispose()
{
notifyIcon.Visible = false;
notifyIcon.ContextMenuStrip?.Dispose();
notifyIcon.Dispose();
}
private static System.Drawing.Icon LoadAppIcon()
{
string? processPath = Environment.ProcessPath;
if (processPath is { Length: > 0 } && System.IO.File.Exists(processPath))
{
return System.Drawing.Icon.ExtractAssociatedIcon(processPath) ?? System.Drawing.SystemIcons.Application;
}
return System.Drawing.SystemIcons.Application;
}
}