-
Notifications
You must be signed in to change notification settings - Fork 28.8k
/
compositePart.ts
524 lines (409 loc) · 17.6 KB
/
compositePart.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'vs/css!./media/compositepart';
import * as nls from 'vs/nls';
import { defaultGenerator } from 'vs/base/common/idGenerator';
import { IDisposable, dispose, DisposableStore, MutableDisposable } from 'vs/base/common/lifecycle';
import { Emitter } from 'vs/base/common/event';
import * as errors from 'vs/base/common/errors';
import { ToolBar } from 'vs/base/browser/ui/toolbar/toolbar';
import { ActionsOrientation, prepareActions } from 'vs/base/browser/ui/actionbar/actionbar';
import { ProgressBar } from 'vs/base/browser/ui/progressbar/progressbar';
import { IAction, WorkbenchActionExecutedEvent, WorkbenchActionExecutedClassification, IActionViewItem } from 'vs/base/common/actions';
import { Part, IPartOptions } from 'vs/workbench/browser/part';
import { Composite, CompositeRegistry } from 'vs/workbench/browser/composite';
import { IComposite } from 'vs/workbench/common/composite';
import { CompositeProgressIndicator } from 'vs/workbench/services/progress/browser/progressIndicator';
import { IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { IProgressIndicator, IEditorProgressService } from 'vs/platform/progress/common/progress';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { attachProgressBarStyler } from 'vs/platform/theme/common/styler';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { Dimension, append, $, hide, show, addClasses } from 'vs/base/browser/dom';
import { AnchorAlignment } from 'vs/base/browser/ui/contextview/contextview';
import { assertIsDefined, withNullAsUndefined } from 'vs/base/common/types';
export interface ICompositeTitleLabel {
/**
* Asks to update the title for the composite with the given ID.
*/
updateTitle(id: string, title: string, keybinding?: string): void;
/**
* Called when theming information changes.
*/
updateStyles(): void;
}
interface CompositeItem {
composite: Composite;
disposable: IDisposable;
progress: IProgressIndicator;
}
export abstract class CompositePart<T extends Composite> extends Part {
protected readonly onDidCompositeOpen = this._register(new Emitter<{ composite: IComposite, focus: boolean }>());
protected readonly onDidCompositeClose = this._register(new Emitter<IComposite>());
protected toolBar: ToolBar | undefined;
protected titleLabelElement: HTMLElement | undefined;
private readonly mapCompositeToCompositeContainer = new Map<string, HTMLElement>();
private readonly mapActionsBindingToComposite = new Map<string, () => void>();
private activeComposite: Composite | undefined;
private lastActiveCompositeId: string;
private readonly instantiatedCompositeItems = new Map<string, CompositeItem>();
private titleLabel: ICompositeTitleLabel | undefined;
private progressBar: ProgressBar | undefined;
private contentAreaSize: Dimension | undefined;
private readonly telemetryActionsListener = this._register(new MutableDisposable());
private currentCompositeOpenToken: string | undefined;
constructor(
private readonly notificationService: INotificationService,
protected readonly storageService: IStorageService,
private readonly telemetryService: ITelemetryService,
protected readonly contextMenuService: IContextMenuService,
protected readonly layoutService: IWorkbenchLayoutService,
protected readonly keybindingService: IKeybindingService,
protected readonly instantiationService: IInstantiationService,
themeService: IThemeService,
protected readonly registry: CompositeRegistry<T>,
private readonly activeCompositeSettingsKey: string,
private readonly defaultCompositeId: string,
private readonly nameForTelemetry: string,
private readonly compositeCSSClass: string,
private readonly titleForegroundColor: string | undefined,
id: string,
options: IPartOptions
) {
super(id, options, themeService, storageService, layoutService);
this.lastActiveCompositeId = storageService.get(activeCompositeSettingsKey, StorageScope.WORKSPACE, this.defaultCompositeId);
}
protected openComposite(id: string, focus?: boolean): Composite | undefined {
// Check if composite already visible and just focus in that case
if (this.activeComposite && this.activeComposite.getId() === id) {
if (focus) {
this.activeComposite.focus();
}
// Fullfill promise with composite that is being opened
return this.activeComposite;
}
// We cannot open the composite if we have not been created yet
if (!this.element) {
return;
}
// Open
return this.doOpenComposite(id, focus);
}
private doOpenComposite(id: string, focus: boolean = false): Composite | undefined {
// Use a generated token to avoid race conditions from long running promises
const currentCompositeOpenToken = defaultGenerator.nextId();
this.currentCompositeOpenToken = currentCompositeOpenToken;
// Hide current
if (this.activeComposite) {
this.hideActiveComposite();
}
// Update Title
this.updateTitle(id);
// Create composite
const composite = this.createComposite(id, true);
// Check if another composite opened meanwhile and return in that case
if ((this.currentCompositeOpenToken !== currentCompositeOpenToken) || (this.activeComposite && this.activeComposite.getId() !== composite.getId())) {
return undefined;
}
// Check if composite already visible and just focus in that case
if (this.activeComposite && this.activeComposite.getId() === composite.getId()) {
if (focus) {
composite.focus();
}
this.onDidCompositeOpen.fire({ composite, focus });
return composite;
}
// Show Composite and Focus
this.showComposite(composite);
if (focus) {
composite.focus();
}
// Return with the composite that is being opened
if (composite) {
this.onDidCompositeOpen.fire({ composite, focus });
}
return composite;
}
protected createComposite(id: string, isActive?: boolean): Composite {
// Check if composite is already created
const compositeItem = this.instantiatedCompositeItems.get(id);
if (compositeItem) {
return compositeItem.composite;
}
// Instantiate composite from registry otherwise
const compositeDescriptor = this.registry.getComposite(id);
if (compositeDescriptor) {
const compositeProgressIndicator = this.instantiationService.createInstance(CompositeProgressIndicator, assertIsDefined(this.progressBar), compositeDescriptor.id, !!isActive);
const compositeInstantiationService = this.instantiationService.createChild(new ServiceCollection(
[IEditorProgressService, compositeProgressIndicator] // provide the editor progress service for any editors instantiated within the composite
));
const composite = compositeDescriptor.instantiate(compositeInstantiationService);
const disposable = new DisposableStore();
// Remember as Instantiated
this.instantiatedCompositeItems.set(id, { composite, disposable, progress: compositeProgressIndicator });
// Register to title area update events from the composite
disposable.add(composite.onTitleAreaUpdate(() => this.onTitleAreaUpdate(composite.getId()), this));
return composite;
}
throw new Error(`Unable to find composite with id ${id}`);
}
protected showComposite(composite: Composite): void {
// Remember Composite
this.activeComposite = composite;
// Store in preferences
const id = this.activeComposite.getId();
if (id !== this.defaultCompositeId) {
this.storageService.store(this.activeCompositeSettingsKey, id, StorageScope.WORKSPACE);
} else {
this.storageService.remove(this.activeCompositeSettingsKey, StorageScope.WORKSPACE);
}
// Remember
this.lastActiveCompositeId = this.activeComposite.getId();
// Composites created for the first time
let compositeContainer = this.mapCompositeToCompositeContainer.get(composite.getId());
if (!compositeContainer) {
// Build Container off-DOM
compositeContainer = $('.composite');
addClasses(compositeContainer, this.compositeCSSClass);
compositeContainer.id = composite.getId();
composite.create(compositeContainer);
composite.updateStyles();
// Remember composite container
this.mapCompositeToCompositeContainer.set(composite.getId(), compositeContainer);
}
// Fill Content and Actions
// Make sure that the user meanwhile did not open another composite or closed the part containing the composite
if (!this.activeComposite || composite.getId() !== this.activeComposite.getId()) {
return undefined;
}
// Take Composite on-DOM and show
const contentArea = this.getContentArea();
if (contentArea) {
contentArea.appendChild(compositeContainer);
}
show(compositeContainer);
// Setup action runner
const toolBar = assertIsDefined(this.toolBar);
toolBar.actionRunner = composite.getActionRunner();
// Update title with composite title if it differs from descriptor
const descriptor = this.registry.getComposite(composite.getId());
if (descriptor && descriptor.name !== composite.getTitle()) {
this.updateTitle(composite.getId(), composite.getTitle());
}
// Handle Composite Actions
let actionsBinding = this.mapActionsBindingToComposite.get(composite.getId());
if (!actionsBinding) {
actionsBinding = this.collectCompositeActions(composite);
this.mapActionsBindingToComposite.set(composite.getId(), actionsBinding);
}
actionsBinding();
// Action Run Handling
this.telemetryActionsListener.value = toolBar.actionRunner.onDidRun(e => {
// Check for Error
if (e.error && !errors.isPromiseCanceledError(e.error)) {
this.notificationService.error(e.error);
}
// Log in telemetry
if (this.telemetryService) {
this.telemetryService.publicLog2<WorkbenchActionExecutedEvent, WorkbenchActionExecutedClassification>('workbenchActionExecuted', { id: e.action.id, from: this.nameForTelemetry });
}
});
// Indicate to composite that it is now visible
composite.setVisible(true);
// Make sure that the user meanwhile did not open another composite or closed the part containing the composite
if (!this.activeComposite || composite.getId() !== this.activeComposite.getId()) {
return;
}
// Make sure the composite is layed out
if (this.contentAreaSize) {
composite.layout(this.contentAreaSize);
}
}
protected onTitleAreaUpdate(compositeId: string): void {
// Active Composite
if (this.activeComposite && this.activeComposite.getId() === compositeId) {
// Title
this.updateTitle(this.activeComposite.getId(), this.activeComposite.getTitle());
// Actions
const actionsBinding = this.collectCompositeActions(this.activeComposite);
this.mapActionsBindingToComposite.set(this.activeComposite.getId(), actionsBinding);
actionsBinding();
}
// Otherwise invalidate actions binding for next time when the composite becomes visible
else {
this.mapActionsBindingToComposite.delete(compositeId);
}
}
private updateTitle(compositeId: string, compositeTitle?: string): void {
const compositeDescriptor = this.registry.getComposite(compositeId);
if (!compositeDescriptor || !this.titleLabel) {
return;
}
if (!compositeTitle) {
compositeTitle = compositeDescriptor.name;
}
const keybinding = this.keybindingService.lookupKeybinding(compositeId);
this.titleLabel.updateTitle(compositeId, compositeTitle, withNullAsUndefined(keybinding?.getLabel()));
const toolBar = assertIsDefined(this.toolBar);
toolBar.setAriaLabel(nls.localize('ariaCompositeToolbarLabel', "{0} actions", compositeTitle));
}
private collectCompositeActions(composite?: Composite): () => void {
// From Composite
const primaryActions: IAction[] = composite?.getActions().slice(0) || [];
const secondaryActions: IAction[] = composite?.getSecondaryActions().slice(0) || [];
// From Part
primaryActions.push(...this.getActions());
secondaryActions.push(...this.getSecondaryActions());
// Update context
const toolBar = assertIsDefined(this.toolBar);
toolBar.context = this.actionsContextProvider();
// Return fn to set into toolbar
return () => toolBar.setActions(prepareActions(primaryActions), prepareActions(secondaryActions));
}
protected getActiveComposite(): IComposite | undefined {
return this.activeComposite;
}
protected getLastActiveCompositetId(): string {
return this.lastActiveCompositeId;
}
protected hideActiveComposite(): Composite | undefined {
if (!this.activeComposite) {
return undefined; // Nothing to do
}
const composite = this.activeComposite;
this.activeComposite = undefined;
const compositeContainer = this.mapCompositeToCompositeContainer.get(composite.getId());
// Indicate to Composite
composite.setVisible(false);
// Take Container Off-DOM and hide
if (compositeContainer) {
compositeContainer.remove();
hide(compositeContainer);
}
// Clear any running Progress
if (this.progressBar) {
this.progressBar.stop().hide();
}
// Empty Actions
if (this.toolBar) {
this.collectCompositeActions()();
}
this.onDidCompositeClose.fire(composite);
return composite;
}
createTitleArea(parent: HTMLElement): HTMLElement {
// Title Area Container
const titleArea = append(parent, $('.composite'));
titleArea.classList.add('title');
// Left Title Label
this.titleLabel = this.createTitleLabel(titleArea);
// Right Actions Container
const titleActionsContainer = append(titleArea, $('.title-actions'));
// Toolbar
this.toolBar = this._register(new ToolBar(titleActionsContainer, this.contextMenuService, {
actionViewItemProvider: action => this.actionViewItemProvider(action),
orientation: ActionsOrientation.HORIZONTAL,
getKeyBinding: action => this.keybindingService.lookupKeybinding(action.id),
anchorAlignmentProvider: () => this.getTitleAreaDropDownAnchorAlignment(),
toggleMenuTitle: nls.localize('viewsAndMoreActions', "Views and More Actions...")
}));
this.collectCompositeActions()();
return titleArea;
}
protected createTitleLabel(parent: HTMLElement): ICompositeTitleLabel {
const titleContainer = append(parent, $('.title-label'));
const titleLabel = append(titleContainer, $('h2'));
this.titleLabelElement = titleLabel;
const $this = this;
return {
updateTitle: (_id, title, keybinding) => {
titleLabel.innerText = title;
titleLabel.title = keybinding ? nls.localize('titleTooltip', "{0} ({1})", title, keybinding) : title;
},
updateStyles: () => {
titleLabel.style.color = $this.titleForegroundColor ? $this.getColor($this.titleForegroundColor) || '' : '';
}
};
}
updateStyles(): void {
super.updateStyles();
// Forward to title label
const titleLabel = assertIsDefined(this.titleLabel);
titleLabel.updateStyles();
}
protected actionViewItemProvider(action: IAction): IActionViewItem | undefined {
// Check Active Composite
if (this.activeComposite) {
return this.activeComposite.getActionViewItem(action);
}
return undefined;
}
protected actionsContextProvider(): unknown {
// Check Active Composite
if (this.activeComposite) {
return this.activeComposite.getActionsContext();
}
return null;
}
createContentArea(parent: HTMLElement): HTMLElement {
const contentContainer = append(parent, $('.content'));
this.progressBar = this._register(new ProgressBar(contentContainer));
this._register(attachProgressBarStyler(this.progressBar, this.themeService));
this.progressBar.hide();
return contentContainer;
}
getProgressIndicator(id: string): IProgressIndicator | undefined {
const compositeItem = this.instantiatedCompositeItems.get(id);
return compositeItem ? compositeItem.progress : undefined;
}
protected getActions(): ReadonlyArray<IAction> {
return [];
}
protected getSecondaryActions(): ReadonlyArray<IAction> {
return [];
}
protected getTitleAreaDropDownAnchorAlignment(): AnchorAlignment {
return AnchorAlignment.RIGHT;
}
layout(width: number, height: number): void {
super.layout(width, height);
// Layout contents
this.contentAreaSize = super.layoutContents(width, height).contentSize;
// Layout composite
if (this.activeComposite) {
this.activeComposite.layout(this.contentAreaSize);
}
}
protected removeComposite(compositeId: string): boolean {
if (this.activeComposite && this.activeComposite.getId() === compositeId) {
return false; // do not remove active composite
}
this.mapCompositeToCompositeContainer.delete(compositeId);
this.mapActionsBindingToComposite.delete(compositeId);
const compositeItem = this.instantiatedCompositeItems.get(compositeId);
if (compositeItem) {
compositeItem.composite.dispose();
dispose(compositeItem.disposable);
this.instantiatedCompositeItems.delete(compositeId);
}
return true;
}
dispose(): void {
this.mapCompositeToCompositeContainer.clear();
this.mapActionsBindingToComposite.clear();
this.instantiatedCompositeItems.forEach(compositeItem => {
compositeItem.composite.dispose();
dispose(compositeItem.disposable);
});
this.instantiatedCompositeItems.clear();
super.dispose();
}
}