-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
74 lines (63 loc) · 1.78 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
const {
app,
Tray,
nativeImage,
BrowserWindow,
globalShortcut,
} = require("electron");
const path = require("path");
let tray;
let window;
app.on("ready", () => {
// Initialise tray
let icon = nativeImage.createFromPath("./images/icon-crop.png");
icon = icon.resize({ width: 16, height: 16 });
tray = new Tray(icon);
tray.on("click", toggleWindowVisibility);
// Initialise menu
window = new BrowserWindow({
width: 320,
height: 240,
show: false,
frame: false,
resizable: false,
alwaysOnTop: true,
webPreferences: { nodeIntegration: true },
});
// Ensure window appears on top of fullscreen applications
window.setAlwaysOnTop(true, "floating");
window.setVisibleOnAllWorkspaces(true);
window.fullScreenable = false;
// Set the window's content
window.loadURL(path.join("file://", __dirname, "index.html"));
window.on("blur", () => {
if (!window.webContents.isDevToolsOpened()) {
window.hide();
}
});
// Configure keyboard shortcuts
globalShortcut.register("CommandOrControl+P", () => {
window.webContents.send("togglepause");
});
globalShortcut.register("CommandOrControl+L", () => {
window.webContents.send("switchchannels");
});
});
const toggleWindowVisibility = () => {
if (window.isVisible()) {
window.hide();
} else {
app.dock.hide(); // Needed to ensure window appears on top of fullscreen apps
showWindow();
}
};
const showWindow = () => {
const trayPos = tray.getBounds();
const windowPos = window.getBounds();
const yScale = process.platform == "darwin" ? 1 : 10;
const x = Math.round(trayPos.x + trayPos.width / 2 - windowPos.width / 2);
const y = Math.round(trayPos.y + trayPos.height * yScale);
window.setPosition(x, y + 16, false);
window.show();
window.focus();
};