-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·445 lines (391 loc) · 13.6 KB
/
main.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
// electron module
const electron = require('electron');
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
const ipcMain = electron.ipcMain;
const Tray = electron.Tray;
const Menu = electron.Menu;
// other modules
const fs = require('fs'); // fs used for user settings
const path = require('path');
const url = require('url');
const robot = require('robotjs');
// my modules
const {menu, preferencesWindow, openPreferences} = require('./menu.js');
const song = require('./HereIGoAgain.js');
let tray = null;
if (app.dock) {
app.dock.setIcon(path.join(__dirname, 'images/trayIconSpread512.png'));
}
console.log(`app.isUnityRunning()? ${app.isUnityRunning()}`)
console.log(app);
const defaultSettings = {
startTimeout: 0,
timeoutUnit: 'm', // can be s or m or h
startTimeoutMS: 0,
alwaysOnTop: false,
clickThrough: false,
clickToCloseIdle: true,
pressAnyKeyToCloseIdle: true,
openAtLogin: false,
idleMode: 'og',
activeMode: 'none',
showDockIcon: false,
};
const windows = new Map();
const readSettings = function () {
const file = `${app.getPath('userData')}/userSettings.json`;
return new Promise((resolve, reject) => {
fs.readFile(file, 'utf8', (err, data) => {
if (err) {
if (err.code == 'ENOENT') {
fs.writeFile(file, JSON.stringify(defaultSettings), err => {
if (err) reject(err);
});
resolve(defaultSettings);
}
else {
reject(err);
}
} else {
resolve(JSON.parse(data));
}
});
});
};
ipcMain.on('preview idle', (event, previewSettings) => {
if (windows.has('swyzzleWindow')) {
windows.get('swyzzleWindow').close();
}
createSwyzzleWindow('idle', previewSettings);
});
ipcMain.on('preview active', (event, previewSettings) => {
if (windows.has('swyzzleWindow')) {
windows.get('swyzzleWindow').close();
}
createSwyzzleWindow('active', previewSettings);
});
ipcMain.on('shader error', (event, arg) => {
console.log('shader error', arg);
});
ipcMain.on('program error', (event, arg) => {
console.log('program error', arg);
});
ipcMain.on('save settings', (event, arg) => {
saveSettings(arg);
});
function saveSettings(settings = global.settings, resetWindows = true) {
// set the app to open/not open on login (only supported on macOS) (and very slowwwww -- seems to block the rest of the app) so we only call this if it actually changed
if (settings.openAtLogin !== global.settings.openAtLogin) {
app.setLoginItemSettings({
openAtLogin: global.settings.openAtLogin
});
}
global.settings = settings;
if (global.settings.showDockIcon) {
app.dock.show();
} else {
app.dock.hide();
}
// save the settings to disk
fs.writeFile(`${app.getPath('userData')}/userSettings.json`, JSON.stringify(settings), err => {
if (err) throw err;
});
if (resetWindows) {
if (windows.has('swyzzleWindow')) {
windows.get('swyzzleWindow').close();
}
resetIdleTimeout();
if (global.settings.activeMode !== 'none') initActive();
}
console.log('new global settings', global.settings);
}
// Get arrays of all the active and idle idle shaders by looing in the filesystem
const idleModes = function() {
return new Promise((resolve, reject) => {
fs.readdir(path.join(__dirname, '/shaders/idle'), (err, files) => {
if (err) { console.error(err); reject(err); }
global.idleModes = files;
resolve(files);
});
});
};
console.log(global.idleModes);
const activeModes = function() {
return new Promise((resolve, reject) => {
fs.readdir(path.join(__dirname, '/shaders/active'), (err, files) => {
if (err) { console.error(err); reject(err); }
global.activeModes = files;
resolve(files);
});
});
};
let cursorInterval;
/**
* Creates and opens the idle window, which is the size of the primary display.
* We never have both idle and active windows open at the same time, to avoid too much GPU activity.
*
* @param {string} swyzzleType can be either 'active' or 'idle'
* @param {object} settings [settings=global.settings]
*/
function createSwyzzleWindow(swyzzleType, settings = global.settings) {
if (windows.has('swyzzleWindow')) {
windows.get('swyzzleWindow').close();
}
console.log('created new window with settings', settings);
if (settings[`${swyzzleType}Mode`] === 'none') return;
app.focus();
const displays = electron.screen.getAllDisplays();
// as of 11/2016, robotjs only supports the main display
const activeDisplay = displays[0];
const width = activeDisplay.workArea.width;
const height = activeDisplay.workArea.height;
// Create the browser window.
windows.set('swyzzleWindow', new BrowserWindow({
show: false, // show the window gracefully
width: width,
height: height,
transparent: true,
frame: false,
x: activeDisplay.bounds.x,
y: activeDisplay.bounds.y,
}));
windows.get('swyzzleWindow').settings = settings;
windows.get('swyzzleWindow').swyzzleType = swyzzleType;
// active swyzzle window can always be clicked through, and mouse events are always ignored
if (swyzzleType === 'active') {
windows.get('swyzzleWindow').setIgnoreMouseEvents(true);
windows.get('swyzzleWindow').setAlwaysOnTop(true);
} else {
windows.get('swyzzleWindow').setIgnoreMouseEvents(settings.clickThrough);
windows.get('swyzzleWindow').setAlwaysOnTop(settings.alwaysOnTop);
}
// and load the index.html of the app.
windows.get('swyzzleWindow').loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
// capture the screen and send it after a timeout
// on Macs, the window can't go all the way to the top because the menu panel bar up there
const workArea = electron.screen.getPrimaryDisplay().workArea;
let screenCapture = robot.screen.capture(workArea.x, workArea.y, workArea.width, workArea.height);
const screenCaptureTimeout = setTimeout(() => {
console.log('sent screen capture');
// check that it exists, in case swyzzle window was closed during that half second
if (windows.has('swyzzleWindow'))
windows.get('swyzzleWindow').webContents.send('screen', screenCapture);
}, 500);
let cursorPos, cursorColor, cursorRGB;
if (cursorInterval) clearInterval(cursorInterval);
cursorInterval = setInterval(() => {
var mouse = electron.screen.getCursorScreenPoint();
cursorPos = mouse;
if ((cursorPos.x >= activeDisplay.workArea.x && cursorPos.y >= activeDisplay.workArea.y) &&
(cursorPos.x <= activeDisplay.workArea.x + width && cursorPos.y <= activeDisplay.workArea.y + height)
) {
cursorColor = robot.getPixelColor(cursorPos.x, cursorPos.y);
// split up color values and convert to 0 -> 1
cursorRGB = {
r: parseInt(cursorColor[0].concat(cursorColor[1]), 16) / 255,
g: parseInt(cursorColor[2].concat(cursorColor[3]), 16) / 255,
b: parseInt(cursorColor[4].concat(cursorColor[5]), 16) / 255
};
if (windows.has('swyzzleWindow')) windows.get('swyzzleWindow').webContents.send('cursor', { pos: cursorPos, color: cursorRGB });
}
}, 16);
// gracefully show the main window
windows.get('swyzzleWindow').once('ready-to-show', () => {
if (windows.has('swyzzleWindow')) windows.get('swyzzleWindow').show();
});
// Emitted when the window is closed.
windows.get('swyzzleWindow').on('closed', function () {
// Dereference the window object
windows.delete('swyzzleWindow');
if (screenCaptureTimeout) clearTimeout(screenCaptureTimeout);
console.log('closed the main window of type', swyzzleType);
// if the window that just closed was the idle window, initiate active, if it exists
if (global.settings.activeMode !== 'none' && swyzzleType === 'idle') initActive();
// startWaitingForIdle();
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
const appReady = function () {
return new Promise((resolve, reject) => {
app.on('ready', () => {
resolve();
});
});
};
let activeModesMenu, idleModesMenu; // for use in the tray
Promise.all([appReady(), readSettings(), idleModes(), activeModes()]).then(values => {
console.log('app ready sir.')
openPreferences();
global.settings = values[1];
if (!global.settings.showDockIcon) app.dock.hide();
// cut off the .js from idleModes and activeModes arrays of file names
for (let i = 2; i <= 3; i++) {
values[i] = values[i].map(file => file.match(/(.*)\.js$/)[1] );
values[i].unshift('none');
}
idleModesMenu = values[2].map(mode => ({
label: mode,
type: 'radio',
checked: global.settings.idleMode == mode ? true : false,
click: mode => {
global.settings.idleMode = mode.label;
saveSettings(global.settings, false);
}
}));
activeModesMenu = values[3].map(mode => ({
label: mode,
type: 'radio',
checked: global.settings.activeMode == mode ? true : false,
click: mode => {
// if the active mode didn't change, we dont want to reset the window
const boo = global.settings.activeMode !== mode.label;
global.settings.activeMode = mode.label;
saveSettings(global.settings, boo);
}
}));
createTray();
electron.Menu.setApplicationMenu(menu);
if (global.settings.activeMode !== 'none') initActive();
startWaitingForIdle();
});
let iteration = 0;
const nestSubmenus = (mySubmenus) => {
if (iteration > song.length) return mySubmenus;
iteration+=2;
return (
[{ label: song[iteration] },
{ type: 'separator' },
{
label: song[iteration+1],
submenu: nestSubmenus(mySubmenus)
},
]
);
};
let lotsaSubmenus = [];
lotsaSubmenus = nestSubmenus(lotsaSubmenus);
function createTray() {
tray = new Tray(path.join(__dirname, 'images/trayIconSpread.png'));
const contextMenu = Menu.buildFromTemplate([
{ label: 'Quit Swyzzle', role: 'quit' },
{ role: 'close' },
{ type: 'separator' },
{ label: 'Active Swyzzle', submenu: activeModesMenu},
{ type: 'separator' },
{ label: 'Idle Swyzzle', submenu: idleModesMenu},
{ label: 'Preview Idle Swyzzle', click: () => {
createSwyzzleWindow('idle', global.settings);
} },
{ type: 'separator' },
{
label: 'Preferences...',
click: openPreferences
},
{
label: 'I wonder',
submenu: [{
label: "what's",
submenu: [{
label: 'in here?',
submenu: [{
label: "Was that a question?",
submenu: [{
label: "I think you know what's down this road.",
submenu: [
{ label: "Goin down", click: openYoutube },
{ label: "the only road", click: openYoutube },
{ label: "I've ever known.", click: openYoutube },
{ type: 'separator' },
{
label: 'If...',
submenu: [
{
label: "you've already been down that road",
submenu: [
{
label: "and instead of immediately playing the song it opened to a commercial",
submenu: [
{ label: "that's really unfortunate." },
{ type: 'separator' },
{
label: "Let's see how deep we can nest these submenus.", submenu: lotsaSubmenus
}
]
}
]
}
]
}
],
}],
}, {
type: 'separator'
},
]
}]
}]
}
]);
tray.setContextMenu(contextMenu);
tray.setToolTip('Swyzzle Fo Shyzzle');
}
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
// if (process.platform !== 'darwin') {
// app.quit()
// }
});
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (!windows.has('swyzzleWindow')) {
if (global.settings.activeMode !== 'none') initActive();
}
});
function openYoutube() {
const win = new BrowserWindow({ width: 800, height: 600 });
win.loadURL('https://youtu.be/i3MXiTeH_Pg?t=1m17s');
}
// get the cursor position every 100ms and check if it has moved.
// this is a hacky way to go about getting system idle timeout
// TODO better method of getting idle time
function startWaitingForIdle() {
let lastMouse = { x: 0, y: 0 };
setInterval(() => {
var mouse = electron.screen.getCursorScreenPoint();
// if you moved the mouse reset the countdown
if (lastMouse.x !== mouse.x || lastMouse.y !== mouse.y) {
resetIdleTimeout();
}
lastMouse = mouse;
}, 100);
}
function initActive() {
if (windows.has('swyzzleWindow')) {
windows.get('swyzzleWindow').close();
}
createSwyzzleWindow('active');
}
let initTimeout;
function resetIdleTimeout() {
if (initTimeout) clearTimeout(initTimeout);
initTimeout = setTimeout(() => {
if (windows.has('swyzzleWindow')) {
windows.get('swyzzleWindow').close();
}
createSwyzzleWindow('idle');
}, global.settings.startTimeoutMS);
}
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.