statusbarPart.ts 12.9 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 9
import { toErrorMessage } from 'vs/base/common/errorMessage';
import { TPromise } from 'vs/base/common/winjs.base';
10
import { dispose, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
J
Johannes Rieken 已提交
11
import { OcticonLabel } from 'vs/base/browser/ui/octiconLabel/octiconLabel';
12
import { Registry } from 'vs/platform/registry/common/platform';
J
Johannes Rieken 已提交
13
import { ICommandService } from 'vs/platform/commands/common/commands';
14
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
J
Johannes Rieken 已提交
15
import { Part } from 'vs/workbench/browser/part';
16
import { IStatusbarRegistry, Extensions, IStatusbarItem } from 'vs/workbench/browser/parts/statusbar/statusbar';
J
Johannes Rieken 已提交
17 18
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
19
import { StatusbarAlignment, IStatusbarService, IStatusbarEntry } from 'vs/platform/statusbar/common/statusbar';
20 21
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { Action } from 'vs/base/common/actions';
B
Benjamin Pasero 已提交
22
import { IThemeService, registerThemingParticipant, ITheme, ICssStyleCollector } from 'vs/platform/theme/common/themeService';
23
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';
24
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
25
import { contrastBorder } from 'vs/platform/theme/common/colorRegistry';
26
import { isThemeColor } from 'vs/editor/common/editorCommon';
27
import { Color } from 'vs/base/common/color';
28
import { addClass, EventHelper, createStyleSheet, addDisposableListener } from 'vs/base/browser/dom';
29
import { INotificationService } from 'vs/platform/notification/common/notification';
30
import { IStorageService } from 'vs/platform/storage2/common/storage2';
E
Erich Gamma 已提交
31 32 33

export class StatusbarPart extends Part implements IStatusbarService {

B
Benjamin Pasero 已提交
34
	_serviceBrand: 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
	private statusItemsContainer: HTMLElement;
40
	private statusMsgDispose: IDisposable;
E
Erich Gamma 已提交
41

42 43
	private styleElement: HTMLStyleElement;

E
Erich Gamma 已提交
44
	constructor(
45
		id: string,
B
Benjamin Pasero 已提交
46
		@IInstantiationService private instantiationService: IInstantiationService,
47
		@IThemeService themeService: IThemeService,
B
Benjamin Pasero 已提交
48
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
49
		@IStorageService storageService: IStorageService
E
Erich Gamma 已提交
50
	) {
51
		super(id, { hasTitle: false }, themeService, storageService);
E
Erich Gamma 已提交
52

B
Benjamin Pasero 已提交
53 54 55 56
		this.registerListeners();
	}

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

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

		// Render entry in status bar
63
		const el = this.doCreateStatusItem(alignment, priority, entry.showBeak ? 'has-beak' : void 0);
B
Benjamin Pasero 已提交
64 65
		const item = this.instantiationService.createInstance(StatusBarEntryItem, entry);
		const toDispose = item.render(el);
E
Erich Gamma 已提交
66 67

		// Insert according to priority
68
		const container = this.statusItemsContainer;
B
Benjamin Pasero 已提交
69
		const neighbours = this.getEntries(alignment);
E
Erich Gamma 已提交
70 71
		let inserted = false;
		for (let i = 0; i < neighbours.length; i++) {
B
Benjamin Pasero 已提交
72
			const neighbour = neighbours[i];
B
Benjamin Pasero 已提交
73
			const nPriority = Number(neighbour.getAttribute(StatusbarPart.PRIORITY_PROP));
E
Erich Gamma 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87
			if (
				alignment === StatusbarAlignment.LEFT && nPriority < priority ||
				alignment === StatusbarAlignment.RIGHT && nPriority > priority
			) {
				container.insertBefore(el, neighbour);
				inserted = true;
				break;
			}
		}

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

88
		return toDisposable(() => {
89
			el.remove();
E
Erich Gamma 已提交
90

91 92
			if (toDispose) {
				toDispose.dispose();
E
Erich Gamma 已提交
93
			}
94
		});
E
Erich Gamma 已提交
95 96 97
	}

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

100
		const container = this.statusItemsContainer;
B
Benjamin Pasero 已提交
101
		const children = container.children;
E
Erich Gamma 已提交
102
		for (let i = 0; i < children.length; i++) {
B
Benjamin Pasero 已提交
103
			const childElement = <HTMLElement>children.item(i);
B
Benjamin Pasero 已提交
104
			if (Number(childElement.getAttribute(StatusbarPart.ALIGNMENT_PROP)) === alignment) {
E
Erich Gamma 已提交
105 106 107 108 109 110 111
				entries.push(childElement);
			}
		}

		return entries;
	}

B
Benjamin Pasero 已提交
112
	createContentArea(parent: HTMLElement): HTMLElement {
113
		this.statusItemsContainer = parent;
E
Erich Gamma 已提交
114 115

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

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
		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 已提交
133

134
		for (const descriptor of descriptors) {
B
Benjamin Pasero 已提交
135 136
			const item = this.instantiationService.createInstance(descriptor.syncDescriptor);
			const el = this.doCreateStatusItem(descriptor.alignment, descriptor.priority);
E
Erich Gamma 已提交
137

B
Benjamin Pasero 已提交
138
			this._register(item.render(el));
139
			this.statusItemsContainer.appendChild(el);
140
		}
E
Erich Gamma 已提交
141 142 143 144

		return this.statusItemsContainer;
	}

145
	protected updateStyles(): void {
146 147
		super.updateStyles();

B
Benjamin Pasero 已提交
148
		const container = this.getContainer();
149

150 151
		// Background colors
		const backgroundColor = this.getColor(this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY ? STATUS_BAR_BACKGROUND : STATUS_BAR_NO_FOLDER_BACKGROUND);
B
Benjamin Pasero 已提交
152 153
		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 已提交
154

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

		// Notification Beak
		if (!this.styleElement) {
B
Benjamin Pasero 已提交
163
			this.styleElement = createStyleSheet(container);
164 165 166
		}

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

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

		if (alignment === StatusbarAlignment.RIGHT) {
177
			addClass(el, 'right');
E
Erich Gamma 已提交
178
		} else {
179
			addClass(el, 'left');
E
Erich Gamma 已提交
180 181
		}

B
Benjamin Pasero 已提交
182 183
		el.setAttribute(StatusbarPart.PRIORITY_PROP, String(priority));
		el.setAttribute(StatusbarPart.ALIGNMENT_PROP, String(alignment));
E
Erich Gamma 已提交
184 185 186 187

		return el;
	}

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

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

		// 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;
	}
E
Erich Gamma 已提交
225 226
}

227
let manageExtensionAction: ManageExtensionAction;
E
Erich Gamma 已提交
228 229 230
class StatusBarEntryItem implements IStatusbarItem {

	constructor(
231
		private entry: IStatusbarEntry,
232
		@ICommandService private commandService: ICommandService,
E
Erich Gamma 已提交
233
		@IInstantiationService private instantiationService: IInstantiationService,
234
		@INotificationService private notificationService: INotificationService,
E
Erich Gamma 已提交
235
		@ITelemetryService private telemetryService: ITelemetryService,
236
		@IContextMenuService private contextMenuService: IContextMenuService,
237
		@IEditorService private editorService: IEditorService,
238
		@IThemeService private themeService: IThemeService
E
Erich Gamma 已提交
239 240
	) {
		this.entry = entry;
241 242 243 244

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

B
Benjamin Pasero 已提交
247
	render(el: HTMLElement): IDisposable {
248
		let toDispose: IDisposable[] = [];
249 250
		addClass(el, 'statusbar-entry');

E
Erich Gamma 已提交
251 252 253 254 255
		// Text Container
		let textContainer: HTMLElement;
		if (this.entry.command) {
			textContainer = document.createElement('a');

B
Benjamin Pasero 已提交
256
			toDispose.push(addDisposableListener(textContainer, 'click', () => this.executeCommand(this.entry.command, this.entry.arguments)));
E
Erich Gamma 已提交
257 258 259 260
		} else {
			textContainer = document.createElement('span');
		}

261 262
		// Label
		new OcticonLabel(textContainer).text = this.entry.text;
E
Erich Gamma 已提交
263 264 265

		// Tooltip
		if (this.entry.tooltip) {
B
Benjamin Pasero 已提交
266
			textContainer.title = this.entry.tooltip;
E
Erich Gamma 已提交
267 268 269
		}

		// Color
270 271 272 273 274 275 276
		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 已提交
277
					textContainer.style.color = colorValue;
278 279
				}));
			}
B
Benjamin Pasero 已提交
280
			textContainer.style.color = color;
E
Erich Gamma 已提交
281 282
		}

283 284
		// Context Menu
		if (this.entry.extensionId) {
B
Benjamin Pasero 已提交
285
			toDispose.push(addDisposableListener(textContainer, 'contextmenu', e => {
286
				EventHelper.stop(e, true);
287 288 289 290

				this.contextMenuService.showContextMenu({
					getAnchor: () => el,
					getActionsContext: () => this.entry.extensionId,
291
					getActions: () => Promise.resolve([manageExtensionAction])
292
				});
B
Benjamin Pasero 已提交
293
			}));
294 295
		}

E
Erich Gamma 已提交
296 297 298 299
		el.appendChild(textContainer);

		return {
			dispose: () => {
J
Joao Moreno 已提交
300
				toDispose = dispose(toDispose);
E
Erich Gamma 已提交
301 302 303 304
			}
		};
	}

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

A
Alex Dima 已提交
308
		// Maintain old behaviour of always focusing the editor here
309 310 311
		const activeTextEditorWidget = this.editorService.activeTextEditorWidget;
		if (activeTextEditorWidget) {
			activeTextEditorWidget.focus();
E
Erich Gamma 已提交
312
		}
A
Alex Dima 已提交
313

314 315 316 317 318 319 320
		/* __GDPR__
			"workbenchActionExecuted" : {
				"id" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
				"from": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
			}
		*/
		this.telemetryService.publicLog('workbenchActionExecuted', { id, from: 'status bar' });
321
		this.commandService.executeCommand(id, ...args).then(undefined, err => this.notificationService.error(toErrorMessage(err)));
E
Erich Gamma 已提交
322
	}
A
Alex Dima 已提交
323
}
324 325 326 327 328 329 330 331 332

class ManageExtensionAction extends Action {

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

B
Benjamin Pasero 已提交
333
	run(extensionId: string): TPromise<any> {
334 335
		return this.commandService.executeCommand('_extensions.manage', extensionId);
	}
B
Benjamin Pasero 已提交
336 337 338 339 340
}

registerThemingParticipant((theme: ITheme, collector: ICssStyleCollector) => {
	const statusBarItemHoverBackground = theme.getColor(STATUS_BAR_ITEM_HOVER_BACKGROUND);
	if (statusBarItemHoverBackground) {
341
		collector.addRule(`.monaco-workbench > .part.statusbar > .statusbar-item a:hover { background-color: ${statusBarItemHoverBackground}; }`);
B
Benjamin Pasero 已提交
342 343 344 345
	}

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

B
Benjamin Pasero 已提交
349 350 351
	const statusBarProminentItemBackground = theme.getColor(STATUS_BAR_PROMINENT_ITEM_BACKGROUND);
	if (statusBarProminentItemBackground) {
		collector.addRule(`.monaco-workbench > .part.statusbar > .statusbar-item .status-bar-info { background-color: ${statusBarProminentItemBackground}; }`);
B
Benjamin Pasero 已提交
352 353
	}

B
Benjamin Pasero 已提交
354 355
	const statusBarProminentItemHoverBackground = theme.getColor(STATUS_BAR_PROMINENT_ITEM_HOVER_BACKGROUND);
	if (statusBarProminentItemHoverBackground) {
356
		collector.addRule(`.monaco-workbench > .part.statusbar > .statusbar-item a.status-bar-info:hover { background-color: ${statusBarProminentItemHoverBackground}; }`);
B
Benjamin Pasero 已提交
357
	}
358
});