-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
314 lines (279 loc) · 9.49 KB
/
MainForm.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using DayTimer.Properties;
using Microsoft.Win32;
namespace DayTimer
{
public enum Activity
{
Startup,
Working,
Pauze,
ExtendedPauze,
InActive,
Lunch,
Meeting
}
public struct Timings
{
public Activity Activity;
public TimeSpan day;
public TimeSpan running;
public TimeSpan work;
public TimeSpan pauze;
public TimeSpan remaining;
public TimeSpan startupInterval;
public TimeSpan workInterval;
public TimeSpan pauzeInterval;
public TimeSpan inactive;
}
public interface ITime
{
Timings Timings { get; }
string Time { get; }
}
public partial class MainForm : Form, ITime
{
private WorkDayTimer _dayTimer;
private Pomodoro _pomodoro;
private int _pauzeExtensions = 0;
public MainForm()
{
InitializeComponent();
this.DataBindings.Add("TopMost", topMostCheckBox, "Checked");
if (Environment.MachineName == "RLAWIN7SP1")
{
Trace.Listeners.Add(new TextWriterTraceListener(@"E:\DayTimer.log"));
}
}
private void MainForm_Load(object sender, EventArgs e)
{
Icon = Properties.Resources.Stopwatch;
notifyIcon.Icon = Properties.Resources.Stopwatch;
_dayTimer = new WorkDayTimer();
_dayTimer.Tick += DayTimer_Tick;
_pomodoro = new Pomodoro();
_pomodoro.Tick += _pomodoro_Tick;
clock.Interval = 1000;
SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
var settings = Settings.Default;
if (settings.Date == DateTime.Today)
{
_pomodoro.LoadSettings(settings.TotalWork, settings.TotalPauze);
}
#if DEBUG
sessionBox.Visible = true;
sessionBox.Tag = true;
sessionBox.Image = Properties.Resources.SecurityLock;
#else
#endif
clock.Start();
_pomodoro.Start();
// Holy Crap!
GC.Collect(); // as the form is minimized and not shown anyway, try to release as much VM as possible.
}
private void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
switch (e.Reason)
{
case SessionSwitchReason.SessionLock:
_pomodoro.SessionLock();
break;
case SessionSwitchReason.SessionUnlock:
ShowNagScreen("Unlock");
break;
}
}
private void DayTimer_Tick(object sender, EventArgs e)
{
label.Text = Time;
WindowState = FormWindowState.Normal;
ShowInTaskbar = true;
TopMost = true;
}
private void clock_Tick(object sender, EventArgs e)
{
label.Text = Time;
}
private void _pomodoro_Tick(object sender, EventArgs e)
{
if (_pomodoro.Activity == Activity.Working)
{
notifyIcon.Icon = Properties.Resources.TimeForPauze; // SystemIcons.Application;
notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon.BalloonTipTitle = "Pomodoro end - time to relax";
notifyIcon.BalloonTipText = string.Format(Properties.Resources.TrayBalloonTipPauze, Pomodoro.PauzeInterval.Minutes);
notifyIcon.ShowBalloonTip(30 * 1000);
}
else
{
ShowNagScreen("Tick");
}
}
private void ShowNagScreen(string Title)
{
using (var sc = new ScreenBlock(this))
{
sc.WindowState = FormWindowState.Maximized;
sc.TopMost = true;
sc.Opacity = .75d;
sc.Debug = Title;
if (sc.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
StartWorking();
}
else
{
ExtendPauze(sc.PauzeInterval);
}
}
}
private void ExtendPauze(int pauze)
{
Trace.TraceInformation("ExtendPauze: {0}", pauze);
_pomodoro.ExtendPauze(pauze);
_pauzeExtensions++;
notifyIcon.Icon = Properties.Resources.PauzeOverTime;
if (_pauzeExtensions > 2) notifyIcon.Icon = Properties.Resources.PauzeWayOverTime;
//PomodoroStart(pauze * MinutesToMilliseconds);
}
private void StartWorking()
{
Trace.TraceInformation("StartWorking");
_pomodoro.StartWorking();
//SwitchPomodoro();
//_activity = Activity.Working;
_pauzeExtensions = 0;
if (autoHideCheckBox.Checked) HideMe();
startWorkingToolStripMenuItem.Visible = false;
notifyIcon.Icon = Properties.Resources.Sleeping_2;
}
private void StartPauze()
{
Trace.TraceInformation("StartPauze");
_pomodoro.StartPauze();
//SwitchPomodoro();
//_activity = Activity.Pauze;
if (showWhenPauzeCheckBox.Checked) ShowMe();
startPauzeToolStripMenuItem.Visible = false;
startWorkingToolStripMenuItem.Visible = true;
notifyIcon.Icon = Properties.Resources.Pauze;
}
public Timings Timings
{
get
{
Timings t = new Timings();
t.Activity = _pomodoro.Activity;
t.startupInterval = Pomodoro.StartupInterval;
t.workInterval = Pomodoro.WorkInterval;
t.pauzeInterval = Pomodoro.PauzeInterval;
t.day = _dayTimer.RunningTime;
t.work = _pomodoro.TotalWork;
t.pauze = _pomodoro.TotalPauze;
t.inactive = _pomodoro.TotalInactive;
t.running = _pomodoro.Elapsed;
t.remaining = _pomodoro.Remaining;
return t;
}
}
public string Time
{
get
{
var t = Timings;
var format = Properties.Resources.MainFormText;
var s = string.Format(format,
t.day.ToString(@"hh\:mm\:ss"),
t.work.ToString(@"hh\:mm\:ss"),
t.pauze.ToString(@"hh\:mm\:ss"),
t.running.ToString(@"mm\:ss"),
t.remaining > TimeSpan.Zero ? string.Empty : "-",
t.remaining.ToString(@"mm\:ss"));
return s;
}
}
private void notifyIcon_BalloonTipClicked(object sender, EventArgs e)
{
StartPauze();
}
private void notifyIcon_BalloonTipClosed(object sender, EventArgs e)
{
_pomodoro.DismissPauze();
startPauzeToolStripMenuItem.Visible = true;
}
private void notifyIcon_MouseMove(object sender, MouseEventArgs e)
{
var t = Timings;
var s = string.Format(Properties.Resources.TrayToolTip,
t.day.ToString(@"hh\:mm\:ss"),
t.work.ToString(@"hh\:mm\:ss"),
t.pauze.ToString(@"hh\:mm\:ss"),
t.running.ToString(@"mm\:ss"),
t.remaining > TimeSpan.Zero ? string.Empty : "-",
t.remaining.ToString(@"mm\:ss"),
t.Activity
);
notifyIcon.Text = s.Substring(0, Math.Min(s.Length, 63));
}
private void MainForm_SizeChanged(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
clock.Enabled = false;
ShowInTaskbar = false;
GC.Collect();
}
else
{
clock.Enabled = true;
}
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowMe();
}
private void ShowMe()
{
WindowState = FormWindowState.Normal;
ShowInTaskbar = true;
Activate();
}
private void HideMe()
{
ShowInTaskbar = false;
WindowState = FormWindowState.Minimized;
}
private void startPauzeToolStripMenuItem_Click(object sender, EventArgs e)
{
StartPauze();
}
private void startWorkingToolStripMenuItem_Click(object sender, EventArgs e)
{
StartWorking();
}
private void sessionBox_Click(object sender, EventArgs e)
{
if ((bool)sessionBox.Tag)
{
sessionBox.Image = Properties.Resources.Keys;
sessionBox.Tag = false;
}
else
{
sessionBox.Image = Properties.Resources.SecurityLock;
sessionBox.Tag = true;
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
var settings = Settings.Default;
settings.Date = DateTime.Today;
settings.DayTimer = _dayTimer.RunningTime;
settings.TotalWork = _pomodoro.TotalWork;
settings.TotalPauze = _pomodoro.TotalPauze;
}
}
}