statusbarPart.ts 12.8 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';
J
Johannes Rieken 已提交
16 17
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
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';
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';
E
Erich Gamma 已提交
30 31 32

export class StatusbarPart extends Part implements IStatusbarService {

B
Benjamin Pasero 已提交
33
	_serviceBrand: any;
E
Erich Gamma 已提交
34

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

38
	private statusItemsContainer: HTMLElement;
39
	private statusMsgDispose: IDisposable;
E
Erich Gamma 已提交
40

41 42
	private styleElement: HTMLStyleElement;

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

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

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

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

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

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

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

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

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

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

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

		return entries;
	}

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

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

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

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

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

		return this.statusItemsContainer;
	}

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

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

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

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

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

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

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

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

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

		return el;
	}

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

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

		// 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 已提交
224 225
}

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

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

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

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

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

M
Matt Bierner 已提交
255
			toDispose.push(addDisposableListener(textContainer, 'click', () => this.executeCommand(this.entry.command!, this.entry.arguments)));
E
Erich Gamma 已提交
256 257 258 259
		} else {
			textContainer = document.createElement('span');
		}

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

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

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

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

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

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

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

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

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

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

class ManageExtensionAction extends Action {

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

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

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

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

B
Benjamin Pasero 已提交
348 349 350
	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 已提交
351 352
	}

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