/
electron.js
238 lines (221 loc) · 5.34 KB
/
electron.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
const electron = require('electron');
const isDev = require('electron-is-dev');
const url = require('url');
const path = require('path');
const Sentry = require('@sentry/electron');
const { autoUpdater } = require('electron-updater');
const { createItems } = require('./app/menu');
const enableKeyboard = require('./app/keyboard');
const enableShortcuts = require('./app/shortcuts');
const config = require('./app/config');
const { app, BrowserWindow, ipcMain, Tray, Menu } = electron;
const isMac = process.platform === 'darwin';
const isWin = process.platform && process.platform.indexOf('win') !== -1;
/**
* Configure error reporting.
*/
if (!isDev) {
Sentry.init({
dsn: 'https://d0b3058a07a143e8b2e80c1eae7299e7@sentry.io/1304463',
});
}
/**
* Global reference of main window will prevent it
* from being destroyed when variable garbage collection
* is performed.
*/
let window;
let tray;
let ready = false;
/**
* Focus the window into the correct position.
*/
function openAndCenterWindow() {
const { screen } = electron;
const { x, y } = screen.getCursorScreenPoint();
const { workArea } = screen.getDisplayNearestPoint({ x, y });
const currentPosition = window.getPosition();
const isInDisplay =
currentPosition[0] >= workArea.x &&
currentPosition[0] <= workArea.x + workArea.width &&
currentPosition[1] >= workArea.y &&
currentPosition[1] <= workArea.y + workArea.height;
if (isInDisplay && window.isVisible()) {
window.hide();
} else {
if (!isInDisplay) {
const windowSize = window.getSize();
const fromLeft = Math.round(
workArea.x + (workArea.width - windowSize[0]) / 2
);
const fromTop = Math.round(
workArea.y + (workArea.height - windowSize[1]) / 2
);
window.setPosition(fromLeft, fromTop);
}
window.show();
window.focus();
window.webContents.send('focused');
}
}
/**
* Create the main application window.
*/
function createWindow() {
if (!ready) {
return;
}
window = new BrowserWindow({
width: 1000,
height: isWin ? 585 : 530,
backgroundColor: '#000',
titleBarStyle: 'hiddenInset',
title: 'Forge',
show: false,
});
const appUrl = isDev
? 'http://localhost:3000'
: url.format({
pathname: path.join(__dirname, '../build/index.html'),
protocol: 'file:',
slashes: true,
});
window.loadURL(appUrl);
window.on('close', event => {
if (window) {
event.preventDefault();
window.hide();
}
});
window.on('closed', () => {
window = null;
});
}
/**
* Determine if there is already an existing window to
* focus on or if we need to create a new window.
*/
function createOrFocusWindow(override) {
if (typeof override === 'boolean' && !override) {
if (window) {
window.hide();
}
} else {
if (!window) {
createWindow();
}
openAndCenterWindow();
}
}
/**
* Create the tray to go in notification icons panel.
*/
function createTray() {
const template = createItems({
app,
getWindow: () => window,
showWindow: createOrFocusWindow,
});
const menu = Menu.buildFromTemplate(template);
tray = new Tray(path.join(__dirname, './assets/electron/TrayTemplate.png'));
tray.setToolTip('Forge');
tray.setContextMenu(menu);
return tray;
}
/**
* Send the app a message when ready to update.
*/
function enableUpdateEvents() {
autoUpdater.on(
'update-available',
() =>
window &&
window.webContents.send('update', {
type: 'available',
})
);
autoUpdater.on(
'update-downloaded',
() =>
window &&
window.webContents.send('update', {
type: 'downloaded',
})
);
autoUpdater.checkForUpdatesAndNotify().catch(error => {
console.log(error);
});
}
/**
* This listens for a "dismiss" event sent from the app.
*/
ipcMain.on('dismiss', () => window && window.hide());
/**
* The relaunch call can be found on the error screen.
*/
ipcMain.on('relaunch', () => {
app.relaunch();
app.exit();
});
/**
* Quit and update the app when it's ready.
*/
ipcMain.on('quitAndUpdate', () => autoUpdater.quitAndInstall());
/**
* Execute tasks once the react application has loaded.
*/
ipcMain.on('ready', () => {
if (!isDev) {
enableUpdateEvents();
}
if (window) {
window.webContents.send('settings', {
platform: process.platform,
});
}
});
/**
* When the electron process has finished initializing
* we can begin to add windows.
*/
app.on('ready', () => {
ready = true;
createTray();
createWindow();
enableKeyboard();
enableShortcuts({ createOrFocusWindow });
if (config.openOnStart) {
openAndCenterWindow();
}
});
/**
* When the user clicks the dock icon to open or focus the app.
*/
if (isMac) {
app.on('activate', createOrFocusWindow);
}
/**
* Hide the app from the dock.
*/
if (isMac && config.hideDockOnStart) {
app.dock.hide();
}
/**
* Get app to open when user logs in.
*/
if (isMac || isWin) {
app.setLoginItemSettings({
openAtLogin: false,
});
}
/**
* Don't delete this... it makes sure that the app doesn't
* close when you press the red "x" in corner. However, it does
* let the app be quit using the menu items - which
* is it's advantage over app.on('will-quit')
*
* @see http://simurai.com/electron.atom.io/docs/all/#event-window-all-closed
*/
app.on('window-all-closed', event => {
event.preventDefault();
});