-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHyperWindowSnap.ahk
276 lines (244 loc) · 9.43 KB
/
HyperWindowSnap.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
/**
* Hyper Window Snap
* Hyper Window Snap is an AutoHotkey script for moving and resizing windows to into quadrants, especially useful for 4k monitors.
* Based on Advanced Window Snap by Andrew Moore.
*
* @author Andrew Moore <andrew+github@awmoore.com>
* @author Jeff Axelrod <jeff+github@theaxelrods.com>
* @version 1.0
*/
/**
* SnapActiveWindow resizes and moves (snaps) the active window to a given position.
* @param {string} winPlaceVertical The vertical placement of the active window.
* Expecting "bottom" or "middle", otherwise assumes
* "top" placement.
* @param {string} winPlaceHorizontal The horizontal placement of the active window.
* Expecting "left" or "right", otherwise assumes
* window should span the "full" width of the monitor.
* @param {string} winSizeHeight The height of the active window in relation to
* the active monitor's height. Expecting "half" size,
* otherwise will resize window to a "third".
*/
SplitSnapActiveWindow(winPlaceVertical, winPlaceHorizontal, winSizeHeight) {
oldClipboard = clipboardAll
clipboard =
while(clipboard == "") {
SendInput ^l^c
}
SendInput ^w^n
WinWaitNotActive
SendInput %clipboard%{enter}
clipboard := %oldClipboard%
SnapActiveWindow(winPlaceVertical, winPlaceHorizontal, winSizeHeight)
}
SnapActiveWindow(winPlaceVertical, winPlaceHorizontal, winSizeHeight) {
heightOffset := 7
widthOffset := 15
xOffset := 7
activeWin := WinExist("A")
activeMon := GetMonitorIndexFromWindow(activeWin)
WinGet, MinMaxState, MinMax, A
If (MinMaxState) {
WinRestore, A
}
SysGet, MonitorWorkArea, MonitorWorkArea, %activeMon%
if (winSizeHeight == "half") {
height := (MonitorWorkAreaBottom - MonitorWorkAreaTop)/2 + heightOffset
} else if (winSizeHeight == "full") {
height := (MonitorWorkAreaBottom - MonitorWorkAreaTop) + heightOffset
} else if (winSizeHeight == "third") {
height := (MonitorWorkAreaBottom - MonitorWorkAreaTop)/3
}
if (winPlaceHorizontal == "left") {
posX := MonitorWorkAreaLeft - xOffset
width := (MonitorWorkAreaRight - MonitorWorkAreaLeft)/2 + widthOffset
} else if (winPlaceHorizontal == "right") {
posX := MonitorWorkAreaLeft + (MonitorWorkAreaRight - MonitorWorkAreaLeft)/2 - xOffset
width := (MonitorWorkAreaRight - MonitorWorkAreaLeft)/2 + widthOffset
} else {
posX := MonitorWorkAreaLeft - xOffset
width := MonitorWorkAreaRight - MonitorWorkAreaLeft + widthOffset
}
if (winPlaceVertical == "bottom") {
posY := MonitorWorkAreaBottom - height + heightOffset
} else if (winPlaceVertical == "middle") {
posY := MonitorWorkAreaTop + height
} else {
posY := MonitorWorkAreaTop
}
WinMove,A,,%posX%,%posY%,%width%,%height%
}
shrinkActiveWindow(command) {
WinGet activeWin, ID, A
activeMon := GetMonitorIndexFromWindow(activeWin)
SysGet, MonitorWorkArea, MonitorWorkArea, %activeMon%
WinGetPos, posX, posY, width, height, A
if (command == "halfbottom") {
height := height/2
posY := posY + height
}
if (command == "halftop") {
height := height/2
}
if (command == "halfright") {
width := width/2
posX := posX + width
}
if (command == "halfleft") {
width := width/2
}
if (command == "halftopleft") {
height := height/2
width := width/2
}
if (command == "halftopright") {
height := height/2
width /= 2
posX := posX + width
}
WinMove,A,,%posX%,%posY%,%width%,%height%
}
max(x,y)
{
return x > y ? x : y
}
min(x,y)
{
return x < y ? x : y
}
activateWindow(num) {
WinGet activeWin, ID, A
activeMon := GetMonitorIndexFromWindow(activeWin)
SysGet, MonitorWorkArea, MonitorWorkArea, %activeMon%
CoordMode, Mouse, Screen
Switch num {
Case 7:
MouseMove MonitorWorkAreaRight / 4, MonitorWorkAreaBottom / 4, 0
Case 8:
MouseMove MonitorWorkAreaRight / 2, MonitorWorkAreaBottom / 4, 0
Case 9:
MouseMove 3 * MonitorWorkAreaRight / 4, MonitorWorkAreaBottom / 4, 0
Case 4:
MouseMove MonitorWorkAreaRight / 4, MonitorWorkAreaBottom / 2, 0
Case 6:
MouseMove 3 * MonitorWorkAreaRight / 4, MonitorWorkAreaBottom / 2, 0
Case 1:
MouseMove MonitorWorkAreaRight / 4, 3 * MonitorWorkAreaBottom / 4, 0
Case 2:
MouseMove MonitorWorkAreaRight / 2, 3 * MonitorWorkAreaBottom / 4, 0
Case 3:
MouseMove 3 * MonitorWorkAreaRight / 4, 3 * MonitorWorkAreaBottom / 4, 0
}
Sleep, 100
MouseGetPos,,, hwnd
WinActivate, ahk_id %hwnd%
}
moveActiveWindow(command) {
WinGet activeWin, ID, A
activeMon := GetMonitorIndexFromWindow(activeWin)
SysGet, MonitorWorkArea, MonitorWorkArea, %activeMon%
WinGetPos, posX, posY, width, height, A
if (command == "moveup" || command == "moveupright" || command == "moveupleft") && (posY - height >= 0) {
posY := posY - height
}
if (command == "movedown" || command = "movedownright" || command = "movedownleft") && (posY + height * 2 <= MonitorWorkAreaBottom) {
posY := posY + height
}
if (command == "moveright" || command == "moveupright" || command == "movedownright") && (posX + width * 2 <= MonitorWorkAreaRight) {
posX := posX + width
}
if (command == "moveleft" || commmand == "moveupleft" || command == "movedownleft") && (posX - height >= 0) {
posX := posX - width
}
WinMove,A,,%posX%,%posY%,%width%,%height%
}
/**
* GetMonitorIndexFromWindow retrieves the HWND (unique ID) of a given window.
* @param {Uint} windowHandle
* @author shinywong
* @link http://www.autohotkey.com/board/topic/69464-how-to-determine-a-window-is-in-which-monitor/?p=440355
*/
GetMonitorIndexFromWindow(windowHandle) {
; Starts with 1.
monitorIndex := 1
VarSetCapacity(monitorInfo, 40)
NumPut(40, monitorInfo)
if (monitorHandle := DllCall("MonitorFromWindow", "uint", windowHandle, "uint", 0x2))
&& DllCall("GetMonitorInfo", "uint", monitorHandle, "uint", &monitorInfo) {
monitorLeft := NumGet(monitorInfo, 4, "Int")
monitorTop := NumGet(monitorInfo, 8, "Int")
monitorRight := NumGet(monitorInfo, 12, "Int")
monitorBottom := NumGet(monitorInfo, 16, "Int")
workLeft := NumGet(monitorInfo, 20, "Int")
workTop := NumGet(monitorInfo, 24, "Int")
workRight := NumGet(monitorInfo, 28, "Int")
workBottom := NumGet(monitorInfo, 32, "Int")
isPrimary := NumGet(monitorInfo, 36, "Int") & 1
SysGet, monitorCount, MonitorCount
Loop, %monitorCount% {
SysGet, tempMon, Monitor, %A_Index%
; Compare location to determine the monitor index.
if ((monitorLeft = tempMonLeft) and (monitorTop = tempMonTop)
and (monitorRight = tempMonRight) and (monitorBottom = tempMonBottom)) {
monitorIndex := A_Index
break
}
}
}
return %monitorIndex%
}
; Numpad unmodified, activate corresponding window
Numpad1::activateWindow(1)
Numpad2::activateWindow(2)
Numpad3::activateWindow(3)
Numpad4::activateWindow(4)
Numpad6::activateWindow(6)
Numpad7::activateWindow(7)
Numpad8::activateWindow(8)
Numpad9::activateWindow(9)
; Win + Numpad = Snap to conrners for diagonals, or top, bottom, left, right of screen (Landscape)
#Numpad7::SnapActiveWindow("top","left","half")
#Numpad8::SnapActiveWindow("top","full","half")
#Numpad9::SnapActiveWindow("top","right","half")
#Numpad4::SnapActiveWindow("top","left","full")
#Numpad6::SnapActiveWindow("top","right","full")
#Numpad1::SnapActiveWindow("bottom","left","half")
#Numpad2::SnapActiveWindow("bottom","full","half")
#Numpad3::SnapActiveWindow("bottom","right","half")
; Ctrl + Alt + Win + keypad = split off Chrome or Edge tab and move to new window
#If WinActive("ahk_exe chrome.exe") || WinActive("ahk_exe msedge.exe")
^#!Numpad7::SplitSnapActiveWindow("top","left","half")
^#!Numpad8::SplitSnapActiveWindow("top","full","half")
^#!Numpad9::SplitSnapActiveWindow("top","right","half")
^#!Numpad4::SplitSnapActiveWindow("top","left","full")
^#!Numpad6::SplitSnapActiveWindow("top","right","full")
^#!Numpad1::SplitSnapActiveWindow("bottom","left","half")
^#!Numpad2::SplitSnapActiveWindow("bottom","full","half")
^#!Numpad3::SplitSnapActiveWindow("bottom","right","half")
#IfWinActive
; Shrink with Windows+Alt num pad
#!Numpad7::shrinkActiveWindow("halftopleft")
#!Numpad8::shrinkActiveWindow("halftop")
#!Up::shrinkActiveWindow("halftop")
#!Numpad9::shrinkActiveWindow("halftopright")
#!Numpad4::shrinkActiveWindow("halfleft")
#!Left::shrinkActiveWindow("halfleft")
#!Right::shrinkActiveWindow("halfright")
#!Numpad6::shrinkActiveWindow("halfright")
#!Numpad1::shrinkActiveWindow("halfbottomleft")
#!Numpad2::shrinkActiveWindow("halfbottom")
#!Down::shrinkActiveWindow("halfbottom")
#!Numpad3::shrinkActiveWindow("halfbottomright")
; Scoot windows around the screen with ctrl+win number pad
^#Numpad7::moveActiveWindow("moveupleft")
^#Numpad8::moveActiveWindow("moveup")
^#Up::moveActiveWindow("moveup")
^#Numpad9::moveActiveWindow("moveupright")
^#Numpad4::moveActiveWindow("moveleft")
^#Left::moveActiveWindow("moveleft")
^#Numpad6::moveActiveWindow("moveright")
^#Right::moveActiveWindow("moveright")
^#Numpad1::moveActiveWindow("movedownleft")
^#Numpad2::moveActiveWindow("movedown")
^#Down::moveActiveWindow("movedown")
^#Numpad3::moveActiveWindow("movedownright")