generated from catdad/electron-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
129 lines (105 loc) · 3.17 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
const path = require('path');
const url = require('url');
const iohook = require('iohook');
const { app, BrowserWindow, screen } = require('electron');
// See:
// https://stackoverflow.com/questions/54763647/transparent-windows-on-linux-electron
// https://github.com/electron/electron/issues/7076
// https://github.com/electron/electron/issues/16809
if (process.platform === 'linux') {
app.commandLine.appendSwitch('enable-transparent-visuals');
}
require('./lib/app-id.js')(app);
require('./lib/versions.js');
const icon = require('./lib/icon.js')();
const log = require('./lib/log.js')('main');
const config = require('./lib/config.js');
const tray = require('./lib/tray.js');
const WINDOWS = [];
const THEME = {
get: () => config.getProp('theme.palette'),
set: name => config.setProp('theme.palette', name)
};
function windowOptionsForDisplay(display) {
if (process.platform === 'linux') {
return display.workArea;
}
return {
x: display.bounds.x + 1,
y: display.bounds.y + 1
};
}
(async () => {
await Promise.all([
app.whenReady(),
config.read()
]);
// see Linux notes above
await new Promise(r => process.platform === 'linux' ? setTimeout(r, 1000) : r());
const displays = screen.getAllDisplays().map(display => {
const windowOptions = {
darkTheme: true,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false
},
icon: icon,
frame: false,
focusable: false,
skipTaskbar: true,
alwaysOnTop: true,
transparent: true,
...windowOptionsForDisplay(display),
};
// Create the browser window.
const window = new BrowserWindow(windowOptions);
window.setIgnoreMouseEvents(true, { forward: true });
window.maximize();
window.loadURL(url.format({
pathname: path.join(__dirname, 'public', 'index.html'),
protocol: 'file:',
slashes: true
}));
WINDOWS.push(window);
return { display, window, bounds: display.bounds };
});
iohook.on('mouseclick', (data) => {
if (data.button !== 1) {
return;
}
const { x, y } = data;
const dpiPoint = screen.screenToDipPoint ?
screen.screenToDipPoint({ x, y }) :
{ x, y };
log.info('CLICK', data, dpiPoint);
const { display, window } = displays.filter(({ bounds }) => {
return dpiPoint.x >= bounds.x && dpiPoint.x <= bounds.x + bounds.width &&
dpiPoint.y >= bounds.y && dpiPoint.y <= bounds.y + bounds.height;
})[0] || {};
if (window) {
window.webContents.send('asynchronous-message', {
command: 'draw',
x: (dpiPoint.x - display.bounds.x),
y: (dpiPoint.y - display.bounds.y),
palette: THEME.get()
});
}
});
iohook.start();
const destroyTray = tray({ theme: THEME });
app.once('before-quit', () => {
log.info('before-quit: cleanup starting');
iohook.unload();
destroyTray();
for (const window of WINDOWS) {
window.close();
}
log.info('before-quit: cleanup complete');
});
})().then(() => {
log.info('application is running');
}).catch(err => {
log.error('application has failed to start', err);
process.exitCode = 1;
});