statusbarPart.ts 13.2 KB
Newer Older
E
Erich Gamma 已提交
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.
 *--------------------------------------------------------------------------------------------*/

B
Benjamin Pasero 已提交
6
import 'vs/css!./media/statusbarpart';
7
import * as nls from 'vs/nls';
J
Johannes Rieken 已提交
8
import { toErrorMessage } from 'vs/base/common/errorMessage';
9
import { dispose, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
J
Johannes Rieken 已提交
10
import { OcticonLabel } from 'vs/base/browser/ui/octiconLabel/octiconLabel';
11
import { Registry } from 'vs/platform/registry/common/platform';
J
Johannes Rieken 已提交
12
import { ICommandService } from 'vs/platform/commands/common/commands';
13
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
J
Johannes Rieken 已提交
14
import { Part } from 'vs/workbench/browser/part';
15
import { IStatusbarRegistry, Extensions, IStatusbarItem } from 'vs/workbench/browser/parts/statusbar/statusbar';
B
Benjamin Pasero 已提交
16
import { IInstantiationService, ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation';
J
Johannes Rieken 已提交
17
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
18
import { StatusbarAlignment, IStatusbarService, IStatusbarEntry } from 'vs/platform/statusbar/common/statusbar';
19 20
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { Action } from 'vs/base/common/actions';
B
Benjamin Pasero 已提交
21
import { IThemeService, registerThemingParticipant, ITheme, ICssStyleCollector } from 'vs/platform/theme/common/themeService';
22
import { STATUS_BAR_BACKGROUND, STATUS_BAR_FOREGROUND, STATUS_BAR_NO_FOLDER_BACKGROUND, STATUS_BAR_ITEM_HOVER_BACKGROUND, STATUS_BAR_ITEM_ACTIVE_BACKGROUND, STATUS_BAR_PROMINENT_ITEM_BACKGROUND, STATUS_BAR_PROMINENT_ITEM_HOVER_BACKGROUND, STATUS_BAR_BORDER, STATUS_BAR_NO_FOLDER_FOREGROUND, STATUS_BAR_NO_FOLDER_BORDER } from 'vs/workbench/common/theme';
23
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
24
import { contrastBorder } from 'vs/platform/theme/common/colorRegistry';
25
import { isThemeColor } from 'vs/editor/common/editorCommon';
26
import { Color } from 'vs/base/common/color';
B
Benjamin Pasero 已提交
27
import { addClass, EventHelper, createStyleSheet, addDisposableListener } from 'vs/base/browser/dom';
28
import { INotificationService } from 'vs/platform/notification/common/notification';
B
Benjamin Pasero 已提交
29
import { IStorageService } from 'vs/platform/storage/common/storage';
30
import { Parts } from 'vs/workbench/services/layout/browser/layoutService';
E
Erich Gamma 已提交
31

32
export class StatusbarPart extends Part implements IStatusbarService {
B
Benjamin Pasero 已提交
33 34

	_serviceBrand: ServiceIdentifier<any>;
E
Erich Gamma 已提交
35

B
Benjamin Pasero 已提交
36 37
	private static readonly PRIORITY_PROP = 'statusbar-entry-priority';
	private static readonly ALIGNMENT_PROP = 'statusbar-entry-alignment';
E
Erich Gamma 已提交
38

39
	//#region IView
E
Erich Gamma 已提交
40

B
Benjamin Pasero 已提交
41 42 43 44
	readonly minimumWidth: number = 0;
	readonly maximumWidth: number = Number.POSITIVE_INFINITY;
	readonly minimumHeight: number = 22;
	readonly maximumHeight: number = 22;
45

46
	//#endregion
47

B
Benjamin Pasero 已提交
48
	private statusMsgDispose: IDisposable;
49 50
	private styleElement: HTMLStyleElement;

E
Erich Gamma 已提交
51
	constructor(
52
		@IInstantiationService private readonly instantiationService: IInstantiationService,
53
		@IThemeService themeService: IThemeService,
54
		@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
55
		@IStorageService storageService: IStorageService
E
Erich Gamma 已提交
56
	) {
57
		super(Parts.STATUSBAR_PART, { hasTitle: false }, themeService, storageService);
E
Erich Gamma 已提交
58

B
Benjamin Pasero 已提交
59 60 61 62
		this.registerListeners();
	}

	private registerListeners(): void {
B
Benjamin Pasero 已提交
63
		this._register(this.contextService.onDidChangeWorkbenchState(() => this.updateStyles()));
E
Erich Gamma 已提交
64 65
	}

B
Benjamin Pasero 已提交
66
	addEntry(entry: IStatusbarEntry, alignment: StatusbarAlignment, priority: number = 0): IDisposable {
E
Erich Gamma 已提交
67 68

		// Render entry in status bar
R
Rob Lourens 已提交
69
		const el = this.doCreateStatusItem(alignment, priority, entry.showBeak ? 'has-beak' : undefined);
B
Benjamin Pasero 已提交
70 71
		const item = this.instantiationService.createInstance(StatusBarEntryItem, entry);
		const toDispose = item.render(el);
E
Erich Gamma 已提交
72 73

		// Insert according to priority
74
		const container = this.element;
B
Benjamin Pasero 已提交
75
		const neighbours = this.getEntries(alignment);
E
Erich Gamma 已提交
76
		let inserted = false;
77
		for (const neighbour of neighbours) {
B
Benjamin Pasero 已提交
78
			const nPriority = Number(neighbour.getAttribute(StatusbarPart.PRIORITY_PROP));
E
Erich Gamma 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92
			if (
				alignment === StatusbarAlignment.LEFT && nPriority < priority ||
				alignment === StatusbarAlignment.RIGHT && nPriority > priority
			) {
				container.insertBefore(el, neighbour);
				inserted = true;
				break;
			}
		}

		if (!inserted) {
			container.appendChild(el);
		}

93
		return toDisposable(() => {
94
			el.remove();
E
Erich Gamma 已提交
95

96 97
			if (toDispose) {
				toDispose.dispose();
E
Erich Gamma 已提交
98
			}
99
		});
E
Erich Gamma 已提交
100 101 102
	}

	private getEntries(alignment: StatusbarAlignment): HTMLElement[] {
B
Benjamin Pasero 已提交
103
		const entries: HTMLElement[] = [];
E
Erich Gamma 已提交
104

105
		const container = this.element;
B
Benjamin Pasero 已提交
106
		const children = container.children;
E
Erich Gamma 已提交
107
		for (let i = 0; i < children.length; i++) {
B
Benjamin Pasero 已提交
108
			const childElement = <HTMLElement>children.item(i);
B
Benjamin Pasero 已提交
109
			if (Number(childElement.getAttribute(StatusbarPart.ALIGNMENT_PROP)) === alignment) {
E
Erich Gamma 已提交
110 111 112 113 114 115 116
				entries.push(childElement);
			}
		}

		return entries;
	}

B
Benjamin Pasero 已提交
117
	createContentArea(parent: HTMLElement): HTMLElement {
118
		this.element = parent;
E
Erich Gamma 已提交
119 120

		// Fill in initial items that were contributed from the registry
B
Benjamin Pasero 已提交
121
		const registry = Registry.as<IStatusbarRegistry>(Extensions.Statusbar);
E
Erich Gamma 已提交
122

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
		const descriptors = registry.items.slice().sort((a, b) => {
			if (a.alignment === b.alignment) {
				if (a.alignment === StatusbarAlignment.LEFT) {
					return b.priority - a.priority;
				} else {
					return a.priority - b.priority;
				}
			} else if (a.alignment === StatusbarAlignment.LEFT) {
				return 1;
			} else if (a.alignment === StatusbarAlignment.RIGHT) {
				return -1;
			} else {
				return 0;
			}
		});
E
Erich Gamma 已提交
138

139
		for (const descriptor of descriptors) {
B
Benjamin Pasero 已提交
140 141
			const item = this.instantiationService.createInstance(descriptor.syncDescriptor);
			const el = this.doCreateStatusItem(descriptor.alignment, descriptor.priority);
E
Erich Gamma 已提交
142

B
Benjamin Pasero 已提交
143
			this._register(item.render(el));
144
			this.element.appendChild(el);
145
		}
E
Erich Gamma 已提交
146

147
		return this.element;
E
Erich Gamma 已提交
148 149
	}

150
	updateStyles(): void {
151 152
		super.updateStyles();

B
Benjamin Pasero 已提交
153
		const container = this.getContainer();
154

155 156
		// Background colors
		const backgroundColor = this.getColor(this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY ? STATUS_BAR_BACKGROUND : STATUS_BAR_NO_FOLDER_BACKGROUND);
B
Benjamin Pasero 已提交
157 158
		container.style.backgroundColor = backgroundColor;
		container.style.color = this.getColor(this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY ? STATUS_BAR_FOREGROUND : STATUS_BAR_NO_FOLDER_FOREGROUND);
B
Benjamin Pasero 已提交
159

160
		// Border color
161
		const borderColor = this.getColor(this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY ? STATUS_BAR_BORDER : STATUS_BAR_NO_FOLDER_BORDER) || this.getColor(contrastBorder);
B
Benjamin Pasero 已提交
162 163 164
		container.style.borderTopWidth = borderColor ? '1px' : null;
		container.style.borderTopStyle = borderColor ? 'solid' : null;
		container.style.borderTopColor = borderColor;
165 166 167

		// Notification Beak
		if (!this.styleElement) {
B
Benjamin Pasero 已提交
168
			this.styleElement = createStyleSheet(container);
169 170
		}

171
		this.styleElement.innerHTML = `.monaco-workbench .part.statusbar > .statusbar-item.has-beak:before { border-bottom-color: ${backgroundColor}; }`;
172 173
	}

174
	private doCreateStatusItem(alignment: StatusbarAlignment, priority: number = 0, extraClass?: string): HTMLElement {
B
Benjamin Pasero 已提交
175
		const el = document.createElement('div');
176
		addClass(el, 'statusbar-item');
177 178 179
		if (extraClass) {
			addClass(el, extraClass);
		}
E
Erich Gamma 已提交
180 181

		if (alignment === StatusbarAlignment.RIGHT) {
182
			addClass(el, 'right');
E
Erich Gamma 已提交
183
		} else {
184
			addClass(el, 'left');
E
Erich Gamma 已提交
185 186
		}

B
Benjamin Pasero 已提交
187 188
		el.setAttribute(StatusbarPart.PRIORITY_PROP, String(priority));
		el.setAttribute(StatusbarPart.ALIGNMENT_PROP, String(alignment));
E
Erich Gamma 已提交
189 190 191 192

		return el;
	}

B
Benjamin Pasero 已提交
193
	setStatusMessage(message: string, autoDisposeAfter: number = -1, delayBy: number = 0): IDisposable {
194 195 196 197 198 199
		if (this.statusMsgDispose) {
			this.statusMsgDispose.dispose(); // dismiss any previous
		}

		// Create new
		let statusDispose: IDisposable;
M
Matt Bierner 已提交
200
		let showHandle: any = setTimeout(() => {
201
			statusDispose = this.addEntry({ text: message }, StatusbarAlignment.LEFT, -Number.MAX_VALUE /* far right on left hand side */);
202 203
			showHandle = null;
		}, delayBy);
204
		let hideHandle: any;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

		// Dispose function takes care of timeouts and actual entry
		const dispose = {
			dispose: () => {
				if (showHandle) {
					clearTimeout(showHandle);
				}

				if (hideHandle) {
					clearTimeout(hideHandle);
				}

				if (statusDispose) {
					statusDispose.dispose();
				}
			}
		};
		this.statusMsgDispose = dispose;

		if (typeof autoDisposeAfter === 'number' && autoDisposeAfter > 0) {
			hideHandle = setTimeout(() => dispose.dispose(), autoDisposeAfter);
		}

		return dispose;
	}
230

B
Benjamin Pasero 已提交
231 232
	layout(width: number, height: number): void {
		super.layoutContents(width, height);
233 234 235 236 237 238 239
	}

	toJSON(): object {
		return {
			type: Parts.STATUSBAR_PART
		};
	}
E
Erich Gamma 已提交
240 241
}

242
let manageExtensionAction: ManageExtensionAction;
E
Erich Gamma 已提交
243 244 245
class StatusBarEntryItem implements IStatusbarItem {

	constructor(
246
		private entry: IStatusbarEntry,
247 248 249 250 251 252 253
		@ICommandService private readonly commandService: ICommandService,
		@IInstantiationService private readonly instantiationService: IInstantiationService,
		@INotificationService private readonly notificationService: INotificationService,
		@ITelemetryService private readonly telemetryService: ITelemetryService,
		@IContextMenuService private readonly contextMenuService: IContextMenuService,
		@IEditorService private readonly editorService: IEditorService,
		@IThemeService private readonly themeService: IThemeService
E
Erich Gamma 已提交
254 255
	) {
		this.entry = entry;
256 257 258 259

		if (!manageExtensionAction) {
			manageExtensionAction = this.instantiationService.createInstance(ManageExtensionAction);
		}
E
Erich Gamma 已提交
260 261
	}

B
Benjamin Pasero 已提交
262
	render(el: HTMLElement): IDisposable {
263
		let toDispose: IDisposable[] = [];
264 265
		addClass(el, 'statusbar-entry');

E
Erich Gamma 已提交
266 267 268 269 270
		// Text Container
		let textContainer: HTMLElement;
		if (this.entry.command) {
			textContainer = document.createElement('a');

M
Matt Bierner 已提交
271
			toDispose.push(addDisposableListener(textContainer, 'click', () => this.executeCommand(this.entry.command!, this.entry.arguments)));
E
Erich Gamma 已提交
272 273 274 275
		} else {
			textContainer = document.createElement('span');
		}

276 277
		// Label
		new OcticonLabel(textContainer).text = this.entry.text;
E
Erich Gamma 已提交
278 279 280

		// Tooltip
		if (this.entry.tooltip) {
B
Benjamin Pasero 已提交
281
			textContainer.title = this.entry.tooltip;
E
Erich Gamma 已提交
282 283 284
		}

		// Color
285 286 287 288 289 290 291
		let color = this.entry.color;
		if (color) {
			if (isThemeColor(color)) {
				let colorId = color.id;
				color = (this.themeService.getTheme().getColor(colorId) || Color.transparent).toString();
				toDispose.push(this.themeService.onThemeChange(theme => {
					let colorValue = (this.themeService.getTheme().getColor(colorId) || Color.transparent).toString();
B
Benjamin Pasero 已提交
292
					textContainer.style.color = colorValue;
293 294
				}));
			}
B
Benjamin Pasero 已提交
295
			textContainer.style.color = color;
E
Erich Gamma 已提交
296 297
		}

298 299
		// Context Menu
		if (this.entry.extensionId) {
B
Benjamin Pasero 已提交
300
			toDispose.push(addDisposableListener(textContainer, 'contextmenu', e => {
301
				EventHelper.stop(e, true);
302 303 304

				this.contextMenuService.showContextMenu({
					getAnchor: () => el,
S
Sandeep Somavarapu 已提交
305
					getActionsContext: () => this.entry.extensionId!.value,
306
					getActions: () => [manageExtensionAction]
307
				});
B
Benjamin Pasero 已提交
308
			}));
309 310
		}

E
Erich Gamma 已提交
311 312 313 314
		el.appendChild(textContainer);

		return {
			dispose: () => {
J
Joao Moreno 已提交
315
				toDispose = dispose(toDispose);
E
Erich Gamma 已提交
316 317 318 319
			}
		};
	}

J
Joao Moreno 已提交
320 321
	private executeCommand(id: string, args?: any[]) {
		args = args || [];
E
Erich Gamma 已提交
322

A
Alex Dima 已提交
323
		// Maintain old behaviour of always focusing the editor here
324 325 326
		const activeTextEditorWidget = this.editorService.activeTextEditorWidget;
		if (activeTextEditorWidget) {
			activeTextEditorWidget.focus();
E
Erich Gamma 已提交
327
		}
A
Alex Dima 已提交
328

329 330 331 332 333 334 335
		/* __GDPR__
			"workbenchActionExecuted" : {
				"id" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
				"from": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
			}
		*/
		this.telemetryService.publicLog('workbenchActionExecuted', { id, from: 'status bar' });
336
		this.commandService.executeCommand(id, ...args).then(undefined, err => this.notificationService.error(toErrorMessage(err)));
E
Erich Gamma 已提交
337
	}
A
Alex Dima 已提交
338
}
339 340 341 342

class ManageExtensionAction extends Action {

	constructor(
343
		@ICommandService private readonly commandService: ICommandService
344 345 346 347
	) {
		super('statusbar.manage.extension', nls.localize('manageExtension', "Manage Extension"));
	}

J
Johannes Rieken 已提交
348
	run(extensionId: string): Promise<any> {
349 350
		return this.commandService.executeCommand('_extensions.manage', extensionId);
	}
B
Benjamin Pasero 已提交
351 352 353 354 355
}

registerThemingParticipant((theme: ITheme, collector: ICssStyleCollector) => {
	const statusBarItemHoverBackground = theme.getColor(STATUS_BAR_ITEM_HOVER_BACKGROUND);
	if (statusBarItemHoverBackground) {
356
		collector.addRule(`.monaco-workbench .part.statusbar > .statusbar-item a:hover { background-color: ${statusBarItemHoverBackground}; }`);
B
Benjamin Pasero 已提交
357 358 359 360
	}

	const statusBarItemActiveBackground = theme.getColor(STATUS_BAR_ITEM_ACTIVE_BACKGROUND);
	if (statusBarItemActiveBackground) {
361
		collector.addRule(`.monaco-workbench .part.statusbar > .statusbar-item a:active { background-color: ${statusBarItemActiveBackground}; }`);
B
Benjamin Pasero 已提交
362 363
	}

B
Benjamin Pasero 已提交
364 365
	const statusBarProminentItemBackground = theme.getColor(STATUS_BAR_PROMINENT_ITEM_BACKGROUND);
	if (statusBarProminentItemBackground) {
366
		collector.addRule(`.monaco-workbench .part.statusbar > .statusbar-item .status-bar-info { background-color: ${statusBarProminentItemBackground}; }`);
B
Benjamin Pasero 已提交
367 368
	}

B
Benjamin Pasero 已提交
369 370
	const statusBarProminentItemHoverBackground = theme.getColor(STATUS_BAR_PROMINENT_ITEM_HOVER_BACKGROUND);
	if (statusBarProminentItemHoverBackground) {
371
		collector.addRule(`.monaco-workbench .part.statusbar > .statusbar-item a.status-bar-info:hover { background-color: ${statusBarProminentItemHoverBackground}; }`);
B
Benjamin Pasero 已提交
372
	}
373
});