-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.js
86 lines (73 loc) · 1.96 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
const {
systemPreferences, app, BrowserWindow, ipcMain, Tray, nativeImage
} = require('electron');
const path = require('path');
const fixPath = require('fix-path');
// Fix GUI apps on macOS doesn't inherit the $PATH defined in your dotfiles
fixPath();
let tray;
let window;
app.dock.hide();
app.on('ready', () => {
const icon = nativeImage.createFromPath(path.join(__dirname, 'assets/menubarIconTemplate.png'));
tray = new Tray(icon);
tray.on('click', (event) => {
toggleWindow();
// window.openDevTools({ mode: 'detach' });
});
// Make the popup window for the menubar
window = new BrowserWindow({
width: 250,
height: 30,
show: false,
frame: false,
transparent: true,
resizable: false
});
// Tell the popup window to load our index.html file
window.loadURL(`file://${path.join(__dirname, 'index.html')}`);
// Only close the window on blur if dev tools isn't opened
window.on('blur', () => {
window.hide();
});
window.on('show', () => {
tray.setHighlightMode('always');
});
window.on('hide', () => {
tray.setHighlightMode('never');
});
});
const toggleWindow = () => {
if (window.isVisible()) {
window.hide();
} else {
showWindow();
}
};
const showWindow = () => {
const trayPos = tray.getBounds();
const windowPos = window.getBounds();
let x;
let y;
if (process.platform === 'darwin') {
x = Math.round(trayPos.x);
y = Math.round(trayPos.y + trayPos.height);
} else {
x = Math.round(trayPos.x + (trayPos.width / 2) - (windowPos.width / 2));
y = Math.round(trayPos.y + trayPos.height * 10);
}
const vibrancy = systemPreferences.isDarkMode() ? 'dark' : 'medium-light';
window.setPosition(x, y, false);
window.setVibrancy(vibrancy);
window.webContents.send('vibrancy', vibrancy);
window.show();
window.focus();
};
ipcMain.on('show-window', () => {
showWindow();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});