-
Notifications
You must be signed in to change notification settings - Fork 382
/
app.ts
357 lines (317 loc) · 10.5 KB
/
app.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
import { BrowserWindow, BrowserWindowConstructorOptions, app } from 'electron';
import { ConstructorOf, Injector } from '@opensumi/di';
import {
ContributionProvider,
EventBusImpl,
ExtensionCandidate,
IEventBus,
URI,
asExtensionCandidate,
createContributionProvider,
} from '@opensumi/ide-core-common';
import { IElectronMainLifeCycleService } from '@opensumi/ide-core-common/lib/electron';
import { suppressNodeJSEpipeError } from '@opensumi/ide-core-common/lib/node';
import { argv } from '@opensumi/ide-core-common/lib/node/cli';
import { ElectronMainModule } from '../electron-main-module';
import { ElectronMainApiRegistryImpl, ElectronURLHandlerRegistryImpl } from './api';
import { serviceProviders } from './services';
import { WindowCreatedEvent, WindowDestroyedEvent } from './services/events';
import {
ElectronAppConfig,
ElectronMainApiRegistry,
ElectronMainContribution,
ElectronURLHandlerRegistry,
ICodeWindowOptions,
IElectronMainApiProvider,
IElectronMainApp,
IParsedArgs,
} from './types';
import { CodeWindow } from './window';
export interface IWindowOpenOptions {
windowId: number;
// @deprecated
replace?: boolean;
}
export class ElectronMainApp {
private codeWindows: Map<number, CodeWindow> = new Map();
private injector: Injector;
private modules: ElectronMainModule[] = [];
private parsedArgs: IParsedArgs = {
extensionDir: argv.extensionDir as string | undefined,
extensionCandidate: argv.extensionCandidate
? Array.isArray(argv.extensionCandidate)
? argv.extensionCandidate
: [argv.extensionCandidate]
: [],
extensionDevelopmentPath: argv.extensionDevelopmentPath as string | undefined,
};
constructor(private config: ElectronAppConfig) {
this.injector = config.injector || new Injector();
config.extensionDir = this.parsedArgs.extensionDir ? this.parsedArgs.extensionDir : config.extensionDir || '';
config.extensionCandidate = [
...config.extensionCandidate,
...this.parsedArgs.extensionCandidate.map((e) => asExtensionCandidate(e, false)),
];
if (this.parsedArgs.extensionDevelopmentPath) {
config.extensionCandidate = config.extensionCandidate.concat(
Array.isArray(this.parsedArgs.extensionDevelopmentPath)
? this.parsedArgs.extensionDevelopmentPath.map((e) => asExtensionCandidate(e, true))
: [asExtensionCandidate(this.parsedArgs.extensionDevelopmentPath, true)],
);
}
config.extensionDevelopmentHost = !!this.parsedArgs.extensionDevelopmentPath;
this.injector.addProviders(
{
token: IEventBus,
useClass: EventBusImpl,
},
{
token: ElectronAppConfig,
useValue: config,
},
{
token: IElectronMainApp,
useValue: this,
},
{
token: ElectronURLHandlerRegistry,
useClass: ElectronURLHandlerRegistryImpl,
},
{
token: ElectronMainApiRegistry,
useClass: ElectronMainApiRegistryImpl,
},
...serviceProviders,
);
this.bootstrapEPIPESuppression();
this.injectLifecycleApi();
createContributionProvider(this.injector, ElectronMainContribution);
this.createElectronMainModules(this.config.modules);
this.onBeforeReadyContribution();
this.registerMainApis();
this.registerURLHandlers();
}
async init() {
await app.whenReady().then(() => {
this.onStartContribution();
});
}
bootstrapEPIPESuppression() {
suppressNodeJSEpipeError(process, () => {
// do nothing
});
}
registerMainApis() {
for (const contribution of this.contributions) {
if (contribution.registerMainApi) {
contribution.registerMainApi(this.injector.get(ElectronMainApiRegistry));
}
}
}
registerURLHandlers() {
for (const contribution of this.contributions) {
if (contribution.registerURLHandler) {
contribution.registerURLHandler(this.injector.get(ElectronURLHandlerRegistry));
}
}
}
onStartContribution() {
for (const contribution of this.contributions) {
if (contribution.onStart) {
contribution.onStart();
}
}
}
onBeforeReadyContribution() {
for (const contribution of this.contributions) {
if (contribution.beforeAppReady) {
contribution.beforeAppReady();
}
}
}
loadWorkspace(
workspace?: string,
metadata: any = {},
options: BrowserWindowConstructorOptions & ICodeWindowOptions = {},
openOptions?: IWindowOpenOptions,
): CodeWindow {
const formattedWorkspace = this.formatWorkspace(workspace);
if (openOptions && openOptions.windowId) {
const lastWindow = this.getCodeWindowByElectronBrowserWindowId(openOptions.windowId);
if (lastWindow) {
lastWindow.setWorkspace(formattedWorkspace!);
lastWindow.metadata = metadata;
lastWindow.reload();
return lastWindow;
}
}
const window = this.injector.get(CodeWindow, [formattedWorkspace, metadata, options]);
window.start();
if (options.show !== false) {
window.getBrowserWindow().show();
}
const windowId = window.getBrowserWindow().id;
this.codeWindows.set(windowId, window);
window.addDispose({
dispose: () => {
this.injector.get(IEventBus).fire(new WindowDestroyedEvent(window));
this.codeWindows.delete(windowId);
},
});
this.injector.get(IEventBus).fire(new WindowCreatedEvent(window));
return window;
}
get contributions() {
return (
this.injector.get(ElectronMainContribution) as ContributionProvider<ElectronMainContribution>
).getContributions();
}
getCodeWindows() {
return Array.from(this.codeWindows.values());
}
getCodeWindowByElectronBrowserWindowId(id: number) {
for (const window of this.getCodeWindows()) {
if (window.getBrowserWindow() && window.getBrowserWindow().id === id) {
return window;
}
}
}
getCodeWindowByWorkspace(workspace: string) {
const normalizeUri = URI.isUriString(workspace) ? URI.parse(workspace) : URI.file(workspace);
for (const codeWindow of this.getCodeWindows()) {
if (codeWindow.workspace && codeWindow.workspace.toString() === normalizeUri.toString()) {
return codeWindow;
}
}
}
private createElectronMainModules(Constructors: Array<ConstructorOf<ElectronMainModule>> = []) {
for (const Constructor of Constructors) {
this.modules.push(this.injector.get(Constructor));
}
for (const instance of this.modules) {
if (instance.providers) {
this.injector.addProviders(...instance.providers);
}
if (instance.contributionProvider) {
if (Array.isArray(instance.contributionProvider)) {
for (const contributionProvider of instance.contributionProvider) {
createContributionProvider(this.injector, contributionProvider);
}
} else {
createContributionProvider(this.injector, instance.contributionProvider);
}
}
}
}
private injectLifecycleApi() {
const registry: ElectronMainApiRegistry = this.injector.get(ElectronMainApiRegistry);
registry.registerMainApi(IElectronMainLifeCycleService, new ElectronMainLifeCycleApi(this));
}
/**
* 兼容不规范的 url 比如 Windows "file://C:\\path\\to\\測試.html?background=#hash=title1"
* 要转换为 c:\path\to\測試.html
* @param workspace
* @returns string | undefined
*/
private formatWorkspace(workspace?: string): string | undefined {
if (!workspace) {
return undefined;
}
if (URI.isUriString(workspace)) {
// 注意这里如果有 unicode 的字符,获取正确的路径:
// 需要 URI.parse().codeUri.fsPath 或者 URI.parse().codeUri.toString(true)
return new URL(workspace).toString();
} else {
return URI.file(workspace).toString();
}
}
}
class ElectronMainLifeCycleApi implements IElectronMainApiProvider<void> {
eventEmitter: undefined;
constructor(private app: ElectronMainApp) {}
openWorkspace(workspace: string, openOptions: IWindowOpenOptions) {
if (workspace) {
for (const window of this.app.getCodeWindows()) {
if (window.workspace && window.workspace.toString() === workspace) {
window.getBrowserWindow().show();
return;
}
}
}
this.app.loadWorkspace(workspace, {}, {}, openOptions);
}
minimizeWindow(windowId: number) {
const window = BrowserWindow.fromId(windowId);
if (window) {
window.minimize();
}
}
fullscreenWindow(windowId: number) {
const window = BrowserWindow.fromId(windowId);
if (window) {
window.setFullScreen(true);
}
}
maximizeWindow(windowId: number) {
const window = BrowserWindow.fromId(windowId);
if (window) {
window.maximize();
}
}
unmaximizeWindow(windowId: number) {
const window = BrowserWindow.fromId(windowId);
if (window) {
window.unmaximize();
}
}
closeWindow(windowId: number) {
const window = BrowserWindow.fromId(windowId);
if (window) {
const codeWindow = this.app.getCodeWindowByElectronBrowserWindowId(windowId);
if (!codeWindow) {
window.close();
return;
}
if (codeWindow.isReloading) {
codeWindow.isReloading = false;
if (!codeWindow.isRemote) {
// reload 的情况下不需要等待 startNode 执行完
// 所以可以同时执行 startNode 和 reload 前端
codeWindow.startNode();
}
window.webContents.reload();
} else {
// 正常关闭窗口的情况下,需要回收子进程,耗时可能会比较长
// 这里先隐藏窗口,体感会更快
window.hide();
codeWindow.clear().finally(() => {
window.close();
});
}
}
}
reloadWindow(windowId: number) {
const codeWindow = this.app.getCodeWindowByElectronBrowserWindowId(windowId);
if (codeWindow) {
codeWindow.reload();
}
}
setExtensionDir(extensionDir: string, windowId: number) {
const window = BrowserWindow.fromId(windowId);
if (window) {
const codeWindow = this.app.getCodeWindowByElectronBrowserWindowId(windowId);
if (codeWindow) {
codeWindow.setExtensionDir(extensionDir);
}
}
}
setExtensionCandidate(candidate: ExtensionCandidate[], windowId: number) {
const window = BrowserWindow.fromId(windowId);
if (window) {
const codeWindow = this.app.getCodeWindowByElectronBrowserWindowId(windowId);
if (codeWindow) {
codeWindow.setExtensionCandidate(candidate);
}
}
}
}