statusbarPart.ts 12.8 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

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

export class StatusbarPart extends Part implements IStatusbarService {

36
	public _serviceBrand: any;
E
Erich Gamma 已提交
37

38 39
	private static readonly PRIORITY_PROP = 'priority';
	private static readonly ALIGNMENT_PROP = 'alignment';
E
Erich Gamma 已提交
40

41
	private statusItemsContainer: HTMLElement;
42
	private statusMsgDispose: IDisposable;
E
Erich Gamma 已提交
43

44 45
	private styleElement: HTMLStyleElement;

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

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

	private registerListeners(): void {
58
		this.toUnbind.push(this.contextService.onDidChangeWorkbenchState(() => this.updateStyles()));
E
Erich Gamma 已提交
59 60 61 62 63
	}

	public addEntry(entry: IStatusbarEntry, alignment: StatusbarAlignment, priority: number = 0): IDisposable {

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

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

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

		return {
			dispose: () => {
				$(el).destroy();

				if (toDispose) {
					toDispose.dispose();
				}
			}
		};
	}

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

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

		return entries;
	}

115 116
	public createContentArea(parent: HTMLElement): HTMLElement {
		this.statusItemsContainer = parent;
E
Erich Gamma 已提交
117 118

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

B
Benjamin Pasero 已提交
121 122
		const leftDescriptors = registry.items.filter(d => d.alignment === StatusbarAlignment.LEFT).sort((a, b) => b.priority - a.priority);
		const rightDescriptors = registry.items.filter(d => d.alignment === StatusbarAlignment.RIGHT).sort((a, b) => a.priority - b.priority);
E
Erich Gamma 已提交
123

B
Benjamin Pasero 已提交
124
		const descriptors = rightDescriptors.concat(leftDescriptors); // right first because they float
E
Erich Gamma 已提交
125

B
Benjamin Pasero 已提交
126
		this.toUnbind.push(...descriptors.map(descriptor => {
B
Benjamin Pasero 已提交
127 128
			const item = this.instantiationService.createInstance(descriptor.syncDescriptor);
			const el = this.doCreateStatusItem(descriptor.alignment, descriptor.priority);
E
Erich Gamma 已提交
129

B
Benjamin Pasero 已提交
130
			const dispose = item.render(el);
131
			this.statusItemsContainer.appendChild(el);
E
Erich Gamma 已提交
132 133 134 135 136 137 138

			return dispose;
		}));

		return this.statusItemsContainer;
	}

139
	protected updateStyles(): void {
140 141
		super.updateStyles();

142
		const container = $(this.getContainer());
143

144 145 146
		// Background colors
		const backgroundColor = this.getColor(this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY ? STATUS_BAR_BACKGROUND : STATUS_BAR_NO_FOLDER_BACKGROUND);
		container.style('background-color', backgroundColor);
147
		container.style('color', this.getColor(this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY ? STATUS_BAR_FOREGROUND : STATUS_BAR_NO_FOLDER_FOREGROUND));
B
Benjamin Pasero 已提交
148

149
		// Border color
150
		const borderColor = this.getColor(this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY ? STATUS_BAR_BORDER : STATUS_BAR_NO_FOLDER_BORDER) || this.getColor(contrastBorder);
151 152 153
		container.style('border-top-width', borderColor ? '1px' : null);
		container.style('border-top-style', borderColor ? 'solid' : null);
		container.style('border-top-color', borderColor);
154 155 156 157 158 159 160

		// Notification Beak
		if (!this.styleElement) {
			this.styleElement = createStyleSheet(container.getHTMLElement());
		}

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

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

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

		$(el).setProperty(StatusbarPart.PRIORITY_PROP, priority);
		$(el).setProperty(StatusbarPart.ALIGNMENT_PROP, alignment);

		return el;
	}

182 183 184 185 186 187 188 189
	public setStatusMessage(message: string, autoDisposeAfter: number = -1, delayBy: number = 0): IDisposable {
		if (this.statusMsgDispose) {
			this.statusMsgDispose.dispose(); // dismiss any previous
		}

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

		// 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 已提交
219 220
}

221
let manageExtensionAction: ManageExtensionAction;
E
Erich Gamma 已提交
222 223 224
class StatusBarEntryItem implements IStatusbarItem {

	constructor(
225
		private entry: IStatusbarEntry,
226
		@ICommandService private commandService: ICommandService,
E
Erich Gamma 已提交
227
		@IInstantiationService private instantiationService: IInstantiationService,
228
		@INotificationService private notificationService: INotificationService,
E
Erich Gamma 已提交
229
		@ITelemetryService private telemetryService: ITelemetryService,
230
		@IContextMenuService private contextMenuService: IContextMenuService,
231
		@INextEditorService private editorService: INextEditorService,
232
		@IThemeService private themeService: IThemeService
E
Erich Gamma 已提交
233 234
	) {
		this.entry = entry;
235 236 237 238

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

	public render(el: HTMLElement): IDisposable {
242
		let toDispose: IDisposable[] = [];
243 244
		addClass(el, 'statusbar-entry');

E
Erich Gamma 已提交
245 246 247 248 249
		// Text Container
		let textContainer: HTMLElement;
		if (this.entry.command) {
			textContainer = document.createElement('a');

J
Joao Moreno 已提交
250
			$(textContainer).on('click', () => this.executeCommand(this.entry.command, this.entry.arguments), toDispose);
E
Erich Gamma 已提交
251 252 253 254
		} else {
			textContainer = document.createElement('span');
		}

255 256
		// Label
		new OcticonLabel(textContainer).text = this.entry.text;
E
Erich Gamma 已提交
257 258 259 260 261 262 263

		// Tooltip
		if (this.entry.tooltip) {
			$(textContainer).title(this.entry.tooltip);
		}

		// Color
264 265 266 267 268 269 270 271 272 273 274
		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();
					$(textContainer).color(colorValue);
				}));
			}
			$(textContainer).color(color);
E
Erich Gamma 已提交
275 276
		}

277 278 279
		// Context Menu
		if (this.entry.extensionId) {
			$(textContainer).on('contextmenu', e => {
280
				EventHelper.stop(e, true);
281 282 283 284 285 286 287 288 289

				this.contextMenuService.showContextMenu({
					getAnchor: () => el,
					getActionsContext: () => this.entry.extensionId,
					getActions: () => TPromise.as([manageExtensionAction])
				});
			}, toDispose);
		}

E
Erich Gamma 已提交
290 291 292 293
		el.appendChild(textContainer);

		return {
			dispose: () => {
J
Joao Moreno 已提交
294
				toDispose = dispose(toDispose);
E
Erich Gamma 已提交
295 296 297 298
			}
		};
	}

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

A
Alex Dima 已提交
302
		// Maintain old behaviour of always focusing the editor here
303 304 305
		const activeTextEditorControl = this.editorService.activeTextEditorControl;
		if (activeTextEditorControl) {
			activeTextEditorControl.focus();
E
Erich Gamma 已提交
306
		}
A
Alex Dima 已提交
307

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

class ManageExtensionAction extends Action {

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

	public run(extensionId: string): TPromise<any> {
		return this.commandService.executeCommand('_extensions.manage', extensionId);
	}
B
Benjamin Pasero 已提交
330 331 332 333 334
}

registerThemingParticipant((theme: ITheme, collector: ICssStyleCollector) => {
	const statusBarItemHoverBackground = theme.getColor(STATUS_BAR_ITEM_HOVER_BACKGROUND);
	if (statusBarItemHoverBackground) {
335
		collector.addRule(`.monaco-workbench > .part.statusbar > .statusbar-item a:hover { background-color: ${statusBarItemHoverBackground}; }`);
B
Benjamin Pasero 已提交
336 337 338 339
	}

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

B
Benjamin Pasero 已提交
343 344 345
	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 已提交
346 347
	}

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