-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFling2.ahk
370 lines (345 loc) · 18.9 KB
/
Fling2.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
;; Fling2 replaced Win+Left and Win+Right by using thirds and halfs instead of only halfs in a monitor setup with a ultra wide monitor
;; It uses thirds on a ultra wide monitor (e.g. LG 34UC98-W) and halfs on standard 16:9/16:10 monitors
;; It also supports doubleColumns when using thirds, so that one windows can use 2/3 of the screen and another 1/3440x1440
;; -----------------------------------------------------------------------
;; Fling2 is based on Fling from Patrick Sheppard (2010)
;; Thank you very much for your great work!
;; Fling Source: https://autohotkey.com/board/topic/51956-flinging-windows-across-a-multi-monitor-system/
;; -----------------------------------------------------------------------
;; Fling (or shift) a window between columns and monitors in a multi-monitor system, using thirds on ultra wide monitors and halfs on wide screen monitors.
;;
;; Function Parameters:
;;
;; FlingDirection The direction of the fling, expected to be either +1 or -1.
;; The function is not limited to just two monitors; it supports
;; as many monitors as are currently connected to the system and
;; can fling a window serially through each of them in turn.
;;
;; WinID The window ID of the window to move. There are two special WinID
;; values supported:
;;
;; 1) The value "A" means to use the Active window (default).
;; 2) The value "M" means to use the window currently under the Mouse.
;;
;; DoubleColumns Can be either 1 or 0. 1 means that on a ultra wide monitor, where thirds are used,
;; two coulmn steps are used so that a window can take 2/3 of the screen.
;;
;; Column Can be 0 or more. When 0, Fling3 flings window as expected, when giving a column > 0,
;; the window is flinged to this position ignoring the Direction setting
;; When DoubleCoumn = 1 is used, the column is extended to the right, the last column is extended to the left
;; (e.g. column 1 is then a double column over 1 and 2)
;;
;; Minimized windows are not modified; they are left exactly where they were.
;;
;; The return value of the function is non-zero if the window was successfully flung.
;;
;; Author: Andreas Bader
;; Date: August, 2016
;; https://github.com/baderas/autohotkey-scripts
#Include %A_LineFile%\..\WindowPlacementFunctions.ahk
Win__Fling2(FlingDirection = 1, WinID = "A", DoubleColumns = 0, Column = 0)
{
HeightCompensationFactor := 10 ;pixels
WidthCompensationFactor := 20 ;pixels
WidthCompensationFactor2 := Round(WidthCompensationFactor/2) ;pixels, other direction
; Figure out which window to move based on the "WinID" function parameter:
; 1) The letter "A" means to use the Active window
; 2) The letter "M" means to use the window under the Mouse
; Otherwise, the parameter value is assumed to be the AHK window ID of the window to use.
if (WinID = "A")
{
; If the user supplied an "A" as the window ID, we use the Active window
WinID := WinExist("A")
}
else if (WinID = "M")
{
; If the user supplied an "M" as the window ID, we use the window currently under the Mouse
MouseGetPos, MouseX, MouseY, WinID ; MouseX & MouseY are retrieved but, for now, not used
}
; Check to make sure we are working with a valid window
IfWinNotExist, ahk_id %WinID%
{
return 0
}
; Here's where we find out just how many monitors we're dealing with
SysGet, MonitorCount, MonitorCount
; For each active monitor, we get Top, Bottom, Left, Right of the monitor's
; 'Work Area' (i.e., excluding taskbar, etc.). From these values we compute Width and Height.
; Results get put into variables named like "Monitor1Top" and "Monitor2Width", etc.,
; with the monitor number embedded in the middle of the variable name.
; Also calculate how many positions are available on each monitor
; for this the width is dived by the height and round up to the next integer
; this gives 2 positions for a 16:9 Monitor (2560x1440 -> DPI corrected to 200% -> 1280x690 )
; and 3 positions for a ultra wide monitor (3440x1440)
Loop, %MonitorCount%
{
SysGet, Monitor%A_Index%, MonitorWorkArea, %A_Index%
Monitor%A_Index%Width := Monitor%A_Index%Right - Monitor%A_Index%Left
Monitor%A_Index%Height := Monitor%A_Index%Bottom - Monitor%A_Index%Top
Monitor%A_Index%Positions := Ceil(Monitor%A_Index%Width / Monitor%A_Index%Height)
Monitor%A_Index%PositionSize := (Monitor%A_Index%Width / Monitor%A_Index%Positions)
}
; Retrieve the target window's original minimized / maximized state
WinGet, WinOriginalMinMaxState, MinMax, ahk_id %WinID%
;WinGetClass, WinClass, ahk_id %WinID%
; We don't do anything with minimized windows (for now... this may change)
if (WinOriginalMinMaxState = -1)
{
; Debatable as to whether or not this should be flagged as an error
return 0
}
; If the window started out maximized, then the plan is to:
; (a) restore it,
; (b) fling it, then
; (c) re-maximize it on the target monitor.
;
; The reason for this is so that the usual maximize / restore windows controls
; work as you'd expect. You want Windows to use the dimensions of the non-maximized
; window when you click the little restore icon on a previously flung (maximized) window.
if (WinOriginalMinMaxState = 1)
{
; Restore a maximized window to its previous state / size ... before "flinging".
;
; Programming Note: It would be nice to hide the window before doing this ...
; the window does some visual calisthenics that the user may construe as a bug.
; Unfortunately, if you hide a window then you can no longer work with it. <Sigh>
WinRestore, ahk_id %WinID%
}
; Retrieve the target window's original (non-maximized) dimensions
WinGetPos, WinX, WinY, WinW, WinH, ahk_id %WinID%
; Find the point at the centre of the target window then use it
; to determine the monitor to which the target window belongs
; (windows don't have to be entirely contained inside any one monitor's area).
; Determine in which Position it is on current monitor
WinCentreX := WinX + WinW / 2
WinCentreY := WinY + WinH / 2
CurrMonitor = 0
Loop, %MonitorCount%
{
if ( (WinCentreX >= Monitor%A_Index%Left) and (WinCentreX < Monitor%A_Index%Right )
and (WinCentreY >= Monitor%A_Index%Top ) and (WinCentreY < Monitor%A_Index%Bottom))
{
CurrMonitor = %A_Index%
; using WinX does not work good enough here.
CurrPos := Ceil((WinCentreX-Monitor%A_Index%Left) / Monitor%A_Index%PositionSize)
; if it is left from the centre
; due to rounding we need 0.1% margin
if (FlingDirection == 1)
{
if ((WinCentreX-Monitor%A_Index%Left)*1.001 < ((Monitor%A_Index%PositionSize*(CurrPos-1)) + 0.5*Monitor%A_Index%PositionSize))
{
CurrPos := CurrPos-1
}
}
else
{
if ((WinCentreX-Monitor%A_Index%Left)*1.001 > ((Monitor%A_Index%PositionSize*(CurrPos-1)) + 0.5*Monitor%A_Index%PositionSize))
{
CurrPos := CurrPos+1
}
}
break
}
}
HeightCompensationFactor := (Monitor%CurrMonitor%Height//100)-(Mod(Monitor%CurrMonitor%Height//100,5))
;WidthCompensationFactor := (Monitor%CurrMonitor%Width//100)-(Mod(Monitor%CurrMonitor%Width//100,5))
;WidthCompensationFactor2 := (WidthCompensationFactor/2)
WidthCompensationFactor := 20
WidthCompensationFactor2 := Round(WidthCompensationFactor/2)
; If a columnt is set, ignore the rest.
if (Column > 0)
{
if (Column > Monitor%CurrMonitor%Positions)
{
Column := Monitor%CurrMonitor%Positions
}
if (DoubleColumns == 1 and Monitor%CurrMonitor%Positions > 2)
{
if (Column == Monitor%CurrMonitor%Positions)
{
Column := Monitor%CurrMonitor%Positions - 1
}
; Only change positions on the same Monitor
WinFlingX := Monitor%CurrMonitor%Left + ((Monitor%CurrMonitor%PositionSize*(Column-1))) - WidthCompensationFactor2
WinFlingY := Monitor%CurrMonitor%Top
WinFlingW := (Monitor%CurrMonitor%PositionSize*2) + WidthCompensationFactor
WinFlingH := Monitor%CurrMonitor%Height + HeightCompensationFactor
}
else
{
; Only change positions on the same Monitor
WinFlingX := Monitor%CurrMonitor%Left+((Monitor%CurrMonitor%PositionSize*(Column-1))) - WidthCompensationFactor2
WinFlingY := Monitor%CurrMonitor%Top
WinFlingW := Monitor%CurrMonitor%PositionSize + WidthCompensationFactor
WinFlingH := Monitor%CurrMonitor%Height + HeightCompensationFactor
}
}
else
{
; Compute the next Position
; coordinates are not exact, +-HeightCompensationFactor and +-WidthCompensationFactor and +-WidthCompensationFactor2 are found by manual testing
if (FlingDirection == 1)
{
if (CurrPos < Monitor%CurrMonitor%Positions)
{
; Check if double columns are activated and move window accordingly
if (DoubleColumns == 1 and Monitor%CurrMonitor%Positions > 2)
{
if (WinW > (Monitor%CurrMonitor%PositionSize+30))
{
; Only change positions on the same Monitor
WinFlingX := Monitor%CurrMonitor%Left + ((Monitor%CurrMonitor%PositionSize*(CurrPos))) - WidthCompensationFactor2
WinFlingY := Monitor%CurrMonitor%Top
WinFlingW := Monitor%CurrMonitor%PositionSize + WidthCompensationFactor
WinFlingH := Monitor%CurrMonitor%Height + HeightCompensationFactor
}
else
{
; Only change positions on the same Monitor
WinFlingX := Monitor%CurrMonitor%Left + ((Monitor%CurrMonitor%PositionSize*(CurrPos-1))) - WidthCompensationFactor2
WinFlingY := Monitor%CurrMonitor%Top
WinFlingW := (Monitor%CurrMonitor%PositionSize*2) + WidthCompensationFactor
WinFlingH := Monitor%CurrMonitor%Height + HeightCompensationFactor
}
}
else
{
; Only change positions on the same Monitor
WinFlingX := Monitor%CurrMonitor%Left + ((Monitor%CurrMonitor%PositionSize*(CurrPos))) - WidthCompensationFactor2
WinFlingY := Monitor%CurrMonitor%Top
WinFlingW := Monitor%CurrMonitor%PositionSize + WidthCompensationFactor
WinFlingH := Monitor%CurrMonitor%Height + HeightCompensationFactor
}
}
else
{
; Change Monitor
; Compute the number of the next monitor in the direction of the specified fling (+1 or -1)
; Valid monitor numbers are 1..MonitorCount, and we effect a circular fling.
NextMonitor := CurrMonitor + FlingDirection
if (NextMonitor > MonitorCount)
{
NextMonitor = 1
}
else if (NextMonitor <= 0)
{
NextMonitor = %MonitorCount%
}
HeightCompensationFactor := (Monitor%NextMonitor%Height//100)-(Mod(Monitor%NextMonitor%Height//100,5))
;WidthCompensationFactor := (Monitor%NextMonitor%Width//100)-(Mod(Monitor%NextMonitor%Width//100,5))
;WidthCompensationFactor2 := (WidthCompensationFactor/2)
WidthCompensationFactor := 20
WidthCompensationFactor2 := Round(WidthCompensationFactor/2)
; Scale the position / dimensions of the target window by the ratio of the monitor sizes.
; Programming Note: Do multiplies before divides in order to maintain accuracy in the integer calculation.
WinFlingX := Monitor%NextMonitor%Left - WidthCompensationFactor2
WinFlingY := Monitor%NextMonitor%Top
WinFlingW := Monitor%NextMonitor%PositionSize + WidthCompensationFactor
WinFlingH := Monitor%NextMonitor%Height + HeightCompensationFactor
}
}
else
{
; The next (quite complex) if is required to perform the step from the most left column to the next monitor without moving into a double column
; the last two "or"s are needed when a window is right from the centre of the first third/half and fling direction is left, see https://github.com/baderas/autohotkey-scripts/issues/1
; the last or fixes the special case that a window is on the left third/half in the rightest monitor and fling direction is left, because then it should not enter this if clause
if (CurrPos > 2 or (CurrPos > 1
and ((DoubleColumns == 1
and Monitor%CurrMonitor%Positions > 2
and WinW > (Monitor%CurrMonitor%PositionSize + WidthCompensationFactor2 + WidthCompensationFactor))
or WinCentreX > Floor((Monitor%CurrMonitor%PositionSize/2) + Monitor%CurrMonitor%PositionSize*(CurrPos - 2) + Monitor%CurrMonitor%Left)
or (WinCentreX == Floor((Monitor%CurrMonitor%PositionSize/2) + Monitor%CurrMonitor%PositionSize*(CurrPos - 2) + Monitor%CurrMonitor%Left)
and CurrPos > Monitor%CurrMonitor%Positions))))
{
; check if double columns are activated and move window accordingly
if (DoubleColumns == 1 and Monitor%CurrMonitor%Positions > 2)
{
if (WinW > (Monitor%CurrMonitor%PositionSize+30)
or WinCentreX > Floor(Monitor%CurrMonitor%PositionSize/2) + Monitor%CurrMonitor%PositionSize*(CurrPos - 2)+ Monitor%CurrMonitor%Left)
{
; Only change positions on the same Monitor
WinFlingX := Monitor%CurrMonitor%Left + ((Monitor%CurrMonitor%PositionSize*(CurrPos-2))) - WidthCompensationFactor2
WinFlingY := Monitor%CurrMonitor%Top
WinFlingW := Monitor%CurrMonitor%PositionSize + WidthCompensationFactor
WinFlingH := Monitor%CurrMonitor%Height + HeightCompensationFactor
}
else
{
; Only change positions on the same Monitor
WinFlingX := Monitor%CurrMonitor%Left + ((Monitor%CurrMonitor%PositionSize*(CurrPos-3))) - WidthCompensationFactor2
WinFlingY := Monitor%CurrMonitor%Top
WinFlingW := (Monitor%CurrMonitor%PositionSize*2) + WidthCompensationFactor
WinFlingH := Monitor%CurrMonitor%Height + HeightCompensationFactor
}
}
else
{
; Only change positions on the same Monitor
WinFlingX := Monitor%CurrMonitor%Left + ((Monitor%CurrMonitor%PositionSize*(CurrPos-3))) - WidthCompensationFactor2
WinFlingY := Monitor%CurrMonitor%Top
WinFlingW := Monitor%CurrMonitor%PositionSize + WidthCompensationFactor
WinFlingH := Monitor%CurrMonitor%Height + HeightCompensationFactor
}
}
else
{
; Change Monitor
; Compute the number of the next monitor in the direction of the specified fling (+1 or -1)
; Valid monitor numbers are 1..MonitorCount, and we effect a circular fling.
NextMonitor := CurrMonitor + FlingDirection
if (NextMonitor > MonitorCount)
{
NextMonitor = 1
}
else if (NextMonitor <= 0)
{
NextMonitor = %MonitorCount%
}
HeightCompensationFactor := (Monitor%NextMonitor%Height//100)-(Mod(Monitor%NextMonitor%Height//100,5))
;WidthCompensationFactor := (Monitor%NextMonitor%Width//100)-(Mod(Monitor%NextMonitor%Width//100,5))
;WidthCompensationFactor2 := (WidthCompensationFactor/2)
WidthCompensationFactor := 20
WidthCompensationFactor2 := Round(WidthCompensationFactor/2)
; Scale the position / dimensions of the target window by the ratio of the monitor sizes.
; Programming Note: Do multiplies before divides in order to maintain accuracy in the integer calculation.
WinFlingX := Monitor%NextMonitor%Left + ((Monitor%NextMonitor%PositionSize*(Monitor%NextMonitor%Positions-1))) - WidthCompensationFactor2
WinFlingY := Monitor%NextMonitor%Top
WinFlingW := Monitor%NextMonitor%PositionSize + WidthCompensationFactor
WinFlingH := Monitor%NextMonitor%Height + HeightCompensationFactor
}
}
}
; It's time for the target window to make its big move
WinMove, ahk_id %WinID%,, WinFlingX, WinFlingY, WinFlingW, WinFlingH
; Fix for some windows like ConEmu
; The problem seems to be dpi/multi monitor related
; When a window touches a border between two monitors, the height of the other one is used,
; even is the window barely touches the border
WinGetPos, WinX2, WinY2, WinW2, WinH2, ahk_id %WinID%
if (MonitorCount > 1 and (WinFlingH != WinH2 or Abs(WinFlingW-WinW2) > 1))
{
; Change Monitor
; Compute the number of the next monitor in the direction of the specified fling (+1 or -1)
; Valid monitor numbers are 1..MonitorCount, and we effect a circular fling.
NextMonitor := CurrMonitor + FlingDirection
if (NextMonitor > MonitorCount)
{
NextMonitor = 1
}
else if (NextMonitor <= 0)
{
NextMonitor = %MonitorCount%
}
CorrectionFactor := Monitor%NextMonitor%Height/Monitor%CurrMonitor%Height
if (WinFlingH != WinH2)
{
WinFlingH := Monitor%NextMonitor%Height+(HeightCompensationFactor*2)
}
if (Abs(WinFlingW-WinW2) > 1)
{
WinFlingW := WinFlingW-((Abs(WinFlingW-WinW2)/2)+1)
}
WinMove, ahk_id %WinID%,, WinFlingX, WinFlingY, WinFlingW, WinFlingH
}
return 1
}