-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
417 lines (338 loc) · 11.9 KB
/
App.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
import * as utils from './utils.js';
/*
Config settings for Modos.
*/
var config;
var configIsLoaded = false;
const loadConfig = async () => {
const configString = await utils.loadJSON('config.json');
console.log(`Config: ${config}`);
config = JSON.parse(configString);
};
loadConfig();
/**
* Returns the local path of the app icon.
* @param {string} appName The app name
*/
const getAppIcon = appName => {
console.log(`Loading icon for ${appName}`)
// if (!configIsLoaded) await loadConfig();
// const iconPath =
return `./assets/app-icons/${appName}.png`;
}
/**
* The modes that should match BTT's configuration,
* each one for a separate macOS space/desktop.
*
* CHANGE THIS TO MATCH YOUR SETUP.
*/
const MODES = [
"General",
"Developer",
"Designer",
"Music"
];
/**
* How often (in milliseconds) to poll data about running app, windows, modes.
*/
const REFRESH_RATE = 500;
var currentAppName;
var currentWindowTitle;
var currentNumberOfWindows = 0;
var currentMode = '';
var enabled = false;
var focused = false;
/*
POLLING INFO FROM APPLESCRIPTS
*/
async function refreshCurrentAppInfo() {
const spinner = document.getElementById("loading-spinner");
if (spinner.classList.contains('show')) {
spinner.classList.remove('show');
spinner.classList.add('hide');
}
let appleScript = `
set windowName to "..."
set currentAppName to ""
set currentAppTitle to "Loading..."
set numberOfWindows to "0"
try
tell application "System Events"
set currentApp to first application process whose frontmost is true
set currentAppName to name of currentApp
tell process currentAppName
tell (1st window whose value of attribute "AXMain" is true)
set windowName to value of attribute "AXTitle"
end tell
set currentAppTitle to title of currentApp
set numberOfWindows to count of windows
end tell
end tell
end try
return {currentAppTitle, currentAppName, windowName, numberOfWindows}
`;
// @ts-ignore
let result = await runAppleScript({ script: appleScript });
let [appTitle, appName, windowTitle, numberOfWindows] = eval(result.replace('{', '[').replace('}', ']'));
// What changed ?
const appChanged = currentAppName !== appName;
const windowChanged = currentWindowTitle !== windowTitle;
const numberOfWindowsChanged = currentNumberOfWindows !== numberOfWindows;
// Set new values
currentAppName = appName;
currentWindowTitle = windowTitle;
currentNumberOfWindows = numberOfWindows;
if (appChanged) {
document.getElementById("current-app-name").textContent = `${appTitle}`;
document.getElementById("current-app-window-count-container").classList.add('show');
document.getElementById("current-app-window-count-container").classList.remove('hide');
document.getElementById("current-app-window-count").textContent = numberOfWindows;
document.getElementById("current-window-name").textContent = appTitle === windowTitle ? "" : windowTitle;
document.getElementById("current-app-icon").setAttribute('src', getAppIcon(appTitle));
} else if (windowChanged) {
document.getElementById("current-app-window-count-container").classList.add('show');
document.getElementById("current-app-window-count-container").classList.remove('hide');
document.getElementById("current-app-window-count").textContent = numberOfWindows;
document.getElementById("current-window-name").textContent = appTitle === windowTitle ? "" : windowTitle;
} else if (numberOfWindowsChanged) {
document.getElementById("current-app-window-count").textContent = numberOfWindows;
}
}
/*
AUTOMATIC UPDATES
*/
/**
* Refreshes the enabled/disabled status.
*/
async function refreshEnabledStatus() {
// @ts-ignore
try {
let result = await callBTT('get_string_variable', { variable_name: 'customVariable3' });
let newEnabled = result === 'enabled';
const changed = enabled !== newEnabled;
if (changed) {
enabled = newEnabled;
// Update UI
const onOff = document.getElementById('on-off');
if (enabled) {
onOff.classList.add('on');
onOff.classList.remove('off');
} else {
onOff.classList.add('off');
onOff.classList.remove('on');
}
}
} catch(e) {
}
}
/**
* Refreshes the focused/default status.
*/
async function refreshFocusedStatus() {
// @ts-ignore
try {
let result = await callBTT('get_string_variable', { variable_name: 'customVariable2' });
let newFocused = result === 'focus';
const changed = focused !== newFocused;
if (changed) {
focused = newFocused;
// Update UI
const toolbar = document.getElementById('modos-toolbar');
if (focused) {
toolbar.classList.add('hide');
toolbar.classList.remove('show');
} else {
toolbar.classList.add('show');
toolbar.classList.remove('hide');
}
}
} catch(e) {
}
}
/**
* Refreshes the current window preset mode.
*/
async function refreshCurrentMode() {
// @ts-ignore
let newMode = await callBTT('get_string_variable', { variable_name: 'customVariable1' });
const changed = currentMode !== newMode;
if (changed) {
currentMode = newMode;
refreshModeList();
}
}
function setModeList() {
// Set the modes
var modeList = document.getElementById("mode-list");
MODES.forEach(mode => {
let child = document.createElement('li');
child.className = 'mode-list-item';
let content = document.createElement('h3');
content.innerText = mode;
if (mode === currentMode) {
child.id = 'current-mode';
}
child.appendChild(content);
// Assign mode selector
child.onclick = () => selectMode(mode);
modeList.appendChild(child);
});
}
function refreshModeList() {
// Set the modes
var modeList = document.getElementById("mode-list");
MODES.forEach((mode, index) => {
let child = modeList.children[index];
if (mode === currentMode) {
child.id = 'current-mode';
} else {
child.id = null;
}
});
}
/*
BTT LIFECYCLE HOOKS
*/
/* This is called after the webview content has loaded*/
function BTTInitialize() {
}
/* This is called before the webview exits and destroys its content*/
function BTTWillCloseWindow() {
}
/* This is called before the webview hides*/
function BTTWillHideWindow() {
}
/* This is called when the webview becomes visible*/
function BTTWindowWillBecomeVisible() {
}
/* This is called when a script variable in BTT changes. */
function BTTNotification(note) {
let data = JSON.parse(note);
console.log(data.note, data.name);
}
/*
SYSTEM ACTIONS
*/
async function showNotification(title, subtitle, text) {
let shellScript = `osascript -e 'display notification \"${text}\" with title \"${title}\" subtitle \"${subtitle}\" sound name "Pop"'`;
let shellScriptWrapper = {
script: shellScript, // mandatory
launchPath: '/bin/bash', //optional - default is /bin/bash
parameters: '-c', // optional - default is -c
environmentVariables: '' //optional e.g. VAR1=/test/;VAR2=/test2/;
};
//@ts-ignore
await runShellScript(shellScriptWrapper);
}
/*
USER ACTIONS
*/
async function selectMode(mode) {
//@ts-ignore
callBTT('set_string_variable', { variable_name: 'customVariable1', to: mode });
}
export async function savePreset() {
console.log('Save preset');
let actionDefinition = {
"BTTPredefinedActionType": 105,
"BTTPredefinedActionName": "Show BTT Preferences",
};
//@ts-ignore
let result = await callBTT('trigger_action', { json: JSON.stringify(actionDefinition) });
console.log(result);
if (result === "success") {
await showNotification("Modos", "BetterTouchTool", `${currentMode} window preset saved!`);
}
}
export async function restorePreset() {
console.log('Restore preset');
let actionDefinition = {
"BTTTriggerType": -1,
"BTTTriggerClass": "BTTTriggerTypeOtherTriggers",
"BTTPredefinedActionType": 268,
"BTTPredefinedActionName": "Save \/ restore specific window layout",
"BTTWindowLayoutName": currentMode
};
//@ts-ignore
let result = await callBTT('trigger_named', { trigger_name: `Restore Layout` });
console.log(result);
if (result === "success") {
await showNotification("Modos", "BetterTouchTool", `${currentMode} window preset restored!`);
}
}
export async function closeWebView() {
await callBTT('trigger_named', { trigger_name: 'test', closeFloatingWebView: 1 });
}
function showKeyboardControlsHint(show) {
if (show) {
// Show keyboard controls hint
var keyboardControlsLeft = document.getElementById("keyboard-controls-left");
var keyboardControlsRight = document.getElementById("keyboard-controls-right");
keyboardControlsLeft.classList.add("show");
keyboardControlsLeft.classList.remove("hide");
keyboardControlsRight.classList.add("show");
keyboardControlsRight.classList.remove("hide");
} else {
// Hide keyboard controls hint
var keyboardControlsLeft = document.getElementById("keyboard-controls-left");
var keyboardControlsRight = document.getElementById("keyboard-controls-right");
keyboardControlsLeft.classList.add("hide");
keyboardControlsLeft.classList.remove("show");
keyboardControlsRight.classList.add("hide");
keyboardControlsRight.classList.remove("show");
}
}
// Check the current app every 2 seconds
setInterval(async () => {
console.log('Getting current app...')
await refreshCurrentAppInfo();
await refreshEnabledStatus();
await refreshFocusedStatus();
await refreshCurrentMode(); // It would be great if BTT could have reactive hooks so we don't have to poll.
}, REFRESH_RATE);
// Toolbar on click
var toolbarSelected = false;
var toolbarElement = document.getElementById("modos-toolbar");
toolbarElement.classList.remove('fade-out');
toolbarElement.classList.add('fade-in');
toolbarElement.classList.add('modos-toolbar-default');
toolbarElement.onclick = (mouseEvent) => {
console.log('CLICK')
toolbarSelected = !toolbarSelected;
if (toolbarSelected) {
// Show highlight
toolbarElement.classList.add("modos-toolbar-selected");
toolbarElement.classList.remove("modos-toolbar-default");
showKeyboardControlsHint(true);
} else {
toolbarElement.classList.add('modos-toolbar-default');
toolbarElement.classList.remove('modos-toolbar-selected');
showKeyboardControlsHint(false);
}
}
document.getElementById('save-button').onclick = savePreset;
document.getElementById('restore-button').onclick = restorePreset;
document.getElementById('close-button').onclick = closeWebView;
// Key listeners
document.onkeydown = checkKey;
function checkKey(e) {
e = e || window.event;
if (toolbarSelected) {
if (e.keyCode == '38') {
// up arrow
}
else if (e.keyCode == '40') {
// down arrow
}
else if (e.keyCode == '37') {
// left arrow
selectMode(MODES[Math.max(0, MODES.indexOf(currentMode) - 1)]);
}
else if (e.keyCode == '39') {
// right arrow
selectMode(MODES[Math.min(MODES.length - 1, MODES.indexOf(currentMode) + 1)]);
}
}
}
setModeList();
showKeyboardControlsHint(false);