/
main.js
59 lines (47 loc) · 1.54 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
const { app, BrowserWindow, desktopCapturer, ipcMain } = require('electron');
const getColors = require('get-image-colors');
const colors = {};
// Fetch the test window.
const getWindow = async () => {
const sources = await desktopCapturer.getSources({ types: ['window'] });
const filtered = sources.filter(s => s.name === 'test-color-window');
if (filtered.length === 0) {
throw new Error('Could not find test window');
}
return filtered[0];
};
async function createWindow () {
const mainWindow = new BrowserWindow({
frame: false,
transparent: true,
vibrancy: 'under-window',
webPreferences: {
contextIsolation: false,
nodeIntegration: true
}
});
await mainWindow.loadFile('index.html');
// Get initial green background color.
const window = await getWindow();
const buf = window.thumbnail.toPNG();
const result = await getColors(buf, { count: 1, type: 'image/png' });
colors.green = result[0].hex();
}
ipcMain.on('set-transparent', async () => {
// Get updated background color.
const window = await getWindow();
const buf = window.thumbnail.toPNG();
const result = await getColors(buf, { count: 1, type: 'image/png' });
colors.transparent = result[0].hex();
const { green, transparent } = colors;
process.exit(green === transparent ? 1 : 0);
});
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});