-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTickerOverlayForm.cs
More file actions
429 lines (368 loc) · 14.5 KB
/
TickerOverlayForm.cs
File metadata and controls
429 lines (368 loc) · 14.5 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace NaturalCommands
{
public enum TickerCategory
{
Info,
Warning,
Critical,
Success
}
public sealed record TickerMessage(string Text, TickerCategory Category = TickerCategory.Info);
public partial class TickerOverlayForm : Form
{
private readonly List<TickerMessage> _messages;
private int _currentIndex;
private int _cycleCount;
private readonly int _maxCycles;
private readonly bool _hideOnDismiss;
private readonly System.Windows.Forms.Timer _cycleTimer;
private readonly System.Windows.Forms.Timer _criticalPulseTimer;
private bool _pulseVisible;
// Hotkey IDs for global Alt+Key accelerators
private const int HOTKEY_ID_PREV = 0xB001;
private const int HOTKEY_ID_NEXT = 0xB002;
private const int HOTKEY_ID_DISMISS = 0xB003;
private const uint MOD_ALT = 0x0001;
private const int WM_HOTKEY = 0x0312;
public TickerOverlayForm(IEnumerable<string> lines, int cycleSeconds = 5, int maxCycles = 3, bool topPosition = false, bool hideOnDismiss = false)
: this(ParseMessages(lines), cycleSeconds, maxCycles, topPosition, hideOnDismiss)
{
}
public TickerOverlayForm(IEnumerable<TickerMessage> messages, int cycleSeconds = 5, int maxCycles = 3, bool topPosition = false, bool hideOnDismiss = false)
{
_messages = messages?.Where(m => !string.IsNullOrWhiteSpace(m.Text)).ToList() ?? new List<TickerMessage>();
if (_messages.Count == 0)
{
_messages.Add(new TickerMessage("No notifications", TickerCategory.Info));
}
_maxCycles = maxCycles; // 0 means never auto-close; >0 means auto-close after N cycles
_hideOnDismiss = hideOnDismiss;
InitializeComponent();
FormBorderStyle = FormBorderStyle.FixedToolWindow;
ShowInTaskbar = false;
TopMost = true;
BackColor = Color.FromArgb(30, 30, 30);
var screen = Screen.PrimaryScreen?.WorkingArea ?? Screen.AllScreens[0].WorkingArea;
Width = screen.Width;
Height = 140;
Left = screen.Left;
Top = topPosition ? screen.Top : screen.Bottom - Height;
_cycleTimer = new System.Windows.Forms.Timer { Interval = cycleSeconds * 1000 };
_cycleTimer.Tick += CycleTimer_Tick;
_criticalPulseTimer = new System.Windows.Forms.Timer { Interval = 500 };
_criticalPulseTimer.Tick += CriticalPulseTimer_Tick;
_labelMessage.Click += (s, e) => NextMessage();
_buttonNext.Click += (s, e) => NextMessage();
_buttonPrev.Click += (s, e) => PreviousMessage();
_buttonDismiss.Click += (s, e) => Dismiss();
KeyPreview = true;
KeyDown += TickerOverlayForm_KeyDown;
MouseEnter += (s, e) => _cycleTimer.Stop();
MouseLeave += (s, e) => StartCycleTimerIfNotCritical();
Load += (s, e) => { ForceTopmost(); TryRegisterHotkeys(); };
ShowMessage(0);
_cycleTimer.Start();
}
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private const uint SWP_NOMOVE = 0x0002;
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_NOACTIVATE = 0x0010;
private void ForceTopmost()
{
SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
}
public static TickerMessage ParseSingleLine(string line)
{
var list = ParseMessages(new[] { line });
return list.Count > 0 ? list[0] : new TickerMessage(line, TickerCategory.Info);
}
private static List<TickerMessage> ParseMessages(IEnumerable<string> lines)
{
var list = new List<TickerMessage>();
if (lines == null)
{
return list;
}
foreach (var raw in lines)
{
if (string.IsNullOrWhiteSpace(raw))
continue;
var line = raw.Trim();
var category = TickerCategory.Info;
var text = line;
var colonIndex = line.IndexOf(':');
if (colonIndex > 0)
{
var prefix = line.Substring(0, colonIndex).Trim();
var remainder = line.Substring(colonIndex + 1).Trim();
if (Enum.TryParse<TickerCategory>(prefix, true, out var parsed))
{
category = parsed;
text = remainder;
}
}
if (string.IsNullOrWhiteSpace(text))
continue;
list.Add(new TickerMessage(text, category));
}
if (!list.Any())
{
list.Add(new TickerMessage("No notifications", TickerCategory.Info));
}
return list;
}
private void StartCriticalPulse()
{
_criticalPulseTimer.Start();
_cycleTimer.Stop();
}
private void StopCriticalPulse()
{
_criticalPulseTimer.Stop();
_pulseVisible = false;
Invalidate();
StartCycleTimerIfNotCritical();
}
private void CriticalPulseTimer_Tick(object? sender, EventArgs e)
{
_pulseVisible = !_pulseVisible;
Invalidate();
}
private void CycleTimer_Tick(object? sender, EventArgs e)
{
if (CurrentCategory == TickerCategory.Critical)
{
return;
}
NextMessage();
}
private void StartCycleTimerIfNotCritical()
{
if (CurrentCategory != TickerCategory.Critical && !_cycleTimer.Enabled)
{
_cycleTimer.Start();
}
}
private void TickerOverlayForm_KeyDown(object? sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
Dismiss();
}
else if (e.KeyCode == Keys.Right)
{
NextMessage();
}
else if (e.KeyCode == Keys.Left)
{
PreviousMessage();
}
}
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private void TryRegisterHotkeys()
{
try
{
// Ensure we start clean
UnregisterHotkeys();
RegisterHotKey(Handle, HOTKEY_ID_PREV, MOD_ALT, (uint)Keys.P);
RegisterHotKey(Handle, HOTKEY_ID_NEXT, MOD_ALT, (uint)Keys.N);
RegisterHotKey(Handle, HOTKEY_ID_DISMISS, MOD_ALT, (uint)Keys.D);
try { NaturalCommands.Helpers.Logger.LogInfo("[TICKER-FORM] Hotkeys registered (Alt+P, Alt+N, Alt+D)"); } catch { }
}
catch (Exception ex)
{
try { NaturalCommands.Helpers.Logger.LogWarning($"[TICKER-FORM] RegisterHotkeys failed: {ex.Message}"); } catch { }
}
}
private void UnregisterHotkeys()
{
try
{
UnregisterHotKey(Handle, HOTKEY_ID_PREV);
}
catch { }
try
{
UnregisterHotKey(Handle, HOTKEY_ID_NEXT);
}
catch { }
try
{
UnregisterHotKey(Handle, HOTKEY_ID_DISMISS);
}
catch { }
}
private TickerMessage CurrentMessage => _messages[_currentIndex];
private TickerCategory CurrentCategory => CurrentMessage.Category;
private void ShowMessage(int newIndex)
{
_currentIndex = (newIndex + _messages.Count) % _messages.Count;
_cycleCount++;
// maxCycles == 0 means never auto-close — user must dismiss explicitly
if (_maxCycles > 0 && _cycleCount > _maxCycles * _messages.Count)
{
Dismiss();
return;
}
var message = CurrentMessage;
var icon = message.Category switch
{
TickerCategory.Warning => "⚠️",
TickerCategory.Critical => "🔴",
TickerCategory.Success => "✅",
_ => "ℹ️",
};
_labelMessage.Text = $"{icon} [{_currentIndex + 1}/{_messages.Count}] {message.Text}";
_labelCounter.Text = message.Category.ToString().ToUpper();
_accentBar.BackColor = message.Category switch
{
TickerCategory.Warning => Color.FromArgb(255, 255, 193, 7),
TickerCategory.Critical => Color.FromArgb(255, 244, 67, 54),
TickerCategory.Success => Color.FromArgb(255, 76, 175, 80),
_ => Color.FromArgb(255, 33, 150, 243),
};
if (message.Category == TickerCategory.Critical)
{
StartCriticalPulse();
}
else
{
StopCriticalPulse();
}
Invalidate();
}
private void NextMessage() => ShowMessage(_currentIndex + 1);
private void PreviousMessage() => ShowMessage(_currentIndex - 1);
private void Dismiss()
{
try { NaturalCommands.Helpers.Logger.LogInfo($"[TICKER-FORM] Dismiss invoked - hideOnDismiss={_hideOnDismiss}, Visible={Visible}"); } catch { }
if (_hideOnDismiss)
{
try { NaturalCommands.Helpers.Logger.LogInfo("[TICKER-FORM] Hiding ticker (hideOnDismiss=true)"); } catch { }
Hide();
return;
}
try { NaturalCommands.Helpers.Logger.LogInfo("[TICKER-FORM] Closing ticker (hideOnDismiss=false)"); } catch { }
Close();
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_HOTKEY)
{
try
{
var id = m.WParam.ToInt32();
if (id == HOTKEY_ID_PREV)
{
PreviousMessage();
return;
}
if (id == HOTKEY_ID_NEXT)
{
NextMessage();
return;
}
if (id == HOTKEY_ID_DISMISS)
{
Dismiss();
return;
}
}
catch { }
}
base.WndProc(ref m);
}
private void EnsureVisible()
{
if (!Visible)
{
try { NaturalCommands.Helpers.Logger.LogInfo("[TICKER-FORM] EnsureVisible: showing ticker (was hidden)"); } catch { }
try { Show(); } catch { }
}
if (WindowState == FormWindowState.Minimized)
{
WindowState = FormWindowState.Normal;
}
try
{
// Make sure the window is topmost and in front. Try to activate if possible.
TopMost = true;
BringToFront();
ForceTopmost();
try { Activate(); } catch { }
}
catch { }
}
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
try { NaturalCommands.Helpers.Logger.LogInfo($"[TICKER-FORM] Visible changed -> {Visible}"); } catch { }
if (Visible)
{
TryRegisterHotkeys();
}
else
{
UnregisterHotkeys();
}
}
/// <summary>
/// Thread-safe: adds a new message to the running ticker and refreshes the display.
/// Safe to call from any thread.
/// </summary>
public void AddMessage(TickerMessage message)
{
if (InvokeRequired)
{
try { BeginInvoke(new Action(() => AddMessage(message))); } catch { }
return;
}
_messages.Add(message);
try { NaturalCommands.Helpers.Logger.LogInfo($"[TICKER-FORM] AddMessage: queued message '{message.Text}' (Category={message.Category})"); } catch { }
// Reset cycle counter so the new message gets a full set of cycles
_cycleCount = 0;
EnsureVisible();
// Show the newly added message immediately
ShowMessage(_messages.Count - 1);
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
// Ensure hotkeys are unregistered when the form is closing
UnregisterHotkeys();
try { NaturalCommands.Helpers.Logger.LogInfo($"[TICKER-FORM] OnFormClosing: reason={e.CloseReason}, hideOnDismiss={_hideOnDismiss}"); } catch { }
if (_hideOnDismiss && e.CloseReason == CloseReason.UserClosing)
{
try { NaturalCommands.Helpers.Logger.LogInfo("[TICKER-FORM] OnFormClosing: canceling close and hiding instead"); } catch { }
e.Cancel = true;
Hide();
return;
}
base.OnFormClosing(e);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using var brush = new SolidBrush(BackColor);
e.Graphics.FillRectangle(brush, ClientRectangle);
using var accentBrush = new SolidBrush(_accentBar.BackColor);
e.Graphics.FillRectangle(accentBrush, 0, 0, 8, Height);
if (CurrentCategory == TickerCategory.Critical && _pulseVisible)
{
using var pen = new Pen(Color.Red, 6);
e.Graphics.DrawRectangle(pen, 2, 2, Width - 5, Height - 5);
}
}
}
}