-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathmain.js
316 lines (278 loc) · 8.59 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
const {
app,
BrowserWindow,
ipcMain,
globalShortcut,
dialog,
Menu,
MenuItem,
shell,
webContents
} = require('electron');
const electron = require('electron');
const electronLocalshortcut = require('electron-localshortcut');
const nodeFs = require('fs');
const nodePath = require('path');
const electron_remote = require('@electron/remote/main');
electron_remote.initialize();
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true';
let cloudSoftwareJson = null;
const usbDetection = require('usb-detection');
usbDetection.startMonitoring();
usbDetection.on('add', (device) => {
setTimeout(() => {
sendCommand({
obj: 'Mixly.Electron.Serial',
func: 'refreshPorts',
args: [device]
});
}, 1000);
});
usbDetection.on('remove', (device) => {
setTimeout(() => {
sendCommand({
obj: 'Mixly.Electron.Serial',
func: 'refreshPorts',
args: [device]
});
}, 1000);
});
function sendCommand (commandObj, win = null) {
let commandStr = '';
try {
commandStr = JSON.stringify(commandObj);
} catch (error) {
return;
}
if (win) {
win.webContents.send('command', commandStr);
} else {
const nowWebContents = webContents.getAllWebContents();
for (let i in nowWebContents) {
nowWebContents[i].send('command', commandStr);
}
}
}
// 程序 ready 前禁用GPU加速,解决软件打开时的黑屏问题
app.disableHardwareAcceleration();
app.allowRendererProcessReuse = false;
if (process.platform !== 'darwin') {
Menu.setApplicationMenu(null);
}
function createWindow(filePath = null, indexUrl = null) {
// 可以创建多个渲染进程
let win = new BrowserWindow({
show: false,
//resizable: false,
minHeight: 400,
minWidth: 700,
width: 0,
height: 0,
icon: __dirname + '/files/mixly_uncompressed.ico',
allowRunningInsecureContent: true,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: false,
allowRunningInsecureContent: true,
enableRemoteModule: true,
contextIsolation: false,
spellcheck: false
}
});
//win.setBackgroundColor("#181818");
electron_remote.enable(win.webContents);
//win.webContents.openDevTools();
//win.maximize()
//win.show()
// 渲染进程中的web页面可以加载本地文件
if (indexUrl)
win.loadURL(indexUrl);
else
win.loadFile('./src/index.html');
//打开或关闭开发者工具
electronLocalshortcut.register(win, 'CmdOrCtrl+Shift+I', () => {
win.webContents.toggleDevTools();
});
//重载页面
electronLocalshortcut.register(win, 'CmdOrCtrl+R', () => {
//win.reload();
sendCommand({
obj: 'Mixly.Electron.Loader',
func: 'reload',
args: []
}, win);
});
//重载页面
electronLocalshortcut.register(win, 'CmdOrCtrl+Shift+R', () => {
win.reload();
});
//最小化窗口
electronLocalshortcut.register(win, 'CmdOrCtrl+M', () => {
win.minimize();
});
//关闭窗口
electronLocalshortcut.register(win, 'CmdOrCtrl+W', () => {
win.close();
});
//还原窗口
electronLocalshortcut.register(win, 'CmdOrCtrl+0', () => {
win.webContents.setZoomFactor(1);
});
//放大窗口
electronLocalshortcut.register(win, 'CmdOrCtrl+Shift+=', () => {
let actualZoom = win.webContents.getZoomFactor();
if (actualZoom == null) {
actualZoom = 1;
}
if (actualZoom < 1.5) {
win.webContents.setZoomFactor(actualZoom + 0.1);
}
});
//缩小窗口
electronLocalshortcut.register(win, 'CmdOrCtrl+Shift+-', () => {
let actualZoom = win.webContents.getZoomFactor();
if (actualZoom == null) {
actualZoom = 1;
}
if (actualZoom > 0.5) {
win.webContents.setZoomFactor(actualZoom - 0.1);
}
});
//重启
electronLocalshortcut.register(win, 'CmdOrCtrl+Q', () => {
app.relaunch();
app.exit(0);
});
//打开帮助页面
electronLocalshortcut.register(win, 'CmdOrCtrl+H', () => {
var sendObj = {};
sendObj.type = "help";
var sendStr = JSON.stringify(sendObj);
win.webContents.send('ping', sendStr);
//win.webContents.executeJavaScript('alert("this is a test!");');
});
//创建一个新页面
electronLocalshortcut.register(win, 'CmdOrCtrl+Shift+N', () => {
createWindow(null);
});
//文件另存为
electronLocalshortcut.register(win, 'CmdOrCtrl+Shift+S', () => {
const commandObj = {
obj: 'Mixly.Electron.File',
func: 'saveAs'
}
const commandStr = JSON.stringify(commandObj);
win.webContents.send('command', commandStr);
});
//新建文件
electronLocalshortcut.register(win, 'CmdOrCtrl+N', () => {
const commandObj = {
obj: 'Mixly.Electron.File',
func: 'newFile'
}
const commandStr = JSON.stringify(commandObj);
win.webContents.send('command', commandStr);
});
win.once('ready-to-show', () => {
win.maximize();
win.show();
if (filePath != null) {
win.webContents.send('open-file', filePath);
}
});
win.on('close', function () {
});
// 记得在页面被关闭后清除该变量,防止内存泄漏
win.on('closed', function () {
win = null;
});
/*if (process.platform === 'win32') {
getLibsJson(function (jsonData) {
if (!jsonData?.version) return;
cloudSoftwareJson = JSON.stringify(jsonData);
//var updateSoftware = changeVersion(app.getVersion(), jsonData.version);
var updateSoftware = (app.getVersion() != jsonData.version);
if (updateSoftware) {
var sendObj = {};
sendObj.type = "update";
sendObj.oldVersion = app.getVersion();
sendObj.newVersion = jsonData.version;
var sendStr = JSON.stringify(sendObj);
setTimeout(function () {
win.webContents.send('ping', sendStr);
}, 1000);
}
});
ipcMain.on('ping', (event, arg) => {
if (arg == "update") {
shell.openPath(nodePath.join(__dirname, '../../一键更新.bat'));
}
event.reply('ping', 'get');
});
}*/
win.on('unresponsive', async () => {
const { response } = await dialog.showMessageBox({
message: 'Mixly3.0无响应',
title: '您想尝试强制重新加载应用程序吗?',
buttons: ['确定', '取消'],
cancelId: 1
});
if (response === 0) {
win.forcefullyCrashRenderer();
win.reload();
}
});
}
app.on('ready', () => {
if (process.argv.length >= 2) {
createWindow(process.argv[1]);
} else {
createWindow(null);
}
});
app.on('activate', () => {
if (!webContents.getAllWebContents().length) {
createWindow(null);
}
});
// 页面全部关闭后关闭主进程,不同平台可能有不同的处理方式
app.on('window-all-closed', () => {
usbDetection.stopMonitoring();
app.quit();
});
app.on('render-process-gone', async (e, w, d) => {
let title;
if(d.reason == "crashed") {
nodeFs.appendFileSync('./log.txt', `${new Date()}渲染进程崩溃${d.reason}\n, ${JSON.stringify(d)}\n\n`);
title = '渲染进程崩溃';
} else {
nodeFs.appendFileSync('./log.txt', `${new Date()}渲染进程被杀死${d.reason}\n`);
title = '渲染进程被杀死';
}
const { response } = await dialog.showMessageBox({
title: title,
message: '您想尝试强制重新加载此页面吗?',
type: 'none',
buttons: ['确定', '取消'],
cancelId: 1
});
w.destroy();
if (response === 0) {
createWindow(null, w.getURL());
}
});
const gotTheLock = app.requestSingleInstanceLock();
if (gotTheLock) {
app.on('second-instance', (event, commandLine, workingDirectory) => {
if (commandLine[0] != app.getPath("exe")) {
} else {
if (commandLine.length >= 3) {
createWindow(commandLine[2]);
} else {
createWindow(null);
}
}
})
} else {
app.quit();
}