forked from kemar/phoenix-mercury-mover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phoenix.js
366 lines (327 loc) · 11.1 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
// ------------------------------------------------------------------------------
// Phoenix config.
// https://github.com/kasper/phoenix/blob/d0a3ac/API.md
//
// Inspired by MercuryMover.
// http://www.heliumfoot.com/mercurymover/
// Move and resize windows from the keyboard, positioning them precisely where you want.
//
// Inspired by @kgrossjo config.
// https://github.com/kgrossjo/phoenix-config/
Phoenix.set({
openAtLogin: true,
})
// ------------------------------------------------------------------------------
// Load config
function loadConfig() {
try {
require('./config.js')
return CONFIG
} catch (e) {
Phoenix.log('No config found.')
Phoenix.log(e)
return {}
}
}
const { PRESETS = [], MAIN_MODIFIERS = ['cmd', 'ctrl'] } = loadConfig()
Phoenix.log(`Loaded ${PRESETS.length} presets`)
// ------------------------------------------------------------------------------
// Const and globals.
const INCREMENT_LOW = 1
const INCREMENT_MID = 10
const INCREMENT_HIGH = 100
const MENU_BAR_HEIGHT = 22
const mainShortcuts = []
// ------------------------------------------------------------------------------
// Shortcut constructor.
class Shortcut {
constructor(key, modifiers, modalText) {
this.modal = Modal.build({ text: modalText, weight: 16 })
this.subShortcuts = []
this.keys = []
mainShortcuts.push(this)
this.key = new Key(key, modifiers, () => {
this.disableSubShortcuts()
this.enableSubShortcuts()
this.showModal()
})
}
showModal() {
this.closePreviouslyOpenedModals()
this.centerModalOnCurrentScreen()
this.modal.show()
}
closePreviouslyOpenedModals() {
// This is necessary because we have several main shortcuts and hitting
// them in a consecutive manner would result in multiple opened modals.
mainShortcuts.forEach(shortcut => {
if (shortcut !== this) {
shortcut.modal.close()
}
})
}
centerModalOnCurrentScreen() {
const screenFrame = Screen.main().frame()
const modalFrame = this.modal.frame()
this.modal.origin = {
x: screenFrame.x + ((screenFrame.width - modalFrame.width) / 2),
y: screenFrame.y + ((screenFrame.height - modalFrame.height) / 2),
}
}
enableSubShortcuts() {
const closeKey = new Key('escape', [], () => {
this.modal.close()
this.disableSubShortcuts()
})
closeKey.enable()
this.keys.push(closeKey)
this.subShortcuts.forEach(subShortcut => {
const key = new Key(subShortcut.key, subShortcut.modifiers, subShortcut.cb)
key.enable()
this.keys.push(key)
})
}
disableSubShortcuts() {
this.keys.forEach(function (key) {
key.disable()
})
this.keys = []
}
addSubShortcut(key, modifiers, cb) {
this.subShortcuts.push({key: key, modifiers: modifiers, cb: cb})
}
}
// ------------------------------------------------------------------------------
// Main shortcuts to activate `move` or `resize` mode.
const arrows = [
'↑',
'← →',
'↓',
].join('\n')
// Move mode.
const moveMode = new Shortcut(
'up',
MAIN_MODIFIERS,
[
'MOVE',
'',
arrows,
'',
'Hit esc to dismiss',
`Use no modifier key to move ${INCREMENT_LOW} pixel.`,
`Use the shift key to move ${INCREMENT_MID} pixels.`,
`Use the option key to move ${INCREMENT_HIGH} pixels.`,
'Use the cmd key to move to the edge of the screen.',
].join('\n')
)
// Resize mode (from right/down).
const resizeMode = new Shortcut(
'right',
MAIN_MODIFIERS,
[
'RESIZE',
'',
arrows,
'',
'Hit esc to dismiss',
`Use no modifier key to resize ${INCREMENT_LOW} pixel.`,
`Use the shift key to resize ${INCREMENT_MID} pixels.`,
`Use the option key to resize ${INCREMENT_HIGH} pixels.`,
'Use the cmd key to resize to the edge of the screen.',
].join('\n')
)
// ------------------------------------------------------------------------------
// Resize.
function resize(increment, direction) {
const window = Window.focused()
if (window) {
let size
switch (direction) {
case 'right':
size = { width: window.size().width + increment, height: window.size().height }
break
case 'left':
size = { width: window.size().width - increment, height: window.size().height }
break
case 'up':
size = { width: window.size().width, height: window.size().height - increment }
break
case 'down':
size = { width: window.size().width, height: window.size().height + increment }
break
}
window.setSize(size)
}
}
function resizeToEdge(direction) {
const window = Window.focused()
if (window) {
let frame
const screenFrame = window.screen().flippedFrame()
switch (direction) {
case 'right':
frame = {
x: window.topLeft().x,
y: window.topLeft().y,
width: screenFrame.width - Math.abs(screenFrame.x - window.topLeft().x),
height: window.size().height,
}
break
case 'left':
frame = {
x: screenFrame.x,
y: window.topLeft().y,
width: Math.abs(screenFrame.x - window.topLeft().x) + window.size().width,
height: window.size().height,
}
break
case 'up':
frame = {
x: window.topLeft().x,
y: screenFrame.y,
width: window.size().width,
height: Math.abs(window.screen().flippedVisibleFrame().y - window.topLeft().y) + window.size().height,
}
break
case 'down':
frame = {
x: window.topLeft().x,
y: window.topLeft().y,
width: window.size().width,
height: screenFrame.height - Math.abs(screenFrame.y - window.topLeft().y),
}
break
}
window.setFrame(frame)
}
}
resizeMode.addSubShortcut('right', [], function () { resize(INCREMENT_LOW, 'right') })
resizeMode.addSubShortcut('left', [], function () { resize(INCREMENT_LOW, 'left') })
resizeMode.addSubShortcut('up', [], function () { resize(INCREMENT_LOW, 'up') })
resizeMode.addSubShortcut('down', [], function () { resize(INCREMENT_LOW, 'down') })
resizeMode.addSubShortcut('right', ['shift'], function () { resize(INCREMENT_MID, 'right') })
resizeMode.addSubShortcut('left', ['shift'], function () { resize(INCREMENT_MID, 'left') })
resizeMode.addSubShortcut('up', ['shift'], function () { resize(INCREMENT_MID, 'up') })
resizeMode.addSubShortcut('down', ['shift'], function () { resize(INCREMENT_MID, 'down') })
resizeMode.addSubShortcut('right', ['alt'], function () { resize(INCREMENT_HIGH, 'right') })
resizeMode.addSubShortcut('left', ['alt'], function () { resize(INCREMENT_HIGH, 'left') })
resizeMode.addSubShortcut('up', ['alt'], function () { resize(INCREMENT_HIGH, 'up') })
resizeMode.addSubShortcut('down', ['alt'], function () { resize(INCREMENT_HIGH, 'down') })
resizeMode.addSubShortcut('right', ['cmd'], function () { resizeToEdge('right') })
resizeMode.addSubShortcut('left', ['cmd'], function () { resizeToEdge('left') })
resizeMode.addSubShortcut('up', ['cmd'], function () { resizeToEdge('up') })
resizeMode.addSubShortcut('down', ['cmd'], function () { resizeToEdge('down') })
// ------------------------------------------------------------------------------
// Move.
function move(increment, direction) {
const window = Window.focused()
if (window) {
let coords
switch (direction) {
case 'right':
coords = { x: window.topLeft().x + increment, y: window.topLeft().y }
break
case 'left':
coords = { x: window.topLeft().x - increment, y: window.topLeft().y }
break
case 'up':
coords = { x: window.topLeft().x, y: window.topLeft().y - increment }
break
case 'down':
coords = { x: window.topLeft().x, y: window.topLeft().y + increment }
break
}
window.setTopLeft(coords)
}
}
function moveToEdge(direction) {
const window = Window.focused()
if (window) {
let coords
const screenFrame = window.screen().flippedFrame()
switch (direction) {
case 'right':
coords = {
x: screenFrame.x + screenFrame.width - window.size().width,
y: window.topLeft().y,
}
break
case 'left':
coords = {
x: screenFrame.x,
y: window.topLeft().y,
}
break
case 'up':
coords = {
x: window.topLeft().x,
y: screenFrame.y,
}
break
case 'down':
coords = {
x: window.topLeft().x,
y: screenFrame.y + screenFrame.height - window.size().height,
}
break
}
window.setTopLeft(coords)
}
}
moveMode.addSubShortcut('right', [], function () { move(INCREMENT_LOW, 'right') })
moveMode.addSubShortcut('left', [], function () { move(INCREMENT_LOW, 'left') })
moveMode.addSubShortcut('up', [], function () { move(INCREMENT_LOW, 'up') })
moveMode.addSubShortcut('down', [], function () { move(INCREMENT_LOW, 'down') })
moveMode.addSubShortcut('right', ['shift'], function () { move(INCREMENT_MID, 'right') })
moveMode.addSubShortcut('left', ['shift'], function () { move(INCREMENT_MID, 'left') })
moveMode.addSubShortcut('up', ['shift'], function () { move(INCREMENT_MID, 'up') })
moveMode.addSubShortcut('down', ['shift'], function () { move(INCREMENT_MID, 'down') })
moveMode.addSubShortcut('right', ['alt'], function () { move(INCREMENT_HIGH, 'right') })
moveMode.addSubShortcut('left', ['alt'], function () { move(INCREMENT_HIGH, 'left') })
moveMode.addSubShortcut('up', ['alt'], function () { move(INCREMENT_HIGH, 'up') })
moveMode.addSubShortcut('down', ['alt'], function () { move(INCREMENT_HIGH, 'down') })
moveMode.addSubShortcut('right', ['cmd'], function () { moveToEdge('right') })
moveMode.addSubShortcut('left', ['cmd'], function () { moveToEdge('left') })
moveMode.addSubShortcut('up', ['cmd'], function () { moveToEdge('up') })
moveMode.addSubShortcut('down', ['cmd'], function () { moveToEdge('down') })
// ------------------------------------------------------------------------------
// Custom size/position shortcuts.
const maximise = () => {
const window = Window.focused()
if (window) {
window.maximise()
}
}
const center = () => {
const window = Window.focused()
if (window) {
const screenFrame = window.screen().flippedVisibleFrame()
window.setTopLeft({
x: parseInt(screenFrame.x + ((screenFrame.width - window.size().width) / 2), 10),
y: parseInt(screenFrame.y + ((screenFrame.height - window.size().height) / 2), 10),
})
}
}
addSubShortcutToMenus('m', maximise)
addSubShortcutToMenus('=', center)
// ------------------------------------------------------------------------------
// Load any presets define in presets.js
// Must be stored in a variable called PRESETS
function configurePresets() {
PRESETS.forEach(addPreset)
}
function addPreset({key, width, height, x, y, shortcut}) {
const defaultShortcut = () => {
const window = Window.focused()
if (window) {
window.setFrame({width, height, x, y})
}
}
addSubShortcutToMenus(key, shortcut || defaultShortcut)
}
function addSubShortcutToMenus(key, shortcut) {
mainShortcuts.forEach(mode => {
mode.addSubShortcut(key, [], shortcut)
})
}
configurePresets()