viewsViewlet.ts 5.4 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
7
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
8
import { IViewDescriptor, IViewDescriptorService, IAddedViewDescriptorRef } from 'vs/workbench/common/views';
9
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
B
Benjamin Pasero 已提交
10
import { IThemeService } from 'vs/platform/theme/common/themeService';
11
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
S
SteVen Batten 已提交
12
import { IStorageService } from 'vs/platform/storage/common/storage';
13
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
14 15
import { ViewPaneContainer } from 'vs/workbench/browser/parts/views/viewPaneContainer';
import { ViewPane, IViewPaneOptions } from 'vs/workbench/browser/parts/views/viewPane';
16
import { Event } from 'vs/base/common/event';
17
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
18
import { IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
19

S
SteVen Batten 已提交
20
export interface IViewletViewOptions extends IViewPaneOptions {
21 22
}

S
SteVen Batten 已提交
23
export abstract class FilterViewPaneContainer extends ViewPaneContainer {
24 25
	private constantViewDescriptors: Map<string, IViewDescriptor> = new Map();
	private allViews: Map<string, Map<string, IViewDescriptor>> = new Map();
26
	private filterValue: string[] | undefined;
27 28 29

	constructor(
		viewletId: string,
30
		onDidChangeFilterValue: Event<string[]>,
31 32 33 34 35 36 37 38
		@IConfigurationService configurationService: IConfigurationService,
		@IWorkbenchLayoutService layoutService: IWorkbenchLayoutService,
		@ITelemetryService telemetryService: ITelemetryService,
		@IStorageService storageService: IStorageService,
		@IInstantiationService instantiationService: IInstantiationService,
		@IThemeService themeService: IThemeService,
		@IContextMenuService contextMenuService: IContextMenuService,
		@IExtensionService extensionService: IExtensionService,
S
SteVen Batten 已提交
39
		@IWorkspaceContextService contextService: IWorkspaceContextService,
S
Sandeep Somavarapu 已提交
40
		@IViewDescriptorService viewDescriptorService: IViewDescriptorService
41
	) {
S
SteVen Batten 已提交
42

43
		super(viewletId, { mergeViewWithContainerWhenSingleView: false }, instantiationService, configurationService, layoutService, contextMenuService, telemetryService, extensionService, themeService, storageService, contextService, viewDescriptorService);
44
		this._register(onDidChangeFilterValue(newFilterValue => {
45 46 47 48
			this.filterValue = newFilterValue;
			this.onFilterChanged(newFilterValue);
		}));

49 50
		this._register(this.viewContainerModel.onDidChangeActiveViewDescriptors(() => {
			this.updateAllViews(this.viewContainerModel.activeViewDescriptors);
51 52 53
		}));
	}

S
Sandeep Somavarapu 已提交
54
	private updateAllViews(viewDescriptors: ReadonlyArray<IViewDescriptor>) {
55 56 57 58 59 60 61 62 63
		viewDescriptors.forEach(descriptor => {
			let filterOnValue = this.getFilterOn(descriptor);
			if (!filterOnValue) {
				return;
			}
			if (!this.allViews.has(filterOnValue)) {
				this.allViews.set(filterOnValue, new Map());
			}
			this.allViews.get(filterOnValue)!.set(descriptor.id, descriptor);
64
			if (this.filterValue && !this.filterValue.includes(filterOnValue)) {
65
				this.viewContainerModel.setVisible(descriptor.id, false);
66 67 68 69
			}
		});
	}

70 71 72 73 74 75
	protected addConstantViewDescriptors(constantViewDescriptors: IViewDescriptor[]) {
		constantViewDescriptors.forEach(viewDescriptor => this.constantViewDescriptors.set(viewDescriptor.id, viewDescriptor));
	}

	protected abstract getFilterOn(viewDescriptor: IViewDescriptor): string | undefined;

76
	private onFilterChanged(newFilterValue: string[]) {
77
		if (this.allViews.size === 0) {
78
			this.updateAllViews(this.viewContainerModel.activeViewDescriptors);
79
		}
80 81
		this.getViewsNotForTarget(newFilterValue).forEach(item => this.viewContainerModel.setVisible(item.id, false));
		this.getViewsForTarget(newFilterValue).forEach(item => this.viewContainerModel.setVisible(item.id, true));
82 83
	}

84 85 86 87 88 89 90 91 92
	private getViewsForTarget(target: string[]): IViewDescriptor[] {
		const views: IViewDescriptor[] = [];
		for (let i = 0; i < target.length; i++) {
			if (this.allViews.has(target[i])) {
				views.push(...Array.from(this.allViews.get(target[i])!.values()));
			}
		}

		return views;
93 94
	}

95
	private getViewsNotForTarget(target: string[]): IViewDescriptor[] {
96 97 98 99
		const iterable = this.allViews.keys();
		let key = iterable.next();
		let views: IViewDescriptor[] = [];
		while (!key.done) {
100 101 102 103 104 105 106 107
			let isForTarget: boolean = false;
			target.forEach(value => {
				if (key.value === value) {
					isForTarget = true;
				}
			});
			if (!isForTarget) {
				views = views.concat(this.getViewsForTarget([key.value]));
108
			}
109

110 111 112 113 114
			key = iterable.next();
		}
		return views;
	}

115
	onDidAddViewDescriptors(added: IAddedViewDescriptorRef[]): ViewPane[] {
A
Alex Ross 已提交
116
		const panes: ViewPane[] = super.onDidAddViewDescriptors(added.sort((a, b) => a.index - b.index));
117 118
		for (let i = 0; i < added.length; i++) {
			if (this.constantViewDescriptors.has(added[i].viewDescriptor.id)) {
119
				panes[i].setExpanded(false);
120 121
			}
		}
A
Alex Ross 已提交
122 123
		// Check that allViews is ready
		if (this.allViews.size === 0) {
124
			this.updateAllViews(this.viewContainerModel.activeViewDescriptors);
A
Alex Ross 已提交
125
		}
126
		return panes;
127 128 129
	}

	abstract getTitle(): string;
130

131
}