-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
plugin-view-registry.ts
481 lines (443 loc) · 19.8 KB
/
plugin-view-registry.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/********************************************************************************
* Copyright (C) 2018 Red Hat, Inc. 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 { injectable, inject, postConstruct } from 'inversify';
import {
ApplicationShell, ViewContainer as ViewContainerWidget, WidgetManager,
ViewContainerIdentifier, ViewContainerTitleOptions, Widget, FrontendApplicationContribution,
StatefulWidget, CommonMenus
} from '@theia/core/lib/browser';
import { ViewContainer, View } from '../../../common';
import { PluginSharedStyle } from '../plugin-shared-style';
import { DebugWidget } from '@theia/debug/lib/browser/view/debug-widget';
import { PluginViewWidget, PluginViewWidgetIdentifier } from './plugin-view-widget';
import { SCM_VIEW_CONTAINER_ID, ScmContribution } from '@theia/scm/lib/browser/scm-contribution';
import { EXPLORER_VIEW_CONTAINER_ID } from '@theia/navigator/lib/browser';
import { FileNavigatorContribution } from '@theia/navigator/lib/browser/navigator-contribution';
import { DebugFrontendApplicationContribution } from '@theia/debug/lib/browser/debug-frontend-application-contribution';
import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
import { CommandRegistry } from '@theia/core/lib/common/command';
import { MenuModelRegistry } from '@theia/core/lib/common/menu';
import { QuickViewService } from '@theia/core/lib/browser/quick-view-service';
import { Emitter } from '@theia/core/lib/common/event';
import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';
export const PLUGIN_VIEW_FACTORY_ID = 'plugin-view';
export const PLUGIN_VIEW_CONTAINER_FACTORY_ID = 'plugin-view-container';
export const PLUGIN_VIEW_DATA_FACTORY_ID = 'plugin-view-data';
export type ViewDataProvider = (params: { state?: object, viewInfo: View }) => Promise<Widget>;
@injectable()
export class PluginViewRegistry implements FrontendApplicationContribution {
@inject(ApplicationShell)
protected readonly shell: ApplicationShell;
@inject(PluginSharedStyle)
protected readonly style: PluginSharedStyle;
@inject(WidgetManager)
protected readonly widgetManager: WidgetManager;
@inject(ScmContribution)
protected readonly scm: ScmContribution;
@inject(FileNavigatorContribution)
protected readonly explorer: FileNavigatorContribution;
@inject(DebugFrontendApplicationContribution)
protected readonly debug: DebugFrontendApplicationContribution;
@inject(CommandRegistry)
protected readonly commands: CommandRegistry;
@inject(MenuModelRegistry)
protected readonly menus: MenuModelRegistry;
@inject(QuickViewService)
protected readonly quickView: QuickViewService;
@inject(ContextKeyService)
protected readonly contextKeyService: ContextKeyService;
protected readonly onDidExpandViewEmitter = new Emitter<string>();
readonly onDidExpandView = this.onDidExpandViewEmitter.event;
private readonly views = new Map<string, [string, View]>();
private readonly viewContainers = new Map<string, [string, ViewContainerTitleOptions]>();
private readonly containerViews = new Map<string, string[]>();
private readonly viewDataProviders = new Map<string, ViewDataProvider>();
private readonly viewDataState = new Map<string, object>();
@postConstruct()
protected init(): void {
this.widgetManager.onDidCreateWidget(({ factoryId, widget }) => {
if (factoryId === EXPLORER_VIEW_CONTAINER_ID && widget instanceof ViewContainerWidget) {
this.prepareViewContainer('explorer', widget);
}
if (factoryId === SCM_VIEW_CONTAINER_ID && widget instanceof ViewContainerWidget) {
this.prepareViewContainer('scm', widget);
}
if (factoryId === DebugWidget.ID && widget instanceof DebugWidget) {
const viewContainer = widget['sessionWidget']['viewContainer'];
this.prepareViewContainer('debug', viewContainer);
}
if (factoryId === PLUGIN_VIEW_CONTAINER_FACTORY_ID && widget instanceof ViewContainerWidget) {
this.prepareViewContainer(this.toViewContainerId(widget.options), widget);
}
if (factoryId === PLUGIN_VIEW_FACTORY_ID && widget instanceof PluginViewWidget) {
this.prepareView(widget);
}
});
this.doRegisterViewContainer('test', 'left', {
label: 'Test',
iconClass: 'theia-plugin-test-tab-icon',
closeable: true
});
this.contextKeyService.onDidChange(e => {
for (const [, view] of this.views.values()) {
if (view.when === undefined) {
continue;
}
if (e.affects(new Set([view.when]))) {
this.updateViewVisibility(view.id);
}
}
});
}
protected async updateViewVisibility(viewId: string): Promise<void> {
const viewInfo = this.views.get(viewId);
if (!viewInfo) {
return;
}
const [viewContainerId] = viewInfo;
const viewContainer = await this.getPluginViewContainer(viewContainerId);
if (!viewContainer) {
return;
}
const widget = await this.getView(viewId);
if (!widget) {
return;
}
const part = viewContainer.getPartFor(widget);
if (!part) {
return;
}
widget.updateViewVisibility(() =>
part.setHidden(!this.isViewVisible(viewId))
);
}
protected isViewVisible(viewId: string): boolean {
const viewInfo = this.views.get(viewId);
if (!viewInfo) {
return false;
}
const [, view] = viewInfo;
return view.when === undefined || this.contextKeyService.match(view.when);
}
registerViewContainer(location: string, viewContainer: ViewContainer): void {
if (this.viewContainers.has(viewContainer.id)) {
console.warn('view container such id alredy registered: ', JSON.stringify(viewContainer));
return;
}
const iconClass = 'plugin-view-container-icon-' + viewContainer.id;
this.style.insertRule('.' + iconClass, () => `
mask: url('${viewContainer.iconUrl}') no-repeat 50% 50%;
-webkit-mask: url('${viewContainer.iconUrl}') no-repeat 50% 50%;
`);
this.doRegisterViewContainer(viewContainer.id, location, {
label: viewContainer.title,
iconClass,
closeable: true
});
}
protected doRegisterViewContainer(id: string, location: string, options: ViewContainerTitleOptions): void {
this.viewContainers.set(id, [location, options]);
const toggleCommandId = `plugin.view-container.${id}.toggle`;
this.commands.registerCommand({
id: toggleCommandId,
label: 'Toggle ' + options.label + ' View'
}, {
execute: async () => {
let widget = await this.getPluginViewContainer(id);
if (widget) {
widget.dispose();
} else {
widget = await this.openViewContainer(id);
if (widget) {
this.shell.activateWidget(widget.id);
}
}
}
});
this.menus.registerMenuAction(CommonMenus.VIEW_VIEWS, {
commandId: toggleCommandId,
label: options.label
});
}
registerView(viewContainerId: string, view: View): void {
if (this.views.has(view.id)) {
console.warn('view with such id alredy registered: ', JSON.stringify(view));
return;
}
this.views.set(view.id, [viewContainerId, view]);
const containerViews = this.containerViews.get(viewContainerId) || [];
containerViews.push(view.id);
this.containerViews.set(viewContainerId, containerViews);
this.quickView.registerItem({
label: view.name,
open: async () => {
const widget = await this.openView(view.id);
if (widget) {
this.shell.activateWidget(widget.id);
}
}
});
}
async getView(viewId: string): Promise<PluginViewWidget | undefined> {
if (!this.views.has(viewId)) {
return undefined;
}
return this.widgetManager.getWidget<PluginViewWidget>(PLUGIN_VIEW_FACTORY_ID, this.toPluginViewWidgetIdentifier(viewId));
}
async openView(viewId: string): Promise<PluginViewWidget | undefined> {
const widget = await this.getView(viewId);
if (widget) {
return widget;
}
const data = this.views.get(viewId);
if (!data) {
return undefined;
}
const [containerId] = data;
await this.openViewContainer(containerId);
return this.getView(viewId);
}
protected async prepareView(widget: PluginViewWidget): Promise<void> {
const data = this.views.get(widget.options.viewId);
if (!data) {
return;
}
const [, view] = data;
widget.title.label = view.name;
const currentDataWidget = widget.widgets[0];
const viewDataWidget = await this.createViewDataWidget(view.id);
if (currentDataWidget !== viewDataWidget) {
if (currentDataWidget) {
currentDataWidget.dispose();
}
if (viewDataWidget) {
widget.addWidget(viewDataWidget);
}
}
}
async openViewContainer(containerId: string): Promise<ViewContainerWidget | undefined> {
if (containerId === 'exporer') {
const widget = await this.explorer.openView();
if (widget.parent instanceof ViewContainerWidget) {
return widget.parent;
}
return undefined;
}
if (containerId === 'scm') {
const widget = await this.scm.openView();
if (widget.parent instanceof ViewContainerWidget) {
return widget.parent;
}
return undefined;
}
if (containerId === 'debug') {
const widget = await this.debug.openView();
return widget['sessionWidget']['viewContainer'];
}
const data = this.viewContainers.get(containerId);
if (!data) {
return undefined;
}
const [location] = data;
const identifier = this.toViewContainerIdentifier(containerId);
const containerWidget = await this.widgetManager.getOrCreateWidget<ViewContainerWidget>(PLUGIN_VIEW_CONTAINER_FACTORY_ID, identifier);
if (!containerWidget.isAttached) {
await this.shell.addWidget(containerWidget, {
area: ApplicationShell.isSideArea(location) ? location : 'left',
rank: Number.MAX_SAFE_INTEGER
});
}
return containerWidget;
}
protected async prepareViewContainer(viewContainerId: string, containerWidget: ViewContainerWidget): Promise<void> {
const data = this.viewContainers.get(viewContainerId);
if (data) {
const [, options] = data;
containerWidget.setTitleOptions(options);
}
for (const viewId of this.containerViews.get(viewContainerId) || []) {
const identifier = this.toPluginViewWidgetIdentifier(viewId);
const widget = await this.widgetManager.getOrCreateWidget<PluginViewWidget>(PLUGIN_VIEW_FACTORY_ID, identifier);
if (containerWidget.getTrackableWidgets().indexOf(widget) === -1) {
containerWidget.addWidget(widget, {
initiallyCollapsed: !!containerWidget.getParts().length,
initiallyHidden: !this.isViewVisible(viewId)
});
}
const part = containerWidget.getPartFor(widget);
if (part) {
// if a view is explicilty hidden then suppress updating visibility based on `when` closure
part.onVisibilityChanged(() => widget.suppressUpdateViewVisibility = part.isHidden);
const tryFireOnDidExpandView = () => {
if (!part.collapsed && part.isVisible) {
toFire.dispose();
}
};
const toFire = new DisposableCollection(
Disposable.create(() => this.onDidExpandViewEmitter.fire(viewId)),
part.onCollapsed(tryFireOnDidExpandView),
part.onVisibilityChanged(tryFireOnDidExpandView)
);
tryFireOnDidExpandView();
}
}
}
protected async getPluginViewContainer(viewContainerId: string): Promise<ViewContainerWidget | undefined> {
if (viewContainerId === 'explorer') {
return this.widgetManager.getWidget<ViewContainerWidget>(EXPLORER_VIEW_CONTAINER_ID);
}
if (viewContainerId === 'scm') {
return this.widgetManager.getWidget<ViewContainerWidget>(SCM_VIEW_CONTAINER_ID);
}
if (viewContainerId === 'debug') {
const debug = await this.widgetManager.getWidget(DebugWidget.ID);
if (debug instanceof DebugWidget) {
return debug['sessionWidget']['viewContainer'];
}
}
const identifier = this.toViewContainerIdentifier(viewContainerId);
return this.widgetManager.getWidget<ViewContainerWidget>(PLUGIN_VIEW_CONTAINER_FACTORY_ID, identifier);
}
async initWidgets(): Promise<void> {
const promises: Promise<void>[] = [];
for (const id of this.viewContainers.keys()) {
promises.push((async () => {
let viewContainer = await this.getPluginViewContainer(id);
if (!viewContainer) {
viewContainer = await this.openViewContainer(id);
if (viewContainer && !viewContainer.getTrackableWidgets().length) {
// close empty view containers
viewContainer.dispose();
}
} else {
await this.prepareViewContainer(this.toViewContainerId(viewContainer.options), viewContainer);
}
})().catch(console.error));
}
promises.push((async () => {
const explorer = await this.widgetManager.getWidget(EXPLORER_VIEW_CONTAINER_ID);
if (explorer instanceof ViewContainerWidget) {
await this.prepareViewContainer('explorer', explorer);
}
})().catch(console.error));
promises.push((async () => {
const scm = await this.widgetManager.getWidget(SCM_VIEW_CONTAINER_ID);
if (scm instanceof ViewContainerWidget) {
await this.prepareViewContainer('scm', scm);
}
})().catch(console.error));
promises.push((async () => {
const debug = await this.widgetManager.getWidget(DebugWidget.ID);
if (debug instanceof DebugWidget) {
const viewContainer = debug['sessionWidget']['viewContainer'];
await this.prepareViewContainer('debug', viewContainer);
}
})().catch(console.error));
await Promise.all(promises);
}
async removeStaleWidgets(): Promise<void> {
const views = this.widgetManager.getWidgets(PLUGIN_VIEW_FACTORY_ID);
for (const view of views) {
if (view instanceof PluginViewWidget) {
const id = this.toViewId(view.options);
if (!this.views.has(id)) {
view.dispose();
}
}
}
const viewContainers = this.widgetManager.getWidgets(PLUGIN_VIEW_CONTAINER_FACTORY_ID);
for (const viewContainer of viewContainers) {
if (viewContainer instanceof ViewContainerWidget) {
const id = this.toViewContainerId(viewContainer.options);
if (!this.viewContainers.has(id)) {
viewContainer.dispose();
}
}
}
}
protected toViewContainerIdentifier(viewContainerId: string): ViewContainerIdentifier {
return { id: PLUGIN_VIEW_CONTAINER_FACTORY_ID + ':' + viewContainerId };
}
protected toViewContainerId(identifier: ViewContainerIdentifier): string {
return identifier.id.substr(PLUGIN_VIEW_CONTAINER_FACTORY_ID.length + 1);
}
protected toPluginViewWidgetIdentifier(viewId: string): PluginViewWidgetIdentifier {
return { id: PLUGIN_VIEW_FACTORY_ID + ':' + viewId, viewId };
}
protected toViewId(identifier: PluginViewWidgetIdentifier): string {
return identifier.viewId;
}
/**
* retrieve restored layout state from previous user session but close widgets
* widgets should be opened only when view data providers are registered
*/
onDidInitializeLayout(): void {
const widgets = this.widgetManager.getWidgets(PLUGIN_VIEW_DATA_FACTORY_ID);
for (const widget of widgets) {
if (StatefulWidget.is(widget)) {
this.viewDataState.set(widget.id, widget.storeState());
}
widget.dispose();
}
}
registerViewDataProvider(viewId: string, provider: ViewDataProvider): Disposable {
if (this.viewDataProviders.has(viewId)) {
console.error(`data provider for '${viewId}' view is already registrered`);
return Disposable.NULL;
}
this.getView(viewId).then(async view => {
if (view) {
if (view.isVisible) {
await this.prepareView(view);
} else {
const toDispose = new DisposableCollection(this.onDidExpandView(async id => {
if (id === viewId) {
await this.prepareView(view);
}
}));
const unsubscribe = () => toDispose.dispose();
view.disposed.connect(unsubscribe);
toDispose.push(Disposable.create(() => view.disposed.disconnect(unsubscribe)));
}
}
});
this.viewDataProviders.set(viewId, provider);
return Disposable.create(() => {
this.viewDataProviders.delete(viewId);
this.viewDataState.delete(viewId);
});
}
protected async createViewDataWidget(viewId: string): Promise<Widget | undefined> {
const view = this.views.get(viewId);
const provider = this.viewDataProviders.get(viewId);
if (!view || !provider) {
return undefined;
}
const [, viewInfo] = view;
const state = this.viewDataState.get(viewId);
const widget = await provider({ state, viewInfo });
if (StatefulWidget.is(widget)) {
const dispose = widget.dispose.bind(widget);
widget.dispose = () => {
this.viewDataState.set(viewId, widget.storeState());
dispose();
};
} else {
this.viewDataState.delete(viewId);
}
return widget;
}
}