-
-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathelectron-main-application.ts
387 lines (359 loc) · 13.8 KB
/
electron-main-application.ts
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import { inject, injectable } from 'inversify';
import { app, BrowserWindow, BrowserWindowConstructorOptions, ipcMain, screen, Event as ElectronEvent } from '@theia/core/electron-shared/electron';
import { fork } from 'child_process';
import { AddressInfo } from 'net';
import { join, dirname } from 'path';
import * as fs from 'fs-extra';
import { initSplashScreen } from '../splash/splash-screen';
import { MaybePromise } from '@theia/core/lib/common/types';
import { ElectronSecurityToken } from '@theia/core/lib/electron-common/electron-token';
import { FrontendApplicationConfig } from '@theia/application-package/lib/application-props';
import {
ElectronMainApplication as TheiaElectronMainApplication,
ElectronMainExecutionParams,
TheiaBrowserWindowOptions,
} from '@theia/core/lib/electron-main/electron-main-application';
import { SplashServiceImpl } from '../splash/splash-service-impl';
import { URI } from '@theia/core/shared/vscode-uri';
import * as electronRemoteMain from '@theia/core/electron-shared/@electron/remote/main';
import { Deferred } from '@theia/core/lib/common/promise-util';
import * as os from '@theia/core/lib/common/os';
import { RELOAD_REQUESTED_SIGNAL, Restart } from '@theia/core/lib/electron-common/messaging/electron-messages';
app.commandLine.appendSwitch('disable-http-cache');
interface WorkspaceOptions {
file: string
x: number
y: number
width: number
height: number
isMaximized: boolean
isFullScreen: boolean
time: number
}
const WORKSPACES = 'workspaces';
@injectable()
export class ElectronMainApplication extends TheiaElectronMainApplication {
protected _windows: BrowserWindow[] = [];
protected startup = false;
protected openFilePromise = new Deferred();
@inject(SplashServiceImpl)
protected readonly splashService: SplashServiceImpl;
async start(config: FrontendApplicationConfig): Promise<void> {
// Explicitly set the app name to have better menu items on macOS. ("About", "Hide", and "Quit")
// See: https://github.com/electron-userland/electron-builder/issues/2468
// Regression in Theia: https://github.com/eclipse-theia/theia/issues/8701
app.on('ready', () => app.setName(config.applicationName));
this.attachFileAssociations();
return super.start(config);
}
attachFileAssociations() {
// OSX: register open-file event
if (os.isOSX) {
app.on('open-file', async (event, uri) => {
event.preventDefault();
if (uri.endsWith('.ino') && await fs.pathExists(uri)) {
this.openFilePromise.reject();
await this.openSketch(dirname(uri));
}
});
setTimeout(() => this.openFilePromise.resolve(), 500);
} else {
this.openFilePromise.resolve();
}
}
protected async isValidSketchPath(uri: string): Promise<boolean | undefined> {
return typeof uri === 'string' && await fs.pathExists(uri);
}
protected async launch(params: ElectronMainExecutionParams): Promise<void> {
try {
// When running on MacOS, we either have to wait until
// 1. The `open-file` command has been received by the app, rejecting the promise
// 2. A short timeout resolves the promise automatically, falling back to the usual app launch
await this.openFilePromise.promise;
} catch {
// Application has received the `open-file` event and will skip the default application launch
return;
}
if (!os.isOSX && await this.launchFromArgs(params)) {
// Application has received a file in its arguments and will skip the default application launch
return;
}
this.startup = true;
const workspaces: WorkspaceOptions[] | undefined = this.electronStore.get(WORKSPACES);
let useDefault = true;
if (workspaces && workspaces.length > 0) {
for (const workspace of workspaces) {
if (await this.isValidSketchPath(workspace.file)) {
useDefault = false;
await this.openSketch(workspace);
}
}
}
this.startup = false;
if (useDefault) {
super.launch(params);
}
}
protected async launchFromArgs(params: ElectronMainExecutionParams): Promise<boolean> {
// Copy to prevent manipulation of original array
const argCopy = [...params.argv];
let uri: string | undefined;
for (const possibleUri of argCopy) {
if (possibleUri.endsWith('.ino') && await this.isValidSketchPath(possibleUri)) {
uri = possibleUri;
break;
}
}
if (uri) {
await this.openSketch(dirname(uri));
return true;
}
return false;
}
protected async openSketch(workspace: WorkspaceOptions | string): Promise<BrowserWindow> {
const options = await this.getLastWindowOptions();
let file: string;
if (typeof workspace === 'object') {
options.x = workspace.x;
options.y = workspace.y;
options.width = workspace.width;
options.height = workspace.height;
options.isMaximized = workspace.isMaximized;
options.isFullScreen = workspace.isFullScreen;
file = workspace.file;
} else {
file = workspace;
}
const [uri, electronWindow] = await Promise.all([this.createWindowUri(), this.createWindow(options)]);
electronWindow.loadURL(uri.withFragment(encodeURI(file)).toString(true));
return electronWindow;
}
protected avoidOverlap(options: TheiaBrowserWindowOptions): TheiaBrowserWindowOptions {
if (this.startup) {
return options;
}
return super.avoidOverlap(options);
}
protected getTitleBarStyle(): 'native' | 'custom' {
return 'native';
}
protected hookApplicationEvents(): void {
app.on('will-quit', this.onWillQuit.bind(this));
app.on('second-instance', this.onSecondInstance.bind(this));
app.on('window-all-closed', this.onWindowAllClosed.bind(this));
ipcMain.on(RELOAD_REQUESTED_SIGNAL, event => this.handleReload(event));
ipcMain.on(Restart, ({ sender }) => {
this.restart(sender.id);
});
}
protected async onSecondInstance(event: ElectronEvent, argv: string[], cwd: string): Promise<void> {
if (!os.isOSX && await this.launchFromArgs({ cwd, argv, secondInstance: true })) {
// Application has received a file in its arguments
return;
}
super.onSecondInstance(event, argv, cwd);
}
/**
* Use this rather than creating `BrowserWindow` instances from scratch, since some security parameters need to be set, this method will do it.
*
* @param options
*/
async createWindow(
asyncOptions: MaybePromise<TheiaBrowserWindowOptions> = this.getDefaultTheiaWindowOptions()
): Promise<BrowserWindow> {
let options = await asyncOptions;
options = this.avoidOverlap(options);
let electronWindow: BrowserWindow | undefined;
if (this._windows.length) {
electronWindow = new BrowserWindow(options);
} else {
const { bounds } = screen.getDisplayNearestPoint(
screen.getCursorScreenPoint()
);
const splashHeight = 450;
const splashWidth = 600;
const splashY = Math.floor(bounds.y + (bounds.height - splashHeight) / 2);
const splashX = Math.floor(bounds.x + (bounds.width - splashWidth) / 2);
const splashScreenOpts: BrowserWindowConstructorOptions = {
height: splashHeight,
width: splashWidth,
x: splashX,
y: splashY,
transparent: true,
alwaysOnTop: true,
focusable: false,
minimizable: false,
maximizable: false,
hasShadow: false,
resizable: false,
};
electronWindow = initSplashScreen(
{
windowOpts: options,
templateUrl: join(
__dirname,
'..',
'..',
'..',
'src',
'electron-main',
'splash',
'static',
'splash.html'
),
delay: 0,
minVisible: 2000,
splashScreenOpts,
},
this.splashService.onCloseRequested
);
}
electronWindow.webContents.on(
'new-window',
(event, url, frameName, disposition, options) => {
if (frameName === 'serialPlotter') {
event.preventDefault();
Object.assign(options, {
width: 800,
minWidth: 620,
height: 500,
minHeight: 320,
x: 100,
y: 100,
webPreferences: {
devTools: true,
nativeWindowOpen: true,
openerId: electronWindow?.webContents.id,
},
});
event.newGuest = new BrowserWindow(options);
event.newGuest.setMenu(null);
event.newGuest?.on('closed', (e: any) => {
electronWindow?.webContents.send('CLOSE_CHILD_WINDOW');
});
event.newGuest?.loadURL(url);
}
}
);
this._windows.push(electronWindow);
electronWindow.on('closed', () => {
if (electronWindow) {
const index = this._windows.indexOf(electronWindow);
if (index === -1) {
console.warn(
`Could not dispose browser window: '${electronWindow.title}'.`
);
} else {
this._windows.splice(index, 1);
electronWindow = undefined;
}
}
});
this.attachClosedWorkspace(electronWindow);
this.attachReadyToShow(electronWindow);
this.attachSaveWindowState(electronWindow);
this.attachGlobalShortcuts(electronWindow);
this.restoreMaximizedState(electronWindow, options);
electronRemoteMain.enable(electronWindow.webContents);
return electronWindow;
}
protected async startBackend(): Promise<number> {
// Check if we should run everything as one process.
const noBackendFork = process.argv.indexOf('--no-cluster') !== -1;
// We cannot use the `process.cwd()` as the application project path (the location of the `package.json` in other words)
// in a bundled electron application because it depends on the way we start it. For instance, on OS X, these are a differences:
// https://github.com/eclipse-theia/theia/issues/3297#issuecomment-439172274
process.env.THEIA_APP_PROJECT_PATH = this.globals.THEIA_APP_PROJECT_PATH;
// Set the electron version for both the dev and the production mode. (https://github.com/eclipse-theia/theia/issues/3254)
// Otherwise, the forked backend processes will not know that they're serving the electron frontend.
process.env.THEIA_ELECTRON_VERSION = process.versions.electron;
if (noBackendFork) {
process.env[ElectronSecurityToken] = JSON.stringify(
this.electronSecurityToken
);
// The backend server main file is supposed to export a promise resolving with the port used by the http(s) server.
const address: AddressInfo = await require(this.globals
.THEIA_BACKEND_MAIN_PATH);
return address.port;
} else {
let args = this.processArgv.getProcessArgvWithoutBin();
// https://github.com/eclipse-theia/theia/issues/8227
if (process.platform === 'darwin') {
// https://github.com/electron/electron/issues/3657
// https://stackoverflow.com/questions/10242115/os-x-strange-psn-command-line-parameter-when-launched-from-finder#comment102377986_10242200
// macOS appends an extra `-psn_0_someNumber` arg if a file is opened from Finder after downloading from the Internet.
// "AppName" is an app downloaded from the Internet. Are you sure you want to open it?
args = args.filter((arg) => !arg.startsWith('-psn'));
}
const backendProcess = fork(
this.globals.THEIA_BACKEND_MAIN_PATH,
args,
await this.getForkOptions()
);
console.log(`Starting backend process. PID: ${backendProcess.pid}`);
return new Promise((resolve, reject) => {
// The backend server main file is also supposed to send the resolved http(s) server port via IPC.
backendProcess.on('message', (address: AddressInfo) => {
resolve(address.port);
});
backendProcess.on('error', (error) => {
reject(error);
});
app.on('quit', () => {
try {
// If we forked the process for the clusters, we need to manually terminate it.
// See: https://github.com/eclipse-theia/theia/issues/835
if (backendProcess.pid) {
process.kill(backendProcess.pid);
}
} catch (e) {
if (e.code === 'ESRCH') {
console.log(
'Could not terminate the backend process. It was not running.'
);
return;
}
throw e;
}
});
});
}
}
protected closedWorkspaces: WorkspaceOptions[] = [];
protected attachClosedWorkspace(window: BrowserWindow): void {
// Since the `before-quit` event is only fired when closing the *last* window
// We need to keep track of recently closed windows/workspaces manually
window.on('close', () => {
const url = window.webContents.getURL();
const workspace = URI.parse(url).fragment;
if (workspace) {
const workspaceUri = URI.file(workspace);
const bounds = window.getNormalBounds();
this.closedWorkspaces.push({
...bounds,
isMaximized: window.isMaximized(),
isFullScreen: window.isFullScreen(),
file: workspaceUri.fsPath,
time: Date.now()
})
}
});
}
protected onWillQuit(event: Electron.Event): void {
// Only add workspaces which were closed within the last second (1000 milliseconds)
const threshold = Date.now() - 1000;
const visited = new Set<string>();
const workspaces = this.closedWorkspaces.filter(e => {
if (e.time < threshold || visited.has(e.file)) {
return false;
}
visited.add(e.file);
return true;
}).sort((a, b) => a.file.localeCompare(b.file));
this.electronStore.set(WORKSPACES, workspaces);
super.onWillQuit(event);
}
get windows(): BrowserWindow[] {
return this._windows.slice();
}
}