views.ts 6.1 KB
Newer Older
S
Sandeep Somavarapu 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { TPromise } from 'vs/base/common/winjs.base';
import { Command } from 'vs/editor/common/modes';
S
Sandeep Somavarapu 已提交
8
import { UriComponents } from 'vs/base/common/uri';
S
Sandeep Somavarapu 已提交
9 10 11 12 13
import Event, { Emitter } from 'vs/base/common/event';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { ITreeViewDataProvider } from 'vs/workbench/common/views';
import { localize } from 'vs/nls';
import { IViewlet } from 'vs/workbench/common/viewlet';
S
Sandeep Somavarapu 已提交
14
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
15
import { IDisposable } from 'vs/base/common/lifecycle';
S
Sandeep Somavarapu 已提交
16 17 18

export class ViewLocation {

S
Sandeep Somavarapu 已提交
19 20 21
	static readonly Explorer = new ViewLocation('workbench.view.explorer');
	static readonly Debug = new ViewLocation('workbench.view.debug');
	static readonly Extensions = new ViewLocation('workbench.view.extensions');
S
Sandeep Somavarapu 已提交
22 23 24 25 26 27 28 29 30 31

	constructor(private _id: string) {
	}

	get id(): string {
		return this._id;
	}

	static getContributedViewLocation(value: string): ViewLocation {
		switch (value) {
S
Sandeep Somavarapu 已提交
32 33
			case 'explorer': return ViewLocation.Explorer;
			case 'debug': return ViewLocation.Debug;
S
Sandeep Somavarapu 已提交
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
		}
		return void 0;
	}
}

export interface IViewDescriptor {

	readonly id: string;

	readonly name: string;

	readonly location: ViewLocation;

	// TODO do we really need this?!
	readonly ctor: any;

	readonly when?: ContextKeyExpr;

	readonly order?: number;

	readonly weight?: number;

	readonly collapsed?: boolean;

	readonly canToggleVisibility?: boolean;
}

export interface IViewsRegistry {

	readonly onViewsRegistered: Event<IViewDescriptor[]>;

	readonly onViewsDeregistered: Event<IViewDescriptor[]>;

	registerViews(views: IViewDescriptor[]): void;

	deregisterViews(ids: string[], location: ViewLocation): void;

	getViews(loc: ViewLocation): IViewDescriptor[];

73 74
	getAllViews(): IViewDescriptor[];

S
Sandeep Somavarapu 已提交
75
	getView(id: string): IViewDescriptor;
S
Sandeep Somavarapu 已提交
76 77 78 79 80 81 82 83 84 85 86

}

export const ViewsRegistry: IViewsRegistry = new class implements IViewsRegistry {

	private _onViewsRegistered: Emitter<IViewDescriptor[]> = new Emitter<IViewDescriptor[]>();
	readonly onViewsRegistered: Event<IViewDescriptor[]> = this._onViewsRegistered.event;

	private _onViewsDeregistered: Emitter<IViewDescriptor[]> = new Emitter<IViewDescriptor[]>();
	readonly onViewsDeregistered: Event<IViewDescriptor[]> = this._onViewsDeregistered.event;

S
Sandeep Somavarapu 已提交
87
	private _viewLocations: ViewLocation[] = [];
S
Sandeep Somavarapu 已提交
88 89 90 91 92 93 94 95 96
	private _views: Map<ViewLocation, IViewDescriptor[]> = new Map<ViewLocation, IViewDescriptor[]>();

	registerViews(viewDescriptors: IViewDescriptor[]): void {
		if (viewDescriptors.length) {
			for (const viewDescriptor of viewDescriptors) {
				let views = this._views.get(viewDescriptor.location);
				if (!views) {
					views = [];
					this._views.set(viewDescriptor.location, views);
S
Sandeep Somavarapu 已提交
97
					this._viewLocations.push(viewDescriptor.location);
S
Sandeep Somavarapu 已提交
98 99
				}
				if (views.some(v => v.id === viewDescriptor.id)) {
100
					throw new Error(localize('duplicateId', "A view with id '{0}' is already registered in the location '{1}'", viewDescriptor.id, viewDescriptor.location.id));
S
Sandeep Somavarapu 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
				}
				views.push(viewDescriptor);
			}
			this._onViewsRegistered.fire(viewDescriptors);
		}
	}

	deregisterViews(ids: string[], location: ViewLocation): void {
		const views = this._views.get(location);

		if (!views) {
			return;
		}

		const viewsToDeregister = views.filter(view => ids.indexOf(view.id) !== -1);

		if (viewsToDeregister.length) {
S
Sandeep Somavarapu 已提交
118 119 120 121 122 123 124
			const remaningViews = views.filter(view => ids.indexOf(view.id) === -1);
			if (remaningViews.length) {
				this._views.set(location, remaningViews);
			} else {
				this._views.delete(location);
				this._viewLocations.splice(this._viewLocations.indexOf(location), 1);
			}
S
Sandeep Somavarapu 已提交
125 126 127 128 129 130 131 132 133
		}

		this._onViewsDeregistered.fire(viewsToDeregister);
	}

	getViews(loc: ViewLocation): IViewDescriptor[] {
		return this._views.get(loc) || [];
	}

134 135 136 137 138 139
	getAllViews(): IViewDescriptor[] {
		const result: IViewDescriptor[] = [];
		this._views.forEach(views => result.push(...views));
		return result;
	}

S
Sandeep Somavarapu 已提交
140 141 142 143 144 145 146 147
	getView(id: string): IViewDescriptor {
		for (const viewLocation of this._viewLocations) {
			const viewDescriptor = (this._views.get(viewLocation) || []).filter(v => v.id === id)[0];
			if (viewDescriptor) {
				return viewDescriptor;
			}
		}
		return null;
S
Sandeep Somavarapu 已提交
148 149 150 151 152
	}
};

export interface IViewsViewlet extends IViewlet {

S
Sandeep Somavarapu 已提交
153
	openView(id: string, focus?: boolean): TPromise<void>;
S
Sandeep Somavarapu 已提交
154 155 156

}

S
Sandeep Somavarapu 已提交
157 158
// Custom views

159 160
export interface ITreeViewer extends IDisposable {

161
	dataProvider: ITreeViewDataProvider;
S
Sandeep Somavarapu 已提交
162

163
	refresh(treeItems?: ITreeItem[]): TPromise<void>;
S
Sandeep Somavarapu 已提交
164

165
	setVisibility(visible: boolean): void;
S
Sandeep Somavarapu 已提交
166

167 168 169 170
	focus(): void;

	layout(height: number): void;

S
Sandeep Somavarapu 已提交
171
	show(container: HTMLElement);
172 173

	getOptimalWidth(): number;
S
Sandeep Somavarapu 已提交
174 175

	reveal(item: ITreeItem, parentChain: ITreeItem[], options: { donotSelect?: boolean }): TPromise<void>;
S
Sandeep Somavarapu 已提交
176 177 178 179
}

export interface ICustomViewDescriptor extends IViewDescriptor {

180
	treeView?: boolean;
S
Sandeep Somavarapu 已提交
181 182 183 184 185 186 187 188

}

export const ICustomViewsService = createDecorator<ICustomViewsService>('customViewsService');

export interface ICustomViewsService {
	_serviceBrand: any;

189
	getTreeViewer(id: string): ITreeViewer;
S
Sandeep Somavarapu 已提交
190 191

	openView(id: string, focus?: boolean): TPromise<void>;
S
Sandeep Somavarapu 已提交
192
}
S
Sandeep Somavarapu 已提交
193 194 195

export type TreeViewItemHandleArg = {
	$treeViewId: string,
S
Sandeep Somavarapu 已提交
196
	$treeItemHandle: string
S
Sandeep Somavarapu 已提交
197 198 199 200 201 202 203 204
};

export enum TreeItemCollapsibleState {
	None = 0,
	Collapsed = 1,
	Expanded = 2
}

205 206
export const FileThemeIconId = 'file';
export const FolderThemeIconId = 'folder';
207

208
export interface IThemeIcon {
209 210 211
	readonly id: string;
}

S
Sandeep Somavarapu 已提交
212 213
export interface ITreeItem {

S
Sandeep Somavarapu 已提交
214
	handle: string;
S
Sandeep Somavarapu 已提交
215

S
Sandeep Somavarapu 已提交
216 217
	parentHandle: string;

S
Sandeep Somavarapu 已提交
218 219
	collapsibleState: TreeItemCollapsibleState;

S
Sandeep Somavarapu 已提交
220
	label?: string;
S
Sandeep Somavarapu 已提交
221

222
	icon?: string | IThemeIcon;
S
Sandeep Somavarapu 已提交
223

224
	iconDark?: string | IThemeIcon;
S
Sandeep Somavarapu 已提交
225

S
Sandeep Somavarapu 已提交
226 227
	resourceUri?: UriComponents;

S
Sandeep Somavarapu 已提交
228 229
	tooltip?: string;

S
Sandeep Somavarapu 已提交
230 231 232 233 234 235 236 237 238 239
	contextValue?: string;

	command?: Command;

	children?: ITreeItem[];

}

export interface ITreeViewDataProvider {

S
Sandeep Somavarapu 已提交
240
	onDidChange: Event<ITreeItem[] | undefined | null>;
S
Sandeep Somavarapu 已提交
241

S
Sandeep Somavarapu 已提交
242 243
	onDispose: Event<void>;

244
	getChildren(element?: ITreeItem): TPromise<ITreeItem[]>;
S
Sandeep Somavarapu 已提交
245
}