-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
frontend-application.ts
394 lines (355 loc) · 14.9 KB
/
frontend-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
388
389
390
391
392
393
394
/********************************************************************************
* Copyright (C) 2017 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { inject, injectable, named } from 'inversify';
import { ContributionProvider, CommandRegistry, MenuModelRegistry, isOSX } from '../common';
import { MaybePromise } from '../common/types';
import { KeybindingRegistry } from './keybinding';
import { Widget } from './widgets';
import { ApplicationShell } from './shell/application-shell';
import { ShellLayoutRestorer, ApplicationShellLayoutMigrationError } from './shell/shell-layout-restorer';
import { FrontendApplicationStateService } from './frontend-application-state';
import { preventNavigation, parseCssTime, animationFrame } from './browser';
import { CorePreferences } from './core-preferences';
import { WindowService } from './window/window-service';
/**
* Clients can implement to get a callback for contributing widgets to a shell on start.
*/
export const FrontendApplicationContribution = Symbol('FrontendApplicationContribution');
export interface FrontendApplicationContribution {
/**
* Called on application startup before configure is called.
*/
initialize?(): void;
/**
* Called before commands, key bindings and menus are initialized.
* Should return a promise if it runs asynchronously.
*/
configure?(app: FrontendApplication): MaybePromise<void>;
/**
* Called when the application is started. The application shell is not attached yet when this method runs.
* Should return a promise if it runs asynchronously.
*/
onStart?(app: FrontendApplication): MaybePromise<void>;
/**
* Called on `beforeunload` event, right before the window closes.
* Return `true` in order to prevent exit.
* Note: No async code allowed, this function has to run on one tick.
*/
onWillStop?(app: FrontendApplication): boolean | void;
/**
* Called when an application is stopped or unloaded.
*
* Note that this is implemented using `window.beforeunload` which doesn't allow any asynchronous code anymore.
* I.e. this is the last tick.
*/
onStop?(app: FrontendApplication): void;
/**
* Called after the application shell has been attached in case there is no previous workbench layout state.
* Should return a promise if it runs asynchronously.
*/
initializeLayout?(app: FrontendApplication): MaybePromise<void>;
/**
* An event is emitted when a layout is initialized, but before the shell is attached.
*/
onDidInitializeLayout?(app: FrontendApplication): MaybePromise<void>;
}
const TIMER_WARNING_THRESHOLD = 100;
/**
* Default frontend contribution that can be extended by clients if they do not want to implement any of the
* methods from the interface but still want to contribute to the frontend application.
*/
@injectable()
export abstract class DefaultFrontendApplicationContribution implements FrontendApplicationContribution {
initialize(): void {
// NOOP
}
}
@injectable()
export class FrontendApplication {
@inject(CorePreferences)
protected readonly corePreferences: CorePreferences;
@inject(WindowService)
protected readonly windowsService: WindowService;
constructor(
@inject(CommandRegistry) protected readonly commands: CommandRegistry,
@inject(MenuModelRegistry) protected readonly menus: MenuModelRegistry,
@inject(KeybindingRegistry) protected readonly keybindings: KeybindingRegistry,
@inject(ShellLayoutRestorer) protected readonly layoutRestorer: ShellLayoutRestorer,
@inject(ContributionProvider) @named(FrontendApplicationContribution)
protected readonly contributions: ContributionProvider<FrontendApplicationContribution>,
@inject(ApplicationShell) protected readonly _shell: ApplicationShell,
@inject(FrontendApplicationStateService) protected readonly stateService: FrontendApplicationStateService
) { }
get shell(): ApplicationShell {
return this._shell;
}
/**
* Start the frontend application.
*
* Start up consists of the following steps:
* - start frontend contributions
* - attach the application shell to the host element
* - initialize the application shell layout
* - reveal the application shell if it was hidden by a startup indicator
*/
async start(): Promise<void> {
await this.startContributions();
this.stateService.state = 'started_contributions';
const host = await this.getHost();
this.attachShell(host);
await animationFrame();
this.stateService.state = 'attached_shell';
await this.initializeLayout();
this.stateService.state = 'initialized_layout';
await this.fireOnDidInitializeLayout();
await this.revealShell(host);
this.registerEventListeners();
this.stateService.state = 'ready';
}
/**
* Return a promise to the host element to which the application shell is attached.
*/
protected getHost(): Promise<HTMLElement> {
if (document.body) {
return Promise.resolve(document.body);
}
return new Promise<HTMLElement>(resolve =>
window.addEventListener('load', () => resolve(document.body), { once: true })
);
}
/**
* Return an HTML element that indicates the startup phase, e.g. with an animation or a splash screen.
*/
protected getStartupIndicator(host: HTMLElement): HTMLElement | undefined {
const startupElements = host.getElementsByClassName('theia-preload');
return startupElements.length === 0 ? undefined : startupElements[0] as HTMLElement;
}
/* vvv HOTFIX begin vvv
*
* This is a hotfix against issues eclipse/theia#6459 and gitpod-io/gitpod#875 .
* It should be reverted after Theia was updated to the newer Monaco.
*/
protected inComposition = false;
/**
* Register composition related event listeners.
*/
protected registerCompositionEventListeners(): void {
window.document.addEventListener('compositionstart', event => {
this.inComposition = true;
});
window.document.addEventListener('compositionend', event => {
this.inComposition = false;
});
}
/* ^^^ HOTFIX end ^^^ */
/**
* Register global event listeners.
*/
protected registerEventListeners(): void {
this.registerCompositionEventListeners(); /* Hotfix. See above. */
this.windowsService.onUnload(() => {
this.stateService.state = 'closing_window';
this.layoutRestorer.storeLayout(this);
this.stopContributions();
});
window.addEventListener('resize', () => this.shell.update());
document.addEventListener('keydown', event => {
if (this.inComposition !== true) {
this.keybindings.run(event);
}
}, true);
document.addEventListener('touchmove', event => { event.preventDefault(); }, { passive: false });
// Prevent forward/back navigation by scrolling in OS X
if (isOSX) {
document.body.addEventListener('wheel', preventNavigation, { passive: false });
}
// Prevent the default browser behavior when dragging and dropping files into the window.
window.addEventListener('dragover', event => {
event.preventDefault();
}, false);
window.addEventListener('drop', event => {
event.preventDefault();
}, false);
}
/**
* Attach the application shell to the host element. If a startup indicator is present, the shell is
* inserted before that indicator so it is not visible yet.
*/
protected attachShell(host: HTMLElement): void {
const ref = this.getStartupIndicator(host);
Widget.attach(this.shell, host, ref);
}
/**
* If a startup indicator is present, it is first hidden with the `theia-hidden` CSS class and then
* removed after a while. The delay until removal is taken from the CSS transition duration.
*/
protected revealShell(host: HTMLElement): Promise<void> {
const startupElem = this.getStartupIndicator(host);
if (startupElem) {
return new Promise(resolve => {
window.requestAnimationFrame(() => {
startupElem.classList.add('theia-hidden');
const preloadStyle = window.getComputedStyle(startupElem);
const transitionDuration = parseCssTime(preloadStyle.transitionDuration, 0);
window.setTimeout(() => {
const parent = startupElem.parentElement;
if (parent) {
parent.removeChild(startupElem);
}
resolve();
}, transitionDuration);
});
});
} else {
return Promise.resolve();
}
}
/**
* Initialize the shell layout either using the layout restorer service or, if no layout has
* been stored, by creating the default layout.
*/
protected async initializeLayout(): Promise<void> {
if (!await this.restoreLayout()) {
// Fallback: Create the default shell layout
await this.createDefaultLayout();
}
await this.shell.pendingUpdates;
}
/**
* Try to restore the shell layout from the storage service. Resolves to `true` if successful.
*/
protected async restoreLayout(): Promise<boolean> {
try {
return await this.layoutRestorer.restoreLayout(this);
} catch (error) {
if (ApplicationShellLayoutMigrationError.is(error)) {
console.warn(error.message);
console.info('Initializing the default layout instead...');
} else {
console.error('Could not restore layout', error);
}
return false;
}
}
/**
* Let the frontend application contributions initialize the shell layout. Override this
* method in order to create an application-specific custom layout.
*/
protected async createDefaultLayout(): Promise<void> {
for (const contribution of this.contributions.getContributions()) {
if (contribution.initializeLayout) {
await this.measure(contribution.constructor.name + '.initializeLayout',
() => contribution.initializeLayout!(this)
);
}
}
}
protected async fireOnDidInitializeLayout(): Promise<void> {
for (const contribution of this.contributions.getContributions()) {
if (contribution.onDidInitializeLayout) {
await this.measure(contribution.constructor.name + '.onDidInitializeLayout',
() => contribution.onDidInitializeLayout!(this)
);
}
}
}
/**
* Initialize and start the frontend application contributions.
*/
protected async startContributions(): Promise<void> {
for (const contribution of this.contributions.getContributions()) {
if (contribution.initialize) {
try {
await this.measure(contribution.constructor.name + '.initialize',
() => contribution.initialize!()
);
} catch (error) {
console.error('Could not initialize contribution', error);
}
}
}
for (const contribution of this.contributions.getContributions()) {
if (contribution.configure) {
try {
await this.measure(contribution.constructor.name + '.configure',
() => contribution.configure!(this)
);
} catch (error) {
console.error('Could not configure contribution', error);
}
}
}
/**
* FIXME:
* - decouple commands & menus
* - consider treat commands, keybindings and menus as frontend application contributions
*/
await this.measure('commands.onStart',
() => this.commands.onStart()
);
await this.measure('keybindings.onStart',
() => this.keybindings.onStart()
);
await this.measure('menus.onStart',
() => this.menus.onStart()
);
for (const contribution of this.contributions.getContributions()) {
if (contribution.onStart) {
try {
await this.measure(contribution.constructor.name + '.onStart',
() => contribution.onStart!(this)
);
} catch (error) {
console.error('Could not start contribution', error);
}
}
}
}
/**
* Stop the frontend application contributions. This is called when the window is unloaded.
*/
protected stopContributions(): void {
console.info('>>> Stopping frontend contributions...');
for (const contribution of this.contributions.getContributions()) {
if (contribution.onStop) {
try {
contribution.onStop(this);
} catch (error) {
console.error('Could not stop contribution', error);
}
}
}
console.info('<<< All frontend contributions have been stopped.');
}
protected async measure<T>(name: string, fn: () => MaybePromise<T>): Promise<T> {
const startMark = name + '-start';
const endMark = name + '-end';
performance.mark(startMark);
const result = await fn();
performance.mark(endMark);
performance.measure(name, startMark, endMark);
for (const item of performance.getEntriesByName(name)) {
const contribution = `Frontend ${item.name}`;
if (item.duration > TIMER_WARNING_THRESHOLD) {
console.warn(`${contribution} is slow, took: ${item.duration.toFixed(1)} ms`);
} else {
console.debug(`${contribution} took: ${item.duration.toFixed(1)} ms`);
}
}
performance.clearMeasures(name);
return result;
}
}