-
Notifications
You must be signed in to change notification settings - Fork 5
/
phoenix.ts
199 lines (174 loc) · 4.43 KB
/
phoenix.ts
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
import { log } from './utils/logger'
import * as spotify from './spotify'
import {
cycleWindowSplit,
cycleWindowPositions,
splitWindowLayout as layout,
moveToNextScreen,
moveToInternalDisplay,
swapAllWindowsBetweenDisplays,
gatherAllWindows,
centeredWindowPositions as centered,
gridPositions,
} from './window-grid'
import { getMainDisplay, initScreens, setMaxBrightnessOnDocking } from './screen'
import { showToast, titleModal } from './modal'
import { hyperCmd, hyper, HYPER } from './hyper'
import { getClipboard, setClipboard } from './utils/clipboard'
import { initWindowCaching } from './window-cache'
import { open } from './utils/open'
console.log('Phoenix Started')
titleModal('Config Loaded', { icon: App.get('Phoenix')?.icon() })
initScreens()
initWindowCaching()
Event.on('screensDidChange', () => {
initScreens()
setMaxBrightnessOnDocking()
})
hyperCmd('r', () => Phoenix.reload())
// App binds
bindApp(';', 'iTerm')
bindApp('g', 'Google Chrome')
bindApp('c', 'Visual Studio Code')
bindApp('r', 'Notion')
bindApp('t', 'ChatGPT')
bindApp('v', 'Spotify')
bindCycleApps('f', ['Slack', 'Discord'])
// Spotify Controls
hyper('\\', () => spotify.playOrPause())
hyper(']', () => spotify.nextTrack())
hyper('[', () => spotify.previousTrack())
// Window Manipulation
// Cycle centered position sizes
hyper(
'w',
cycleWindowPositions([
centered.full,
centered.big,
centered.med,
centered.sm,
centered.xs,
// centered.topHalf,
// centered.bottomHalf,
// centered.narrowLg,
// centered.narrowMd,
// centered.narrowSm,
]),
)
hyperCmd('w', cycleWindowSplit([layout.left50, layout.right50]))
hyper('1', cycleWindowSplit([layout.left33, layout.right66]))
// Cycle 2-window split, left side primary
hyper(
'q',
cycleWindowSplit([
layout.left66,
layout.left60,
layout.left50,
layout.left40,
layout.left33,
]),
)
// Cycle 2-window split, right side primary
hyper(
'e',
cycleWindowSplit([
layout.right33,
layout.right40,
layout.right50,
layout.right60,
layout.right66,
]),
)
// Cycle left side sizes
hyperCmd(
'q',
cycleWindowPositions([
gridPositions.left66,
gridPositions.left60,
gridPositions.left50,
gridPositions.left40,
gridPositions.left33,
]),
)
// Cycle right side sizes
hyperCmd(
'e',
cycleWindowPositions([
gridPositions.right33,
gridPositions.right40,
gridPositions.right50,
gridPositions.right60,
gridPositions.right66,
]),
)
hyper('tab', () => {
moveToNextScreen()
})
hyperCmd('tab', () => {
moveToInternalDisplay()
})
// hyperCmd('t', () => {
// gatherAllWindows()
// // swapAllWindowsBetweenDisplays()
// titleModal('Gathered all windows')
// })
/**
* Open https link from clipboard with proper protocol for desktop apps
* Supports: Discord, Notion
*/
hyper('o', async () => {
let link = await getClipboard()
log('clipboard:', link)
const linkReplacements = [
{
name: 'Discord',
includes: 'discord.com/channels/',
replaceProtocol: 'discord://',
},
{ name: 'Notion', includes: 'notion.so/', replaceProtocol: 'notion://' },
]
if (link.startsWith('https://')) {
linkReplacements.forEach(({ name, includes, replaceProtocol }) => {
if (link.includes(includes)) {
link = link.replace('https://', replaceProtocol)
// log('Opening link:', link)
showToast(`Opening ${name} link`)
}
})
setClipboard(link)
}
open(link)
})
// TODO: Gather all windows from single app
/**
* Launch or focus last app window
*/
function bindApp(key: string, appName: string) {
Key.on(key, HYPER, () => {
const app = App.get(appName)
if (!app || !app.windows().length) {
App.launch(appName, { focus: true })
} else if (
Window.focused()?.app().bundleIdentifier() === app.bundleIdentifier()
) {
// focus second to last window if it exists
app.windows()[1]?.focus() // TODO: Make this cycle all app windows
} else {
app.mainWindow()?.focus() // last used window
}
})
}
/**
* Cycle between 2 apps
*/
function bindCycleApps(key: string, appNames: string[]) {
Key.on(key, HYPER, () => {
const apps = appNames.map(a => App.get(a)).filter(Boolean)
const currentApp = App.focused().name()
if (currentApp === apps[0]?.name()) {
apps[1]?.focus() || App.launch(appNames[1], { focus: true })
} else {
apps[0]?.focus() || App.launch(appNames[0], { focus: true })
}
})
}