This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
351 lines (277 loc) · 9.53 KB
/
Form1.cs
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
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Tomato.Properties;
using System.Diagnostics;
namespace Tomato
{
public enum Activity
{
Idle = 0,
Working = 1,
Paused = 2,
BreakShort = 3,
BreakLong = 4
}
public partial class Form1 : Form
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
const int ACTIVITY_DURATION = 60 * 25;
int activityClock, pauseClock, shorts, totals;
Timer clockTimer;
Activity activity;
string goal;
NotifyIcon tray;
bool forceClose;
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
void Form1_Load(object sender, EventArgs e)
{
tray = new NotifyIcon();
tray.Icon = Resources.pomodoro_icon;
tray.Text = "Pomodoro";
tray.ContextMenuStrip = contextMenuStrip1;
tray.Visible = true;
clockTimer = new Timer()
{
Enabled = true
};
clockTimer.Tick += clockTimer_Tick;
redata();
this.FormClosing += Form1_FormClosing;
tray.DoubleClick += tray_DoubleClick;
ctxExit.Click += ctxExit_Click;
ctxShow.Click += ctxShow_Click;
}
private void tray_DoubleClick(object sender, EventArgs e)
{
RevealTray();
}
private void ctxShow_Click(object sender, EventArgs e)
{
RevealTray();
}
public void RevealTray()
{
this.Show();
this.Activate();
this.WindowState = FormWindowState.Normal;
}
private void ctxExit_Click(object sender, EventArgs e)
{
KillIt();
}
private void pomoNotif(string notif)
{
tray.ShowBalloonTip(5000, "Mr. Pomodoro", notif, ToolTipIcon.Info);
}
public void KillIt()
{
tray.Visible = false;
tray.Dispose();
Application.DoEvents();
forceClose = true;
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (!forceClose)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
Application.DoEvents();
this.Hide();
pomoNotif("Pomodoro has been minimized to your tray and is, in fact, still running.");
}
else
{
this.clockTimer.Abort();
}
}
private void clockTimer_Tick(object sender, EventArgs e)
{
redata(true);
}
private bool promptActivity(string pre = null)
{
var d = new ActivityDialog(pre);
if (d.ShowDialog() == DialogResult.Cancel)
{
return false;
}
goal = d.Input;
Logging.WriteActivityLog(goal, DateTime.Now);
pomoNotif("Starting work" + (String.IsNullOrWhiteSpace(goal) ? "" : ": " + goal));
return true;
}
private void redata(bool timer = false)
{
this.Invoke(new Action(() =>
{
if (activityClock >= ACTIVITY_DURATION)
{
if (shorts >= 3)
{
Alarm.ActivityEnd();
activity = Activity.BreakLong;
shorts = 0;
pomoNotif("The activity has ended. Time for a long break.");
}
else
{
Alarm.PauseNotify();
activity = Activity.BreakShort;
shorts++;
pomoNotif("The activity has ended. Time for a short break.");
}
totals++;
RevealTray();
}
slbCount.Text = "Pomodoros today: " + totals;
slbBreakInfo.Text = shorts < 3 ? (4 - shorts) + " Pomodoros to go until long break" : "Next break is long";
lblDateTime.Text = DateTime.Now.ToString();
lblTime.ForeColor = Color.Black;
button2.Enabled = true;
switch (activity)
{
case Activity.BreakLong:
activityClock = 0;
lblExplain.Text = "Taking a long break (15-30 min)";
button1.Text = "Go back to work";
button2.Text = "Reset break timer";
if (timer)
{
pauseClock++;
}
tray.Text = "Pomodoro is on a short break";
setTimer(pauseClock, true);
break;
case Activity.BreakShort:
activityClock = 0;
lblExplain.Text = "Taking a short break (3-5 min)";
button1.Text = "Go back to work";
button2.Text = "Reset break timer";
if (timer)
{
pauseClock++;
}
tray.Text = "Pomodoro is on a long break";
setTimer(pauseClock, true);
break;
case Activity.Idle:
tray.Text = "Pomodoro";
lblExplain.Text = "Not started yet";
button1.Text = "Start now";
button2.Text = "You should start working!";
button2.Enabled = false;
setTimer(0);
break;
case Activity.Paused:
tray.Text = "Pomodoro is paused";
lblTime.ForeColor = Color.Red;
lblExplain.Text = "Paused";
button1.Text = "Resume";
button2.Text = "New activity";
setTimer(ACTIVITY_DURATION - activityClock);
break;
case Activity.Working:
lblExplain.Text = String.IsNullOrWhiteSpace(goal) ? "Working" : goal;
button1.Text = "Pause";
if (timer)
{
activityClock++;
}
button2.Text = "Reset activity";
tray.Text = "Pomodoro is working ";
setTimer(ACTIVITY_DURATION - activityClock, true);
break;
}
}));
}
private void setTimer(int secs, bool traytext = false)
{
int mins = (int)Math.Floor(secs / 60d);
int secsLeft = (secs - (mins * 60));
var minString = mins.ToString();
var secString = secsLeft.ToString();
if (minString.Length == 1)
{
minString = "0" + minString;
}
if (secString.Length == 1)
{
secString = "0" + secString;
}
lblTime.Text = minString + ":" + secString;
if (traytext)
{
tray.Text += minString + ":" + secString;
}
}
private void button1_Click(object sender, EventArgs e)
{
switch (activity)
{
case Activity.BreakLong:
case Activity.BreakShort:
if (promptActivity())
{
pauseClock = 0;
activityClock = 0;
activity = Activity.Working;
}
break;
case Activity.Paused:
activity = Activity.Working;
break;
case Activity.Working:
activity = Activity.Paused;
break;
case Activity.Idle:
if (promptActivity())
{
activity = Activity.Working;
}
break;
}
redata();
}
private void button2_Click(object sender, EventArgs e)
{
switch (activity)
{
case Activity.BreakLong:
case Activity.BreakShort:
pauseClock = 0;
redata();
break;
case Activity.Working:
case Activity.Paused:
if (promptActivity(goal))
{
activityClock = 0;
redata();
}
break;
}
}
private void tsLogLink_Click(object sender, EventArgs e)
{
if (!Logging.FileExists)
{
Logging.CreateFileFromTemplate();
}
try
{
Process.Start(Logging.FilePath);
}
catch (Exception) { }
}
}
}