-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrayApplicationContext.cs
More file actions
218 lines (187 loc) · 6.17 KB
/
TrayApplicationContext.cs
File metadata and controls
218 lines (187 loc) · 6.17 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Runtime.InteropServices;
namespace OpenWebUiSystray;
sealed class TrayApplicationContext : ApplicationContext
{
private const uint EventSystemForeground = 0x0003;
private const uint WineventOutOfContext = 0;
private readonly NotifyIcon _trayIcon;
private readonly string _startUrl;
private readonly Control _invokeControl = new();
private readonly WinEventDelegate _foregroundWinEvent;
private MainForm? _mainForm;
private GlobalHotkeyWindow? _globalHotkey;
private IntPtr _winEventHook;
public TrayApplicationContext(string startUrl)
{
_startUrl = startUrl;
_foregroundWinEvent = OnForegroundWinEvent;
var quitItem = new ToolStripMenuItem("Quit", null, (_, _) => Application.Exit());
var contextMenu = new ContextMenuStrip();
contextMenu.Items.Add(quitItem);
_trayIcon = new NotifyIcon
{
Icon = GenerateIcon(),
Text = "Open WebUI Systray",
ContextMenuStrip = contextMenu,
Visible = true
};
_trayIcon.MouseClick += OnTrayClick;
_invokeControl.CreateControl();
_globalHotkey = new GlobalHotkeyWindow(ToggleMainWindow);
_winEventHook = SetWinEventHook(
EventSystemForeground,
EventSystemForeground,
IntPtr.Zero,
_foregroundWinEvent,
0,
0,
WineventOutOfContext);
SyncHotkeyRegistration();
}
private IntPtr? MainFormHandle()
{
if (_mainForm is not { IsDisposed: false })
return null;
try
{
return _mainForm.Handle;
}
catch (ObjectDisposedException)
{
return null;
}
}
private void OnForegroundWinEvent(
IntPtr hWinEventHook,
uint eventType,
IntPtr hwnd,
int idObject,
int idChild,
uint idEventThread,
uint dwmsTimeStamp)
{
if (_invokeControl.InvokeRequired)
_invokeControl.BeginInvoke(SyncHotkeyRegistration);
else
SyncHotkeyRegistration();
}
private void SyncHotkeyRegistration()
{
if (_globalHotkey == null)
return;
if (FullscreenHotkeyPolicy.ShouldYieldGlobalHotkey(MainFormHandle))
_globalHotkey.Unregister();
else
_globalHotkey.TryRegister();
}
private void OnTrayClick(object? sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
ToggleMainWindow();
}
private void ToggleMainWindow()
{
if (_mainForm == null || _mainForm.IsDisposed)
{
_mainForm = new MainForm(_startUrl) { Icon = _trayIcon.Icon };
ShowMainWindow();
SyncHotkeyRegistration();
return;
}
if (_mainForm.Visible && _mainForm.WindowState != FormWindowState.Minimized)
{
_mainForm.Hide();
SyncHotkeyRegistration();
return;
}
ShowMainWindow();
SyncHotkeyRegistration();
}
private void ShowMainWindow()
{
_mainForm!.Show();
_mainForm.WindowState = FormWindowState.Normal;
_mainForm.BringToFront();
_mainForm.Activate();
}
private static Icon GenerateIcon()
{
const int size = 32;
using var bmp = new Bitmap(size, size);
using var g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
g.Clear(Color.Transparent);
var rect = new Rectangle(2, 2, size - 4, size - 4);
using var path = RoundedRect(rect, 6);
using (var brush = new SolidBrush(Color.White))
g.FillPath(brush, path);
using var font = new Font("Segoe UI", 14f, FontStyle.Bold, GraphicsUnit.Pixel);
const string text = "OI";
var textSize = g.MeasureString(text, font);
float x = (size - textSize.Width) / 2f;
float y = (size - textSize.Height) / 2f;
using (var brush = new SolidBrush(Color.FromArgb(30, 30, 30)))
g.DrawString(text, font, brush, x, y);
var hIcon = bmp.GetHicon();
using var temp = Icon.FromHandle(hIcon);
var icon = (Icon)temp.Clone();
DestroyIcon(hIcon);
return icon;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool DestroyIcon(IntPtr handle);
private static GraphicsPath RoundedRect(Rectangle bounds, int radius)
{
int d = radius * 2;
var path = new GraphicsPath();
path.AddArc(bounds.X, bounds.Y, d, d, 180, 90);
path.AddArc(bounds.Right - d, bounds.Y, d, d, 270, 90);
path.AddArc(bounds.Right - d, bounds.Bottom - d, d, d, 0, 90);
path.AddArc(bounds.X, bounds.Bottom - d, d, d, 90, 90);
path.CloseFigure();
return path;
}
[DllImport("user32.dll")]
private static extern IntPtr SetWinEventHook(
uint eventMin,
uint eventMax,
IntPtr hmodWinEventProc,
WinEventDelegate lpfnWinEventProc,
uint idProcess,
uint idThread,
uint dwFlags);
[DllImport("user32.dll")]
private static extern bool UnhookWinEventHook(IntPtr hWinEventHook);
private delegate void WinEventDelegate(
IntPtr hWinEventHook,
uint eventType,
IntPtr hwnd,
int idObject,
int idChild,
uint idEventThread,
uint dwmsTimeStamp);
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (_winEventHook != IntPtr.Zero)
{
UnhookWinEventHook(_winEventHook);
_winEventHook = IntPtr.Zero;
}
_invokeControl.Dispose();
_globalHotkey?.Dispose();
_globalHotkey = null;
_trayIcon.Visible = false;
_trayIcon.Dispose();
if (_mainForm is { IsDisposed: false })
_mainForm.Close();
_mainForm?.Dispose();
}
base.Dispose(disposing);
}
}