-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClockBar.ahk
652 lines (495 loc) · 14.9 KB
/
ClockBar.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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
AUTOEXEC_ClockBar: ; Workaround for Autohotkey's ugly auto-exec feature. Must be first line.
/* API:
ClockBar_TurnOn()
ClockBar_TurnOff()
ClockBar_IsTurnedOn()
ClockBar_ResetHere()
*/
global g_ClockBar ; the single instance of ClockBar
global g_hwndClockBar
global gu_ClockText
global g_menutext_clockbar_TurnOn := "Turn on ClockBar"
global g_menutext_clockbar_TurnOff := "Turn off ClockBar"
global g_menutext_clockbar_ResetHere := "Reset my ClockBar here"
ClockBar_Init()
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
return ; End of auto-execute section.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
class ClockBar
{
static Id := "ClockBar"
static _tmp_ := AmDbg_SetDesc(ClockBar.Id, "Debug messages from ClockBar module.")
static TimerIntervalMs := 1000 ; 1 second
static EXBOUND := 3 ; const adjustable
static OFF1 := 1
isGuiVisible := false
timerobj := ""
dragpoint_offx := 0
dragpoint_offy := 0
is_dragging := false
followingHwnd := "" ; ClockBar is following which HWND?
tempsave_hwnd := ""
snap_corner := "" ; LT, RT, LB, RB
offx_to_corner := 0
offy_to_corner := 0
pos_cache := {} ; as returned by dev_WinGetPos_byHwnd()
ctxmenu := "ClockBar.CtxMenu" ; its only menuname
menutarget := "" ; set in __New()
dbg(msg, lv) {
AmDbg_output(ClockBar.Id, Format("[{}] ", ClockBar.Id) msg, lv)
}
dbg0(msg) {
ClockBar.dbg(msg, 0)
}
dbg1(msg) {
ClockBar.dbg(msg, 1)
}
dbg2(msg) {
ClockBar.dbg(msg, 2)
}
ethrow(msg) {
ClockBar.dbg1(msg)
throw Exception(msg, -1)
}
__New()
{
this.timerobj := Func("ClockBar.TimerUpdateClock").Bind(this)
this.menutarget := Func("ClockBar.MenuExec_ToggleFollow").Bind(this)
dev_MenuAddItem(this.ctxmenu, "(menu text to change)", this.menutarget) ; 1&
dev_MenuAddItem(this.ctxmenu, g_menutext_clockbar_TurnOff, "ClockBar_TurnOff") ; 2&
this.CreateGui()
}
CreateGui()
{
GuiName := ClockBar.Id
Gui_New(GuiName)
Gui_AssociateHwndVarname(GuiName, "g_hwndClockBar")
Gui_ChangeOpt(GuiName, "-Caption +ToolWindow")
Gui_SetXYMargin(GuiName, 1, 3)
Gui_Switch_Font(GuiName, 16, "0x606060", "Arial")
Gui_WindowColor(GuiName, "FEFECC")
Gui_Add_TxtLabel(GuiName, "gu_ClockText", 0, "Center", Format("_{}_", this.NowTimeStr()))
; -- Make extra two spaces(_), before & after time-string, bcz sometimes when I turn-off then turn-on ClockBar,
; the TxtLabel Uic weirdly shrinks its width(200px ¡ú 198px), resulting HHMMSS substring to disappear from sight.
; So, the two spaces makes extra room for that accidental shrink.
Gui_As_LastFoundWindow(GuiName)
WinSet_Transparent(128)
WinSet_AlwaysOnTop(true)
dev_OnMessageRegister(win32c.WM_LBUTTONDOWN, Func("Clockbar_WM_LBUTTONDOWN"))
dev_OnMessageRegister(win32c.WM_MOUSEMOVE, "Clockbar_WM_MOUSEMOVE")
dev_OnMessageRegister(win32c.WM_LBUTTONUP, "Clockbar_WM_LBUTTONUP")
}
ShowGui()
{
if(this.isGuiVisible)
{
dev_WinActivateHwnd(g_hwndClockBar) ; bring it to front
return ; already shown
}
this.dbg1("ShowGui(). Start timer.")
dev_StartTimerPeriodicEx(ClockBar.TimerIntervalMs, true, this.timerobj)
; Note: using NoActivate, bcz we do not intend to get user input from the ClockBar.
Gui_Show(ClockBar.Id, "AutoSize NoActivate", ClockBar.Id " title")
this.isGuiVisible := true
}
HideGui()
{
this.dbg1("HideGui(). Stop timer.")
dev_StopTimer(this.timerobj)
Gui_Hide(ClockBar.Id)
this.SetClockText("(ClockBar hidden)")
this.isGuiVisible := false
}
NowTimeStr()
{
FormatTime, outvar, , % "yyyy-MM-dd HH:mm:ss"
return outvar
}
SetClockText(text)
{
GuiControl_SetText(ClockBar.Id, "gu_ClockText", text)
}
TimerUpdateClock()
{
this.SetClockText(this.NowTimeStr())
if(this.is_dragging)
return
this.DoFollowTarget()
}
DoFollowTarget()
{
Critical On
; -- Important! This is to avoid interrupted by Systray command.
; Otherwise, this.followingHwnd may be changed(reset to null) midway
; by user's executing ClockBar_ResetHere() or g_ClockBar.TurnOff_Follow().
mehwnd := g_hwndClockBar
hehwnd := this.followingHwnd
mepos := dev_WinGetPos_byHwnd(mehwnd)
hepos := dev_WinGetPos_byHwnd(hehwnd)
dev_assert(mehwnd)
dev_assert(mepos)
if(not mepos.hidden)
{
; Make it Always-on-Top again, so that ClockBar can again be shown above other top-most window.
WinSet_AlwaysOnTop(true, "ahk_id " mehwnd)
}
;
if(hehwnd)
{
if(not hepos)
{
; Target window may have been destroyed out-of-band, so no need to follow any more.
this.dbg1(Format("Target HWND(0x{:X}) has vanished. Turn-off following.", hehwnd))
this.TurnOff_Follow()
return
}
; Determine whether we should:
; [1] Hide or show ClockBar according to whether the target-hwnd is hidden.
; [2] Adjust ClockBar's position to follow the target.
; [3] Hide or show ClockBar according to whether the ClockBar is fully "covered" by non-target window.
; Phase [1]
if(hepos.minimized)
{
this.dbg2(Format("HE is minimized. X[{}~{}] Y[{}~{}]", hepos.x, hepos.x_, hepos.y, hepos.y_))
}
if(hepos.hidden)
{
this.dbg2(Format("HE is hidden. X[{}~{}] Y[{}~{}]", hepos.x, hepos.x_, hepos.y, hepos.y_))
}
if(hepos.minimized or hepos.hidden)
{
dev_WinHide_byHwnd(g_hwndClockBar)
return
}
if(mepos.hidden)
{
; AmDbg0(Format("??? mepos: X[{}~{}] Y[{}~{}] ", mepos.x, mepos.x_, mepos.y, mepos.y_))
; mepos.x, mepos.y is probably empty, so load from cache
mepos := this.pos_cache
}
else
{
this.pos_cache := mepos.clone()
}
dev_assert(mepos.x || mepos.y || mepos.x_ || mepos.y_)
; Phase [2]
this.dbg2(Format("ME x[{}~{}] y[{}~{}] following 0x{:X}, X[{}~{}] Y[{}~{}]"
, mepos.x, mepos.x_, mepos.y, mepos.y_
, hehwnd
, hepos.x, hepos.x_, hepos.y, hepos.y_))
newpos := this.AdjustMyRect(mepos.w, mepos.h, hepos)
newpos.x_ := newpos.x + mepos.w
newpos.y_ := newpos.y + mepos.h
if(newpos.x==mepos.x and newpos.y==mepos.y)
{
this.dbg2("No move needed.")
}
else
{
this.dbg1(Format("Move ME from ({},{}) to ({},{})", mepos.x, mepos.y, newpos.x, newpos.y))
dev_WinMoveHwnd(mehwnd, newpos.x, newpos.y)
}
; Phase [3]
if(this.IsWholyCoveredbyOthers(newpos))
dev_WinHide_byHwnd(mehwnd)
else
{
dev_WinShow_byHwnd(mehwnd)
; Move again, bcz previous WinMove may not have taken effect when mehwnd was hidden.
dev_WinMoveHwnd(mehwnd, newpos.x, newpos.y)
}
}
}
AdjustMyRect(me_width, me_height, hepos)
{
mepos := {}
; Step 1: Adjust according to .snap_corner .
dev_assert(strlen(this.snap_corner)==2)
if(this.snap_corner=="LT") {
mepos.x := hepos.x + this.offx_to_corner
mepos.y := hepos.y + this.offy_to_corner
}
else if(this.snap_corner=="RT") {
mepos.x := hepos.x_ + this.offx_to_corner
mepos.y := hepos.y + this.offy_to_corner
}
else if(this.snap_corner=="LB") {
mepos.x := hepos.x + this.offx_to_corner
mepos.y := hepos.y_ + this.offy_to_corner
}
else if(this.snap_corner=="RB") {
mepos.x := hepos.x_ + this.offx_to_corner
mepos.y := hepos.y_ + this.offy_to_corner
}
; Step 2: Check if mepos is within the rect-area of hepos.
; If not, move mepos to reside in hepos.
movex := ClockBar.Adjust1D(mepos.x, mepos.x+me_width, hepos.x, hepos.x_)
movey := ClockBar.Adjust1D(mepos.y, mepos.y+me_height, hepos.y, hepos.y_)
mepos.x += movex
mepos.y += movey
return mepos
}
Adjust1D(me1, me2, he1, he2) ;static
{
; Try to make [me1,me2] fall into [he1,he2], with EXBOUND constraint
eb := ClockBar.EXBOUND
move2 := (me2+eb)<=he2 ? 0 : he2-(me2+eb)
dev_assert(move2<=0)
newme1 := me1 + move2
move1 := newme1>=(he1+eb) ? 0 : (he1+eb)-newme1
dev_assert(move1>=0)
return move2 + move1
}
GuiContextMenu(GuiHwnd, CtlHwnd, EventInfo, IsRightClick, X, Y)
{
this.dbg1(Format("{}: GuiHwnd=0x{:X} , CtlHwnd=0x{:X} , IsRightClick={}"
, this.ctxmenu, GuiHwnd, CtlHwnd, IsRightClick))
this.dbg1("EventInfo: " EventInfo) ; seems always 0
GuiName := ClockBar.Id
if(not this.followingHwnd)
{
; We are not following any HWND yet.
; Detect the HWND under mouse and suggest user to follow it.
dev_WinHide_byHwnd(g_hwndClockBar)
dict := dev_GetHwndUnderMouse()
dev_WinShow_byHwnd(g_hwndClockBar)
short_title := ClockBar.GetShortWinTitle(dict.hwndtop)
this.tempsave_hwnd := dict.hwndtop
menutext := Format("Click to follow HWND 0x{:X} ({})", dict.hwndtop, short_title)
dev_MenuRenameItem(this.ctxmenu, "1&", menutext)
dev_MenuTickItem(this.ctxmenu, "1&", false)
}
else
{
; GuiControl_SetFont(GuiName, "gu_ClockText", "", "Underline")
short_title := ClockBar.GetShortWinTitle(this.followingHwnd)
menutext := Format("Following HWND 0x{:X} ({})", this.followingHwnd, short_title)
dev_MenuRenameItem(this.ctxmenu, "1&", menutext)
dev_MenuTickItem(this.ctxmenu, "1&", true)
}
dev_MenuShow(this.ctxmenu) ; AHK-thread blocks inside
this.tempsave_hwnd := "" ; must be AFTER dev_MenuShow()
}
GetShortWinTitle(hwnd) ; static
{
wintitle := dev_WinGetTitle_byHwnd(hwnd)
short_title := SubStr(wintitle, 1, 20)
if(wintitle != short_title)
short_title .= "..."
return short_title
}
MenuExec_ToggleFollow()
{
GuiName := ClockBar.Id
; AmDbg0("ToggleFollow()..... clicked")
if(not this.followingHwnd)
{
this.followingHwnd := this.tempsave_hwnd
this.PrepareSnapCornerInfo()
GuiControl_SetFont(GuiName, "gu_ClockText", "", "Underline")
this.DoFollowTarget()
}
else
{
this.TurnOff_Follow()
}
}
TurnOff_Follow(extra_hint:="")
{
GuiName := ClockBar.Id
this.followingHwnd := ""
GuiControl_SetFont(GuiName, "gu_ClockText", "", "Normal")
dev_WinShow_byHwnd(g_hwndClockBar)
if(extra_hint)
{
dev_TooltipAutoClear(extra_hint)
}
}
WM_LBUTTONDOWN()
{
CoordMode, Mouse, Screen
MouseGetPos, mxScreen, myScreen
CoordMode, Mouse, Window
pos := dev_WinGetPos_byHwnd(g_hwndClockBar)
this.dragpoint_offx := mxScreen - pos.x
this.dragpoint_offy := myScreen - pos.y
this.dbg1(Format("SetCapture. Drag-point offset to self-hwnd: ({},{})", this.dragpoint_offx, this.dragpoint_offy))
DllCall("SetCapture", "Ptr", g_hwndClockBar)
this.is_dragging := true
}
WM_MOUSEMOVE()
{
if(not this.is_dragging)
return
CoordMode, Mouse, Screen
MouseGetPos, mxScreen, myScreen
CoordMode, Mouse, Window
newx := mxScreen - this.dragpoint_offx
newy := myScreen - this.dragpoint_offy
this.dbg2(Format("Dragging... new window position: ({},{})", newx, newy))
dev_WinMoveHwnd(g_hwndClockBar, newx, newy)
}
WM_LBUTTONUP()
{
this.dbg1("ReleaseCapture.")
DllCall("ReleaseCapture")
this.is_dragging := false
this.dragpoint_offx := 0
this.dragpoint_offy := 0
if(not this.followingHwnd)
return
this.PrepareSnapCornerInfo()
}
MyDistanceTo(targx, targy)
{
mepos := dev_WinGetPos_byHwnd(g_hwndClockBar)
; just take ClockBar's center point as my position
mex := (mepos.x + mepos.x_)/2
mey := (mepos.y + mepos.y_)/2
distance := Sqrt( (mex-targx)*(mex-targx) + (mey-targy)*(mey-targy) )
return distance
}
PrepareSnapCornerInfo()
{
dev_assert(this.followingHwnd)
; Now check which corner of followingHwnd is most adjacent to the ClockBar.
; We will have the ClockBar snap to that corner.
me := dev_WinGetPos_byHwnd(g_hwndClockBar)
tg := dev_WinGetPos_byHwnd(this.followingHwnd)
toLT := this.MyDistanceTo(tg.x , tg.y) ; LT: left-top
toRT := this.MyDistanceTo(tg.x_, tg.y)
toLB := this.MyDistanceTo(tg.x , tg.y_)
toRB := this.MyDistanceTo(tg.x_, tg.y_)
min_dist := Min(toLT, toRT, toLB, toRB)
if(min_dist==toLT) {
this.snap_corner := "LT"
this.offx_to_corner := me.x - tg.x
this.offy_to_corner := me.y - tg.y
}
else if(min_dist==toRT) {
this.snap_corner := "RT"
this.offx_to_corner := me.x - tg.x_ ; rela to target's right border
this.offy_to_corner := me.y - tg.y
}
else if(min_dist==toLB) {
this.snap_corner := "LB"
this.offx_to_corner := me.x - tg.x
this.offy_to_corner := me.y - tg.y_
}
else if(min_dist==toRB) {
this.snap_corner := "RB"
this.offx_to_corner := me.x - tg.x_
this.offy_to_corner := me.y - tg.y_
}
}
IsWholyCoveredbyOthers(mepos)
{
; Check four corner(outbounded by OFF1) pixels of ClockBar.
; If they all "belong" to other top-level window, I consider it wholy covered.
; We need to use OFF1, bcz checking OFF0 always get g_hwndClockBar itself.
hwndLT := dev_GetTopHwndAtScreenXY(mepos.x-1, mepos.y-1)
hwndRT := dev_GetTopHwndAtScreenXY(mepos.x_+1, mepos.y-1)
hwndLB := dev_GetTopHwndAtScreenXY(mepos.x-1, mepos.y_+1)
hwndrB := dev_GetTopHwndAtScreenXY(mepos.x_+1, mepos.y_+1)
dev_assert(this.followingHwnd)
tgh := this.followingHwnd
if(hwndLT!=tgh and hwndRT!=tgh and hwndLB!=tgh and hwndRB!=tgh)
{
this.dbg1("ClockBar wholy covered by no-target-window, so hide it now.")
return true
}
else
return false
}
}
ClockbarGuiClose:
ClockbarGuiEscape:
return ; Do not allow closing ClockBar the normal way.
Clockbar_WM_LBUTTONDOWN()
{
if( A_Gui != ClockBar.Id )
return
g_ClockBar.WM_LBUTTONDOWN()
}
Clockbar_WM_MOUSEMOVE()
{
if( A_Gui != ClockBar.Id )
return
g_ClockBar.WM_MOUSEMOVE()
}
Clockbar_WM_LBUTTONUP()
{
if( A_Gui != ClockBar.Id )
return
g_ClockBar.WM_LBUTTONUP()
}
Clockbar_WM_NCLBUTTONUP() ; xxx
{
if( A_Gui != ClockBar.Id )
return
AmDbg0("NC mouse up....")
}
ClockBar_TurnOn()
{
if(not g_ClockBar)
g_ClockBar := new ClockBar()
dev_MenuTickItem("TRAY", g_menutext_clockbar_TurnOn, true)
g_ClockBar.ShowGui()
}
ClockBar_TurnOff()
{
dev_MenuTickItem("TRAY", g_menutext_clockbar_TurnOn, false)
g_ClockBar.HideGui()
}
ClockBar_IsTurnedOn()
{
if(!g_ClockBar)
return false
return g_ClockBar.isGuiVisible
}
ClockBarGuiContextMenu(args*)
{
; XXXGuiContextMenu() intrinsic function.
; This is executed automatically when user right clicks on the clockbar.
g_ClockBar.GuiContextMenu(args*)
}
ClockBar_Init()
{
dev_MenuAddItem("TRAY", g_menutext_clockbar_TurnOn, "ClockBar_Systray_TurnOnOff")
dev_MenuAddItem("TRAY", g_menutext_clockbar_ResetHere, "ClockBar_ResetHere")
}
ClockBar_Systray_TurnOnOff()
{
if(not ClockBar_IsTurnedOn())
{
ClockBar_TurnOn()
}
else
{
ClockBar_TurnOff()
}
}
ClockBar_ResetHere()
{
if(not ClockBar_IsTurnedOn())
{
ClockBar_TurnOn()
}
g_ClockBar.TurnOff_Follow()
mpos := dev_GetMouseScreenXY()
bpos := dev_WinGetPos_byHwnd(g_hwndClockBar)
; Make ClockBar-center above mouse pointer
newx := mpos.x - bpos.w/2
newy := mpos.y - bpos.h - 5
dev_WinMoveHwnd(g_hwndClockBar, newx, newy)
}
debug_ClockBar_StopTimer()
{
dev_TooltipAutoClear("debug_ClockBar_StopTimer()")
dev_StopTimer(g_ClockBar.timerobj)
}
debug_ClockBar_ManualTimerOnce()
{
dev_TooltipAutoClear("debug_ClockBar_ManualTimerOnce()")
g_ClockBar.TimerUpdateClock()
}