-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountdownTimer.ps1
More file actions
411 lines (340 loc) · 12.6 KB
/
Copy pathCountdownTimer.ps1
File metadata and controls
411 lines (340 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# Countdown Timer Ring - PowerShell WinForms
# Save as: CountdownTimer.ps1
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Timer state variables
$timer = $null
$script:totalSeconds = 0
$script:remainingSeconds = 0
$script:isRunning = $false
$script:startTime = [DateTime]::Now
# Colors
$bgColor = [System.Drawing.Color]::FromArgb(30, 30, 30)
$ringBgColor = [System.Drawing.Color]::FromArgb(60, 60, 60)
$ringFillColor = [System.Drawing.Color]::FromArgb(139, 0, 0)
$controlColor = [System.Drawing.Color]::FromArgb(70, 70, 70)
$titleBarColor = [System.Drawing.Color]::FromArgb(50, 50, 50)
# Create main form with THIN BORDER for resizing
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(300, 400)
$form.MinimumSize = New-Object System.Drawing.Size(100, 125)
$form.StartPosition = "CenterScreen"
$form.FormBorderStyle = "Sizable"
$form.BackColor = $bgColor
$form.Opacity = 0.6
$form.TopMost = $true
$form.ControlBox = $false
# Variables for dragging
$script:mouseDown = $false
$script:lastLocation = $null
# Create custom title bar (hidden by default) - MUCH SHORTER
$titleBar = New-Object System.Windows.Forms.Panel
$titleBar.Dock = "Top"
$titleBar.Height = 8 # CHANGED: Reduced to ~10% of original (was 30)
$titleBar.BackColor = $titleBarColor
$titleBar.Visible = $false # Initially hidden
# REMOVED: Title label completely (no text needed)
# Close button - smaller and positioned for new height
$closeButton = New-Object System.Windows.Forms.Button
$closeButton.Text = "✕"
$closeButton.Size = New-Object System.Drawing.Size(20, 8) # CHANGED: Smaller to fit
$closeButton.Location = New-Object System.Drawing.Point(280, 0) # CHANGED: Positioned at top right
$closeButton.FlatStyle = "Flat"
$closeButton.BackColor = [System.Drawing.Color]::FromArgb(80, 80, 80)
$closeButton.ForeColor = [System.Drawing.Color]::White
$closeButton.FlatAppearance.BorderSize = 0
$closeButton.Font = New-Object System.Drawing.Font("Arial", 6) # CHANGED: Smaller font
$closeButton.Add_Click({ $form.Close() })
$titleBar.Controls.Add($closeButton)
# Make the title bar draggable to move the window
$titleBar.Add_MouseDown({
param($sender, $e)
if ($e.Button -eq [System.Windows.Forms.MouseButtons]::Left) {
$script:mouseDown = $true
$script:lastLocation = $e.Location
}
})
$titleBar.Add_MouseMove({
param($sender, $e)
if ($script:mouseDown) {
$newX = $form.Left + ($e.X - $script:lastLocation.X)
$newY = $form.Top + ($e.Y - $script:lastLocation.Y)
$form.Location = New-Object System.Drawing.Point($newX, $newY)
}
})
$titleBar.Add_MouseUp({
param($sender, $e)
$script:mouseDown = $false
})
# PictureBox for drawing the ring
$pictureBox = New-Object System.Windows.Forms.PictureBox
$pictureBox.Dock = "Fill"
$pictureBox.BackColor = $bgColor
# Make the picture box draggable too (when controls are visible)
$pictureBox.Add_MouseDown({
param($sender, $e)
if ($titleBar.Visible -and $e.Button -eq [System.Windows.Forms.MouseButtons]::Left) {
$script:mouseDown = $true
$script:lastLocation = $e.Location
}
})
$pictureBox.Add_MouseMove({
param($sender, $e)
if ($titleBar.Visible -and $script:mouseDown) {
$newX = $form.Left + ($e.X - $script:lastLocation.X)
$newY = $form.Top + ($e.Y - $script:lastLocation.Y)
$form.Location = New-Object System.Drawing.Point($newX, $newY)
}
})
$pictureBox.Add_MouseUp({
param($sender, $e)
$script:mouseDown = $false
})
# Timer display function with error handling
function Update-Display {
param([float]$progress = 0)
try {
if ($pictureBox.Width -le 0 -or $pictureBox.Height -le 0) {
return
}
# Ensure progress is between 0 and 1
if ($progress -lt 0) { $progress = 0 }
if ($progress -gt 1) { $progress = 1 }
# Create new bitmap
$newBitmap = New-Object System.Drawing.Bitmap([Math]::Max(1, $pictureBox.Width), [Math]::Max(1, $pictureBox.Height))
$graphics = [System.Drawing.Graphics]::FromImage($newBitmap)
$graphics.SmoothingMode = "AntiAlias"
$graphics.Clear($bgColor)
# Calculate ring dimensions (minimum 50 pixels)
$size = [Math]::Max(50, [Math]::Min($pictureBox.Width, $pictureBox.Height) * 0.8)
$penWidth = $size * 0.1
$x = ($pictureBox.Width - $size) / 2
$y = ($pictureBox.Height - $size) / 2
# Draw background ring
$bgPen = New-Object System.Drawing.Pen($ringBgColor, $penWidth)
$graphics.DrawEllipse($bgPen, $x, $y, $size, $size)
$bgPen.Dispose()
# Draw progress arc if any
if ($progress -gt 0) {
$fillPen = New-Object System.Drawing.Pen($ringFillColor, $penWidth)
$sweepAngle = 360 * $progress
$graphics.DrawArc($fillPen, $x, $y, $size, $size, -90, $sweepAngle)
$fillPen.Dispose()
}
$graphics.Dispose()
# Safely swap bitmaps
$oldBitmap = $pictureBox.Image
$pictureBox.Image = $newBitmap
# Dispose old bitmap after new one is assigned
if ($oldBitmap -ne $null) {
$oldBitmap.Dispose()
}
# Force immediate redraw
$pictureBox.Refresh()
} catch {
Write-Host "Error in Update-Display: $_"
}
}
# Create timer
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 100
# Timer tick event with proper script-scoped variables
$timer.Add_Tick({
if ($script:isRunning -and $script:totalSeconds -gt 0) {
try {
# 1. GET CURRENT TIME from the system
$currentTime = [DateTime]::Now
# 2. SUBTRACT from the recorded start time
$elapsedTime = ($currentTime - $script:startTime).TotalSeconds
# 3. CALCULATE progress and remaining time
$progress = $elapsedTime / $script:totalSeconds
$script:remainingSeconds = $script:totalSeconds - $elapsedTime
if ($script:remainingSeconds -le 0) {
# Timer completed
$script:remainingSeconds = 0
$progress = 1
$script:isRunning = $false
$timer.Stop()
# Timer completed notification
# Uncomment to allow beep
# [System.Media.SystemSounds]::Beep.Play()
# Update UI
$btnStart.Enabled = $true
$btnStop.Enabled = $false
$txtTime.Enabled = $true
}
# 4. UPDATE DISPLAY with the calculated progress
Update-Display -progress $progress
} catch {
Write-Host "ERROR in timer tick: $_"
}
}
})
# Create controls panel (hidden by default)
$panel = New-Object System.Windows.Forms.Panel
$panel.Dock = "Bottom"
$panel.Height = 120
$panel.BackColor = $controlColor
$panel.Visible = $false
# Time input
$label = New-Object System.Windows.Forms.Label
$label.Text = "Minutes:"
$label.ForeColor = [System.Drawing.Color]::White
$label.Location = New-Object System.Drawing.Point(20, 15)
$label.Size = New-Object System.Drawing.Size(80, 20)
$panel.Controls.Add($label)
$txtTime = New-Object System.Windows.Forms.TextBox
$txtTime.Location = New-Object System.Drawing.Point(100, 12)
$txtTime.Size = New-Object System.Drawing.Size(80, 20)
$txtTime.Text = "30"
$txtTime.BackColor = [System.Drawing.Color]::FromArgb(50, 50, 50)
$txtTime.ForeColor = [System.Drawing.Color]::White
$panel.Controls.Add($txtTime)
# Buttons - Stacked vertically
$btnStart = New-Object System.Windows.Forms.Button
$btnStart.Location = New-Object System.Drawing.Point(20, 40)
$btnStart.Size = New-Object System.Drawing.Size(160, 30)
$btnStart.Text = "Start"
$btnStart.BackColor = [System.Drawing.Color]::FromArgb(80, 80, 80)
$btnStart.ForeColor = [System.Drawing.Color]::White
$btnStart.FlatStyle = "Flat"
$panel.Controls.Add($btnStart)
$btnStop = New-Object System.Windows.Forms.Button
$btnStop.Location = New-Object System.Drawing.Point(20, 75)
$btnStop.Size = New-Object System.Drawing.Size(160, 30)
$btnStop.Text = "Stop"
$btnStop.Enabled = $false
$btnStop.BackColor = [System.Drawing.Color]::FromArgb(80, 80, 80)
$btnStop.ForeColor = [System.Drawing.Color]::White
$btnStop.FlatStyle = "Flat"
$panel.Controls.Add($btnStop)
# ========== SIMPLIFIED HOVER LOGIC ==========
# Create a timer for delayed hiding
$hideTimer = New-Object System.Windows.Forms.Timer
$hideTimer.Interval = 500
$hideTimer.Enabled = $false
# Function to show controls
function Show-Controls {
$titleBar.Visible = $true
$panel.Visible = $true
$hideTimer.Stop()
}
# Function to start hiding controls (with delay)
function Start-HideTimer {
$hideTimer.Start()
}
# Timer tick to hide controls
$hideTimer.Add_Tick({
$titleBar.Visible = $false
$panel.Visible = $false
$hideTimer.Stop()
})
# Mouse enter events - show controls immediately
$form.Add_MouseEnter({ Show-Controls })
$pictureBox.Add_MouseEnter({ Show-Controls })
$titleBar.Add_MouseEnter({ Show-Controls })
$panel.Add_MouseEnter({ Show-Controls })
# Mouse leave events - start hide timer
$form.Add_MouseLeave({ Start-HideTimer })
$pictureBox.Add_MouseLeave({ Start-HideTimer })
$titleBar.Add_MouseLeave({ Start-HideTimer })
$panel.Add_MouseLeave({ Start-HideTimer })
# Also handle mouse events for controls inside the panel
foreach ($control in $panel.Controls) {
$control.Add_MouseEnter({ Show-Controls })
$control.Add_MouseLeave({ Start-HideTimer })
}
# Also handle mouse events for controls inside the title bar
foreach ($control in $titleBar.Controls) {
$control.Add_MouseEnter({ Show-Controls })
$control.Add_MouseLeave({ Start-HideTimer })
}
# ========== END OF SIMPLIFIED HOVER LOGIC ==========
# Button event handlers
$btnStart.Add_Click({
if ($script:isRunning) {
return
}
# Parse minutes input
$minutesInput = $txtTime.Text.Trim()
# Try parsing as minutes only
if ($minutesInput -match "^\d+$") {
$script:totalSeconds = [int]$minutesInput * 60
}
else {
[System.Windows.Forms.MessageBox]::Show("Please enter minutes as a whole number (e.g., 5)", "Invalid Input")
return
}
if ($script:totalSeconds -le 0) {
[System.Windows.Forms.MessageBox]::Show("Time must be greater than 0 minutes", "Invalid Input")
return
}
# Start timer
$script:remainingSeconds = $script:totalSeconds
$script:isRunning = $true
$script:startTime = [DateTime]::Now
$btnStop.Enabled = $true
$btnStart.Enabled = $false
$txtTime.Enabled = $false
# Immediate display update when starting
Update-Display -progress 0
$timer.Start()
})
$btnStop.Add_Click({
$script:isRunning = $false
$timer.Stop()
$btnStop.Enabled = $false
$btnStart.Enabled = $true
$txtTime.Enabled = $true
Update-Display -progress 0
})
# Form resize event
$form.Add_Resize({
# Adjust close button position when form resizes (stays top right)
$closeButton.Left = $form.ClientSize.Width - $closeButton.Width - 10
# Adjust button widths to fit panel width
$panelWidth = $panel.Width
$btnStart.Width = [Math]::Max(75, $panelWidth - 40)
$btnStop.Width = [Math]::Max(75, $panelWidth - 40)
$txtTime.Left = [Math]::Max(100, ($panelWidth - 80) / 2)
$label.Left = $txtTime.Left - 80
if ($script:totalSeconds -gt 0 -and $script:isRunning) {
# Use the same real-time calculation for consistency
$elapsedTime = ([DateTime]::Now - $script:startTime).TotalSeconds
$progress = $elapsedTime / $script:totalSeconds
Update-Display -progress $progress
} else {
Update-Display -progress 0
}
})
# Add all controls to form
$form.Controls.Add($pictureBox)
$form.Controls.Add($panel)
$form.Controls.Add($titleBar)
# Set the order so PictureBox is behind everything
$form.Controls.SetChildIndex($pictureBox, 0)
$form.Controls.SetChildIndex($panel, 1)
$form.Controls.SetChildIndex($titleBar, 2)
# Initial display
Update-Display -progress 0
# Handle form closing to clean up resources
$form.Add_FormClosing({
if ($timer -ne $null) {
$timer.Stop()
$timer.Dispose()
}
if ($hideTimer -ne $null) {
$hideTimer.Stop()
$hideTimer.Dispose()
}
# Clean up bitmap
if ($pictureBox.Image -ne $null) {
try {
$pictureBox.Image.Dispose()
} catch {
# Ignore disposal errors on close
}
}
})
# Show form
[System.Windows.Forms.Application]::Run($form)