-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmMain.cs
378 lines (318 loc) · 12.8 KB
/
frmMain.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
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
using System;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace TeaTime
{
public partial class frmMain : Form
{
#region -- Variables --
private Rectangle smallRect, bigRect, optionsRect;
private bool isSmall = false;
private Point mouseDownPosition = new Point(0,0);
private int rigthBounds = 30;
private ProgressBar currentActivePB = null;
private string appCaption = "Tea Time";
private string _currPhrase;
#endregion
#region --Event Handlers --
public frmMain()
{
InitializeComponent();
InitForms();
InitProgresses();
InitTimers();
}
private void frmMain_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point p = PointToScreen(new Point(e.X, e.Y));
Left = p.X - mouseDownPosition.X;
Top = p.Y - mouseDownPosition.Y;
}
}
private void frmMain_MouseDown(object sender, MouseEventArgs e)
{
//remember mouse position
mouseDownPosition = new Point(e.X, e.Y);
}
private void frmMain_MouseUp(object sender, MouseEventArgs e)
{
//drop mouse down coordinates
mouseDownPosition.X = 0;
mouseDownPosition.Y = 0;
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
if (pnlMain.Visible)
{
config.AppSettings.Settings["xMain"].Value = Location.X.ToString();
config.AppSettings.Settings["yMain"].Value = Location.Y.ToString();
}
else if (pnlOptions.Visible)
{
config.AppSettings.Settings["xOptions"].Value = Location.X.ToString();
config.AppSettings.Settings["yOptions"].Value = Location.Y.ToString();
}
else if (pnlSmallProgress.Visible)
{
config.AppSettings.Settings["xSmall"].Value = Location.X.ToString();
config.AppSettings.Settings["ySmall"].Value = Location.Y.ToString();
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
InitForms();
}
private void frmMain_KeyDown(object sender, KeyEventArgs e)
{
//is small then close. If not small then make small
if (isSmall && e.KeyCode == Keys.Escape)
Close();
else if (!isSmall && e.KeyCode == Keys.Escape)
tmrMain_Tick(sender, e);
tmrMain.Stop();
tmrMain.Start();
}
private void frmMain_MouseEnter(object sender, EventArgs e)
{
Opacity = 1;
}
private void frmMain_MouseLeave(object sender, EventArgs e)
{
if (!pnlOptions.Visible)
Opacity = 0.8;
}
private void btnOptions_Click(object sender, EventArgs e)
{
ShowOptions();
}
private void frmMain_Shown(object sender, EventArgs e)
{
ShowSmall();
}
private void btnSave_Click(object sender, EventArgs e)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings["breakTime"].Value = (int.Parse(txtBy.Text)*60*1000).ToString();
config.AppSettings.Settings["workTime"].Value = (int.Parse(txtEach.Text)*60*1000).ToString();
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
InitProgresses();
InitTimers();
ShowSmall();
}
private void btnCancelOptions_Click(object sender, EventArgs e)
{
ShowSmall();
}
private void btnResetOptions_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Вы действительно хотите сбросить настройки?", appCaption, MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings["breakTime"].Value = (15*60*1000).ToString();
config.AppSettings.Settings["workTime"].Value = (45*60*1000).ToString();
config.AppSettings.Settings["xSmall"].Value =
(Screen.PrimaryScreen.Bounds.Width - smallRect.Width - rigthBounds).ToString();
config.AppSettings.Settings["ySmall"].Value =
(Screen.PrimaryScreen.Bounds.Height - smallRect.Height - rigthBounds*2).ToString();
config.AppSettings.Settings["xMain"].Value =
(Screen.PrimaryScreen.Bounds.Width/2 - bigRect.Width/2).ToString();
config.AppSettings.Settings["yMain"].Value =
(Screen.PrimaryScreen.Bounds.Height/2 - bigRect.Height/2).ToString();
config.AppSettings.Settings["xOptions"].Value =
(Screen.PrimaryScreen.Bounds.Width - optionsRect.Width - rigthBounds).ToString();
config.AppSettings.Settings["yOptions"].Value =
(Screen.PrimaryScreen.Bounds.Height - optionsRect.Height - rigthBounds*2).ToString();
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
MessageBox.Show("Настройки сброшены", appCaption);
InitProgresses();
InitTimers();
ShowSmall();
}
}
private void btnSkip_Click(object sender, EventArgs e)
{
ShowSmall();
}
#region - Timers work -
private void tmrMain_Tick(object sender, EventArgs e)
{
ShowMessage();
tmrMain.Enabled = false;
tmrBreak.Enabled = true;
}
private void tmrBreak_Tick(object sender, EventArgs e)
{
InitTimers();
ShowSmall();
tmrMain.Enabled = true;
tmrBreak.Enabled = false;
}
private void tmrTicker_Tick(object sender, EventArgs e)
{
if (currentActivePB != null)
{
if (currentActivePB.Value == currentActivePB.Maximum)
currentActivePB.Value = 0;
currentActivePB.Value += 1;
}
}
#endregion
#endregion
#region -- Methods --
private void SetTicker(Timer tmr, ProgressBar pb)
{
tmrTicker.Interval = (int)Math.Round((double)tmr.Interval/(double)pb.Maximum);
currentActivePB = pb;
currentActivePB.Value = 0;
tmrTicker.Enabled = true;
}
/// <summary>
/// Show options panel
/// </summary>
private void ShowOptions()
{
Height = optionsRect.Height;
Top = optionsRect.Y;
Left = optionsRect.X;
Width = optionsRect.Width;
txtBy.Text = TimeSpan.FromMilliseconds(tmrBreak.Interval).TotalMinutes.ToString();
txtEach.Text = TimeSpan.FromMilliseconds(tmrMain.Interval).TotalMinutes.ToString();
pnlOptions.Width = Width + 2;
pnlOptions.Height = Height + 2;
pnlOptions.Visible = true;
pnlSmallProgress.Visible = false;
pnlMain.Visible = false;
tmrTicker.Enabled = tmrMain.Enabled = tmrBreak.Enabled = false;
Opacity = 1;
Activate();
}
/// <summary>
/// Show small panel
/// </summary>
private void ShowSmall()
{
Height = smallRect.Height;
Top = smallRect.Y;
Left = smallRect.X;
Width = smallRect.Width;
SetTicker(tmrMain, pbSmall);
pnlOptions.Visible = false;
pnlSmallProgress.Visible = true;
pnlMain.Visible = false;
tmrMain.Enabled = true;
tmrBreak.Enabled = false;
Opacity = 0.8;
Activate();
}
private string CurrPhrase
{
get { return _currPhrase; }
set
{
_currPhrase = value;
label1.Text = value;
}
}
/// <summary>
/// Show main panel
/// </summary>
private void ShowMessage()
{
Height = bigRect.Height;
Top = bigRect.Y;
Left = bigRect.X;
Width = bigRect.Width;
pnlOptions.Visible = false;
pnlSmallProgress.Visible = false;
pnlMain.Visible = true;
CurrPhrase = PhrasesProvider.GetNextPhrase();
SetTicker(tmrBreak, pbMain);
tmrMain.Enabled = false;
tmrBreak.Enabled = true;
Opacity = 1;
Activate();
}
/// <summary>
/// Initialize maximums for progress bars for smooth sliding
/// </summary>
private void InitProgresses()
{
//set progress bars maximum to 1 percent
pbSmall.Maximum =(int)Math.Round((double)tmrMain.Interval/100);
pbMain.Maximum = (int)Math.Round((double)tmrBreak.Interval / 100);
}
private void txtShowSmallGo_TextChanged(object sender, EventArgs e)
{
if (txtShowSmallGo.Text.Equals(CurrPhrase))
{
ShowSmall();
txtShowSmallGo.Text = "";
}
}
/// <summary>
/// Set save or default values for timers
/// </summary>
private void InitTimers()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
try
{
tmrBreak.Interval = int.Parse(config.AppSettings.Settings["breakTime"].Value);
tmrMain.Interval = int.Parse(config.AppSettings.Settings["workTime"].Value);
} catch
{
config.AppSettings.Settings["breakTime"].Value = (45 * 60 * 1000).ToString();
config.AppSettings.Settings["workTime"].Value = (15 * 60 * 1000).ToString();
}
}
/// <summary>
/// Set saved or default values for panels
/// </summary>
private void InitForms()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
//set small rectangle
smallRect = new Rectangle(1000, 850, 123, 27);
try
{
smallRect.X = int.Parse(config.AppSettings.Settings["xSmall"].Value);
smallRect.Y = int.Parse(config.AppSettings.Settings["ySmall"].Value);
}
catch
{
smallRect.X = Screen.PrimaryScreen.Bounds.Width - smallRect.Width - rigthBounds;
smallRect.Y = Screen.PrimaryScreen.Bounds.Height - smallRect.Height - rigthBounds * 2;
}
//set options rectangle
optionsRect = new Rectangle(170, 70, 120, 64);
try
{
optionsRect.X = int.Parse(config.AppSettings.Settings["xOptions"].Value);
optionsRect.Y = int.Parse(config.AppSettings.Settings["yOptions"].Value);
}
catch
{
optionsRect.X = Screen.PrimaryScreen.Bounds.Width - optionsRect.Width - rigthBounds;
optionsRect.Y = Screen.PrimaryScreen.Bounds.Height - optionsRect.Height - rigthBounds * 2;
}
//set big window rectangle
bigRect = new Rectangle(500, 400, pnlMain.Width, pnlMain.Height);
try
{
bigRect.X = int.Parse(config.AppSettings.Settings["xMain"].Value);
bigRect.Y = int.Parse(config.AppSettings.Settings["yMain"].Value);
}
catch
{
bigRect.X = Screen.PrimaryScreen.Bounds.Width / 2 - bigRect.Width / 2;
bigRect.Y = Screen.PrimaryScreen.Bounds.Height / 2 - bigRect.Height / 2;
}
//set panels position
pnlSmallProgress.Location = pnlMain.Location = pnlOptions.Location = new Point(1, 1);
}
#endregion
}
}