-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
81 lines (72 loc) · 2.06 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
'use strict';
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const globalShortcut = electron.globalShortcut;
const { ipcMain } = require('electron');
// adds debug features like hotkeys for triggering dev tools and reload
require('electron-debug')();
// prevent window being garbage collected
let mainWindow;
function onClosed() {
// dereference the window
// for multiple windows store them in an array
mainWindow = null;
}
function createMainWindow() {
let win;
const windowProperties = {
icon: __dirname + '/images/icon128.png',
title: app.getName()
};
if (process.env.NODE_ENV === 'development') {
windowProperties.width = 900;
windowProperties.height = 370;
win = new BrowserWindow(windowProperties);
win.webContents.openDevTools();
} else {
windowProperties.width = 400;
windowProperties.height = 370;
win = new BrowserWindow(windowProperties);
}
win.loadURL(`file://${__dirname}/page/page.html`);
win.on('closed', onClosed);
return win;
}
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
globalShortcut.unregisterAll();
app.quit();
}
});
app.on('activate', () => {
if (!mainWindow) {
mainWindow = createMainWindow();
}
});
app.on('ready', () => {
mainWindow = createMainWindow();
mainWindow.webContents.on('did-finish-load', function() {
ipcMain.on('title', function(event, prefix) {
let title = '';
if (typeof prefix === 'string' && prefix.length > 0) {
title += prefix + ' - ';
}
title += app.getName();
mainWindow.setTitle(title);
});
mainWindow.webContents.send('version', app.getVersion());
});
['MediaPlayPause', 'MediaStop'].forEach(registerMediaKey);
});
app.on('before-quit', () => {
mainWindow.webContents.send('quit');
});
function registerMediaKey(key) {
const success = globalShortcut.register(key, () => {
mainWindow.webContents.send('media-key', key);
});
if (!success) {
process.stderr.write('failed to bind key ' + key + '\n');
}
}