-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·250 lines (225 loc) · 5.78 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
//@ts-check
const electron = require('electron');
const path = require('path');
const url = require('url');
const splashscreen = require('@trodi/electron-splashscreen');
const {
autoUpdater
} = require('electron-updater');
//const ipc = electron.ipcMain;
const shell = electron.shell;
//const BrowserWindow = electron.BrowserWindow;
const Menu = electron.Menu;
const MenuItem = electron.MenuItem;
const app = electron.app;
const shortcut = electron.globalShortcut;
const downloadDir = `${app.getPath('downloads')}\\LoopClient\\`;
const rootPath = path.join(__dirname, 'dist', 'app');
let splash;
const prod = true;
// check for update at lunch time
function checkForUpdates() {
try {
autoUpdater.logger = require('electron-log');
autoUpdater.checkForUpdatesAndNotify();
} catch (error) {}
}
function check() {
autoUpdater
.checkForUpdates()
.then(res => console.log(res))
.catch(err => console.log(err));
}
function createWindow() {
// Browser window options
const windowOptions = {
width: 1024,
height: 650,
show: false,
minWidth: 960,
frame: false,
webPreferences: {
nodeIntegration: true,
webSecurity: false,
scrollBounce: true
}
};
// check platform and set icon🎇
if (process.platform === 'win32') {
windowOptions.icon = path.join(rootPath, '/assets/icons/icon.win.ico');
} else if (process.platform === 'linux') {
windowOptions.icon = path.join(rootPath, '/assets/icons/icon.png');
} else if (process.platform === 'darwin') {
windowOptions.icon = path.join(rootPath, '/assets/icons/icon.png');
}
splash = splashscreen.initSplashScreen({
windowOpts: windowOptions,
templateUrl: path.join(rootPath, '/assets/splash/splash.gif'),
delay: 0,
minVisible: 1500,
splashScreenOpts: {
width: 500,
height: 375,
transparent: true,
icon: path.join(rootPath, '/assets/icons/icon.png'),
backgroundColor: '#ffffff',
title: 'Loop Client Loading',
zoomToPageWidth: false,
movable: false,
skipTaskbar: false,
focusable: false,
acceptFirstMouse: false
}
});
if (!prod) {
splash.loadURL(
url.format({
pathname: path.join(rootPath, '/index.html'),
protocol: 'file',
slashes: true
})
);
} else if (prod) {
splash.loadURL(`http://localhost:4200`);
}
// Event when the window is closed.
splash.on('closed', () => {
splash = null;
});
// When browser window is finished loading
splash.once('ready-to-show', () => {
splash.show();
});
}
app.on('window-all-closed', () => {
// On macOS specific close process
app.quit();
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// macOS specific close process
if (splash === null) {
createWindow();
}
});
app.on('will-quit', () => {
shortcut.unregisterAll();
});
//Handle all downloads
async function downloadHandler() {
splash.webContents.session.on('will-download', (event, item, webContents) => {
// Set the save path, making Electron not to prompt a save dialog.
let fileName = `[LOOP] ${item.getFilename()}`;
let downloadPath = `${downloadDir}/${fileName}`
if (process.platform == 'win32') {
downloadPath = `${downloadDir}\\LoopClient\\${fileName}`
} else if (process.platform == 'linux') {
downloadPath = `${downloadDir}/LoopClient/${fileName}`;
}
item.setSavePath(downloadPath);
item.on('updated', (event, state) => {
if (state === 'interrupted') {
console.log('Download is interrupted but can be resumed');
} else if (state === 'progressing') {
if (item.isPaused()) {
console.log('Download is paused');
} else {
console.log(`Received bytes: ${item.getReceivedBytes()}`);
}
}
});
item.once('done', (event, state) => {
if (state === 'completed') {
console.log('Download successfully');
shell.beep();
shell.openExternal(downloadPath);
} else {
console.log(`Download failed: ${state}`);
}
});
});
}
function reloadWindow() {
app.relaunch({
args: process.argv.slice(1).concat(['--relaunch'])
});
app.exit(0);
}
function openDownloadPath() {
shell.openExternal(downloadDir);
}
// when app is ready
app.on('ready', () => {
// create window
createWindow();
// load menu
Menu.setApplicationMenu(null);
// register shortcuts
shortcut.register('CommandOrControl+Shift+T', () => {
splash.webContents.openDevTools();
});
shortcut.register('CommandOrControl+D', () => {
openDownloadPath();
});
// load contex-menu
const menu = new Menu();
menu.append(
new MenuItem({
role: 'cut'
})
);
menu.append(
new MenuItem({
role: 'copy'
})
);
menu.append(
new MenuItem({
role: 'paste',
accelerator: ''
})
);
menu.append(
new MenuItem({
role: 'selectall'
})
);
menu.append(
new MenuItem({
type: 'separator'
})
);
menu.append(
new MenuItem({
label: 'Reload',
click: reloadWindow
})
);
menu.append(
new MenuItem({
label: 'Open Download Location',
click: openDownloadPath
})
);
menu.append(
new MenuItem({
type: 'separator'
})
);
menu.append(
new MenuItem({
label: 'Exit',
role: 'close'
})
);
splash.webContents.on('context-menu', function (e, params) {
// @ts-ignore
menu.popup(splash, params.x, params.y);
});
// handle downloads
downloadHandler();
checkForUpdates();
// check();
});