-
Notifications
You must be signed in to change notification settings - Fork 419
/
main.js
455 lines (399 loc) · 13.8 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/**
* Main Node Process for SysMocap
*
* A part of SysMocap, open sourced under Mozilla Public License 2.0
*
* https://github.com/xianfei/SysMocap
*
* xianfei 2022.4
*/
const {
app,
BrowserWindow,
BrowserView,
ipcMain,
TouchBar,
shell,
nativeTheme,
screen
} = require("electron");
const { TouchBarLabel, TouchBarButton, TouchBarSpacer } = TouchBar
const os = require("os");
const platform = os.platform();
const { Worker } = require("worker_threads");
const Color = require('color');
// Make profile file on user home dir
const path = require("path");
const storage = require("electron-localstorage");
const fs = require("fs");
const _path = path.join(
app.getPath("home"),
app.getName() + "/",
"profile.json"
);
const _path_dir = path.dirname(_path);
if (!fs.existsSync(_path_dir)) {
try {
fs.mkdirSync(_path_dir);
} catch (e) { }
}
storage.setStoragePath(_path);
// Restart with force using the dedicated GPU
const { spawn } = require("child_process");
if (
platform === "win32" &&
// process.env.GPUSET !== "true" &&
storage.getItem("useDgpu")
) {
// Force using discrete GPU when there are multiple GPUs available.
// Improve performance when your PC has discrete GPU
app.commandLine.appendSwitch("force_high_performance_gpu", true);
// spawn(process.execPath, process.argv, {
// env: {
// ...process.env,
// SHIM_MCCOMPAT: "0x800000001", // this forces windows to use the dedicated GPU for the process
// GPUSET: "true",
// },
// detached: true,
// });
// process.exit(0);
}
// Modules to control application life and create native browser window
const electronRemoteMain = require("@electron/remote/main");
electronRemoteMain.initialize();
global.storagePath = { jsonPath: storage.getStoragePath() };
global.appInfo = { appVersion: app.getVersion(), appName: app.getName() };
// Prevents Chromium from lowering the priority of invisible pages' renderer processes.
// Improve performance when Mocap is running and forward motion data in background
app.commandLine.appendSwitch("disable-renderer-backgrounding", true);
app.commandLine.appendSwitch("enable-unsafe-webgpu");
// Support H265/hevc hardware decode
app.commandLine.appendSwitch('enable-features', 'PlatformHEVCDecoderSupport');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1180,
height: 750,
titleBarStyle: "hidden",
trafficLightPosition: { x: 18, y: 15 },
fullscreenable: false,
// titleBarOverlay: {
// color: '#fff',
// symbolColor: '#111'
// },
webPreferences: {
nodeIntegration: true,
webviewTag: true,
contextIsolation: false,
enableRemoteModule: true,
backgroundThrottling: false,
nodeIntegrationInSubFrames: true,
},
});
// and load the index.html of the app.
mainWindow.loadFile("mainview/framework.html");
if(storage.getItem("useDark")){
nativeTheme.themeSource = "dark";
}else{
nativeTheme.themeSource = "light";
}
if (
storage.getItem("useDMoc") ||
(!storage.getItem("used") && platform == "darwin")
) {
console.log('BrowserView Inited')
renderView = new BrowserView({
webPreferences: {
nodeIntegration: true,
webviewTag: true,
contextIsolation: false,
enableRemoteModule: true,
backgroundThrottling: false,
nodeIntegrationInSubFrames: true,
},
});
mainWindow.setBrowserView(renderView);
renderView.setBounds({ x: 0, y: 0, width: 0, height: 0 });
renderView.webContents.loadURL("about:blank");
// renderView.webContents.openDevTools({ mode: "detach" });
electronRemoteMain.enable(renderView.webContents);
}
ipcMain.on("sendRenderData", function (event, arg) {
// console.log("sendRenderData")
mainWindow.webContents.send("sendRenderDataForward", arg);
});
// createWelcomeWindow()
// showDoc()
// Open the DevTools.
// mainWindow.webContents.openDevTools()
electronRemoteMain.enable(mainWindow.webContents);
// Emitted when the window is closed.
mainWindow.on("closed", function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
renderView = null;
mainWindow = null;
});
if (process.platform !== 'darwin') return;
// 附赠彩蛋:老虎机
let spinning = false
// Reel labels
const reel1 = new TouchBarLabel({ label: '<- Easter egg!🌟' })
const reel2 = new TouchBarLabel({ label: "Sysmocap v" + app.getVersion() })
const reel3 = new TouchBarLabel()
// Spin result label
const result = new TouchBarLabel()
// Spin button
const spin = new TouchBarButton({
label: '🎰 Spin',
// backgroundColor: '#4d386e',
click: () => {
if (spinning) return // Ignore clicks if already spinning
spinning = true
result.label = ''
let timeout = 10
const spinLength = 4 * 1000 // 4 seconds
const startTime = Date.now()
const spinReels = () => {
updateReels()
if ((Date.now() - startTime) >= spinLength) {
finishSpin()
} else {
timeout *= 1.1 // Slow down a bit on each spin
setTimeout(spinReels, timeout)
}
}
spinReels()
}
})
const getRandomValue = () => {
const values = ['🍒', '💎', '🍭', '🍊', '🔔', '⭐', '🍇', '🍀']
return values[Math.floor(Math.random() * values.length)]
}
const updateReels = () => {
reel1.label = getRandomValue()
reel2.label = getRandomValue()
reel3.label = getRandomValue()
}
const finishSpin = () => {
const uniqueValues = new Set([reel1.label, reel2.label, reel3.label]).size
if (uniqueValues === 1) {
// All 3 values are the same
result.label = '💰 Jackpot!'
result.textColor = '#FDFF00'
} else if (uniqueValues === 2) {
// 2 values are the same
result.label = '😍 Winner!'
result.textColor = '#FDFF00'
} else {
// No values are the same
result.label = '🙁 Spin Again'
result.textColor = null
}
spinning = false
}
var a = new TouchBarButton({
label: '🏛 Library',
backgroundColor: null,
click: () => {
mainWindow.webContents.send('switch-tab', 'model')
}
}), b =
new TouchBarButton({
label: '📹 Mocap',
backgroundColor: null,
click: () => {
mainWindow.webContents.send('switch-tab', 'render')
}
}), c =
new TouchBarButton({
label: '⚙️ Setting',
backgroundColor: null,
click: () => {
mainWindow.webContents.send('switch-tab', 'settings')
}
})
ipcMain.on('tabChanged', (n,tab,color1,color2)=>{
a.backgroundColor = b.backgroundColor = c.backgroundColor = null
var color = Color(color1)
color = color.darken(0.4)
if(tab=='model') {a.backgroundColor = color.hex()}
if(tab=='render') {b.backgroundColor = color.hex()}
if(tab=='settings') {c.backgroundColor = color.hex()}
})
var tbItems = [
a, b, c,
spin,
new TouchBarSpacer({ size: 'small' }),
reel1,
new TouchBarSpacer({ size: 'small' }),
reel2,
new TouchBarSpacer({ size: 'small' }),
reel3,
new TouchBarSpacer({ size: 'large' }),
result
]
const touchBar = new TouchBar({
items: tbItems
})
mainWindow.setTouchBar(touchBar)
}
function createModelViewerWindow(args) {
// console.log(screen.getPrimaryDisplay().scaleFactor)
// Create the browser window.
var addtionalArgs = { backgroundColor: "#eee" };
if (args.useGlass) {
addtionalArgs = {
vibrancy: 'hud',
backgroundMaterial: 'acrylic',
backgroundColor: "#00000000",
};
}
var viewer = new BrowserWindow({
titleBarStyle: platform === "darwin" ? "hiddenInset" : "hidden",
autoHideMenuBar: true,
fullscreenable: false,
show: false,
width: 820,
height: 540,
...addtionalArgs,
titleBarOverlay: {
color: args.backgroundColor,
symbolColor: args.color,
},
webPreferences: {
nodeIntegration: true,
webviewTag: true,
contextIsolation: false,
enableRemoteModule: true,
additionalArguments: ["argsData", JSON.stringify(args)],
},
});
// and load the index.html of the app.
viewer.loadURL('about:blank')
electronRemoteMain.enable(viewer.webContents);
viewer.webContents.once('dom-ready', () => {
viewer.show();
viewer.loadFile("modelview/modelview.html");
});
// Open the DevTools.
// viewer.webContents.openDevTools()
// Emitted when the window is closed.
viewer.on("closed", function () {
viewer = null;
});
}
function createPdfViewerWindow(args) {
// Create the browser window.
var viewer = new BrowserWindow({
width: 1180,
height: 750,
autoHideMenuBar: true,
titleBarStyle: "hidden",
fullscreenable: false,
trafficLightPosition: { x: 10, y: 8 },
titleBarOverlay: {
color: "#0000",
symbolColor: nativeTheme.themeSource=='dark'?'#eee':'#111',
},
// vibrancy: "dark",
webPreferences: {
nodeIntegration: true,
webviewTag: true,
contextIsolation: false,
enableRemoteModule: true,
additionalArguments: ["pdfPath", JSON.stringify(args)],
},
});
viewer.webContents.on('will-navigate', (e, url) => {
e.preventDefault()
shell.openExternal(url)
})
// 处理 window.open 跳转
viewer.webContents.setWindowOpenHandler((data) => {
shell.openExternal(data.url)
return {
action: 'deny'
}
})
// and load the index.html of the app.
viewer.loadFile("pdfviewer/viewer.html");
electronRemoteMain.enable(viewer.webContents);
// Open the DevTools.
// viewer.webContents.openDevTools()
// Emitted when the window is closed.
viewer.on("closed", function () {
viewer = null;
});
}
function createGpuInfoWindow() {
// Create the browser window.
var viewer = new BrowserWindow({
width: 1000,
height: 600,
title: "GPU Info",
autoHideMenuBar: true,
fullscreenable: false,
webPreferences: {
nodeIntegration: true,
webviewTag: true,
contextIsolation: false,
enableRemoteModule: true,
},
});
// and load the index.html of the app.
viewer.loadURL("chrome://gpu");
// Emitted when the window is closed.
viewer.on("closed", function () {
viewer = null;
});
}
ipcMain.on("openDocument", function (event, arg) {
createPdfViewerWindow(__dirname + "/pdfs/document.pdf");
});
ipcMain.on("openModelViewer", function (event, arg) {
createModelViewerWindow(arg);
});
ipcMain.on("openGpuInfo", function (event, arg) {
createGpuInfoWindow();
});
ipcMain.on("openPDF", function (event, arg) {
createPdfViewerWindow(arg);
});
var worker = null;
ipcMain.on("startWebServer", function (event, ...arg) {
worker = new Worker(__dirname + "/webserv/worker.js");
worker.postMessage({ type: "startWebServer", arg: arg });
});
ipcMain.on("sendBoradcast", function (event, arg) {
if (worker)
worker.postMessage({ type: "sendBroadcast", arg: JSON.stringify(arg) });
});
ipcMain.on("sendBoradcastNew", function (event, arg) {
mainWindow.webContents.send("sendRenderDataForward", arg);
if (worker)
worker.postMessage({ type: "sendBroadcast", arg: JSON.stringify(arg) });
});
ipcMain.on("stopWebServer", function (event, arg) {
if (worker) worker.postMessage({ type: "stopWebServer" });
worker = null;
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", createWindow);
// Quit when all windows are closed.
app.on("window-all-closed", function () {
app.quit();
});
app.on("activate", function () {
// 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 (mainWindow === null) createWindow();
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.