-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
98 lines (84 loc) · 2.47 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
const { app, BrowserWindow } = require('electron');
const path = require('node:path');
const { Command, Option } = require('commander');
const program = new Command();
program
.addOption(
new Option(
'-v, --view <view>',
'The view type you wish to load (audio or video)',
)
.default('audio')
.choices(['audio', 'video']),
)
.addOption(
new Option(
'-rt, --room-type <room-type>',
'The key of the active room type you wish to load.'
)
.default('active')
)
;
program.parse();
app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required');
async function sleep(seconds) {
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
}
// Somwhow loadURL is a bit bugged, this seems to fix it on windows
// https://github.com/electron/electron/issues/28208#issuecomment-1129612599
async function attemptLoad(win, view) {
let loaded = false;
do {
try {
await win.loadURL(`http://localhost:9090/bundles/nodecg-vdoninja/graphics/${view}-view/main.html`);
loaded = true;
} catch (err) {
await sleep(5);
}
} while (!loaded);
}
async function createWindow () {
const { view, roomType } = program.opts();
const titleInfo = [
roomType || null
].filter(Boolean).join(',');
const win = new BrowserWindow({
width: 1280,
height: 720,
title: `VDO.Ninja Viewer - ${view === 'audio' ? 'Audio' : 'Video'} View ${titleInfo.length ? `(${titleInfo})` : ''}`,
fullscreen: view === 'video',
webPreferences: {
backgroundThrottling: false,
contextIsolation: true,
nodeIntegration: true,
preload: path.join(__dirname, 'preload.js'),
additionalArguments: [
`roomType:${roomType}`,
'applyDelay:false', // TODO: implement when we need this
],
},
});
// hack and a half
// https://www.electronjs.org/docs/latest/api/browser-window#event-page-title-updated
win.on('page-title-updated', (event) => {
event.preventDefault();
});
await attemptLoad(win, view);
// Still nothing? Try again after 10 seconds
await sleep(10);
await attemptLoad(win, view);
}
app.whenReady().then(() => {
createWindow().catch(console.log);
// Specific macos stuff, doubt we ever need this.
app.once('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow().catch(console.log);
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});