-
Notifications
You must be signed in to change notification settings - Fork 1
/
.phoenix.js
619 lines (489 loc) · 19.7 KB
/
.phoenix.js
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
// .phoenix.js - this file is loaded automatically by phoenix
const INCREMENT_WIDTH = 200;
// Since Window.recent() can sometimes take ~500ms to return, maintain a cache of windows in recent order
// TODO: ideally would have one recent window cache per space
var RECENT_WINDOW_CACHE = null;
function isOverlap(window1, window2) {
let f1 = window1.frame();
let f2 = window2.frame();
return !(
f1.x >= f2.x + f2.width ||
f2.x >= f1.x + f1.width ||
f1.y >= f2.y + f2.height ||
f2.y >= f1.y + f1.height
);
}
function computeDisplayFrames() {
let screenFrame = Screen.main().flippedVisibleFrame();
let leftHalfFrame = { x: screenFrame.x, y: screenFrame.y, width: screenFrame.width / 2, height: screenFrame.height };
let rightHalfFrame = { x: screenFrame.x + screenFrame.width / 2, y: screenFrame.y, width: screenFrame.width / 2, height: screenFrame.height };
let leftThirdFrame = { x: screenFrame.x, y: screenFrame.y, width: screenFrame.width / 3, height: screenFrame.height };
let middleThirdFrame = { x: screenFrame.x + screenFrame.width / 3, y: screenFrame.y, width: screenFrame.width / 3, height: screenFrame.height };
let rightThirdFrame = { x: screenFrame.x + 2 * screenFrame.width / 3, y: screenFrame.y, width: screenFrame.width / 3, height: screenFrame.height };
let leftTwoThirdsFrame = { x: screenFrame.x, y: screenFrame.y, width: 2 * screenFrame.width / 3, height: screenFrame.height };
let rightTwoThirdsFrame = { x: screenFrame.x + screenFrame.width / 3, y: screenFrame.y, width: 2 * screenFrame.width / 3, height: screenFrame.height };
return { leftHalfFrame, rightHalfFrame, leftThirdFrame, middleThirdFrame, rightThirdFrame, leftTwoThirdsFrame, rightTwoThirdsFrame };
}
// Finds three windows that are tiled side-by-side
function findThreeTiledWindows(recentWindows) {
let currentWindow = Window.focused();
let windows = recentWindows;
let screenFrame = Space.active().screens()[0].visibleFrame();
// loop over all possible 3 window combinations
for (let i = 0; i < windows.length; i++) {
for (let j = i + 1; j < windows.length; j++) {
let firstWindow = currentWindow;
let secondWindow = windows[i];
let thirdWindow = windows[j];
if (
isOverlap(firstWindow, secondWindow) ||
isOverlap(firstWindow, thirdWindow) ||
isOverlap(secondWindow, thirdWindow)
) {
continue;
}
let leftmostWindow = null;
let middleWindow = null;
let rightmostWindow = null;
if (firstWindow.frame().x < secondWindow.frame().x) {
if (firstWindow.frame().x < thirdWindow.frame().x) {
leftmostWindow = firstWindow;
if (secondWindow.frame().x < thirdWindow.frame().x) {
middleWindow = secondWindow;
rightmostWindow = thirdWindow;
} else {
middleWindow = thirdWindow;
rightmostWindow = secondWindow;
}
} else {
leftmostWindow = thirdWindow;
middleWindow = firstWindow;
rightmostWindow = secondWindow;
}
} else {
if (secondWindow.frame().x < thirdWindow.frame().x) {
leftmostWindow = secondWindow;
if (firstWindow.frame().x < thirdWindow.frame().x) {
middleWindow = firstWindow;
rightmostWindow = thirdWindow;
} else {
middleWindow = thirdWindow;
rightmostWindow = firstWindow;
}
} else {
leftmostWindow = thirdWindow;
middleWindow = secondWindow;
rightmostWindow = firstWindow;
}
}
let firstWindowSize =
firstWindow.frame().width * firstWindow.frame().height;
let secondWindowSize =
secondWindow.frame().width * secondWindow.frame().height;
let thirdWindowSize =
thirdWindow.frame().width * thirdWindow.frame().height;
if (
firstWindowSize + secondWindowSize + thirdWindowSize ==
screenFrame.width * screenFrame.height
) {
let result = [firstWindow, secondWindow, thirdWindow].sort(
(a, b) => a.frame().x - b.frame().x
);
console.log(
`the three window titles are: ${JSON.stringify([
result[0].title(),
result[1].title(),
result[2].title(),
])}`
);
return result;
}
}
}
return null;
}
function findTwoTiledWindows(recentWindows) {
let currentWindow = Window.focused();
let windows = recentWindows;
let screenFrame = Space.active().screens()[0].visibleFrame();
for (const w2 of windows) {
let w1 = currentWindow;
if (w1.isEqual(w2) || isOverlap(w1, w2)) {
continue;
}
// console.log(`comparing ${w1.app().name()} (${w1.title()}) and ${w2.app().name()} (${w2.title()})`);
let w1Frame = w1.frame();
let w2Frame = w2.frame();
let w1Size = w1Frame.width * w1Frame.height;
let w2Size = w2Frame.width * w2Frame.height;
if (w1Size + w2Size === screenFrame.width * screenFrame.height) {
let result = [w1, w2].sort((a, b) => a.frame().x - b.frame().x);
console.log(`the two window titles are: ${JSON.stringify([result[0].title(), result[1].title()])}`);
return result;
}
}
return null;
}
// Finds the two side-by-side tile windows and adjusts the split ratio
function retileTwoWindows(direction) {
let currentSpace = Space.active();
let screen = currentSpace.screens()[0];
let screenFrame = screen.visibleFrame();
// console.log(`screen frame is ${JSON.stringify(screenFrame)}`);
var start = Date.now();
let recentWindows = RECENT_WINDOW_CACHE ? RECENT_WINDOW_CACHE : Window.recent();
console.log(`took ${Date.now() - start} ms to call Window.recent()`);
let windows = recentWindows.filter((wndow) => wndow.isVisible() === true);
// console.log(`checking windows in order: ${windows.map(window => window.app().name())}`);
// find two windows whose size adds up to screen
start = Date.now();
let window1 = null;
let window2 = null;
let breakCheck1 = false; // break out of outer loop
for (const w1 of windows) {
for (const w2 of windows) {
if (w1.isEqual(w2) || isOverlap(w1, w2)) {
continue;
}
// console.log(`comparing ${w1.app().name()} (${w1.title()}) and ${w2.app().name()} (${w2.title()})`);
let w1Frame = w1.frame();
let w2Frame = w2.frame();
let w1Size = w1Frame.width * w1Frame.height;
let w2Size = w2Frame.width * w2Frame.height;
if (w1Size + w2Size === screenFrame.width * screenFrame.height) {
window1 = w1;
window2 = w2;
breakCheck1 = true;
break;
}
}
if (breakCheck1) {
break;
}
}
if (window1 === null || window2 === null) {
console.log("could not find windows");
return;
}
console.log(`took ${Date.now() - start} ms to find windows`);
let leftWindow = null;
let rightWindow = null;
let w1Frame = window1.frame();
let w2Frame = window2.frame();
if (w1Frame.x < w2Frame.x) {
leftWindow = window1;
rightWindow = window2;
} else {
leftWindow = window2;
rightWindow = window1;
}
console.log(
`found windows ${leftWindow.app().name()} (${leftWindow.title()}) [left] and ${rightWindow
.app()
.name()} (${rightWindow.title()} [right])`
);
let leftFrame = leftWindow.frame();
let rightFrame = rightWindow.frame();
console.log(`current frames are ${JSON.stringify([leftFrame, rightFrame])}`);
leftFrame.width -= INCREMENT_WIDTH * direction;
rightFrame.width += INCREMENT_WIDTH * direction;
rightFrame.x -= INCREMENT_WIDTH * direction;
console.log(`new frames are ${JSON.stringify([leftFrame, rightFrame])}`);
// TODO: seems like if you try to change both the width and the x of Orion.app window at the same time, it will keep the the same x position (but correctly change the width) - best workaround is likely to always tile Orion.app windows to the left since we only need to adjust one frame property of the left window
start = Date.now();
leftWindow.setFrame(leftFrame);
rightWindow.setFrame(rightFrame);
console.log(`took ${Date.now() - start} ms to set frames`);
}
// Detects whether the current window config is either two-window tile, three-window tile, or not a tile
function getTiledWindowsConfig(recentWindows) {
// First check for three-window tile, since that config is less likely to occur by accident
let threeTiledWindows = findThreeTiledWindows(recentWindows);
if (threeTiledWindows !== null) {
return { type: "three-tile", windows: threeTiledWindows };
}
let twoTiledWindows = findTwoTiledWindows(recentWindows);
if (twoTiledWindows !== null) {
return { type: "two-tile", windows: twoTiledWindows };
}
return { type: "none", windows: [] };
}
// When two windows are tiled side-by-side, Ctrl-Shift-H will adjust the split to increase the width of the *right* window
Key.on("h", ["ctrl", "shift"], () => {
retileTwoWindows(1);
});
// When two windows are tiled side-by-side, Ctrl-Shift-L will adjust the split to increase the width of the *left* window
Key.on("l", ["ctrl", "shift"], () => {
retileTwoWindows(-1);
});
function moveFocusToRightWindowTile() {
let currentWindow = Window.focused();
let recentWindows = RECENT_WINDOW_CACHE ? RECENT_WINDOW_CACHE : Window.recent();
let tiledWindows = getTiledWindowsConfig(recentWindows);
if (tiledWindows.type === "none") {
return;
}
if (tiledWindows.type === "three-tile") {
let leftWindow = tiledWindows.windows[0];
let middleWindow = tiledWindows.windows[1];
let rightWindow = tiledWindows.windows[2];
if (currentWindow.hash() === leftWindow.hash()) {
middleWindow.focus();
} else if (currentWindow.hash() === middleWindow.hash()) {
rightWindow.focus();
} else if (currentWindow.hash() === rightWindow.hash()) {
leftWindow.focus();
}
}
if (tiledWindows.type === "two-tile") {
let leftWindow = tiledWindows.windows[0];
let rightWindow = tiledWindows.windows[1];
if (currentWindow.hash() === leftWindow.hash()) {
rightWindow.focus();
} else if (currentWindow.hash() === rightWindow.hash()) {
leftWindow.focus();
}
}
}
// When windows are tiled (either two-way or three-way), Ctrl+Shift+K will move window focus to the spatially east window.
Key.on("k", ["ctrl", "shift"], () => {
moveFocusToRightWindowTile();
});
Key.on("m", ["ctrl"], () => {
moveFocusToRightWindowTile();
});
// When windows are tiled (either two-way or three-way), Ctrl+Shift+J will move window focus to the spatially west window.
Key.on("j", ["ctrl", "shift"], () => {
let currentWindow = Window.focused();
let recentWindows = Window.recent();
let tiledWindows = getTiledWindowsConfig(recentWindows);
if (tiledWindows.type === "none") {
return;
}
if (tiledWindows.type === "three-tile") {
let leftWindow = tiledWindows.windows[0];
let middleWindow = tiledWindows.windows[1];
let rightWindow = tiledWindows.windows[2];
if (currentWindow.hash() === leftWindow.hash()) {
rightWindow.focus();
} else if (currentWindow.hash() === middleWindow.hash()) {
leftWindow.focus();
} else if (currentWindow.hash() === rightWindow.hash()) {
middleWindow.focus();
}
}
if (tiledWindows.type === "two-tile") {
let leftWindow = tiledWindows.windows[0];
let rightWindow = tiledWindows.windows[1];
if (currentWindow.hash() === leftWindow.hash()) {
rightWindow.focus();
} else if (currentWindow.hash() === rightWindow.hash()) {
leftWindow.focus();
}
}
});
// Move focus to left-most window in tile (if windows are tiled)
Key.on("1", ["ctrl", "cmd"], () => {
let recentWindows = Window.recent();
let tiledWindows = getTiledWindowsConfig(recentWindows);
if (tiledWindows.type === "none") {
return;
}
if (tiledWindows.type === "three-tile") {
tiledWindows.windows[0].focus();
}
if (tiledWindows.type === "two-tile") {
tiledWindows.windows[0].focus();
}
});
// Move focus to middle window in tile (if windows are tiled) (or right window if there are only two windows)
Key.on("2", ["ctrl", "cmd"], () => {
let recentWindows = Window.recent();
let tiledWindows = getTiledWindowsConfig(recentWindows);
if (tiledWindows.type === "none") {
return;
}
if (tiledWindows.type === "three-tile") {
tiledWindows.windows[1].focus();
}
if (tiledWindows.type === "two-tile") {
tiledWindows.windows[1].focus();
}
});
// Move focus to right-most window in tile (if windows are tiled)
Key.on("3", ["ctrl", "cmd"], () => {
let recentWindows = Window.recent();
let tiledWindows = getTiledWindowsConfig(recentWindows);
if (tiledWindows.type === "none") {
return;
}
if (tiledWindows.type === "three-tile") {
tiledWindows.windows[2].focus();
}
if (tiledWindows.type === "two-tile") {
tiledWindows.windows[1].focus();
}
});
// TODO: Also ignore ScreenFloat.app
const APPS_TO_IGNORE = [
"rcmd",
"Bartender 4",
"Spotify",
"Dash",
"Prime Video",
];
// Normal web dev layout
// Move VSCode to the center 1/3rd of the screen, move Chrome to the right 1/3rd of the screen, and move all other windows to the left 1/3rd of the screen
Key.on("a", ["ctrl", "option", "shift", "cmd"], () => {
const { leftThirdFrame, middleThirdFrame, rightThirdFrame } = computeDisplayFrames();
let windows = Space.active().windows().filter(w => !APPS_TO_IGNORE.includes(w.app().name()));
let vscodeWindows = windows.filter(w => w.app().name() === "Code");
let chromeWindows = windows.filter(w => w.app().name() === "Google Chrome");
let remainingWindows = windows.filter(w => w.app().name() !== "Code" && w.app().name() !== "Google Chrome");
vscodeWindows.forEach(w => w.setFrame(middleThirdFrame));
chromeWindows.forEach(w => w.setFrame(rightThirdFrame));
remainingWindows.forEach(w => w.setFrame(leftThirdFrame));
let figmaWindows = windows.filter(w => w.app().name() === "Figma");
// Quickly focus Figma to bring it to the front
// Note that .raise() won't work here since that only makes it the frontmost window in the app, not the frontmost window on the screen
if (figmaWindows.length > 0) {
figmaWindows[0].focus();
}
setTimeout(() => {
vscodeWindows[0].focus();
}, 100);
});
// Debug web dev layout
// Move VSCode and iTerm to the left 1/3rd of the screen, move Chrome Dev Tools to the middle 1/3rd of the screen, move Chrome to the right 1/3rd of the screen (and leave all other app windows untouched)
Key.on("b", ["ctrl", "option", "shift", "cmd"], () => {
let screenFrame = Screen.main().flippedVisibleFrame();
const { leftThirdFrame, middleThirdFrame, rightThirdFrame } = computeDisplayFrames();
let windows = Space.active().windows().filter(w => !APPS_TO_IGNORE.includes(w.app().name()));
let vscodeWindows = windows.filter(w => w.app().name() === "Code");
let chromeDevToolWindows = windows.filter(w => w.app().name() === "Google Chrome" && w.title().includes("DevTools"));
let itermWindows = windows.filter(w => w.app().name() === "iTerm2");
let otherChromeWindows = windows.filter(w => w.app().name() === "Google Chrome" && !w.title().includes("DevTools"));
vscodeWindows.forEach(w => w.setFrame(leftThirdFrame));
itermWindows.forEach(w => w.setFrame(leftThirdFrame));
chromeDevToolWindows.forEach(w => w.setFrame(middleThirdFrame));
otherChromeWindows.forEach(w => w.setFrame(rightThirdFrame));
vscodeWindows[0].focus();
});
// Expanded web dev layout
// Move VSCode to left 2/3rd of the screen, and move every other window to the right 1/3rd of the screen
Key.on("c", ["ctrl", "option", "shift", "cmd"], () => {
let screenFrame = Screen.main().flippedVisibleFrame();
const { leftTwoThirdsFrame, rightThirdFrame } = computeDisplayFrames();
let windows = Space.active().windows().filter(w => !APPS_TO_IGNORE.includes(w.app().name()));
let vscodeWindows = windows.filter(w => w.app().name() === "Code");
let remainingWindows = windows.filter(w => w.app().name() !== "Code");
vscodeWindows.forEach(w => w.setFrame(leftTwoThirdsFrame));
remainingWindows.forEach(w => w.setFrame(rightThirdFrame));
vscodeWindows[0].focus();
// Raise the browser Chrome window above the DevTools window
let chromeBrowserWindows = Space.active().windows().filter(w => w.app().name() === "Google Chrome" && !w.title().includes("DevTools"));
chromeBrowserWindows.forEach(w => w.raise());
});
// Expanded web dev layout alt
// Move VSCode to right 2/3rd of the screen, and move every other window to left 1/3rd of the screen
Key.on("d", ["ctrl", "option", "shift", "cmd"], () => {
let screenFrame = Screen.main().flippedVisibleFrame();
const { leftThirdFrame, rightTwoThirdsFrame } = computeDisplayFrames();
let windows = Space.active().windows().filter(w => !APPS_TO_IGNORE.includes(w.app().name()));
let vscodeWindows = windows.filter(w => w.app().name() === "Code");
let remainingWindows = windows.filter(w => w.app().name() !== "Code");
remainingWindows.forEach(w => w.setFrame(leftThirdFrame));
vscodeWindows.forEach(w => w.setFrame(rightTwoThirdsFrame));
let figmaWindows = windows.filter(w => w.app().name() === "Figma");
// Quickly focus Figma to bring it to the front
// Note that .raise() won't work here since that only makes it the frontmost window in the app, not the frontmost window on the screen
if (figmaWindows.length > 0) {
figmaWindows[0].focus();
}
setTimeout(() => {
vscodeWindows[0].focus();
}, 100);
});
function tileTwoMostRecentWindows() {
const { leftHalfFrame, rightHalfFrame } = computeDisplayFrames();
let windows = Window.recent();
if (windows.length < 2) {
return;
}
let leftWindow = windows[0];
let rightWindow = windows[1];
// Try to put Roam Research.app on the right
if (leftWindow.app().name() === "Roam Research") {
[leftWindow, rightWindow] = [rightWindow, leftWindow];
}
// Make sure Orion.app is always on the left
if (rightWindow.app().name() === "Orion") {
[leftWindow, rightWindow] = [rightWindow, leftWindow];
}
leftWindow.setFrame(leftHalfFrame);
rightWindow.setFrame(rightHalfFrame);
}
// Tile the two most recent windows
// Special cases/notes:
// - Will always tile Orion.app on the left (see TODO above for context)
Key.on(",", ["option", "shift"], () => {
tileTwoMostRecentWindows();
});
Key.on("0", ["ctrl"], () => {
tileTwoMostRecentWindows();
});
const DEBOUNCE_TIME_MS = 1000;
var invalidateRecentWindowCacheTimeout = null;
function invalidateRecentWindowCacheDebounced() {
clearTimeout(invalidateRecentWindowCacheTimeout);
invalidateRecentWindowCacheTimeout = setTimeout(() => {
invalidateRecentWindowCacheTimeout = null;
let start = new Date().getTime();
RECENT_WINDOW_CACHE = Window.recent();
let finish = new Date().getTime();
console.log(`refreshed recent window cache, took ${finish - start}ms`);
}, DEBOUNCE_TIME_MS);
}
Event.on('windowDidOpen', (window) => {
if (!window.title()) {
return;
}
invalidateRecentWindowCacheDebounced();
});
Event.on('windowDidClose', (window) => {
if (!window.title()) {
return;
}
invalidateRecentWindowCacheDebounced();
});
Event.on('windowDidMove', (window) => {
if (!window.title()) {
return;
}
invalidateRecentWindowCacheDebounced();
});
Event.on('windowDidResize', (window) => {
if (!window.title()) {
return;
}
invalidateRecentWindowCacheDebounced();
});
// App events don't change any window positions, but they can change the order of recent windows
Event.on('appDidLaunch', (app) => {
invalidateRecentWindowCacheDebounced();
});
Event.on('appDidTerminate', (app) => {
invalidateRecentWindowCacheDebounced();
});
Event.on('appDidActivate', (app) => {
invalidateRecentWindowCacheDebounced();
});
Event.on('appDidHide', (app) => {
invalidateRecentWindowCacheDebounced();
});
Event.on('appDidShow', (app) => {
invalidateRecentWindowCacheDebounced();
});
Event.on('spaceDidChange', (space) => {
invalidateRecentWindowCacheDebounced();
});