Skip to content

Commit 4edf0e8

Browse files
committed
Make a dynamic tooltip that tells remaining time
1 parent 855018e commit 4edf0e8

1 file changed

Lines changed: 52 additions & 6 deletions

File tree

Program.cs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Caffeinated.Helpers;
22
using Caffeinated.Properties;
3+
using Humanizer;
34
using Microsoft.Win32;
45
using RegistryUtils;
56
using System;
@@ -19,7 +20,9 @@ public class AppContext : ApplicationContext {
1920
private Icon? onIcon;
2021
private Icon? offIcon;
2122
private bool isActivated = false;
23+
private DateTime? endTime;
2224
private readonly Timer? timer;
25+
private readonly Timer updateTooltipTimer;
2326
private SettingsForm? settingsForm = null;
2427
private AboutForm? aboutForm = null;
2528
private bool isLightTheme = false;
@@ -52,6 +55,11 @@ public AppContext() {
5255
timer = new Timer(components);
5356
timer.Tick += new EventHandler(timer_Tick);
5457

58+
updateTooltipTimer = new Timer(components);
59+
updateTooltipTimer.Tick += new EventHandler(UpdateTooltipTimer_Tick);
60+
updateTooltipTimer.Interval = 10000; // 5 seconds
61+
updateTooltipTimer.Start();
62+
5563
appSettings = new AppSettings();
5664

5765
SetIsLightTheme();
@@ -81,11 +89,22 @@ public AppContext() {
8189
deactivate();
8290
}
8391

84-
if (appSettings.ShowMessageOnLaunch) {
92+
if (appSettings.ShowMessageOnLaunch || appSettings.IsFirstLaunch) {
93+
if (appSettings.IsFirstLaunch)
94+
appSettings.IsFirstLaunch = false;
95+
8596
showSettings();
8697
}
8798
}
8899

100+
private void UpdateTooltipTimer_Tick(object? sender, EventArgs e)
101+
{
102+
if (notifyIcon is null)
103+
return;
104+
105+
updateNotifyIconText();
106+
}
107+
89108
private void SetIsLightTheme(object? sender = null, EventArgs? e = null) {
90109
try {
91110
using RegistryKey? key = Registry.CurrentUser.OpenSubKey(themeKeyPath);
@@ -351,17 +370,44 @@ private void activate(int duration) {
351370
ShowError();
352371
ExitThread();
353372
}
354-
if (duration > 0
373+
int timerIntervalInMilliseconds = duration * 60 * 1000;
374+
if (timerIntervalInMilliseconds > 0
355375
&& timer != null) {
356-
timer.Interval = duration * 60 * 1000;
376+
timer.Interval = timerIntervalInMilliseconds;
357377
timer.Start();
378+
endTime = DateTime.Now.AddMilliseconds(timerIntervalInMilliseconds);
379+
}
380+
else {
381+
endTime = null;
358382
}
359383
isActivated = true;
360384

361-
if (notifyIcon != null) {
362-
notifyIcon.Icon = onIcon;
363-
notifyIcon.Text = "Caffeinated: sleep not allowed!";
385+
386+
if (notifyIcon is null)
387+
return;
388+
389+
notifyIcon.Icon = onIcon;
390+
updateNotifyIconText();
391+
}
392+
393+
private void updateNotifyIconText() {
394+
if (notifyIcon is null)
395+
return;
396+
397+
if (notifyIcon.Icon == offIcon)
398+
{
399+
notifyIcon.Text = "Caffeinated: sleep allowed";
400+
return;
364401
}
402+
403+
if (endTime is null) {
404+
notifyIcon.Text = $"Caffeinated: No sleep indefinitely";
405+
return;
406+
}
407+
408+
string timeRemaining = endTime.Value.AddSeconds(2).Humanize() ;
409+
Debug.WriteLine($"timeRemaining {timeRemaining}");
410+
notifyIcon.Text = $"Caffeinated: No sleep for about {timeRemaining}";
365411
}
366412

367413
private void deactivate() {

0 commit comments

Comments
 (0)