-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathModules.ahk
124 lines (111 loc) · 3.53 KB
/
Modules.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
class Modules {
add(mod, timer:="", args*) {
; timer=0 prevents initialization and
; negative timer causes firstRun even if not defined in the class. (This is only recomended for functions)
; mod can be function name (but not a func object), but the functionality will be severely limited
; When a function name is given:
; the function will be run when initialized if timer isnt given and
; the function is used in setTimer if timer is non-zero
if !this.list
this.list:={}, this.orderedList:=[]
if isObject(mod) { ; Assume it is a class
this.orderedList.push(mod.__class)
this.list[mod.__class]:={name:mod.__class, class:mod, timer:timer, args:args}
} else { ; Assume it is a function NAME
this.orderedList.push(mod)
this.list[mod]:={name:mod, func:Func(mod).bind(args*), timer:timer, args:args}
; To check b/w class and fn, check "if (this.list[name].func)"
}
;msgbox % (isObject(mod)? mod.__class :"""" mod """") "`n" timer
return isObject(mod)? mod.__class :mod ; Return the name
}
initialize(opts:="") {
this.opts:= opts? opts :{}
for _,name in this.orderedList {
tooltip(name, {life:500})
this._initFn(name)
this._setTimer(name)
}
}
startTimers() {
tooltip("Starting Timers", {life:500})
delayedTimer.start(False)
fs:=ObjBindMethod(this,"_onFullScreen")
setTimer, % fs, 1000
}
firstRun() {
tooltip()
delayedTimer.firstRun()
while delayedTimer.running
sleep 100
Toast.show("Script Loaded")
}
_initFn(name){
mod:=this.list[name]
if mod.timer!=0 {
if mod.func {
if !mod.timer ; If timer doesnt exist, initialize with the fn
mod.func.call()
} else {
if mod.class.__module_init
mod.class.__module_init(mod.args*)
else
mod.class.__new(mod.args*)
this.list[name].obj:= mod.class.__module? mod.class.__module :{}
}
}
}
_setTimer(name){
mod:=this.list[name]
;msgbox % name "`n" !!mod.obj.timerFn
if mod.timer {
if mod.func ; If timer is non-zero, set timer for the function
delayedTimer.set(mod.func, abs(mod.timer), mod.timer<0)
else if mod.obj.timerFn
delayedTimer.set(mod.obj.timerFn, abs(mod.timer), mod.timer<0 || mod.obj.runTimerAtStart)
this.list[name].timerRunning:=True
}
}
_resumeTimer(name){
mod:=this.list[name]
obj:= mod.func? mod.func : mod.obj.timerFn
if mod.timer && obj {
SetTimer, % obj, On
this.list[name].timerRunning:=True
}
}
_stopTimer(name){
mod:=this.list[name]
obj:= mod.func? mod.func : mod.obj.timerFn
if mod.timer && obj {
SetTimer, % obj, Off
this.list[name].timerRunning:=False
}
}
_onFullScreen(){
static lastFS:=false, lastWin
fs:= isFullScreen(,1) && !isActiveWinInList(this.opts.fullScreenExceptions), win:=WinExist("A")
if ( fs==lastFS && (!fs || win==lastWin) ) ; Reduce the number of times the loop below needs to run
return
else {
lastFS:=fs
lastWin:=win
}
for name, mod in this.list { ; Order doesn't matter
if !mod.func { ; Dont do anything for functions
if fs&&mod._fs || !fs&&!mod._fs ; Already triggered
continue
if fs && isActiveWinInList(mod.obj.fullScreenExceptions)
continue
this.list[name]._fs:=fs ; mark that the mod is in FS/Win mode
;msgbox % name "`n" mod.class._module_onFullScreen "`n" mod._module_suspendOnFullScreen
if fs && mod.obj.onFullScreen
mod.obj.onFullScreen()
else if !fs && mod.obj.onWindowed
mod.obj.onWindowed()
if !mod.obj.suspendOnFullScreen ; Suspend needs to be explicitly defined
ObjBindMethod(this, fs? "_stopTimer" : "_resumeTimer", name).call()
}
}
}
}