-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrayIconManager.cs
More file actions
395 lines (327 loc) · 11.1 KB
/
TrayIconManager.cs
File metadata and controls
395 lines (327 loc) · 11.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
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
using System.Drawing;
using ScreenTimeTracker.Utilities;
namespace ScreenTimeTracker.App;
/// <summary>
/// Manages the system tray icon and context menu.
/// </summary>
public class TrayIconManager : IDisposable
{
private NotifyIcon? _notifyIcon;
private ContextMenuStrip? _contextMenu;
private ToolStripMenuItem? _pauseMenuItem;
private ToolStripMenuItem? _resumeMenuItem;
private ToolStripMenuItem? _focusPauseMenuItem;
private ToolStripMenuItem? _focusStopMenuItem;
private ToolStripSeparator? _focusSeparator;
private bool _isPaused;
private bool _isInFocusMode;
private bool _disposed;
/// <summary>
/// Raised when user requests to pause tracking.
/// </summary>
public event EventHandler? PauseRequested;
/// <summary>
/// Raised when user requests to resume tracking.
/// </summary>
public event EventHandler? ResumeRequested;
/// <summary>
/// Raised when user requests to exit.
/// </summary>
public event EventHandler? ExitRequested;
/// <summary>
/// Raised when user requests to view stats.
/// </summary>
public event EventHandler? StatsRequested;
/// <summary>
/// Raised when user requests to pause focus session via tray.
/// </summary>
public event EventHandler? FocusPauseRequested;
/// <summary>
/// Raised when user requests to stop focus session via tray.
/// </summary>
public event EventHandler? FocusStopRequested;
/// <summary>
/// Shows the tray icon.
/// </summary>
public void Show()
{
if (_notifyIcon != null)
return;
CreateContextMenu();
CreateNotifyIcon();
Logger.Info("Tray icon shown");
}
/// <summary>
/// Hides the tray icon.
/// </summary>
public void Hide()
{
if (_notifyIcon == null)
return;
_notifyIcon.Visible = false;
Logger.Info("Tray icon hidden");
}
/// <summary>
/// Updates the tray icon tooltip.
/// </summary>
public void UpdateTooltip(string text)
{
if (_notifyIcon != null)
{
// Tooltip max length is 127 chars
_notifyIcon.Text = text.Length > 127 ? text[..127] : text;
}
}
/// <summary>
/// Updates the paused state in the menu.
/// </summary>
public void SetPaused(bool isPaused)
{
_isPaused = isPaused;
if (_pauseMenuItem != null)
_pauseMenuItem.Visible = !isPaused;
if (_resumeMenuItem != null)
_resumeMenuItem.Visible = isPaused;
// Update icon to indicate paused state
if (_notifyIcon != null)
{
UpdateTooltip(isPaused ? "Screen Time Tracker (Paused)" : "Screen Time Tracker");
}
}
private void CreateNotifyIcon()
{
_notifyIcon = new NotifyIcon
{
Icon = CreateDefaultIcon(),
Text = "Screen Time Tracker",
Visible = true,
ContextMenuStrip = _contextMenu
};
_notifyIcon.MouseClick += (s, e) =>
{
// Open dashboard only on left click
if (e.Button == MouseButtons.Left)
{
StatsRequested?.Invoke(this, EventArgs.Empty);
}
};
}
private void CreateContextMenu()
{
_contextMenu = new ContextMenuStrip();
// Status header
var statusItem = new ToolStripMenuItem("Screen Time Tracker")
{
Enabled = false,
Font = new Font(_contextMenu.Font, FontStyle.Bold)
};
_contextMenu.Items.Add(statusItem);
_contextMenu.Items.Add(new ToolStripSeparator());
// Pause/Resume
_pauseMenuItem = new ToolStripMenuItem("Pause Tracking", null, (s, e) =>
{
PauseRequested?.Invoke(this, EventArgs.Empty);
});
_contextMenu.Items.Add(_pauseMenuItem);
_resumeMenuItem = new ToolStripMenuItem("Resume Tracking", null, (s, e) =>
{
ResumeRequested?.Invoke(this, EventArgs.Empty);
})
{
Visible = false
};
_contextMenu.Items.Add(_resumeMenuItem);
_contextMenu.Items.Add(new ToolStripSeparator());
// View stats
var viewStatsItem = new ToolStripMenuItem("View Stats", null, (s, e) =>
{
StatsRequested?.Invoke(this, EventArgs.Empty);
});
_contextMenu.Items.Add(viewStatsItem);
// Settings (future)
var settingsItem = new ToolStripMenuItem("Settings", null, (s, e) =>
{
ShowBalloon("Coming Soon", "Settings will be available in a future update");
});
_contextMenu.Items.Add(settingsItem);
// Focus mode separator and items (hidden by default)
_focusSeparator = new ToolStripSeparator { Visible = false };
_contextMenu.Items.Add(_focusSeparator);
_focusPauseMenuItem = new ToolStripMenuItem("⏸ Pause Focus", null, (s, e) =>
{
FocusPauseRequested?.Invoke(this, EventArgs.Empty);
})
{
Visible = false
};
_contextMenu.Items.Add(_focusPauseMenuItem);
_focusStopMenuItem = new ToolStripMenuItem("✕ End Focus Session", null, (s, e) =>
{
FocusStopRequested?.Invoke(this, EventArgs.Empty);
})
{
Visible = false
};
_contextMenu.Items.Add(_focusStopMenuItem);
_contextMenu.Items.Add(new ToolStripSeparator());
// Exit
var exitItem = new ToolStripMenuItem("Exit", null, (s, e) =>
{
ExitRequested?.Invoke(this, EventArgs.Empty);
});
_contextMenu.Items.Add(exitItem);
}
private void ShowBalloon(string title, string text)
{
_notifyIcon?.ShowBalloonTip(3000, title, text, ToolTipIcon.Info);
}
private static Icon CreateDefaultIcon()
{
// Create a simple clock-like icon programmatically
// In production, you'd load from resources
var bitmap = new Bitmap(32, 32);
using (var g = Graphics.FromImage(bitmap))
{
g.SmoothingMode = global::System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// Background circle
using (var brush = new SolidBrush(Color.FromArgb(76, 175, 80))) // Green
{
g.FillEllipse(brush, 2, 2, 28, 28);
}
// Clock hands
using (var pen = new Pen(Color.White, 2))
{
// Center point
var centerX = 16;
var centerY = 16;
// Hour hand (pointing to 10 o'clock)
g.DrawLine(pen, centerX, centerY, centerX - 5, centerY - 7);
// Minute hand (pointing to 2 o'clock)
g.DrawLine(pen, centerX, centerY, centerX + 7, centerY - 5);
}
// Center dot
using (var brush = new SolidBrush(Color.White))
{
g.FillEllipse(brush, 14, 14, 4, 4);
}
}
var hIcon = bitmap.GetHicon();
var icon = Icon.FromHandle(hIcon);
return icon;
}
/// <summary>
/// Enters focus mode - changes icon and shows focus menu items.
/// </summary>
public void StartFocusMode(int totalMinutes)
{
_isInFocusMode = true;
// Show focus menu items
if (_focusSeparator != null) _focusSeparator.Visible = true;
if (_focusPauseMenuItem != null) _focusPauseMenuItem.Visible = true;
if (_focusStopMenuItem != null) _focusStopMenuItem.Visible = true;
// Change icon to focus icon
if (_notifyIcon != null)
{
_notifyIcon.Icon = CreateFocusIcon(1.0);
}
UpdateFocusTime(TimeSpan.FromMinutes(totalMinutes), TimeSpan.FromMinutes(totalMinutes));
Logger.Info("Tray entered focus mode");
}
/// <summary>
/// Updates the focus timer display in tray.
/// </summary>
public void UpdateFocusTime(TimeSpan remaining, TimeSpan total)
{
if (_notifyIcon == null) return;
var minutes = (int)remaining.TotalMinutes;
var seconds = remaining.Seconds;
_notifyIcon.Text = $"🎯 Focus: {minutes:D2}:{seconds:D2}";
// Update icon progress
double progress = 1 - (remaining.TotalSeconds / total.TotalSeconds);
_notifyIcon.Icon = CreateFocusIcon(progress);
}
/// <summary>
/// Updates focus pause state.
/// </summary>
public void SetFocusPaused(bool isPaused)
{
if (_focusPauseMenuItem != null)
{
_focusPauseMenuItem.Text = isPaused ? "▶ Resume Focus" : "⏸ Pause Focus";
}
if (_notifyIcon != null && _isInFocusMode)
{
var currentText = _notifyIcon.Text ?? "";
if (isPaused && !currentText.Contains("(Paused)"))
{
_notifyIcon.Text = currentText + " (Paused)";
}
}
}
/// <summary>
/// Exits focus mode - restores normal icon.
/// </summary>
public void EndFocusMode()
{
_isInFocusMode = false;
// Hide focus menu items
if (_focusSeparator != null) _focusSeparator.Visible = false;
if (_focusPauseMenuItem != null) _focusPauseMenuItem.Visible = false;
if (_focusStopMenuItem != null) _focusStopMenuItem.Visible = false;
// Restore default icon
if (_notifyIcon != null)
{
_notifyIcon.Icon = CreateDefaultIcon();
_notifyIcon.Text = "Screen Time Tracker";
}
Logger.Info("Tray exited focus mode");
}
private static Icon CreateFocusIcon(double progress)
{
var bitmap = new Bitmap(32, 32);
using (var g = Graphics.FromImage(bitmap))
{
g.SmoothingMode = global::System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.Clear(Color.Transparent);
// Background circle (dark)
using (var brush = new SolidBrush(Color.FromArgb(30, 30, 30)))
{
g.FillEllipse(brush, 2, 2, 28, 28);
}
// Progress arc (teal green)
if (progress > 0)
{
using (var pen = new Pen(Color.FromArgb(0, 224, 150), 3))
{
// Draw arc based on progress
int sweepAngle = (int)(360 * progress);
g.DrawArc(pen, 3, 3, 26, 26, -90, sweepAngle);
}
}
// Center timer icon (small play triangle or pause bars)
using (var brush = new SolidBrush(Color.White))
{
// Simple dot
g.FillEllipse(brush, 13, 13, 6, 6);
}
}
var hIcon = bitmap.GetHicon();
return Icon.FromHandle(hIcon);
}
public void Dispose()
{
if (_disposed)
return;
Hide();
_notifyIcon?.Dispose();
_notifyIcon = null;
_contextMenu?.Dispose();
_contextMenu = null;
_disposed = true;
GC.SuppressFinalize(this);
}
~TrayIconManager()
{
Dispose();
}
}