forked from Ixiko/AHK-libs-and-classes-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClass_Dock.ahk
335 lines (288 loc) · 7.18 KB
/
Class_Dock.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
/*
Class Dock
Attach a window to another
Author
Soft (visionary1 예지력)
version
0.1 (2017.04.20)
0.2 (2017.05.06)
0.2.1 (2017.05.07)
0.2.1.1 bug fixed (2017.05.09)
0.2.2 testing multiple docks... (2017.05.09)
License
WTFPL (http://wtfpl.net/)
Dev env
Windows 10 pro x64
AutoHotKey H v1.1.25.01 32bit
To Do...
Multiple Dock, group windows...
thanks to
Helgef for overall coding advices
*/
class Dock
{
static EVENT_OBJECT_LOCATIONCHANGE := 0x800B
, EVENT_OBJECT_FOCUS := 0x8005, EVENT_OBJECT_DESTROY := 0x8001
, EVENT_MIN := 0x00000001, EVENT_MAX := 0x7FFFFFFF ;for debug
, EVENT_SYSTEM_FOREGROUND := 0x0003
/*
Instance := new Dock(Host hwnd, Client hwnd, [Callback], [CloseCallback])
Host hwnd
hwnd of a Host window
Client hwnd
hwnd of a window that follows Host window (window that'll be attached to a Host window)
[Callback]
a func object, or a bound func object
if omitted, default EventsHandler will be used, which is hard-coded in 'Dock.EventsHandler'
To construct your own events handler, I advise you to see Dock.EventsHandler first
[CloseCallback]
a func object, or a bound func object
called when Host window is destroyed, see 'Dock Example.ahk' for practical usuage
*/
__New(Host, Client, Callback := "", CloseCallback := "")
{
this.hwnd := []
this.hwnd.Host := Host
this.hwnd.Client := Client
WinSet, ExStyle, +0x80, % "ahk_id " this.hwnd.Client
this.Bound := []
this.Callback := IsObject(Callback) ? Callback : ObjBindMethod(Dock.EventsHandler, "Calls")
this.CloseCallback := IsFunc(CloseCallback) || IsObject(CloseCallback) ? CloseCallback
/*
lpfnWinEventProc
*/
this.hookProcAdr := RegisterCallback("_DockHookProcAdr",,, &this)
/*
idProcess
*/
;WinGet, idProcess, PID, % "ahk_id " . this.hwnd.Host
idProcess := 0
/*
idThread
*/
;idThread := DllCall("GetWindowThreadProcessId", "Ptr", this.hwnd.Host, "Int", 0)
idThread := 0
DllCall("CoInitialize", "Int", 0)
this.Hook := DllCall("SetWinEventHook"
, "UInt", Dock.EVENT_SYSTEM_FOREGROUND ;eventMin
, "UInt", Dock.EVENT_OBJECT_LOCATIONCHANGE ;eventMax
, "Ptr", 0 ;hmodWinEventProc
, "Ptr", this.hookProcAdr ;lpfnWinEventProc
, "UInt", idProcess ;idProcess
, "UInt", idThread ;idThread
, "UInt", 0) ;dwFlags
}
/*
Instance.Unhook()
unhooks Dock and frees memory
*/
Unhook()
{
DllCall("UnhookWinEvent", "Ptr", this.Hook)
DllCall("CoUninitialize")
DllCall("GlobalFree", "Ptr", this.hookProcAdr)
this.Hook := ""
this.hookProcAdr := ""
this.Callback := ""
WinSet, ExStyle, -0x80, % "ahk_id " this.hwnd.Client
}
__Delete()
{
this.Delete("Bound")
If (this.Hook)
this.Unhook()
this.CloseCallback := ""
}
/*
provisional
*/
Add(hwnd, pos := "")
{
static last_hwnd := 0
this.Bound.Push( new this( !NumGet(&this.Bound, 4*A_PtrSize) ? this.hwnd.Client : last_hwnd, hwnd ) )
If pos Contains Top,Bottom,R,Right,L,Left
this.Bound[NumGet(&this.Bound, 4*A_PtrSize)].Position(pos)
last_hwnd := hwnd
}
/*
Instance.Position(pos)
pos - sets position to dock client window
Top - sets to Top side of the host window
Bottom - sets to bottom side of the host window
R or Right - right side
L or Left - left side
*/
Position(pos)
{
this.pos := pos
Return this.EventsHandler.EVENT_OBJECT_LOCATIONCHANGE(this, "host")
}
/*
Default EventsHandler
*/
class EventsHandler extends Dock.HelperFunc
{
Calls(self, hWinEventHook, event, hwnd)
{
Critical
If (hwnd = self.hwnd.Host)
{
Return this.Host(self, event)
}
If (hwnd = self.hwnd.Client)
{
Return this.Client(self, event)
}
}
Host(self, event)
{
If (event = Dock.EVENT_SYSTEM_FOREGROUND)
{
Return this.EVENT_SYSTEM_FOREGROUND(self.hwnd.Client)
}
If (event = Dock.EVENT_OBJECT_LOCATIONCHANGE)
{
Return this.EVENT_OBJECT_LOCATIONCHANGE(self, "host")
}
If (event = Dock.EVENT_OBJECT_DESTROY)
{
self.Unhook()
If (IsFunc(self.CloseCallback) || IsObject(self.CloseCallback))
Return self.CloseCallback()
}
}
Client(self, event)
{
If (event = Dock.EVENT_SYSTEM_FOREGROUND)
{
Return this.EVENT_SYSTEM_FOREGROUND(self.hwnd.Host)
}
If (event = Dock.EVENT_OBJECT_LOCATIONCHANGE)
{
Return this.EVENT_OBJECT_LOCATIONCHANGE(self, "client")
}
}
/*
Called when host window got focus
without this, client window can't be showed (can't set to top)
*/
EVENT_SYSTEM_FOREGROUND(hwnd)
{
Return this.WinSetTop(hwnd)
}
/*
Called when host window is moved
*/
EVENT_OBJECT_LOCATIONCHANGE(self, via)
{
Host := this.WinGetPos(self.hwnd.Host)
Client := this.WinGetPos(self.hwnd.Client)
If InStr(self.pos, "Top")
{
If (via = "host")
{
Return this.MoveWindow(self.hwnd.Client ;hwnd
, Host.x ;x
, Host.y - Client.h ;y
, Client.w ;width
, Client.h) ;height
}
If (via = "client")
{
Return this.MoveWindow(self.hwnd.Host ;hwnd
, Client.x ;x
, Client.y + Client.h ;y
, Host.w ;width
, Host.h) ;height
}
}
If InStr(self.pos, "Bottom")
{
If (via = "host")
{
Return this.MoveWindow(self.hwnd.Client ;hwnd
, Host.x ;x
, Host.y + Host.h ;y
, Client.w ;width
, Client.h) ;height
}
If (via = "client")
{
Return this.MoveWindow(self.hwnd.Host ;hwnd
, Client.x ;x
, Client.y - Host.h ;y
, Host.w ;width
, Host.h) ;height
}
}
If InStr(self.pos, "R")
{
If (via = "host")
{
Return this.MoveWindow(self.hwnd.Client ;hwnd
, Host.x + Host.w ;x
, Host.y ;y
, Client.w ;width
, Client.h) ;height
}
If (via = "client")
{
Return this.MoveWindow(self.hwnd.Host ;hwnd
, Client.x - Host.w ;x
, Client.y ;y
, Host.w ;width
, Host.h) ;height
}
}
If InStr(self.pos, "L")
{
If (via = "host")
{
Return this.MoveWindow(self.hwnd.Client ;hwnd
, Host.x - Client.w ;x
, Host.y ;y
, Client.w ;width
, Client.h) ;height
}
If (via = "client")
{
Return this.MoveWindow(self.hwnd.Host ;hwnd
, Client.x + Client.w ;x
, Client.y ;y
, Host.w ;width
, Host.h) ;height
}
}
}
}
class HelperFunc
{
WinGetPos(hwnd)
{
WinGetPos, hX, hY, hW, hH, % "ahk_id " . hwnd
Return {x: hX, y: hY, w: hW, h: hH}
}
WinSetTop(hwnd)
{
WinSet, AlwaysOnTop, On, % "ahk_id " . hwnd
WinSet, AlwaysOnTop, Off, % "ahk_id " . hwnd
}
MoveWindow(hwnd, x, y, w, h)
{
Return DllCall("MoveWindow", "Ptr", hwnd, "Int", x, "Int", y, "Int", w, "Int", h, "Int", 1)
}
Run(Target)
{
Try Run, % Target,,, OutputVarPID
Catch,
Throw, "Couldn't run " Target
WinWait, % "ahk_pid " OutputVarPID
Return WinExist("ahk_pid " OutputVarPID)
}
}
}
_DockHookProcAdr(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
{
this := Object(A_EventInfo)
this.Callback.Call(this, hWinEventHook, event, hwnd)
}