views.ts 12.1 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
import { ContextKeyExpr, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
S
Sandeep Somavarapu 已提交
10 11 12
import { ITreeViewDataProvider } from 'vs/workbench/common/views';
import { localize } from 'vs/nls';
import { IViewlet } from 'vs/workbench/common/viewlet';
13
import { createDecorator, ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation';
14
import { IDisposable, Disposable } from 'vs/base/common/lifecycle';
S
Sandeep Somavarapu 已提交
15
import { ThemeIcon } from 'vs/platform/theme/common/themeService';
S
Sandeep Somavarapu 已提交
16
import { values, keys } 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 { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
S
Sandeep Somavarapu 已提交
21

22
export const TEST_VIEW_CONTAINER_ID = 'workbench.view.extension.test';
S
Sandeep Somavarapu 已提交
23
export const FocusedViewContext = new RawContextKey<string>('focusedView', '');
S
Sandeep Somavarapu 已提交
24

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

S
Sandeep Somavarapu 已提交
30 31 32 33 34 35
export interface IViewContainersRegistry {
	/**
	 * An event that is triggerred when a view container is registered.
	 */
	readonly onDidRegister: Event<ViewContainer>;

S
Sandeep Somavarapu 已提交
36 37 38 39 40
	/**
	 * An event that is triggerred when a view container is deregistered.
	 */
	readonly onDidDeregister: Event<ViewContainer>;

S
Sandeep Somavarapu 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
	/**
	 * 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.
	 */
54
	registerViewContainer(id: string, hideIfEmpty?: boolean, extensionId?: ExtensionIdentifier, viewOrderDelegate?: ViewOrderDelegate): ViewContainer;
S
Sandeep Somavarapu 已提交
55

S
Sandeep Somavarapu 已提交
56 57 58 59 60 61
	/**
	 * Deregisters the given view container
	 * No op if the view container is not registered
	 */
	deregisterViewContainer(viewContainer: ViewContainer): void;

S
Sandeep Somavarapu 已提交
62 63 64 65 66
	/**
	 * Returns the view container with given id.
	 *
	 * @returns the view container with given id.
	 */
67
	get(id: string): ViewContainer | undefined;
S
Sandeep Somavarapu 已提交
68
}
69

70 71 72 73
interface ViewOrderDelegate {
	getOrder(group?: string): number | undefined;
}

S
Sandeep Somavarapu 已提交
74
export class ViewContainer {
75
	protected constructor(readonly id: string, readonly hideIfEmpty: boolean, readonly extensionId?: ExtensionIdentifier, readonly orderDelegate?: ViewOrderDelegate) { }
S
Sandeep Somavarapu 已提交
76 77
}

78
class ViewContainersRegistryImpl extends Disposable implements IViewContainersRegistry {
S
Sandeep Somavarapu 已提交
79

80
	private readonly _onDidRegister = this._register(new Emitter<ViewContainer>());
S
Sandeep Somavarapu 已提交
81
	readonly onDidRegister: Event<ViewContainer> = this._onDidRegister.event;
82

83
	private readonly _onDidDeregister = this._register(new Emitter<ViewContainer>());
S
Sandeep Somavarapu 已提交
84 85
	readonly onDidDeregister: Event<ViewContainer> = this._onDidDeregister.event;

S
Sandeep Somavarapu 已提交
86 87 88 89 90 91
	private viewContainers: Map<string, ViewContainer> = new Map<string, ViewContainer>();

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

92
	registerViewContainer(id: string, hideIfEmpty?: boolean, extensionId?: ExtensionIdentifier, viewOrderDelegate?: ViewOrderDelegate): ViewContainer {
93 94 95
		const existing = this.viewContainers.get(id);
		if (existing) {
			return existing;
S
Sandeep Somavarapu 已提交
96
		}
97 98 99

		const viewContainer = new class extends ViewContainer {
			constructor() {
100
				super(id, !!hideIfEmpty, extensionId, viewOrderDelegate);
101 102 103 104 105
			}
		};
		this.viewContainers.set(id, viewContainer);
		this._onDidRegister.fire(viewContainer);
		return viewContainer;
S
Sandeep Somavarapu 已提交
106 107
	}

S
Sandeep Somavarapu 已提交
108 109 110 111 112 113 114 115
	deregisterViewContainer(viewContainer: ViewContainer): void {
		const existing = this.viewContainers.get(viewContainer.id);
		if (existing) {
			this.viewContainers.delete(viewContainer.id);
			this._onDidDeregister.fire(viewContainer);
		}
	}

116
	get(id: string): ViewContainer | undefined {
S
Sandeep Somavarapu 已提交
117 118
		return this.viewContainers.get(id);
	}
S
Sandeep Somavarapu 已提交
119 120
}

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

S
Sandeep Somavarapu 已提交
123 124 125 126 127 128
export interface IViewDescriptor {

	readonly id: string;

	readonly name: string;

129
	readonly ctorDescriptor: { ctor: any, arguments?: any[] };
S
Sandeep Somavarapu 已提交
130 131 132

	readonly when?: ContextKeyExpr;

133 134
	readonly group?: string;

S
Sandeep Somavarapu 已提交
135 136 137 138 139 140 141
	readonly order?: number;

	readonly weight?: number;

	readonly collapsed?: boolean;

	readonly canToggleVisibility?: boolean;
142

143
	// Applies only to newly created views
144
	readonly hideByDefault?: boolean;
S
Sandeep Somavarapu 已提交
145

S
Sandeep Somavarapu 已提交
146 147
	readonly workspace?: boolean;

S
Sandeep Somavarapu 已提交
148
	readonly focusCommand?: { id: string, keybindings?: IKeybindings };
S
Sandeep Somavarapu 已提交
149 150
}

S
Sandeep Somavarapu 已提交
151
export interface IViewDescriptorCollection extends IDisposable {
S
Sandeep Somavarapu 已提交
152 153
	readonly onDidChangeActiveViews: Event<{ added: IViewDescriptor[], removed: IViewDescriptor[] }>;
	readonly activeViewDescriptors: IViewDescriptor[];
S
Sandeep Somavarapu 已提交
154
	readonly allViewDescriptors: IViewDescriptor[];
155 156
}

S
Sandeep Somavarapu 已提交
157 158
export interface IViewsRegistry {

159
	readonly onViewsRegistered: Event<{ views: IViewDescriptor[], viewContainer: ViewContainer }>;
S
Sandeep Somavarapu 已提交
160

161
	readonly onViewsDeregistered: Event<{ views: IViewDescriptor[], viewContainer: ViewContainer }>;
S
Sandeep Somavarapu 已提交
162

163 164
	readonly onDidChangeContainer: Event<{ views: IViewDescriptor[], from: ViewContainer, to: ViewContainer }>;

165
	registerViews(views: IViewDescriptor[], viewContainer: ViewContainer): void;
S
Sandeep Somavarapu 已提交
166

S
Sandeep Somavarapu 已提交
167
	deregisterViews(views: IViewDescriptor[], viewContainer: ViewContainer): void;
S
Sandeep Somavarapu 已提交
168

S
Sandeep Somavarapu 已提交
169
	moveViews(views: IViewDescriptor[], viewContainer: ViewContainer): void;
170

171
	getViews(viewContainer: ViewContainer): IViewDescriptor[];
S
Sandeep Somavarapu 已提交
172

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

175
	getViewContainer(id: string): ViewContainer | null;
S
Sandeep Somavarapu 已提交
176 177
}

178
class ViewsRegistry extends Disposable implements IViewsRegistry {
S
Sandeep Somavarapu 已提交
179

180
	private readonly _onViewsRegistered: Emitter<{ views: IViewDescriptor[], viewContainer: ViewContainer }> = this._register(new Emitter<{ views: IViewDescriptor[], viewContainer: ViewContainer }>());
181
	readonly onViewsRegistered: Event<{ views: IViewDescriptor[], viewContainer: ViewContainer }> = this._onViewsRegistered.event;
S
Sandeep Somavarapu 已提交
182

183
	private readonly _onViewsDeregistered: Emitter<{ views: IViewDescriptor[], viewContainer: ViewContainer }> = this._register(new Emitter<{ views: IViewDescriptor[], viewContainer: ViewContainer }>());
184
	readonly onViewsDeregistered: Event<{ views: IViewDescriptor[], viewContainer: ViewContainer }> = this._onViewsDeregistered.event;
S
Sandeep Somavarapu 已提交
185

186
	private readonly _onDidChangeContainer: Emitter<{ views: IViewDescriptor[], from: ViewContainer, to: ViewContainer }> = this._register(new Emitter<{ views: IViewDescriptor[], from: ViewContainer, to: ViewContainer }>());
187 188
	readonly onDidChangeContainer: Event<{ views: IViewDescriptor[], from: ViewContainer, to: ViewContainer }> = this._onDidChangeContainer.event;

189
	private _viewContainers: ViewContainer[] = [];
S
Sandeep Somavarapu 已提交
190
	private _views: Map<ViewContainer, IViewDescriptor[]> = new Map<ViewContainer, IViewDescriptor[]>();
S
Sandeep Somavarapu 已提交
191

S
Sandeep Somavarapu 已提交
192 193 194
	registerViews(views: IViewDescriptor[], viewContainer: ViewContainer): void {
		this.addViews(views, viewContainer);
		this._onViewsRegistered.fire({ views: views, viewContainer });
S
Sandeep Somavarapu 已提交
195 196
	}

S
Sandeep Somavarapu 已提交
197 198 199 200
	deregisterViews(viewDescriptors: IViewDescriptor[], viewContainer: ViewContainer): void {
		const views = this.removeViews(viewDescriptors, viewContainer);
		if (views.length) {
			this._onViewsDeregistered.fire({ views, viewContainer });
S
Sandeep Somavarapu 已提交
201 202 203
		}
	}

S
Sandeep Somavarapu 已提交
204 205
	moveViews(viewsToMove: IViewDescriptor[], viewContainer: ViewContainer): void {
		keys(this._views).forEach(container => {
206
			if (container !== viewContainer) {
S
Sandeep Somavarapu 已提交
207 208 209 210
				const views = this.removeViews(viewsToMove, container);
				if (views.length) {
					this.addViews(views, viewContainer);
					this._onDidChangeContainer.fire({ views, from: container, to: viewContainer });
211 212 213 214 215
				}
			}
		});
	}

S
Sandeep Somavarapu 已提交
216
	getViews(loc: ViewContainer): IViewDescriptor[] {
S
Sandeep Somavarapu 已提交
217 218 219
		return this._views.get(loc) || [];
	}

M
Matt Bierner 已提交
220
	getView(id: string): IViewDescriptor | null {
221
		for (const viewContainer of this._viewContainers) {
S
Sandeep Somavarapu 已提交
222
			const viewDescriptor = (this._views.get(viewContainer) || []).filter(v => v.id === id)[0];
S
Sandeep Somavarapu 已提交
223 224 225 226 227
			if (viewDescriptor) {
				return viewDescriptor;
			}
		}
		return null;
S
Sandeep Somavarapu 已提交
228
	}
S
Sandeep Somavarapu 已提交
229

230 231 232 233 234 235 236 237
	getViewContainer(viewId: string): ViewContainer | null {
		for (const viewContainer of this._viewContainers) {
			const viewDescriptor = (this._views.get(viewContainer) || []).filter(v => v.id === viewId)[0];
			if (viewDescriptor) {
				return viewContainer;
			}
		}
		return null;
S
Sandeep Somavarapu 已提交
238
	}
S
Sandeep Somavarapu 已提交
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

	private addViews(viewDescriptors: IViewDescriptor[], viewContainer: ViewContainer): void {
		let views = this._views.get(viewContainer);
		if (!views) {
			views = [];
			this._views.set(viewContainer, views);
			this._viewContainers.push(viewContainer);
		}
		for (const viewDescriptor of viewDescriptors) {
			if (views.some(v => v.id === viewDescriptor.id)) {
				throw new Error(localize('duplicateId', "A view with id '{0}' is already registered in the container '{1}'", viewDescriptor.id, viewContainer.id));
			}
			views.push(viewDescriptor);
		}
	}

	private removeViews(viewDescriptors: IViewDescriptor[], viewContainer: ViewContainer): IViewDescriptor[] {
		const views = this._views.get(viewContainer);
		if (!views) {
			return [];
		}
		const viewsToDeregister: IViewDescriptor[] = [];
		const remaningViews: IViewDescriptor[] = [];
		for (const view of views) {
			if (viewDescriptors.indexOf(view) === -1) {
				remaningViews.push(view);
			} else {
				viewsToDeregister.push(view);
			}
		}
		if (viewsToDeregister.length) {
			if (remaningViews.length) {
				this._views.set(viewContainer, remaningViews);
			} else {
				this._views.delete(viewContainer);
				this._viewContainers.splice(this._viewContainers.indexOf(viewContainer), 1);
			}
		}
		return viewsToDeregister;
	}
279 280 281
}

Registry.add(Extensions.ViewsRegistry, new ViewsRegistry());
S
Sandeep Somavarapu 已提交
282

S
Sandeep Somavarapu 已提交
283 284 285 286 287 288
export interface IView {

	readonly id: string;

}

S
Sandeep Somavarapu 已提交
289 290
export interface IViewsViewlet extends IViewlet {

I
isidor 已提交
291
	openView(id: string, focus?: boolean): IView;
S
Sandeep Somavarapu 已提交
292 293 294

}

295 296 297
export const IViewsService = createDecorator<IViewsService>('viewsService');

export interface IViewsService {
298
	_serviceBrand: ServiceIdentifier<any>;
299

M
Matt Bierner 已提交
300
	openView(id: string, focus?: boolean): Promise<IView | null>;
301

S
Sandeep Somavarapu 已提交
302
	getViewDescriptors(container: ViewContainer): IViewDescriptorCollection | null;
303 304
}

S
Sandeep Somavarapu 已提交
305 306
// Custom views

S
rename  
Sandeep Somavarapu 已提交
307
export interface ITreeView extends IDisposable {
308

309
	dataProvider: ITreeViewDataProvider | undefined;
S
Sandeep Somavarapu 已提交
310

311 312
	showCollapseAllAction: boolean;

313
	message?: string;
314

315 316
	readonly visible: boolean;

317 318 319 320
	readonly onDidExpandItem: Event<ITreeItem>;

	readonly onDidCollapseItem: Event<ITreeItem>;

321 322
	readonly onDidChangeSelection: Event<ITreeItem[]>;

323 324
	readonly onDidChangeVisibility: Event<boolean>;

325
	readonly onDidChangeActions: Event<void>;
S
Sandeep Somavarapu 已提交
326

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

329
	setVisibility(visible: boolean): void;
S
Sandeep Somavarapu 已提交
330

331 332
	focus(): void;

333
	layout(height: number, width: number): void;
334

335
	show(container: HTMLElement): void;
336 337

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

J
Johannes Rieken 已提交
339
	reveal(item: ITreeItem): Promise<void>;
340

J
Johannes Rieken 已提交
341
	expand(itemOrItems: ITreeItem | ITreeItem[]): Promise<void>;
342 343 344 345

	setSelection(items: ITreeItem[]): void;

	setFocus(item: ITreeItem): void;
346 347 348 349

	getPrimaryActions(): IAction[];

	getSecondaryActions(): IAction[];
S
Sandeep Somavarapu 已提交
350 351
}

352 353 354 355 356 357 358 359 360 361
export interface IRevealOptions {

	select?: boolean;

	focus?: boolean;

	expand?: boolean | number;

}

362
export interface ITreeViewDescriptor extends IViewDescriptor {
S
Sandeep Somavarapu 已提交
363

S
rename  
Sandeep Somavarapu 已提交
364
	readonly treeView: ITreeView;
365

S
Sandeep Somavarapu 已提交
366
}
S
Sandeep Somavarapu 已提交
367 368 369

export type TreeViewItemHandleArg = {
	$treeViewId: string,
S
Sandeep Somavarapu 已提交
370
	$treeItemHandle: string
S
Sandeep Somavarapu 已提交
371 372 373 374 375 376 377 378
};

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

S
Sandeep Somavarapu 已提交
379 380 381 382
export interface ITreeItemLabel {

	label: string;

383
	highlights?: [number, number][];
S
Sandeep Somavarapu 已提交
384 385 386

}

S
Sandeep Somavarapu 已提交
387 388
export interface ITreeItem {

S
Sandeep Somavarapu 已提交
389
	handle: string;
S
Sandeep Somavarapu 已提交
390

S
Sandeep Somavarapu 已提交
391
	parentHandle?: string;
S
Sandeep Somavarapu 已提交
392

S
Sandeep Somavarapu 已提交
393 394
	collapsibleState: TreeItemCollapsibleState;

S
Sandeep Somavarapu 已提交
395
	label?: ITreeItemLabel;
S
Sandeep Somavarapu 已提交
396

397 398
	description?: string | boolean;

399
	icon?: UriComponents;
S
Sandeep Somavarapu 已提交
400

401
	iconDark?: UriComponents;
S
Sandeep Somavarapu 已提交
402

S
Sandeep Somavarapu 已提交
403
	themeIcon?: ThemeIcon;
S
Sandeep Somavarapu 已提交
404

S
Sandeep Somavarapu 已提交
405 406
	resourceUri?: UriComponents;

S
Sandeep Somavarapu 已提交
407 408
	tooltip?: string;

S
Sandeep Somavarapu 已提交
409 410 411 412 413 414 415 416 417
	contextValue?: string;

	command?: Command;

	children?: ITreeItem[];
}

export interface ITreeViewDataProvider {

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

I
isidor 已提交
420
}