Repository navigation
Expand file tree
/
Copy pathForm1.cs
More file actions
245 lines (213 loc) · 7.75 KB
/
Copy pathForm1.cs
File metadata and controls
245 lines (213 loc) · 7.75 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
using System;
using System.Drawing;
using System.Media;
using System.Threading.Tasks;
using System.Windows.Forms;
using CandleTimer.Services;
namespace CandleTimer
{
public partial class Form1 : Form
{
private System.Windows.Forms.Timer clockTimer = null!;
private Label lblTimer = null!;
private NumericUpDown numMinutes = null!;
private Label lblMinutesTag = null!;
private Label lblCompactTimeframe = null!;
private CheckBox chkAlwaysOnTop = null!;
private CheckBox chkSound = null!;
private CheckBox chkColorWarning = null!;
private Button btnSync = null!;
private Button btnCompact = null!;
private readonly NtpService ntpService = new NtpService();
private int selectedIntervalSeconds = 60;
private bool isCompact = false;
public Form1()
{
InitializeComponent();
SetupCustomUI();
// Initialize timer directly or pass components if available
clockTimer = components != null
? new System.Windows.Forms.Timer(components)
: new System.Windows.Forms.Timer();
clockTimer.Interval = 100;
clockTimer.Tick += ClockTimer_Tick;
clockTimer.Start();
TriggerNtpSync();
}
private async void TriggerNtpSync()
{
btnSync.Text = "Syncing...";
btnSync.Enabled = false;
bool success = await ntpService.SyncAsync();
if (success)
{
btnSync.Text = "Time Synced ✓";
btnSync.ForeColor = Color.Green;
}
else
{
btnSync.Text = "Sync Failed";
btnSync.ForeColor = Color.Red;
}
btnSync.Enabled = true;
}
private void SetupCustomUI()
{
this.Text = "CandleTimer";
this.Size = new Size(220, 230);
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
this.StartPosition = FormStartPosition.CenterScreen;
numMinutes = new NumericUpDown
{
Location = new Point(12, 12),
Width = 70,
Minimum = 1,
Maximum = 1440,
Value = 1
};
numMinutes.ValueChanged += NumMinutes_ValueChanged;
this.Controls.Add(numMinutes);
lblMinutesTag = new Label
{
Text = "min(s)",
Location = new Point(88, 14),
AutoSize = true,
Font = new Font("Segoe UI", 9F, FontStyle.Regular)
};
this.Controls.Add(lblMinutesTag);
lblCompactTimeframe = new Label
{
Text = "[ 1 min ]",
Location = new Point(12, 48),
Size = new Size(180, 15),
TextAlign = ContentAlignment.MiddleCenter,
Font = new Font("Segoe UI", 8.5F, FontStyle.Bold),
ForeColor = Color.DarkGray,
Visible = false
};
this.Controls.Add(lblCompactTimeframe);
lblTimer = new Label
{
Location = new Point(12, 42),
Size = new Size(180, 45),
Font = new Font("Consolas", 28, FontStyle.Bold),
TextAlign = ContentAlignment.MiddleCenter,
Text = "00:00"
};
this.Controls.Add(lblTimer);
chkAlwaysOnTop = new CheckBox
{
Text = "Always on Top",
Location = new Point(12, 90),
AutoSize = true,
Checked = true
};
chkAlwaysOnTop.CheckedChanged += (s, e) => { this.TopMost = chkAlwaysOnTop.Checked; };
this.Controls.Add(chkAlwaysOnTop);
chkSound = new CheckBox
{
Text = "Audio Alert",
Location = new Point(12, 112),
AutoSize = true,
Checked = false
};
this.Controls.Add(chkSound);
chkColorWarning = new CheckBox
{
Text = "Red <10s Warning",
Location = new Point(12, 134),
AutoSize = true,
Checked = true
};
this.Controls.Add(chkColorWarning);
btnSync = new Button
{
Text = "Sync NTP",
Location = new Point(12, 160),
Width = 95,
Height = 25
};
btnSync.Click += (s, e) => TriggerNtpSync();
this.Controls.Add(btnSync);
btnCompact = new Button
{
Text = "Compact",
Location = new Point(112, 160),
Width = 80,
Height = 25
};
btnCompact.Click += BtnCompact_Click;
this.Controls.Add(btnCompact);
}
private void NumMinutes_ValueChanged(object? sender, EventArgs e)
{
int mins = (int)numMinutes.Value;
if (mins < 1) return;
selectedIntervalSeconds = mins * 60;
lblCompactTimeframe.Text = $"[ {mins} min{(mins > 1 ? "s" : "")} ]";
}
private void BtnCompact_Click(object? sender, EventArgs e)
{
isCompact = !isCompact;
if (isCompact)
{
numMinutes.Visible = false;
lblMinutesTag.Visible = false;
chkAlwaysOnTop.Visible = false;
chkSound.Visible = false;
chkColorWarning.Visible = false;
btnSync.Visible = false;
lblCompactTimeframe.Visible = true;
btnCompact.Text = "Expand";
btnCompact.Location = new Point(12, 62);
btnCompact.Width = 180;
lblTimer.Location = new Point(12, 2);
this.Size = new Size(220, 130);
}
else
{
numMinutes.Visible = true;
lblMinutesTag.Visible = true;
chkAlwaysOnTop.Visible = true;
chkSound.Visible = true;
chkColorWarning.Visible = true;
btnSync.Visible = true;
lblCompactTimeframe.Visible = false;
btnCompact.Text = "Compact";
btnCompact.Location = new Point(112, 160);
btnCompact.Width = 80;
lblTimer.Location = new Point(12, 42);
this.Size = new Size(220, 230);
}
}
private void ClockTimer_Tick(object? sender, EventArgs e)
{
DateTime now = ntpService.GetCorrectedTime();
int secondsRemainingInCurrentMinute = 60 - now.Second;
if (secondsRemainingInCurrentMinute == 60)
{
secondsRemainingInCurrentMinute = 0;
}
int extraMinutes = (int)numMinutes.Value - 1;
long displaySeconds = (extraMinutes * 60) + secondsRemainingInCurrentMinute;
if (displaySeconds == 0 && now.Millisecond < 150)
{
if (chkSound.Checked)
{
Task.Run(() => SystemSounds.Asterisk.Play());
}
}
if (chkColorWarning.Checked && displaySeconds <= 10 && displaySeconds > 0)
{
lblTimer.ForeColor = Color.Red;
}
else
{
lblTimer.ForeColor = SystemColors.ControlText;
}
TimeSpan ts = TimeSpan.FromSeconds(displaySeconds);
lblTimer.Text = ts.TotalHours >= 1 ? ts.ToString(@"hh\:mm\:ss") : ts.ToString(@"mm\:ss");
}
}
}