views.ts 9.0 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';
21
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
S
Sandeep Somavarapu 已提交
22

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

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

S
Sandeep Somavarapu 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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.
	 */
48
	registerViewContainer(id: string, extensionId?: ExtensionIdentifier): ViewContainer;
S
Sandeep Somavarapu 已提交
49 50 51 52 53 54

	/**
	 * Returns the view container with given id.
	 *
	 * @returns the view container with given id.
	 */
55
	get(id: string): ViewContainer | undefined;
S
Sandeep Somavarapu 已提交
56
}
57

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

class ViewContainersRegistryImpl implements IViewContainersRegistry {

64
	private readonly _onDidRegister = new Emitter<ViewContainer>();
S
Sandeep Somavarapu 已提交
65
	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: ExtensionIdentifier): ViewContainer {
74 75 76
		const existing = this.viewContainers.get(id);
		if (existing) {
			return existing;
S
Sandeep Somavarapu 已提交
77
		}
78 79 80 81 82 83 84 85 86

		const viewContainer = new class extends ViewContainer {
			constructor() {
				super(id, extensionId);
			}
		};
		this.viewContainers.set(id, viewContainer);
		this._onDidRegister.fire(viewContainer);
		return viewContainer;
S
Sandeep Somavarapu 已提交
87 88
	}

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

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

S
Sandeep Somavarapu 已提交
96 97 98 99 100 101
export interface IViewDescriptor {

	readonly id: string;

	readonly name: string;

S
Sandeep Somavarapu 已提交
102
	readonly container: ViewContainer;
S
Sandeep Somavarapu 已提交
103

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

	readonly when?: ContextKeyExpr;

	readonly order?: number;

	readonly weight?: number;

	readonly collapsed?: boolean;

	readonly canToggleVisibility?: boolean;
116

117
	// Applies only to newly created views
118
	readonly hideByDefault?: boolean;
S
Sandeep Somavarapu 已提交
119 120

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

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

S
Sandeep Somavarapu 已提交
129 130 131 132 133 134 135 136
export interface IViewsRegistry {

	readonly onViewsRegistered: Event<IViewDescriptor[]>;

	readonly onViewsDeregistered: Event<IViewDescriptor[]>;

	registerViews(views: IViewDescriptor[]): void;

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

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

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

S
Sandeep Somavarapu 已提交
143
	getAllViews(): IViewDescriptor[];
S
Sandeep Somavarapu 已提交
144 145 146 147
}

export const ViewsRegistry: IViewsRegistry = new class implements IViewsRegistry {

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

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

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

	registerViews(viewDescriptors: IViewDescriptor[]): void {
		if (viewDescriptors.length) {
			for (const viewDescriptor of viewDescriptors) {
S
Sandeep Somavarapu 已提交
160
				let views = this._views.get(viewDescriptor.container);
S
Sandeep Somavarapu 已提交
161 162
				if (!views) {
					views = [];
S
Sandeep Somavarapu 已提交
163 164
					this._views.set(viewDescriptor.container, views);
					this._viewContainer.push(viewDescriptor.container);
S
Sandeep Somavarapu 已提交
165 166
				}
				if (views.some(v => v.id === viewDescriptor.id)) {
S
Sandeep Somavarapu 已提交
167
					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 已提交
168 169 170 171 172 173 174
				}
				views.push(viewDescriptor);
			}
			this._onViewsRegistered.fire(viewDescriptors);
		}
	}

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

		if (!views) {
			return;
		}

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

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

	}

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

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

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

S
Sandeep Somavarapu 已提交
218 219 220 221 222 223
export interface IView {

	readonly id: string;

}

S
Sandeep Somavarapu 已提交
224 225
export interface IViewsViewlet extends IViewlet {

I
isidor 已提交
226
	openView(id: string, focus?: boolean): IView;
S
Sandeep Somavarapu 已提交
227 228 229

}

230 231 232 233 234
export const IViewsService = createDecorator<IViewsService>('viewsService');

export interface IViewsService {
	_serviceBrand: any;

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

	getViewDescriptors(container: ViewContainer): IViewDescriptorCollection;
238 239
}

S
Sandeep Somavarapu 已提交
240 241
// Custom views

S
rename  
Sandeep Somavarapu 已提交
242
export interface ITreeView extends IDisposable {
243

244
	dataProvider: ITreeViewDataProvider;
S
Sandeep Somavarapu 已提交
245

246 247
	showCollapseAllAction: boolean;

248 249
	message: string | IMarkdownString;

250 251
	readonly visible: boolean;

252 253 254 255
	readonly onDidExpandItem: Event<ITreeItem>;

	readonly onDidCollapseItem: Event<ITreeItem>;

256 257
	readonly onDidChangeSelection: Event<ITreeItem[]>;

258 259
	readonly onDidChangeVisibility: Event<boolean>;

260
	readonly onDidChangeActions: Event<void>;
S
Sandeep Somavarapu 已提交
261

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

264
	setVisibility(visible: boolean): void;
S
Sandeep Somavarapu 已提交
265

266 267 268 269
	focus(): void;

	layout(height: number): void;

S
Sandeep Somavarapu 已提交
270
	show(container: HTMLElement);
271 272

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

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

J
Johannes Rieken 已提交
276
	expand(itemOrItems: ITreeItem | ITreeItem[]): Promise<void>;
277 278 279 280

	setSelection(items: ITreeItem[]): void;

	setFocus(item: ITreeItem): void;
281 282 283 284

	getPrimaryActions(): IAction[];

	getSecondaryActions(): IAction[];
S
Sandeep Somavarapu 已提交
285 286
}

287 288 289 290 291 292 293 294 295 296
export interface IRevealOptions {

	select?: boolean;

	focus?: boolean;

	expand?: boolean | number;

}

S
Sandeep Somavarapu 已提交
297 298
export interface ICustomViewDescriptor extends IViewDescriptor {

S
rename  
Sandeep Somavarapu 已提交
299
	readonly treeView: ITreeView;
S
Sandeep Somavarapu 已提交
300
	readonly extensionId: ExtensionIdentifier;
S
Sandeep Somavarapu 已提交
301
}
S
Sandeep Somavarapu 已提交
302 303 304

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

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

S
Sandeep Somavarapu 已提交
314 315 316 317
export interface ITreeItemLabel {

	label: string;

318
	highlights?: [number, number][];
S
Sandeep Somavarapu 已提交
319 320 321

}

S
Sandeep Somavarapu 已提交
322 323
export interface ITreeItem {

S
Sandeep Somavarapu 已提交
324
	handle: string;
S
Sandeep Somavarapu 已提交
325

S
Sandeep Somavarapu 已提交
326 327
	parentHandle: string;

S
Sandeep Somavarapu 已提交
328 329
	collapsibleState: TreeItemCollapsibleState;

S
Sandeep Somavarapu 已提交
330
	label?: ITreeItemLabel;
S
Sandeep Somavarapu 已提交
331

332 333
	description?: string | boolean;

334
	icon?: UriComponents;
S
Sandeep Somavarapu 已提交
335

336
	iconDark?: UriComponents;
S
Sandeep Somavarapu 已提交
337

S
Sandeep Somavarapu 已提交
338
	themeIcon?: ThemeIcon;
S
Sandeep Somavarapu 已提交
339

S
Sandeep Somavarapu 已提交
340 341
	resourceUri?: UriComponents;

S
Sandeep Somavarapu 已提交
342 343
	tooltip?: string;

S
Sandeep Somavarapu 已提交
344 345 346 347 348 349 350 351 352
	contextValue?: string;

	command?: Command;

	children?: ITreeItem[];
}

export interface ITreeViewDataProvider {

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

I
isidor 已提交
355
}