forked from digimezzo/dopamine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
288 lines (288 loc) · 10.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable @typescript-eslint/no-floating-promises */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-unsafe-return */
const electron_1 = require("electron");
const electron_log_1 = require("electron-log");
const Store = require("electron-store");
const windowStateKeeper = require("electron-window-state");
const os = require("os");
const path = require("path");
const url = require("url");
const worker_threads_1 = require("worker_threads");
/**
* Command line parameters
*/
electron_1.app.commandLine.appendSwitch('disable-color-correct-rendering'); // Prevents incorrect color rendering
electron_1.app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required'); // Prevents requiring user interaction to play audio
electron_1.app.commandLine.appendSwitch('disable-http-cache'); // Disables clearing of the cache folder at each startup
/**
* Logging
*/
electron_log_1.default.create('main');
electron_log_1.default.transports.file.resolvePath = () => path.join(electron_1.app.getPath('userData'), 'logs', 'Dopamine.log');
/**
* Variables
*/
const globalAny = global; // Global does not allow setting custom properties. We need to cast it to "any" first.
const settings = new Store();
const args = process.argv.slice(1);
const isServing = args.some((val) => val === '--serve');
let mainWindow;
let tray;
let isQuitting;
// Static folder is not detected correctly in production
if (process.env.NODE_ENV !== 'development') {
globalAny.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\');
}
/**
* Functions
*/
function windowHasFrame() {
if (!settings.has('useSystemTitleBar')) {
settings.set('useSystemTitleBar', false);
}
return settings.get('useSystemTitleBar');
}
function shouldShowIconInNotificationArea() {
if (!settings.has('showIconInNotificationArea')) {
settings.set('showIconInNotificationArea', true);
}
return settings.get('showIconInNotificationArea');
}
function shouldMinimizeToNotificationArea() {
if (!settings.has('minimizeToNotificationArea')) {
settings.set('minimizeToNotificationArea', false);
}
return settings.get('minimizeToNotificationArea');
}
function shouldCloseToNotificationArea() {
if (!settings.has('closeToNotificationArea')) {
settings.set('closeToNotificationArea', false);
}
return settings.get('closeToNotificationArea');
}
function getTrayIcon() {
if (os.platform() === 'darwin') {
return path.join(globalAny.__static, 'icons/trayTemplate.png');
}
const invertColor = settings.get('invertNotificationAreaIconColor');
if (os.platform() === 'win32') {
if (!invertColor) {
// Defaulting to black for Windows
return path.join(globalAny.__static, 'icons/tray_black.ico');
}
else {
return path.join(globalAny.__static, 'icons/tray_white.ico');
}
}
else {
if (!invertColor) {
// Defaulting to white for Linux
return path.join(globalAny.__static, 'icons/tray_white.png');
}
else {
return path.join(globalAny.__static, 'icons/tray_black.png');
}
}
}
function createMainWindow() {
// Suppress the default menu
electron_1.Menu.setApplicationMenu(null);
// Load the previous state with fallback to defaults
const windowState = windowStateKeeper({
defaultWidth: 1000,
defaultHeight: 650,
});
const remoteMain = require('@electron/remote/main');
remoteMain.initialize();
// Create the browser window
mainWindow = new electron_1.BrowserWindow({
x: windowState.x,
y: windowState.y,
width: windowState.width,
height: windowState.height,
backgroundColor: '#fff',
frame: windowHasFrame(),
icon: path.join(globalAny.__static, os.platform() === 'win32' ? 'icons/icon.ico' : 'icons/64x64.png'),
webPreferences: {
webSecurity: false,
nodeIntegration: true,
contextIsolation: false,
},
show: false,
});
remoteMain.enable(mainWindow.webContents);
globalAny.windowHasFrame = windowHasFrame();
windowState.manage(mainWindow);
if (isServing) {
require('electron-reload')(__dirname, {
electron: require(`${__dirname}/node_modules/electron`),
});
mainWindow.loadURL('http://localhost:4200');
}
else {
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'dist/index.html'),
protocol: 'file:',
slashes: true,
}));
}
// Emitted when the window is closed.
mainWindow.on('closed', () => {
// Dereference the window object, usually you would store window
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = undefined;
});
// 'ready-to-show' doesn't fire on Windows in dev mode. In prod it seems to work.
// See: https://github.com/electron/electron/issues/7779
mainWindow.on('ready-to-show', () => {
mainWindow.show();
mainWindow.focus();
});
// Makes links open in external browser
const handleRedirect = (e, localUrl) => {
// Check that the requested url is not the current page
if (localUrl !== mainWindow.webContents.getURL()) {
e.preventDefault();
require('electron').shell.openExternal(localUrl);
}
};
mainWindow.webContents.on('will-navigate', handleRedirect);
mainWindow.webContents.on('before-input-event', (event, input) => {
if (input.key.toLowerCase() === 'f12') {
// if (serve) {
mainWindow.webContents.toggleDevTools();
// }
event.preventDefault();
}
});
mainWindow.on('minimize', (event) => {
if (shouldMinimizeToNotificationArea()) {
event.preventDefault();
mainWindow.hide();
}
});
mainWindow.on('close', (event) => {
if (shouldCloseToNotificationArea()) {
if (!isQuitting) {
event.preventDefault();
mainWindow.hide();
}
return false;
}
});
}
/**
* Main
*/
try {
electron_log_1.default.info('[Main] [Main] +++ Starting +++');
const gotTheLock = electron_1.app.requestSingleInstanceLock();
if (!gotTheLock) {
electron_log_1.default.info('[Main] [Main] There is already another instance running. Closing.');
electron_1.app.quit();
}
else {
electron_1.app.on('second-instance', (event, argv, workingDirectory) => {
electron_log_1.default.info('[Main] [Main] Attempt to run second instance. Showing existing window.');
mainWindow.webContents.send('arguments-received', argv);
// Someone tried to run a second instance, we should focus the existing window.
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
}
});
// 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.
electron_1.app.on('ready', createMainWindow);
// Quit when all windows are closed.
electron_1.app.on('window-all-closed', () => {
electron_log_1.default.info('[App] [window-all-closed] +++ Stopping +++');
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
electron_1.app.quit();
}
});
electron_1.app.on('activate', () => {
// On OS X 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 == undefined) {
createMainWindow();
}
});
electron_1.app.on('before-quit', () => {
isQuitting = true;
});
electron_1.app.whenReady().then(() => {
// See: https://github.com/electron/electron/issues/23757
electron_1.protocol.registerFileProtocol('file', (request, callback) => {
const pathname = decodeURI(request.url.replace('file:///', ''));
callback(pathname);
});
if (shouldShowIconInNotificationArea()) {
tray = new electron_1.Tray(getTrayIcon());
tray.setToolTip('Dopamine');
}
});
electron_1.nativeTheme.on('updated', () => {
if (tray == undefined) {
return;
}
tray.setImage(getTrayIcon());
});
electron_1.ipcMain.on('update-tray-context-menu', (event, arg) => {
if (tray == undefined) {
return;
}
const contextMenu = electron_1.Menu.buildFromTemplate([
{
label: arg.showDopamineLabel,
click() {
mainWindow.show();
mainWindow.focus();
},
},
{
label: arg.exitLabel,
click() {
electron_1.app.quit();
},
},
]);
tray.setContextMenu(contextMenu);
});
electron_1.ipcMain.on('update-tray-icon', (event, arg) => {
if (tray == undefined) {
return;
}
tray.setImage(getTrayIcon());
});
electron_1.ipcMain.on('metadata-worker-request', (event, arg) => {
const workerThread = new worker_threads_1.Worker(path.join(__dirname, 'main/metadata/metadata-worker.js'), {
workerData: { arg },
});
workerThread.on('message', (filledIndexableTracks) => {
mainWindow.webContents.send('metadata-worker-response', { filledIndexableTracks: filledIndexableTracks });
});
});
}
}
catch (e) {
electron_log_1.default.error(`[Main] [Main] Could not start. Error: ${e.message}`);
throw e;
}
//# sourceMappingURL=main.js.map