UNPKG

4.84 kBJavaScriptView Raw
1import 'source-map-support/register';
2import fs from 'fs';
3import path from 'path';
4import { app, crashReporter, globalShortcut } from 'electron';
5import electronDownload from 'electron-dl';
6
7import createLoginWindow from './components/login/loginWindow';
8import createMainWindow from './components/mainWindow/mainWindow';
9import createTrayIcon from './components/trayIcon/trayIcon';
10import helpers from './helpers/helpers';
11import inferFlash from './helpers/inferFlash';
12
13const electronSquirrelStartup = require('electron-squirrel-startup');
14
15// Entrypoint for electron-squirrel-startup.
16// See https://github.com/jiahaog/nativefier/pull/744 for sample use case
17if (electronSquirrelStartup) {
18 app.exit();
19}
20
21const { isOSX } = helpers;
22
23const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json');
24const appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));
25
26const fileDownloadOptions = Object.assign({}, appArgs.fileDownloadOptions);
27electronDownload(fileDownloadOptions);
28
29if (appArgs.processEnvs) {
30 Object.keys(appArgs.processEnvs).forEach((key) => {
31 /* eslint-env node */
32 process.env[key] = appArgs.processEnvs[key];
33 });
34}
35
36let mainWindow;
37
38if (typeof appArgs.flashPluginDir === 'string') {
39 app.commandLine.appendSwitch('ppapi-flash-path', appArgs.flashPluginDir);
40} else if (appArgs.flashPluginDir) {
41 const flashPath = inferFlash();
42 app.commandLine.appendSwitch('ppapi-flash-path', flashPath);
43}
44
45if (appArgs.ignoreCertificate) {
46 app.commandLine.appendSwitch('ignore-certificate-errors');
47}
48
49if (appArgs.disableGpu) {
50 app.disableHardwareAcceleration();
51}
52
53if (appArgs.ignoreGpuBlacklist) {
54 app.commandLine.appendSwitch('ignore-gpu-blacklist');
55}
56
57if (appArgs.enableEs3Apis) {
58 app.commandLine.appendSwitch('enable-es3-apis');
59}
60
61if (appArgs.diskCacheSize) {
62 app.commandLine.appendSwitch('disk-cache-size', appArgs.diskCacheSize);
63}
64
65if (appArgs.basicAuthUsername) {
66 app.commandLine.appendSwitch(
67 'basic-auth-username',
68 appArgs.basicAuthUsername,
69 );
70}
71
72if (appArgs.basicAuthPassword) {
73 app.commandLine.appendSwitch(
74 'basic-auth-password',
75 appArgs.basicAuthPassword,
76 );
77}
78
79// do nothing for setDockBadge if not OSX
80let setDockBadge = () => {};
81
82if (isOSX()) {
83 let currentBadgeCount = 0;
84
85 setDockBadge = (count, bounce = false) => {
86 app.dock.setBadge(count);
87 if (bounce && count > currentBadgeCount) app.dock.bounce();
88 currentBadgeCount = count;
89 };
90}
91
92app.on('window-all-closed', () => {
93 if (!isOSX() || appArgs.fastQuit) {
94 app.quit();
95 }
96});
97
98app.on('activate', (event, hasVisibleWindows) => {
99 if (isOSX()) {
100 // this is called when the dock is clicked
101 if (!hasVisibleWindows) {
102 mainWindow.show();
103 }
104 }
105});
106
107app.on('before-quit', () => {
108 // not fired when the close button on the window is clicked
109 if (isOSX()) {
110 // need to force a quit as a workaround here to simulate the osx app hiding behaviour
111 // Somehow sokution at https://github.com/atom/electron/issues/444#issuecomment-76492576 does not work,
112 // e.prevent default appears to persist
113
114 // might cause issues in the future as before-quit and will-quit events are not called
115 app.exit(0);
116 }
117});
118
119if (appArgs.crashReporter) {
120 app.on('will-finish-launching', () => {
121 crashReporter.start({
122 companyName: appArgs.companyName || '',
123 productName: appArgs.name,
124 submitURL: appArgs.crashReporter,
125 uploadToServer: true,
126 });
127 });
128}
129
130// quit if singleInstance mode and there's already another instance running
131const shouldQuit = appArgs.singleInstance && !app.requestSingleInstanceLock();
132if (shouldQuit) {
133 app.quit();
134} else {
135 app.on('second-instance', () => {
136 if (mainWindow) {
137 if (!mainWindow.isVisible()) {
138 // try
139 mainWindow.show();
140 }
141 if (mainWindow.isMinimized()) {
142 // minimized
143 mainWindow.restore();
144 }
145 mainWindow.focus();
146 }
147 });
148
149 app.on('ready', () => {
150 mainWindow = createMainWindow(appArgs, app.quit, setDockBadge);
151 createTrayIcon(appArgs, mainWindow);
152
153 // Register global shortcuts
154 if (appArgs.globalShortcuts) {
155 appArgs.globalShortcuts.forEach((shortcut) => {
156 globalShortcut.register(shortcut.key, () => {
157 shortcut.inputEvents.forEach((inputEvent) => {
158 mainWindow.webContents.sendInputEvent(inputEvent);
159 });
160 });
161 });
162 }
163 });
164}
165
166app.on('new-window-for-tab', () => {
167 mainWindow.emit('new-tab');
168});
169
170app.on('login', (event, webContents, request, authInfo, callback) => {
171 // for http authentication
172 event.preventDefault();
173
174 if (
175 appArgs.basicAuthUsername !== null &&
176 appArgs.basicAuthPassword !== null
177 ) {
178 callback(appArgs.basicAuthUsername, appArgs.basicAuthPassword);
179 } else {
180 createLoginWindow(callback);
181 }
182});