forked from yehuozhili/bigbear-mindmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
272 lines (266 loc) · 6.35 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
const { app, BrowserWindow, globalShortcut, dialog } = require("electron");
const isDev = require("electron-is-dev");
const { Menu, ipcMain } = require("electron");
const { shell } = require("electron");
const path = require("path");
const { autoUpdater } = require("electron-updater");
let mainWindow;
let settingwindow;
app.on("ready", () => {
autoUpdater.autoDownload = false;
autoUpdater.checkForUpdatesAndNotify();
autoUpdater.on("error", (error) => {
dialog.showErrorBox(
"Error: ",
error == null ? "unknown" : (error.stack || error).toString()
);
});
autoUpdater.on("checking-for-update", () => {
console.log("Checking for update...");
});
autoUpdater.on("update-available", () => {
dialog.showMessageBox(
{
type: "info",
title: "应用有新的版本",
message: "发现新版本,是否现在更新?",
buttons: ["是", "否"],
},
(buttonIndex) => {
if (buttonIndex === 0) {
autoUpdater.downloadUpdate();
}
}
);
});
autoUpdater.on("update-not-available", () => {
mainWindow.webContents.send("update_msg", "当前已经是最新版本");
});
autoUpdater.on("download-progress", (progressObj) => {
let log_message = "Download speed: " + progressObj.bytesPerSecond;
log_message =
log_message + " - Downloaded " + progressObj.percent + "%";
log_message =
log_message +
" (" +
progressObj.transferred +
"/" +
progressObj.total +
")";
console.log(log_message);
mainWindow.webContents.send("update_msg", log_message);
});
autoUpdater.on("update-downloaded", () => {
dialog.showMessageBox(
{
title: "安装更新",
message: "更新下载完毕,应用将重启并进行安装",
},
() => {
setImmediate(() => autoUpdater.quitAndInstall());
}
);
});
mainWindow = new BrowserWindow({
width: 1200,
height: 680,
webPreferences: {
nodeIntegration: true,
},
});
globalShortcut.register("CommandOrControl+S", () => {
mainWindow.webContents.send("save-edit-file");
});
globalShortcut.register("CommandOrControl+N", () => {
mainWindow.webContents.send("create-new-file");
});
globalShortcut.register("CommandOrControl+I", () => {
mainWindow.webContents.send("import-file");
});
const urlLocation = isDev
? "http://localhost:3000"
: `file://${path.join(__dirname, "./build/index.html")}`;
mainWindow.loadURL(urlLocation);
ipcMain.on("open-settings", () => {
settingwindow = new BrowserWindow({
width: 500,
height: 400,
webPreferences: {
nodeIntegration: true,
},
show: false,
backgroundColor: "#efefef",
});
settingwindow.once("ready-to-show", () => {
settingwindow.show();
});
const settingsFileLocation = `file://${path.join(
__dirname,
"./settings/settings.html"
)}`;
settingwindow.loadURL(settingsFileLocation);
settingwindow.removeMenu();
settingwindow.on("closed", () => {
settingwindow = null;
});
});
});
//当所有窗口都被关闭后退出
app.on("window-all-closed", () => {
// 在 macOS 上,除非用户用 Cmd + Q 确定地退出,
// 否则绝大部分应用及其菜单栏会保持激活。
if (process.platform !== "darwin") {
app.quit();
}
});
const isMac = process.platform === "darwin";
const template = [
// { role: 'appMenu' }
...(isMac
? [
{
label: app.name,
submenu: [
{ role: "about" },
{ type: "separator" }, //
{ role: "services" },
{ type: "separator" },
{ role: "hide" },
{ role: "hideothers" },
{ role: "unhide" },
{ type: "separator" },
{ role: "quit" },
],
},
]
: []),
// { role: 'fileMenu' }
{
label: "文件",
submenu: [
{
label: "新建",
accelerator: "CommandOrControl+N",
click: (menuItem, browserWindow, event) => {
browserWindow.webContents.send("create-new-file");
},
},
{
label: "保存",
accelerator: "CommandOrControl+S",
click: (menuItem, browserWindow, event) => {
browserWindow.webContents.send("save-edit-file");
},
},
{
label: "导入",
accelerator: "CommandOrControl+I",
click: (menuItem, browserWindow, event) => {
browserWindow.webContents.send("import-file");
},
},
{
label: "设置",
click: (menuItem, browserWindow, event) => {
ipcMain.emit("open-settings");
},
},
isMac ? { role: "close" } : { role: "quit" },
],
},
// { role: 'editMenu' }
{
label: "编辑",
submenu: [
{ role: "undo" },
{ role: "redo" },
{ type: "separator" },
{ role: "cut" },
{ role: "copy" },
{ role: "paste" },
...(isMac
? [
{ role: "pasteAndMatchStyle" },
{ role: "delete" },
{ role: "selectAll" },
{ type: "separator" },
{
label: "Speech",
submenu: [
{ role: "startspeaking" },
{ role: "stopspeaking" },
],
},
]
: [
{ role: "delete" },
{ type: "separator" },
{ role: "selectAll" },
]),
],
},
// { role: 'viewMenu' }
{
label: "菜单",
submenu: [
{
label: "刷新页面",
role: "reload",
},
{
label: "强制刷新页面",
role: "forcereload",
},
{
label: "开发者工具",
role: "toggledevtools",
},
{ type: "separator" },
{ role: "resetzoom" },
{ role: "zoomin" },
{ role: "zoomout" },
{ type: "separator" },
{ label: "切换全屏", role: "togglefullscreen" },
],
},
// { role: 'windowMenu' }
{
label: "窗口",
submenu: [
{ role: "minimize" },
...(isMac
? [
{ type: "separator" },
{ role: "front" },
{ type: "separator" },
{ role: "window" },
]
: [{ role: "close" }]),
],
},
{
label: "帮助",
submenu: [
{
label: "关于我",
click: async () => {
const { dialog } = require("electron");
dialog.showMessageBox({
title: "作者yehuozhili",
message: "使用过程中若有问题,联系微信号h1637830449",
});
},
},
{
label: "github",
click: () => {
shell.openExternal(
"https://github.com/yehuozhili/bigbear-mindmap"
);
},
},
],
},
];
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);