This repository has been archived by the owner on Sep 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
main.js
132 lines (109 loc) · 3.51 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
import electron from 'electron';
import path from 'path';
import _ from 'lodash';
import { Project } from 'xdl';
import { autoUpdater } from 'electron-updater';
import Menu from './remote/Menu';
import config from './config';
const { app, BrowserWindow, ipcMain } = electron;
if (require('electron-squirrel-startup')) {
app.quit();
}
// Report crashes to our server.
// electron.CrashReporter.start();
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is GCed.
let mainWindow = null;
let projectRoots = [];
ipcMain.on('project-opened', (event, projectRoot) => {
console.log(`Opened project at ${projectRoot}`);
projectRoots.push(projectRoot);
Menu.setupMenu(mainWindow, true);
});
ipcMain.on('project-closed', (event, projectRoot) => {
console.log(`Closed project at ${projectRoot}`);
projectRoots = _.without(projectRoots, projectRoot);
Menu.setupMenu(mainWindow, false);
});
ipcMain.on('check-for-update', () => {
autoUpdater.checkForUpdates();
});
ipcMain.on('quit-and-update', () => {
autoUpdater.quitAndInstall();
});
app.on('window-all-closed', () => {
app.quit();
});
// Clean up all open projects before exiting
app.on('will-quit', async event => {
if (projectRoots.length > 0) {
event.preventDefault();
try {
await Promise.all(projectRoots.map(root => Project.stopAsync(root)));
} catch (e) {
// not much we can do here
console.error(e);
}
projectRoots = [];
app.quit();
}
});
app.on('ready', () => {
if (process.env.NODE_ENV === 'development') {
const devToolsInstaller = require('electron-devtools-installer');
const { default: installExtension, REACT_DEVELOPER_TOOLS } = devToolsInstaller;
installExtension(REACT_DEVELOPER_TOOLS)
.then(name => console.log(`Added Extension: ${name}`))
.catch(err => console.log('An error occurred: ', err));
}
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
minWidth: 700,
minHeight: 500,
// For mac
// TODO: re-enable once we have a better solution for notifications
// titleBarStyle: 'hidden-inset',
// for windows. osx gets icon from post install task
icon: path.resolve(__dirname, '../build/xde.ico'),
webPreferences: {
backgroundThrottling: false,
},
});
mainWindow.commandLineArgs = process.argv;
mainWindow.loadURL(`file://${path.resolve(__dirname, '../web/index.html')}`);
// Open the devtools.
if (config.__DEV__) {
mainWindow.openDevTools();
}
// Setup the menu bar
Menu.setupMenu(mainWindow, false);
mainWindow.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
let webContents = mainWindow.webContents;
var handleRedirect = (e, url) => {
if (url !== webContents.getURL()) {
e.preventDefault();
require('electron').shell.openExternal(url);
}
};
webContents.on('will-navigate', handleRedirect);
webContents.on('new-window', handleRedirect);
const AUTO_UPDATER_EVENTS = [
'error',
'checking-for-update',
'update-available',
'update-not-available',
'update-downloaded',
];
for (let updateEventName of AUTO_UPDATER_EVENTS) {
autoUpdater.on(updateEventName, (...args) => {
mainWindow.webContents.send('auto-updater', updateEventName, ...args);
});
}
});