statusbarPart.ts 12.5 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';
E
Erich Gamma 已提交
9
import nls = require('vs/nls');
J
Johannes Rieken 已提交
10 11 12 13 14
import { toErrorMessage } from 'vs/base/common/errorMessage';
import { TPromise } from 'vs/base/common/winjs.base';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { Builder, $ } from 'vs/base/browser/builder';
import { OcticonLabel } from 'vs/base/browser/ui/octiconLabel/octiconLabel';
15
import { Registry } from 'vs/platform/registry/common/platform';
J
Johannes Rieken 已提交
16 17 18 19 20 21 22 23
import { ICommandService } from 'vs/platform/commands/common/commands';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
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 { IMessageService, Severity } from 'vs/platform/message/common/message';
import { IStatusbarService, IStatusbarEntry } from 'vs/platform/statusbar/common/statusbar';
24
import { getCodeEditor } from 'vs/editor/browser/services/codeEditorService';
25 26
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { Action } from 'vs/base/common/actions';
B
Benjamin Pasero 已提交
27
import { IThemeService, registerThemingParticipant, ITheme, ICssStyleCollector } from 'vs/platform/theme/common/themeService';
28
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';
29
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
30
import { contrastBorder } from 'vs/platform/theme/common/colorRegistry';
31
import { isThemeColor } from 'vs/editor/common/editorCommon';
32
import { Color } from 'vs/base/common/color';
33
import { addClass, EventHelper } from 'vs/base/browser/dom';
E
Erich Gamma 已提交
34 35 36

export class StatusbarPart extends Part implements IStatusbarService {

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

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

	private statusItemsContainer: Builder;
43
	private statusMsgDispose: IDisposable;
E
Erich Gamma 已提交
44 45

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

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

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

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

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

		// Insert according to priority
B
Benjamin Pasero 已提交
68 69
		const container = this.statusItemsContainer.getHTMLElement();
		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 73
			const neighbour = neighbours[i];
			const nPriority = $(neighbour).getProperty(StatusbarPart.PRIORITY_PROP);
E
Erich Gamma 已提交
74 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
			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 已提交
100
		const entries: HTMLElement[] = [];
E
Erich Gamma 已提交
101

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

		return entries;
	}

	public createContentArea(parent: Builder): Builder {
		this.statusItemsContainer = $(parent);

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

B
Benjamin Pasero 已提交
120 121
		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 已提交
122

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

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

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

			return dispose;
		}));

		return this.statusItemsContainer;
	}

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

141 142
		const container = this.getContainer();

143 144
		container.style('color', this.getColor(this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY ? STATUS_BAR_FOREGROUND : STATUS_BAR_NO_FOLDER_FOREGROUND));
		container.style('background-color', this.getColor(this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY ? STATUS_BAR_BACKGROUND : STATUS_BAR_NO_FOLDER_BACKGROUND));
B
Benjamin Pasero 已提交
145

146
		const borderColor = this.getColor(this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY ? STATUS_BAR_BORDER : STATUS_BAR_NO_FOLDER_BORDER) || this.getColor(contrastBorder);
147 148 149
		container.style('border-top-width', borderColor ? '1px' : null);
		container.style('border-top-style', borderColor ? 'solid' : null);
		container.style('border-top-color', borderColor);
150 151
	}

E
Erich Gamma 已提交
152
	private doCreateStatusItem(alignment: StatusbarAlignment, priority: number = 0): HTMLElement {
B
Benjamin Pasero 已提交
153
		const el = document.createElement('div');
154
		addClass(el, 'statusbar-item');
E
Erich Gamma 已提交
155 156

		if (alignment === StatusbarAlignment.RIGHT) {
157
			addClass(el, 'right');
E
Erich Gamma 已提交
158
		} else {
159
			addClass(el, 'left');
E
Erich Gamma 已提交
160 161 162 163 164 165 166 167
		}

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

		return el;
	}

168 169 170 171 172 173 174 175
	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(() => {
176
			statusDispose = this.addEntry({ text: message }, StatusbarAlignment.LEFT, -Number.MAX_VALUE /* far right on left hand side */);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
			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 已提交
205 206
}

207
let manageExtensionAction: ManageExtensionAction;
E
Erich Gamma 已提交
208 209 210
class StatusBarEntryItem implements IStatusbarItem {

	constructor(
211
		private entry: IStatusbarEntry,
212
		@ICommandService private commandService: ICommandService,
E
Erich Gamma 已提交
213 214 215
		@IInstantiationService private instantiationService: IInstantiationService,
		@IMessageService private messageService: IMessageService,
		@ITelemetryService private telemetryService: ITelemetryService,
216
		@IContextMenuService private contextMenuService: IContextMenuService,
217 218
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IThemeService private themeService: IThemeService
E
Erich Gamma 已提交
219 220
	) {
		this.entry = entry;
221 222 223 224

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

	public render(el: HTMLElement): IDisposable {
228
		let toDispose: IDisposable[] = [];
229 230
		addClass(el, 'statusbar-entry');

E
Erich Gamma 已提交
231 232 233 234 235
		// Text Container
		let textContainer: HTMLElement;
		if (this.entry.command) {
			textContainer = document.createElement('a');

J
Joao Moreno 已提交
236
			$(textContainer).on('click', () => this.executeCommand(this.entry.command, this.entry.arguments), toDispose);
E
Erich Gamma 已提交
237 238 239 240
		} else {
			textContainer = document.createElement('span');
		}

241 242
		// Label
		new OcticonLabel(textContainer).text = this.entry.text;
E
Erich Gamma 已提交
243 244 245 246 247 248 249

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

		// Color
250 251 252 253 254 255 256 257 258 259 260
		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 已提交
261 262
		}

263 264 265
		// Context Menu
		if (this.entry.extensionId) {
			$(textContainer).on('contextmenu', e => {
266
				EventHelper.stop(e, true);
267 268 269 270 271 272 273 274 275

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

E
Erich Gamma 已提交
276 277 278 279
		el.appendChild(textContainer);

		return {
			dispose: () => {
J
Joao Moreno 已提交
280
				toDispose = dispose(toDispose);
E
Erich Gamma 已提交
281 282 283 284
			}
		};
	}

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

A
Alex Dima 已提交
288
		// Maintain old behaviour of always focusing the editor here
B
Benjamin Pasero 已提交
289 290
		const activeEditor = this.editorService.getActiveEditor();
		const codeEditor = getCodeEditor(activeEditor);
A
Alex Dima 已提交
291 292
		if (codeEditor) {
			codeEditor.focus();
E
Erich Gamma 已提交
293
		}
A
Alex Dima 已提交
294

295 296 297 298 299 300 301
		/* __GDPR__
			"workbenchActionExecuted" : {
				"id" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
				"from": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
			}
		*/
		this.telemetryService.publicLog('workbenchActionExecuted', { id, from: 'status bar' });
J
Joao Moreno 已提交
302
		this.commandService.executeCommand(id, ...args).done(undefined, err => this.messageService.show(Severity.Error, toErrorMessage(err)));
E
Erich Gamma 已提交
303
	}
A
Alex Dima 已提交
304
}
305 306 307 308 309 310 311 312 313 314 315 316

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 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329
}

registerThemingParticipant((theme: ITheme, collector: ICssStyleCollector) => {
	const statusBarItemHoverBackground = theme.getColor(STATUS_BAR_ITEM_HOVER_BACKGROUND);
	if (statusBarItemHoverBackground) {
		collector.addRule(`.monaco-workbench > .part.statusbar > .statusbar-item a:hover:not([disabled]):not(.disabled) { background-color: ${statusBarItemHoverBackground}; }`);
	}

	const statusBarItemActiveBackground = theme.getColor(STATUS_BAR_ITEM_ACTIVE_BACKGROUND);
	if (statusBarItemActiveBackground) {
		collector.addRule(`.monaco-workbench > .part.statusbar > .statusbar-item a:active:not([disabled]):not(.disabled) { background-color: ${statusBarItemActiveBackground}; }`);
	}

B
Benjamin Pasero 已提交
330 331 332
	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 已提交
333 334
	}

B
Benjamin Pasero 已提交
335 336 337
	const statusBarProminentItemHoverBackground = theme.getColor(STATUS_BAR_PROMINENT_ITEM_HOVER_BACKGROUND);
	if (statusBarProminentItemHoverBackground) {
		collector.addRule(`.monaco-workbench > .part.statusbar > .statusbar-item a.status-bar-info:hover:not([disabled]):not(.disabled) { background-color: ${statusBarProminentItemHoverBackground}; }`);
B
Benjamin Pasero 已提交
338
	}
339
});