-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwork-timer.ahk
338 lines (322 loc) · 10.7 KB
/
work-timer.ahk
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
;Process ini
IniRead, ColorAlert, work-timer.ini, Data, ColorAlert, 1
IniRead, OnColor, work-timer.ini, Data, OnColor, B0FFFF
IniRead, OffColor, work-timer.ini, Data, OffColor, F07070
IniRead, IdleTime, work-timer.ini, Data, Timeout, 10
IniRead, Program1, work-timer.ini, Data, Program1, %A_Space%
IniRead, Program2, work-timer.ini, Data, Program2, %A_Space%
IniRead, Program3, work-timer.ini, Data, Program3, %A_Space%
IniRead, Program4, work-timer.ini, Data, Program4, %A_Space%
IniRead, Program5, work-timer.ini, Data, Program5, %A_Space%
IniRead, Program6, work-timer.ini, Data, Program6, %A_Space%
IniRead, Program7, work-timer.ini, Data, Program7, %A_Space%
IniRead, Program8, work-timer.ini, Data, Program8, %A_Space%
IniRead, Program9, work-timer.ini, Data, Program9, %A_Space%
IniRead, MoreSlots, work-timer.ini, Data, MoreSlots, 0
IniRead, LastTime, work-timer.ini, Data, LastTime, 00:00:00
IniRead, xSaved, work-timer.ini, Data, xSaved, 0
IniRead, ySaved, work-timer.ini, Data, ySaved, 0
IniWrite, %ColorAlert%, work-timer.ini, Data, ColorAlert
IniWrite, %OnColor%, work-timer.ini, Data, OnColor
IniWrite, %OffColor%, work-timer.ini, Data, OffColor
IniWrite, %IdleTime%, work-timer.ini, Data, Timeout
IniWrite, %Program1%, work-timer.ini, Data, Program1
IniWrite, %Program2%, work-timer.ini, Data, Program2
IniWrite, %Program3%, work-timer.ini, Data, Program3
IniWrite, %Program4%, work-timer.ini, Data, Program4
IniWrite, %Program5%, work-timer.ini, Data, Program5
IniWrite, %Program6%, work-timer.ini, Data, Program6
IniWrite, %Program7%, work-timer.ini, Data, Program7
IniWrite, %Program8%, work-timer.ini, Data, Program8
IniWrite, %Program9%, work-timer.ini, Data, Program9
IniWrite, %MoreSlots%, work-timer.ini, Data, MoreSlots
IniWrite, %LastTime%, work-timer.ini, Data, LastTime
IniWrite, %xSaved%, work-timer.ini, Data, xSaved
IniWrite, %ySaved%, work-timer.ini, Data, ySaved
;Move window by dragging anywhere on the GUI
;When left mouse down (WM_LBUTTONDOWN) is detected, call MoveGUI function
OnMessage(0x201, "MoveGUI")
;Simulate dragging title bar (WM_NCLBUTTONDOWN) on active window
MoveGUI()
{
PostMessage, 0xA1, 2,,, A
}
;Create main GUI
;Script runs at maximum speed
SetBatchLines, -1
;Set GUI qualities
Gui -Caption +LastFound +AlwaysOnTop
;Set color
Gui, Color, %OffColor%
if(ColorAlert = 0)
Gui, Color, %OnColor%
;Set display font (20pt = 27px)
Gui, Font, S20 CDefault Bold, Courier New
;Create display with placeholder timer text
Gui, Add, Text, x8 y5 vDisplayText, 00:00:00
;Set floats to be two digits max, no decimals
SetFormat, Float, 02.0
;Reset timer
h := m := s := "00"
;Update every second
SetTimer, Update, 1000
;Update now
Gosub, Update
;Show GUI
Gui, Show, w145 h37, Work Timer
;End of startup process
Return
;Handles right click context menu
GuiContextMenu:
;Set color
Gui, Color, %OffColor%
if(ColorAlert = 0)
Gui, Color, %OnColor%
;Clear any previous context menu
Menu, contextMenu, Add
Menu, contextMenu, DeleteAll
;Create context menu
if(H = 99 && M = 59 && S = 59){
}
Menu, contextMenu, Add, Resume previous time, Prev
Menu, contextMenu, Add, Go to saved position, GoToPos
Menu, contextMenu, Add
programDisplay := processProgram(Program1, 30)
Menu, contextMenu, Add, Program 1`: %programDisplay%, SetProgram1
programDisplay := processProgram(Program2, 30)
Menu, contextMenu, Add, Program 2`: %programDisplay%, SetProgram2
programDisplay := processProgram(Program3, 30)
Menu, contextMenu, Add, Program 3`: %programDisplay%, SetProgram3
if(MoreSlots = 1){
programDisplay := processProgram(Program4, 30)
Menu, contextMenu, Add, Program 4`: %programDisplay%, SetProgram4
programDisplay := processProgram(Program5, 30)
Menu, contextMenu, Add, Program 5`: %programDisplay%, SetProgram5
programDisplay := processProgram(Program6, 30)
Menu, contextMenu, Add, Program 6`: %programDisplay%, SetProgram6
programDisplay := processProgram(Program7, 30)
Menu, contextMenu, Add, Program 7`: %programDisplay%, SetProgram7
programDisplay := processProgram(Program8, 30)
Menu, contextMenu, Add, Program 8`: %programDisplay%, SetProgram8
programDisplay := processProgram(Program9, 30)
Menu, contextMenu, Add, Program 9`: %programDisplay%, SetProgram9
}
Menu, contextMenu, Add
Menu, contextMenu, Add, Save current position, SavePos
Menu, contextMenu, Add, Timeout`: %IdleTime%, SetTimeout
Menu, contextMenu, Add
Menu, contextMenu, Add, Color Alert, ChangeColor
if(ColorAlert = 1)
Menu, contextMenu, Check, Color Alert
Menu, contextMenu, Add, More Program Slots, ChangeSlots
if(MoreSlots = 1)
Menu, contextMenu, Check, More Program Slots
Menu, contextMenu, Add
Menu, contextMenu, Add, Reset Timer, ResetTimer
Menu, contextMenu, Show
Return
;Function helps create menu items that display process paths
processProgram(program, len){
if(StrLen(program) >= len){
removeLen := StrLen(program) - len
StringTrimLeft, program, program, removeLen
Return "... " . program
}
if(program = "")
Return "(Not set)"
MsgBox, %program%
Return program
}
GoToPos:
WinMove, A, , xSaved, ySaved
Return
SavePos:
WinGetPos, xSaved, ySaved, wMain, hMain, A
IniWrite, %xSaved%, work-timer.ini, Data, xSaved
IniWrite, %ySaved%, work-timer.ini, Data, ySaved
Return
;Reset timer if maxed or otherwise
ResetTimer:
H := 00
M := 00
S := 00
GuiControl,, DisplayText, %H%:%M%:%S%
SetTimer, Update, On
Return
;Toggle color alert
ChangeColor:
if(ColorAlert = 1){
Menu, contextMenu, UnCheck, Color Alert
ColorAlert = 0
IniWrite, 0, work-timer.ini, Data, ColorAlert
Gui, Color, %OnColor%
}else{
Menu, contextMenu, Check, Color Alert
ColorAlert = 1
IniWrite, 1, work-timer.ini, Data, ColorAlert
Gui, Color, %OffColor%
}
Return
;Toggle more program slots
ChangeSlots:
if(MoreSlots = 1){
Menu, contextMenu, UnCheck, More Program Slots
MoreSlots := 0
IniWrite, 0, work-timer.ini, Data, MoreSlots
}else{
Menu, contextMenu, Check, More Program Slots
MoreSlots := 1
IniWrite, 1, work-timer.ini, Data, MoreSlots
}
Return
;Restore previous time
Prev:
StringSplit, LastTimeA, LastTime, `:
H := LastTimeA1
M := LastTimeA2
S := LastTimeA3
GuiControl,, DisplayText, %H%:%M%:%S%
Return
;Set allowed program
SetProgram1:
Gosub, WaitProgram
Program1 := activeProcess
IniWrite, %activeProcess%, work-timer.ini, Data, Program1
Return
SetProgram2:
Gosub, WaitProgram
Program2 := activeProcess
IniWrite, %activeProcess%, work-timer.ini, Data, Program2
Return
SetProgram3:
Gosub, WaitProgram
Program3 := activeProcess
IniWrite, %activeProcess%, work-timer.ini, Data, Program3
Return
SetProgram4:
Gosub, WaitProgram
Program4 := activeProcess
IniWrite, %activeProcess%, work-timer.ini, Data, Program4
Return
SetProgram5:
Gosub, WaitProgram
Program5 := activeProcess
IniWrite, %activeProcess%, work-timer.ini, Data, Program5
Return
SetProgram6:
Gosub, WaitProgram
Program6 := activeProcess
IniWrite, %activeProcess%, work-timer.ini, Data, Program6
Return
SetProgram7:
Gosub, WaitProgram
Program7 := activeProcess
IniWrite, %activeProcess%, work-timer.ini, Data, Program7
Return
SetProgram8:
Gosub, WaitProgram
Program8 := activeProcess
IniWrite, %activeProcess%, work-timer.ini, Data, Program8
Return
SetProgram9:
Gosub, WaitProgram
Program9 := activeProcess
IniWrite, %activeProcess%, work-timer.ini, Data, Program9
Return
;Wait for program to change
WaitProgram:
;Display waiting
GuiControl,, DisplayText, Waiting...
;Infinite loop until window focus leaves GUI
while (activeClass = "AutoHotkeyGUI"){
}
;Wait in case of window focus quickly switching
Sleep, 250
;Display timer
GuiControl,, DisplayText, %H%:%M%:%S%
Return
;Timeout input GUI handling
SetTimeout:
WinGetPos, xMain, yMain, wMain, hMain, A
Gui, Timeout: Destroy
Gui, Timeout: -Caption +LastFound +AlwaysOnTop
Gui, Timeout: Add, Edit, r1 limit3 vNewTimeout, [Enter] idle timeout in seconds
Gui, Timeout: Add, Button, x-10 y-10 w1 h1 Default gTimeoutSubmit
Gui, Timeout: Show
WinGetPos, , , wTime, hTime, A
WinMove, xMain + wMain / 2 - wTime / 2, yMain + hMain / 2 - hTime / 2
Return
TimeoutGuiClose:
TimeoutGuiEscape:
Gui, Timeout: Cancel
Return
TimeoutSubmit:
Gui, Timeout: Submit
if NewTimeout is integer
IdleTime := NewTimeout
if NewTimeout is float
IdleTime := Round(NewTimeout)
if(NewTimeout < 0)
IdleTime := 0
IniWrite, %IdleTime%, work-timer.ini, Data, Timeout
Return
;Handles constant updates
Update:
;Stop timer if maxed
if(H = 99 && M = 59 && S = 59){
SetTimer, Update, Off
}
;Get active window ID, class, and process path
activeID := WinExist("A")
WinGetClass, activeClass, ahk_id %activeID%
WinGet, activeProcess, ProcessPath, ahk_id %activeID%
;Ignore explorer.exe
if(activeProcess = "C:\Windows\explorer.exe"){
activeID := ""
activeClass := ""
activeProcess := ""
}
;Check if active program is non-blank and approved
ProgramActive = 1
if(activeProcess = ""){
ProgramActive := 0
}else{
if(activeProcess != Program1)
if(activeProcess != Program2)
if(activeProcess != Program3)
if(MoreSlots = 1){
if(activeProcess != Program4)
if(activeProcess != Program5)
if(activeProcess != Program6)
if(activeProcess != Program7)
if(activeProcess != Program8)
if(activeProcess != Program9)
ProgramActive := 0
}else{
ProgramActive := 0
}
}
;Handle idle or forbidden program
if((A_TimeIdle > IdleTime*1000) || (ProgramActive = 0)){
if(ColorAlert = 1)
Gui, Color, %OffColor%
Return
}
;Handle active and allowed program
if(ColorAlert = 1)
Gui, Color, %OnColor%
S += 1.0
if(S >= 60){
M += 1.0
S := 00
}
if(M >= 60){
H += 1.0
M := 00
}
GuiControl,, DisplayText, %H%:%M%:%S%
Return
GuiClose:
GuiEscape:
IniWrite, %H%:%M%:%S%, work-timer.ini, Data, LastTime
Exitapp