-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
139 lines (123 loc) · 3.25 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
130
131
132
133
134
135
136
137
138
139
const {
app,
dialog,
ipcMain,
BrowserWindow,
Menu,
Tray,
MenuItem,
} = require("electron");
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit();
return;
}
const process = require("process");
const path = require("path");
const isDev = process.env.NODE_ENV === "development";
const iconPath = path.join(__dirname, "/public/favicon.ico");
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
minWidth: 480,
minHeight: 360,
frame: false,
// transparent: true,
icon: path.join(__dirname, "/public/favicon.ico"),
webPreferences: {
preload: path.join(__dirname, "preload.js"),
},
});
if (isDev) {
win.loadURL("http://localhost:4000");
} else {
win.loadFile("./webContent/index.html");
}
// 设置托盘
const appTray = new Tray(iconPath);
const trayMenu = new Menu();
trayMenu.append(
new MenuItem({
type: "normal",
label: "退出",
click: app.quit,
})
);
appTray.setContextMenu(trayMenu);
appTray.setToolTip("I Player");
appTray.on("double-click", () => {
win.show();
});
win.on("maximize", () => {
win.webContents.send("win-maximize");
});
win.on("unmaximize", () => {
win.webContents.send("win-unmaximize");
});
// 打开文件浏览器
ipcMain.on("open-files", async (evt) => {
const { filePaths } = await dialog.showOpenDialog(win, {
properties: ["openFile", "multiSelections"],
filters: [
{
name: "Audio Files",
extensions: [
"ipus",
"flac",
"webm",
"weba",
"wav",
"ogg",
"m4a",
"oga",
// "mid",
"mp3",
"aiff",
"wma",
"au",
],
},
],
});
evt.sender.send("open-files-callback", filePaths);
});
// 窗口最大化 / 恢复
ipcMain.on("toggle-maximize", () => {
if (win.isMaximized()) {
win.unmaximize();
} else {
win.maximize();
}
});
// 窗口最小化
ipcMain.on("minimize", () => {
win.minimize();
});
// 隐藏窗口
ipcMain.on("hide", () => {
win.hide();
});
return win;
}
app.whenReady().then(() => {
let myWin = createWindow();
app.on("activate", () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
myWin = createWindow();
}
});
app.on("second-instance", () => {
if (myWin) {
myWin.show();
myWin.focus();
}
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
});