-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathWindowManager.tq
353 lines (323 loc) · 13.2 KB
/
WindowManager.tq
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
import "Accessibility"
import "AppKit/NSWorkspace"
@NSNotificationCenter
{
- observe: name with: lambda `self addObserverForName: name object: nil queue: nil usingBlock:lambda`
}
@WindowManager
{
accessor: #applications initialValue: []
accessor: #activeWindows initialValue: []
accessor: #inactiveWindows initialValue: []
accessor: #windowsInMaster initialValue: 1
accessor: #defaultRatio initialValue: 0.7
accessor: #defaultMaxMasters initialValue: 1
accessor: #layouts initialValue: []
accessor: #windowChecker
- init
{
@currLayouts = {}
@maxMasters = {}
@ratios = {}
@screensThatNeedReflowing = []
@spacesThatNeedReflowing = []
workspace = NSWorkspace sharedWorkspace
workspace runningApplications each: { app |
self addApplicationWithPID: app processIdentifier unless app bundleIdentifier hasPrefix: "com.apple.dashboard"
}
workspace notificationCenter
observe: NSWorkspaceDidLaunchApplicationNotification with: { n |
^nil if (n userInfo at:#NSApplicationBundleIdentifier) == nil
~self addApplicationWithPID: (n userInfo at:#NSWorkspaceApplicationKey) processIdentifier;
reflow
};
observe: NSWorkspaceDidTerminateApplicationNotification with: { n |
~self removeApplicationWithPID: (n userInfo at:#NSWorkspaceApplicationKey) processIdentifier;
reflow
};
observe: NSWorkspaceDidUnhideApplicationNotification with: { n |
~self activateApplicationWithPID: (n userInfo at:#NSWorkspaceApplicationKey) processIdentifier;
reflow
};
observe: NSWorkspaceDidHideApplicationNotification with: { n |
~self deactivateApplicationWithPID: (n userInfo at:#NSWorkspaceApplicationKey) processIdentifier;
reflow
};
observe: NSWorkspaceActiveSpaceDidChangeNotification with: { n |
~self markAllScreensForReflowing;
markSpaceForReflowing: self currentSpaceId;
reflow
}
^self
}
- currentSpaceId `CGSCopySpaces(_CGSDefaultConnection(), KCGSSpaceCurrent) at: 0`
- currentSpace
{
spaces = CGSCopySpaces(_CGSDefaultConnection(), KCGSSpaceAll)
idx = spaces indexOfObject: self currentSpaceId
^spaces count - idx
}
- switchToSpace: idx
{
spaces = CGSCopySpaces(_CGSDefaultConnection(), KCGSSpaceAll)
^nil if spaces count <= idx
currentSpace = CGSCopySpaces(_CGSDefaultConnection(), KCGSSpaceCurrent)[0]
destSpace = spaces[spaces count - idx - 1]
if currentSpace ~= destSpace {
CGSHideSpaces(_CGSDefaultConnection(), [currentSpace])
CGSShowSpaces(_CGSDefaultConnection(), [destSpace])
CGSManagedDisplaySetCurrentSpace(_CGSDefaultConnection(), KCGSPackagesMainDisplayIdentifier, destSpace)
self markSpaceForReflowing: destSpace
self markAllScreensForReflowing
(self managedWindowsForScreen: NSScreen mainScreen spaces: destSpace) first raise
}
}
\ This doesn't work yet, not sure if my function prototype is wrong, or if you need elevated permissions to move windows around
\ - moveWindow: window toSpace: destSpaceIdx
\ {
\ ^nil if window spaceIds count > 1
\
\ conn = _CGSDefaultConnection()
\ spaces = CGSCopySpaces(conn, KCGSSpaceAll)
\ srcSpace = window spaceIds at: 0
\ destSpace = spaces[destSpaceIdx]
\
\ ^nil if srcSpace == destSpace
\ destSpace print
\ CGSMoveWorkspaceWindowList(conn,
\ TQPointer to: window _cgWindow withType: #i,
\ 1, destSpace) print
\ CGSRemoveWindowsFromSpaces(conn, [window _cgWindow], [srcSpace]) print
\ CGSAddWindowsToSpaces(conn, [window _cgWindow], [destSpace]) print
\ }
- applicationWithPID: pid
{
^@applications find: `app| (app pid) == pid`
}
- addApplicationWithPID: pid
{
^nil if self applicationWithPID: pid
app = AccessibilityElement withPID: pid
^nil unless app isKindOfClass: AccessibilityApplication
@applications push: app
app[#AXWindows] each: { window |
self addWindow: window active: (0 == app[#AXHidden])
}
\ Watch for new window notifications
app observe: #AXWindowCreated on: app with: { newWindow |
~self addWindow: newWindow; reflow
}
}
- addWindow: window `self addWindow: window active: yes`
- addWindow: window active: isActive
{
^nil unless window isKindOfClass: AccessibilityWindow
^nil unless window isResizable
^nil unless @windowChecker == nil or @windowChecker(window)
self markScreenForReflowing: window screen
self markSpacesForReflowing: window spaceIds
if isActive && window[#AXMinimized] boolValue ~= 1 then
@activeWindows insert: window at: 0
else
@inactiveWindows push: window
app = self applicationWithPID: window pid
app observe: #AXUIElementDestroyed on: window with: { w |
~self removeWindow: window; reflow
}
app observe: #AXWindowMiniaturized on: window with: { w |
~self deactivateWindow: window; reflow
}
app observe: #AXWindowDeminiaturized on: window with: { w |
~self activateWindow: window; reflow
}
^valid
}
- managedWindowsForScreen: screen space: spaceId
{
^@activeWindows select: { window |
^no unless (window level == 0) && (window screen == screen)
^yes if window spaceIds containsObject: spaceId
^no
}
}
- activateWindow: window
{
self markScreenForReflowing: window screen
self markSpacesForReflowing: window spaceIds
@activeWindows push: window
@inactiveWindows remove: window
}
- deactivateWindow: window
{
self markScreenForReflowing: window screen
self markSpacesForReflowing: window spaceIds
@activeWindows remove: window
@inactiveWindows push: window
}
- removeWindow: window
{
\ We need to get the original reference in order to access the cached screen
window = @activeWindows find: `w| w == window`
self markScreenForReflowing: window screen
self markSpacesForReflowing: window spaceIds
@activeWindows remove: window
@inactiveWindows remove: window
}
- activateApplicationWithPID: pid
{
activated = @inactiveWindows select: `win| (win pid) == pid`
activated each: { window |
self markScreenForReflowing: window screen
self markSpacesForReflowing: window spaceIds
@inactiveWindows remove: window
@activeWindows push: window
}
}
- deactivateApplicationWithPID: pid
{
deactivated = @activeWindows select: `win| (win pid) == pid`
deactivated each: { window |
self markScreenForReflowing: window screen
self markSpacesForReflowing: window spaceIds
@activeWindows remove: window
@inactiveWindows push: window
}
}
- removeApplicationWithPID: pid
{
windowsToRemove = @activeWindows select: `win| (win pid) == pid`
windowsToRemove each: { window |
self markScreenForReflowing: window screen
self markSpacesForReflowing: window spaceIds
}
@activeWindows -= windowsToRemove
@applications = @applications select: `app| (app pid) ~= pid`
}
- markAllScreensForReflowing `NSScreen screens each: { s | self markScreenForReflowing: s }`
- markScreenForReflowing: screen
{
@screensThatNeedReflowing push: screen unless @screensThatNeedReflowing contains: screen
}
- markSpaceForReflowing: spaceId
{
@spacesThatNeedReflowing push: spaceId unless @spacesThatNeedReflowing contains: spaceId
}
- markSpacesForReflowing: spaceIds
{
spaceIds each: `spaceId| self markSpaceForReflowing: spaceId `
}
- reflow
{
currSpace = self currentSpaceId
^nil unless @spacesThatNeedReflowing containsObject: currSpace
@screensThatNeedReflowing each: { screen |
(self currentLayoutForScreen:screen space:currSpace) reflow: self screen: screen space: currSpace
}
@screensThatNeedReflowing = []
@spacesThatNeedReflowing remove: currSpace
^nil
}
- cycleLayouts `self cycleLayoutsForScreen: AccessibilityWindow frontMostWindow screen space: self currentSpaceId`
- cycleLayoutsForScreen: screen space: spaceId
{
@currLayouts[screen] ||= {}
layoutsForScreen = @currLayouts[screen]
layoutsForScreen[spaceId] ||= 0
layoutsForScreen[spaceId] = 0 if ++layoutsForScreen[spaceId] >= @layouts count
(self currentLayoutForScreen:screen space:spaceId) prepare:self
self markScreenForReflowing:screen
self markSpaceForReflowing:spaceId
}
- currentLayoutForScreen: screen space: spaceId
{
layoutsForScreen = @currLayouts[screen]
layoutsForScreen[spaceId] ||= 0
^@layouts[layoutsForScreen[spaceId]]
}
- windowBefore: win `(self currentLayoutForScreen: win screen space:self currentSpaceId) windowBefore: win inWm: self`
- windowAfter: win `(self currentLayoutForScreen: win screen space:self currentSpaceId) windowAfter: win inWm: self`
- focusOnWindow: win `(self currentLayoutForScreen: win screen space:self currentSpaceId) focusOnWindow: win inWm: self`
- selectPreviousWindow: window `self focusOnWindow: (self windowBefore: window)`
- selectNextWindow: window `self focusOnWindow: (self windowAfter: window)`
- swapWindow: a with: b
{
if a ~= b {
idxA = @activeWindows indexOf: a
idxB = @activeWindows indexOf: b
@activeWindows[idxA], @activeWindows[idxB] = @activeWindows[idxB], @activeWindows[idxA]
self markScreenForReflowing: a screen
self markSpacesForReflowing: a spaceIds
self markScreenForReflowing: b screen
self markSpacesForReflowing: b spaceIds
^b
}
}
- swapWithPreviousWindow: window
{
self swapWindow: window with: (self windowBefore: window)
}
- swapWithNextWindow: window
{
self swapWindow: window with: (self windowAfter: window)
}
\ Swaps the window with it's closest counterpart on the other side
- swapWithCounterpart: window
{
\ Get the existing reference managing the same physical window
screen = window screen
space = self currentSpaceId
windows = self managedWindowsForScreen: screen space: space
idx = windows indexOf: window
masterCount = self maxMastersOnScreen: screen space: space
isOnLeft = idx < masterCount
winCount = windows count
if isOnLeft {
otherIdx = masterCount + (winCount - masterCount) * (idx / masterCount)
otherIdx = otherIdx ceil if (idx / masterCount) >= 0.5
} else
otherIdx = masterCount * ((idx - masterCount) / (winCount - masterCount))
self swapWindow: window with: windows[otherIdx] if otherIdx < windows count
}
- focusOnScreen: screen
{
(self managedWindowsForScreen: screen space: self currentSpaceId) first raise
}
- moveWindow: window toScreen: destScreen
{
srcScreen = window screen
^nil if srcScreen == destScreen
currSpace = self currentSpaceId
self markScreenForReflowing: srcScreen
self markScreenForReflowing: destScreen
self markSpaceForReflowing: currSpace
@activeWindows remove: window
@activeWindows insert: window at: 0
screenFrame = destScreen flippedFrame
windowsOnDestScreen = self managedWindowsForScreen: destScreen space: currSpace
if windowsOnDestScreen count > 0 {
currentlyFocused = windowsOnDestScreen[0]
window setFrame: currentlyFocused frame
} else
window setFrame: screenFrame
}
- ratioForScreen: screen space: spaceId `@ratios[screen][spaceId] || @defaultRatio`
- setRatio: ratio forScreen: screen space: spaceId
{
if (ratio > 0) && (ratio < 1) {
@ratios[screen] ||= {}
@ratios[screen][spaceId] = ratio
self markScreenForReflowing: screen
self markSpaceForReflowing: self currentSpaceId
}
}
- maxMastersOnScreen: screen space: spaceId `@maxMasters[screen][spaceId] || @defaultMaxMasters`
- setMaxMasters: max onScreen: screen space: spaceId
{
if max > 0 {
@maxMasters[screen] ||= {}
@maxMasters[screen][spaceId] = max
self markScreenForReflowing: screen
self markSpaceForReflowing: self currentSpaceId
}
}
}