forked from TheJoeFin/Caffeinated
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
377 lines (326 loc) · 12.6 KB
/
Program.cs
File metadata and controls
377 lines (326 loc) · 12.6 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Caffeinated.Helpers;
using Caffeinated.Properties;
using Microsoft.Win32;
namespace Caffeinated {
public class Duration: IComparable {
public int Minutes { get; set; }
public string Description {
get {
return Duration.ToDescription(this.Minutes);
}
}
public static string ToDescription(int time) {
if (time == 0) {
return "Indefinitely";
}
string returnDescription = "";
if(time >= 60) {
int hours = time / 60;
if (hours == 1) {
returnDescription = "1 hr ";
}
else {
returnDescription = String.Format("{0} hrs ", hours);
}
}
int mins = time % 60;
if(mins == 1) {
returnDescription += String.Format("{0} min", mins);
}
if (mins > 1) {
returnDescription += String.Format("{0} mins", mins);
}
return returnDescription;
}
public int CompareTo(object obj) {
if (obj == null) return 1;
Duration otherDuration = obj as Duration;
if (otherDuration != null)
{
if (otherDuration.Minutes > this.Minutes)
return 1;
if (otherDuration.Minutes < this.Minutes)
return -1;
return 0;
}
else
return 1;
}
}
internal static class NativeMethods {
[DllImport("kernel32.dll")]
public static extern uint SetThreadExecutionState(uint esFlags);
public const uint ES_CONTINUOUS = 0x80000000;
public const uint ES_SYSTEM_REQUIRED = 0x00000001;
public const uint ES_DISPLAY_REQUIRED = 0x00000002;
}
public class AppContext : ApplicationContext {
private NotifyIcon notifyIcon;
private IContainer components;
private Icon onIcon;
private Icon offIcon;
private bool isActivated = false;
private Timer timer;
private SettingsForm settingsForm = null;
private AboutForm aboutForm = null;
private bool isLightTheme = false;
AppSettings appSettings;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var context = new AppContext();
if(context.notifyIcon == null)
Application.Exit();
else
Application.Run(context);
}
public AppContext() {
// Caffeinated.exe
var processes = Process.GetProcessesByName("Caffeinated");
if (processes.Length > 1)
{
// Is already running
return;
}
this.components = new Container();
this.timer = new Timer(components);
timer.Tick += new EventHandler(timer_Tick);
appSettings = new AppSettings();
SetIsLightTheme();
setIcons();
this.notifyIcon = new NotifyIcon(this.components);
setContextMenu();
// tooltip
notifyIcon.Text = "Caffeinated";
notifyIcon.Visible = true;
// Handle the DoubleClick event to activate the form.
notifyIcon.MouseClick += new MouseEventHandler(notifyIcon1_Click);
if (appSettings.ActivateOnLaunch)
activate(Settings.Default.DefaultDuration);
else
deactivate();
if (appSettings.ShowMessageOnLaunch)
showSettings();
}
void SetIsLightTheme() {
try {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize")) {
if (key != null) {
Object o = key.GetValue("SystemUsesLightTheme");
if (o != null) {
if (o.ToString() == "1")
isLightTheme = true;
else
isLightTheme = false;
}
}
}
}
catch (Exception) {
isLightTheme = false;
}
}
private static String HexConverter(Color c) {
return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
}
private void setIcons() {
switch (appSettings.Icon) {
case TrayIcon.Mug:
if (isLightTheme){
this.offIcon = new Icon(
Properties.Resources.Mug_Sleep_Black_icon,
SystemInformation.SmallIconSize
);
this.onIcon = new Icon(
Properties.Resources.Mug_Active_Black_icon,
SystemInformation.SmallIconSize
);
} else{
this.offIcon = new Icon(
Properties.Resources.mug_sleep_icon,
SystemInformation.SmallIconSize
);
this.onIcon = new Icon(
Properties.Resources.mug_active_icon,
SystemInformation.SmallIconSize
);
}
break;
case TrayIcon.EyeWithZzz:
if (isLightTheme){
this.offIcon = new Icon(
Properties.Resources.Eye_zzz_Sleep_Black_icon,
SystemInformation.SmallIconSize
);
this.onIcon = new Icon(
Properties.Resources.Eye_zzz_Active_Black_icon,
SystemInformation.SmallIconSize
);
} else {
this.offIcon = new Icon(
Properties.Resources.Eye_zzz_Sleep_icon,
SystemInformation.SmallIconSize
);
this.onIcon = new Icon(
Properties.Resources.Eye_zzz_Active_icon,
SystemInformation.SmallIconSize
);
}
break;
default:
if (isLightTheme) {
this.offIcon = new Icon(
Properties.Resources.Caffeine_Black_icon,
SystemInformation.SmallIconSize
);
this.onIcon = new Icon(
Properties.Resources.SleepEye_Black_icon,
SystemInformation.SmallIconSize
);
} else {
this.offIcon = new Icon(
Properties.Resources.cup_coffee_icon_bw,
SystemInformation.SmallIconSize
);
this.onIcon = new Icon(
Properties.Resources.cup_coffee_icon,
SystemInformation.SmallIconSize
);
}
break;
}
}
public void setContextMenu() {
var contextMenu = new ContextMenuStrip();
var exitItem = new ToolStripMenuItem("E&xit");
exitItem.Click += new EventHandler(this.exitItem_Click);
// If the user deleted all time settings, add 0 back in.
if (appSettings.Durations.Count == 0)
appSettings.DefaultDuration = 0;
// we want the lower durations to be closer to the mouse. So,
var times = appSettings.Durations;
IEnumerable<int> sortedTimes = Enumerable.Empty<int>();
if ((new Taskbar()).Position == TaskbarPosition.Top) {
sortedTimes = times.OrderBy(i => i);
}
else {
sortedTimes = times.OrderByDescending(i => i);
}
var settingsItem = new ToolStripMenuItem("&Settings...");
settingsItem.Click += new EventHandler(settingsItem_Click);
var aboutItem = new ToolStripMenuItem("&About...");
aboutItem.Click += new EventHandler(aboutItem_Click);
var settingsMenuItem = new ContextMenuStrip(){
Text = "Settings",
};
contextMenu.Items.AddRange(
new ToolStripMenuItem[] {
settingsItem,
aboutItem,
exitItem
}
);
contextMenu.Items.Add(new ToolStripSeparator());
foreach (var time in sortedTimes) {
var item = new ToolStripMenuItem(Duration.ToDescription(time));
item.Tag = time;
item.Click += new EventHandler(item_Click);
contextMenu.Items.Add(item);
}
notifyIcon.ContextMenuStrip = contextMenu;
}
void aboutItem_Click(object sender, EventArgs e) {
if (Application.OpenForms.OfType<AboutForm>().Any() == false) {
aboutForm = new AboutForm();
aboutForm.Show();
}
}
void settingsItem_Click(object sender, EventArgs e) {
if(Application.OpenForms.OfType<SettingsForm>().Any() == false)
showSettings();
}
void showSettings() {
settingsForm = new SettingsForm(appSettings);
settingsForm.FormClosing += SettingsForm_FormClosing;
settingsForm.Show();
}
private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e) {
setContextMenu();
SetIsLightTheme();
setIcons();
if (isActivated)
notifyIcon.Icon = onIcon;
else
notifyIcon.Icon = offIcon;
}
void timer_Tick(object sender, EventArgs e) {
deactivate();
}
void item_Click(object sender, EventArgs e) {
int time = (int)((ToolStripMenuItem)sender).Tag;
this.activate(time);
}
void notifyIcon1_Click(object sender, MouseEventArgs e) {
if (e.Button != MouseButtons.Left) return;
if (this.isActive())
this.deactivate();
else
this.activate(appSettings.DefaultDuration);
}
void ShowError() {
MessageBox.Show(
"Call to SetThreadExecutionState failed.",
"Caffeinated",
MessageBoxButtons.OK
);
}
bool isActive() {
return notifyIcon.Icon == onIcon;
}
void activate(int duration) {
var sleepDisabled = NativeMethods.ES_CONTINUOUS |
NativeMethods.ES_DISPLAY_REQUIRED;
uint previousState = NativeMethods.SetThreadExecutionState(sleepDisabled);
if (previousState == 0) {
ShowError();
ExitThread();
}
if (duration > 0) {
this.timer.Interval = duration * 60 * 1000;
this.timer.Start();
}
this.isActivated = true;
this.notifyIcon.Icon = onIcon;
this.notifyIcon.Text = "Caffeinated: sleep not allowed!";
}
void deactivate() {
timer.Stop();
uint result = NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS);
if (result == 0) {
ShowError();
}
this.isActivated = false;
this.notifyIcon.Icon = offIcon;
this.notifyIcon.Text = "Caffeinated: sleep allowed";
}
private void exitItem_Click(object Sender, EventArgs e) {
deactivate();
notifyIcon.Dispose();
ExitThread();
}
protected override void Dispose(bool disposing) {
if (disposing && components != null)
components.Dispose();
base.Dispose(disposing);
}
}
}