/
main.js
113 lines (98 loc) · 2.44 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
const {
BrowserWindow,
app,
ipcMain,
Notification,
Tray,
nativeImage,
Menu,
dialog,
} = require("electron");
const path = require("path");
const isDev = !app.isPackaged;
let tray, mainWindow, menu, sound;
function createWindow() {
mainWindow = new BrowserWindow({
width: 420,
height: 10,
backgroundColor: "#023047",
show: false,
frame: false,
fullscreenable: false,
resizable: false,
transparent: false,
alwaysOnTop: true,
webPreferences: {
nodeIntegration: false,
worldSafeExecuteJavaScript: true,
contextIsolation: true,
preload: path.join(__dirname, "preload.js"),
},
});
mainWindow.loadFile("index.html");
}
function createTray() {
const icon = nativeImage.createFromPath("./assets/tangerine32@2x.png");
tray = new Tray(icon);
tray.setToolTip("EMP");
tray.on("click", () => toggleWindow());
tray.on("right-click", () => toggleMenu());
createMenu();
}
function createMenu() {
menu = Menu.buildFromTemplate([
{ label: "About" },
{ label: "Quit", click: () => app.quit() },
]);
}
function toggleWindow() {
mainWindow.isVisible() ? hideWindow() : showWindow();
}
function toggleMenu() {
tray.popUpContextMenu(menu);
}
function showWindow() {
const windowBounds = mainWindow.getBounds();
const trayBounds = tray.getBounds();
const x = Math.round(
trayBounds.x + (trayBounds.width / 2 - windowBounds.width / 2)
);
const y = Math.round(trayBounds.y + trayBounds.height);
mainWindow.setPosition(x, y);
mainWindow.show();
mainWindow.setSize(420, 560, true);
}
function hideWindow() {
mainWindow.hide();
mainWindow.setSize(420, 10);
}
if (isDev) {
require("electron-reload")(__dirname, {
electron: path.join(__dirname, "node_modules", ".bin", "electron"),
});
}
ipcMain.on("notify", (_, title, body) => {
new Notification({ title: title, body: body }).show();
});
ipcMain.on("startAudio", (_, path) => {
sound = require("sound-play");
sound.play(path);
});
async function handleFileOpen() {
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ["openFile", "multiSelections"],
filters: [{ name: "Audio", extensions: ["mp3", "wav"] }],
});
if (canceled) {
return;
} else {
return filePaths;
}
}
app.whenReady().then(() => {
ipcMain.handle("dialog:openFile", handleFileOpen);
app.dock.setIcon("./assets/tangerine192.png");
app.dock.hide();
createWindow();
createTray();
});