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

import { Command } from 'vs/editor/common/modes';
S
Sandeep Somavarapu 已提交
7
import { UriComponents } from 'vs/base/common/uri';
M
Matt Bierner 已提交
8
import { Event, Emitter } from 'vs/base/common/event';
S
Sandeep Somavarapu 已提交
9 10 11 12
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 已提交
13
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
14
import { IDisposable } from 'vs/base/common/lifecycle';
S
Sandeep Somavarapu 已提交
15
import { ThemeIcon } from 'vs/platform/theme/common/themeService';
S
Sandeep Somavarapu 已提交
16
import { values } from 'vs/base/common/map';
S
Sandeep Somavarapu 已提交
17
import { Registry } from 'vs/platform/registry/common/platform';
S
Sandeep Somavarapu 已提交
18
import { IKeybindings } from 'vs/platform/keybinding/common/keybindingsRegistry';
19
import { IAction } from 'vs/base/common/actions';
20
import { IMarkdownString } from 'vs/base/common/htmlContent';
S
Sandeep Somavarapu 已提交
21

22
export const TEST_VIEW_CONTAINER_ID = 'workbench.view.extension.test';
S
Sandeep Somavarapu 已提交
23

S
Sandeep Somavarapu 已提交
24 25 26
export namespace Extensions {
	export const ViewContainersRegistry = 'workbench.registry.view.containers';
}
S
Sandeep Somavarapu 已提交
27

S
Sandeep Somavarapu 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
export interface IViewContainersRegistry {
	/**
	 * An event that is triggerred when a view container is registered.
	 */
	readonly onDidRegister: Event<ViewContainer>;

	/**
	 * All registered view containers
	 */
	readonly all: ViewContainer[];

	/**
	 * Registers a view container with given id
	 * No op if a view container is already registered with the given id.
	 *
	 * @param id of the view container.
	 *
	 * @returns the registered ViewContainer.
	 */
47
	registerViewContainer(id: string, extensionId?: string): ViewContainer;
S
Sandeep Somavarapu 已提交
48 49 50 51 52 53 54 55 56

	/**
	 * Returns the view container with given id.
	 *
	 * @param id
	 * @returns the view container with given id.
	 */
	get(id: string): ViewContainer;
}
57

S
Sandeep Somavarapu 已提交
58
export class ViewContainer {
59
	protected constructor(readonly id: string, readonly extensionId: string) { }
S
Sandeep Somavarapu 已提交
60 61 62 63 64 65
}

class ViewContainersRegistryImpl implements IViewContainersRegistry {

	private readonly _onDidRegister: Emitter<ViewContainer> = new Emitter<ViewContainer>();
	readonly onDidRegister: Event<ViewContainer> = this._onDidRegister.event;
66

S
Sandeep Somavarapu 已提交
67 68 69 70 71 72
	private viewContainers: Map<string, ViewContainer> = new Map<string, ViewContainer>();

	get all(): ViewContainer[] {
		return values(this.viewContainers);
	}

73
	registerViewContainer(id: string, extensionId: string): ViewContainer {
S
Sandeep Somavarapu 已提交
74 75 76
		if (!this.viewContainers.has(id)) {
			const viewContainer = new class extends ViewContainer {
				constructor() {
77
					super(id, extensionId);
S
Sandeep Somavarapu 已提交
78 79 80 81 82 83 84 85 86 87 88
				}
			};
			this.viewContainers.set(id, viewContainer);
			this._onDidRegister.fire(viewContainer);
		}
		return this.get(id);
	}

	get(id: string): ViewContainer {
		return this.viewContainers.get(id);
	}
S
Sandeep Somavarapu 已提交
89 90
}

S
Sandeep Somavarapu 已提交
91 92
Registry.add(Extensions.ViewContainersRegistry, new ViewContainersRegistryImpl());

S
Sandeep Somavarapu 已提交
93 94 95 96 97 98
export interface IViewDescriptor {

	readonly id: string;

	readonly name: string;

S
Sandeep Somavarapu 已提交
99
	readonly container: ViewContainer;
S
Sandeep Somavarapu 已提交
100

B
Benjamin Pasero 已提交
101
	// TODO@Sandeep do we really need this?!
S
Sandeep Somavarapu 已提交
102 103 104 105 106 107 108 109 110 111 112
	readonly ctor: any;

	readonly when?: ContextKeyExpr;

	readonly order?: number;

	readonly weight?: number;

	readonly collapsed?: boolean;

	readonly canToggleVisibility?: boolean;
113

114
	// Applies only to newly created views
115
	readonly hideByDefault?: boolean;
S
Sandeep Somavarapu 已提交
116 117

	readonly focusCommand?: { id: string, keybindings?: IKeybindings };
S
Sandeep Somavarapu 已提交
118 119
}

120
export interface IViewDescriptorCollection {
S
Sandeep Somavarapu 已提交
121 122
	readonly onDidChangeActiveViews: Event<{ added: IViewDescriptor[], removed: IViewDescriptor[] }>;
	readonly activeViewDescriptors: IViewDescriptor[];
S
Sandeep Somavarapu 已提交
123
	readonly allViewDescriptors: IViewDescriptor[];
124 125
}

S
Sandeep Somavarapu 已提交
126 127 128 129 130 131 132 133
export interface IViewsRegistry {

	readonly onViewsRegistered: Event<IViewDescriptor[]>;

	readonly onViewsDeregistered: Event<IViewDescriptor[]>;

	registerViews(views: IViewDescriptor[]): void;

S
Sandeep Somavarapu 已提交
134
	deregisterViews(ids: string[], container: ViewContainer): void;
S
Sandeep Somavarapu 已提交
135

S
Sandeep Somavarapu 已提交
136
	getViews(loc: ViewContainer): IViewDescriptor[];
S
Sandeep Somavarapu 已提交
137

M
Matt Bierner 已提交
138
	getView(id: string): IViewDescriptor | null;
S
Sandeep Somavarapu 已提交
139

S
Sandeep Somavarapu 已提交
140
	getAllViews(): IViewDescriptor[];
S
Sandeep Somavarapu 已提交
141 142 143 144
}

export const ViewsRegistry: IViewsRegistry = new class implements IViewsRegistry {

M
Matt Bierner 已提交
145
	private readonly _onViewsRegistered: Emitter<IViewDescriptor[]> = new Emitter<IViewDescriptor[]>();
S
Sandeep Somavarapu 已提交
146 147
	readonly onViewsRegistered: Event<IViewDescriptor[]> = this._onViewsRegistered.event;

M
Matt Bierner 已提交
148
	private readonly _onViewsDeregistered: Emitter<IViewDescriptor[]> = new Emitter<IViewDescriptor[]>();
S
Sandeep Somavarapu 已提交
149 150
	readonly onViewsDeregistered: Event<IViewDescriptor[]> = this._onViewsDeregistered.event;

S
Sandeep Somavarapu 已提交
151 152
	private _viewContainer: ViewContainer[] = [];
	private _views: Map<ViewContainer, IViewDescriptor[]> = new Map<ViewContainer, IViewDescriptor[]>();
S
Sandeep Somavarapu 已提交
153 154 155 156

	registerViews(viewDescriptors: IViewDescriptor[]): void {
		if (viewDescriptors.length) {
			for (const viewDescriptor of viewDescriptors) {
S
Sandeep Somavarapu 已提交
157
				let views = this._views.get(viewDescriptor.container);
S
Sandeep Somavarapu 已提交
158 159
				if (!views) {
					views = [];
S
Sandeep Somavarapu 已提交
160 161
					this._views.set(viewDescriptor.container, views);
					this._viewContainer.push(viewDescriptor.container);
S
Sandeep Somavarapu 已提交
162 163
				}
				if (views.some(v => v.id === viewDescriptor.id)) {
S
Sandeep Somavarapu 已提交
164
					throw new Error(localize('duplicateId', "A view with id '{0}' is already registered in the container '{1}'", viewDescriptor.id, viewDescriptor.container.id));
S
Sandeep Somavarapu 已提交
165 166 167 168 169 170 171
				}
				views.push(viewDescriptor);
			}
			this._onViewsRegistered.fire(viewDescriptors);
		}
	}

S
Sandeep Somavarapu 已提交
172 173
	deregisterViews(ids: string[], container: ViewContainer): void {
		const views = this._views.get(container);
S
Sandeep Somavarapu 已提交
174 175 176 177 178 179 180 181

		if (!views) {
			return;
		}

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

		if (viewsToDeregister.length) {
S
Sandeep Somavarapu 已提交
182 183
			const remaningViews = views.filter(view => ids.indexOf(view.id) === -1);
			if (remaningViews.length) {
S
Sandeep Somavarapu 已提交
184
				this._views.set(container, remaningViews);
S
Sandeep Somavarapu 已提交
185
			} else {
S
Sandeep Somavarapu 已提交
186 187
				this._views.delete(container);
				this._viewContainer.splice(this._viewContainer.indexOf(container), 1);
S
Sandeep Somavarapu 已提交
188
			}
189
			this._onViewsDeregistered.fire(viewsToDeregister);
S
Sandeep Somavarapu 已提交
190 191 192 193
		}

	}

S
Sandeep Somavarapu 已提交
194
	getViews(loc: ViewContainer): IViewDescriptor[] {
S
Sandeep Somavarapu 已提交
195 196 197
		return this._views.get(loc) || [];
	}

M
Matt Bierner 已提交
198
	getView(id: string): IViewDescriptor | null {
S
Sandeep Somavarapu 已提交
199 200
		for (const viewContainer of this._viewContainer) {
			const viewDescriptor = (this._views.get(viewContainer) || []).filter(v => v.id === id)[0];
S
Sandeep Somavarapu 已提交
201 202 203 204 205
			if (viewDescriptor) {
				return viewDescriptor;
			}
		}
		return null;
S
Sandeep Somavarapu 已提交
206
	}
S
Sandeep Somavarapu 已提交
207 208 209 210 211 212

	getAllViews(): IViewDescriptor[] {
		const allViews: IViewDescriptor[] = [];
		this._views.forEach(views => allViews.push(...views));
		return allViews;
	}
S
Sandeep Somavarapu 已提交
213 214
};

S
Sandeep Somavarapu 已提交
215 216 217 218 219 220
export interface IView {

	readonly id: string;

}

S
Sandeep Somavarapu 已提交
221 222
export interface IViewsViewlet extends IViewlet {

I
isidor 已提交
223
	openView(id: string, focus?: boolean): IView;
S
Sandeep Somavarapu 已提交
224 225 226

}

227 228 229 230 231
export const IViewsService = createDecorator<IViewsService>('viewsService');

export interface IViewsService {
	_serviceBrand: any;

232
	openView(id: string, focus?: boolean): Thenable<IView>;
233 234

	getViewDescriptors(container: ViewContainer): IViewDescriptorCollection;
235 236
}

S
Sandeep Somavarapu 已提交
237 238
// Custom views

S
rename  
Sandeep Somavarapu 已提交
239
export interface ITreeView extends IDisposable {
240

241
	dataProvider: ITreeViewDataProvider;
S
Sandeep Somavarapu 已提交
242

243 244
	showCollapseAllAction: boolean;

245 246
	message: string | IMarkdownString;

247 248
	readonly visible: boolean;

249 250 251 252
	readonly onDidExpandItem: Event<ITreeItem>;

	readonly onDidCollapseItem: Event<ITreeItem>;

253 254
	readonly onDidChangeSelection: Event<ITreeItem[]>;

255 256
	readonly onDidChangeVisibility: Event<boolean>;

257
	readonly onDidChangeActions: Event<void>;
S
Sandeep Somavarapu 已提交
258

259
	refresh(treeItems?: ITreeItem[]): Promise<void>;
S
Sandeep Somavarapu 已提交
260

261
	setVisibility(visible: boolean): void;
S
Sandeep Somavarapu 已提交
262

263 264 265 266
	focus(): void;

	layout(height: number): void;

S
Sandeep Somavarapu 已提交
267
	show(container: HTMLElement);
268 269

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

271
	reveal(item: ITreeItem): Thenable<void>;
272

273
	expand(itemOrItems: ITreeItem | ITreeItem[]): Thenable<void>;
274 275 276 277

	setSelection(items: ITreeItem[]): void;

	setFocus(item: ITreeItem): void;
278 279 280 281

	getPrimaryActions(): IAction[];

	getSecondaryActions(): IAction[];
S
Sandeep Somavarapu 已提交
282 283
}

284 285 286 287 288 289 290 291 292 293
export interface IRevealOptions {

	select?: boolean;

	focus?: boolean;

	expand?: boolean | number;

}

S
Sandeep Somavarapu 已提交
294 295
export interface ICustomViewDescriptor extends IViewDescriptor {

S
rename  
Sandeep Somavarapu 已提交
296
	readonly treeView: ITreeView;
S
Sandeep Somavarapu 已提交
297 298

}
S
Sandeep Somavarapu 已提交
299 300 301

export type TreeViewItemHandleArg = {
	$treeViewId: string,
S
Sandeep Somavarapu 已提交
302
	$treeItemHandle: string
S
Sandeep Somavarapu 已提交
303 304 305 306 307 308 309 310
};

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

S
Sandeep Somavarapu 已提交
311 312 313 314
export interface ITreeItemLabel {

	label: string;

315
	highlights?: [number, number][];
S
Sandeep Somavarapu 已提交
316 317 318

}

S
Sandeep Somavarapu 已提交
319 320
export interface ITreeItem {

S
Sandeep Somavarapu 已提交
321
	handle: string;
S
Sandeep Somavarapu 已提交
322

S
Sandeep Somavarapu 已提交
323 324
	parentHandle: string;

S
Sandeep Somavarapu 已提交
325 326
	collapsibleState: TreeItemCollapsibleState;

S
Sandeep Somavarapu 已提交
327
	label?: ITreeItemLabel;
S
Sandeep Somavarapu 已提交
328

329 330
	description?: string | boolean;

331
	icon?: UriComponents;
S
Sandeep Somavarapu 已提交
332

333
	iconDark?: UriComponents;
S
Sandeep Somavarapu 已提交
334

S
Sandeep Somavarapu 已提交
335
	themeIcon?: ThemeIcon;
S
Sandeep Somavarapu 已提交
336

S
Sandeep Somavarapu 已提交
337 338
	resourceUri?: UriComponents;

S
Sandeep Somavarapu 已提交
339 340
	tooltip?: string;

S
Sandeep Somavarapu 已提交
341 342 343 344 345 346 347 348 349
	contextValue?: string;

	command?: Command;

	children?: ITreeItem[];
}

export interface ITreeViewDataProvider {

350
	getChildren(element?: ITreeItem): Promise<ITreeItem[]>;
S
Sandeep Somavarapu 已提交
351

I
isidor 已提交
352
}