statusbarPart.ts 12.1 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 10
import dom = require('vs/base/browser/dom');
import nls = require('vs/nls');
J
Johannes Rieken 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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';
import { Registry } from 'vs/platform/platform';
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 { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actionRegistry';
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';
import { getCodeEditor } from 'vs/editor/common/services/codeEditorService';
27 28
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { Action } from 'vs/base/common/actions';
B
Benjamin Pasero 已提交
29 30
import { IThemeService, registerThemingParticipant, ITheme, ICssStyleCollector } from 'vs/platform/theme/common/themeService';
import { STATUS_BAR_BACKGROUND, STATUS_BAR_FOREGROUND, STATUS_BAR_NO_FOLDER_BACKGROUND, STATUS_BAR_INFO_ITEM_BACKGROUND, STATUS_BAR_INFO_ITEM_HOVER_BACKGROUND, STATUS_BAR_ITEM_HOVER_BACKGROUND, STATUS_BAR_ITEM_ACTIVE_BACKGROUND } from 'vs/workbench/common/theme';
31
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
B
Benjamin Pasero 已提交
32
import { highContrastBorder } from 'vs/platform/theme/common/colorRegistry';
E
Erich Gamma 已提交
33 34 35

export class StatusbarPart extends Part implements IStatusbarService {

36
	public _serviceBrand: any;
E
Erich Gamma 已提交
37 38 39 40 41 42

	private static PRIORITY_PROP = 'priority';
	private static ALIGNMENT_PROP = 'alignment';

	private toDispose: IDisposable[];
	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 53 54 55 56 57 58

		this.toDispose = [];
	}

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

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

		// Insert according to priority
B
Benjamin Pasero 已提交
64 65
		const container = this.statusItemsContainer.getHTMLElement();
		const neighbours = this.getEntries(alignment);
E
Erich Gamma 已提交
66 67
		let inserted = false;
		for (let i = 0; i < neighbours.length; i++) {
B
Benjamin Pasero 已提交
68 69
			const neighbour = neighbours[i];
			const nPriority = $(neighbour).getProperty(StatusbarPart.PRIORITY_PROP);
E
Erich Gamma 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
			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 已提交
96
		const entries: HTMLElement[] = [];
E
Erich Gamma 已提交
97

B
Benjamin Pasero 已提交
98 99
		const container = this.statusItemsContainer.getHTMLElement();
		const children = container.children;
E
Erich Gamma 已提交
100
		for (let i = 0; i < children.length; i++) {
B
Benjamin Pasero 已提交
101
			const childElement = <HTMLElement>children.item(i);
E
Erich Gamma 已提交
102 103 104 105 106 107 108 109 110 111 112 113
			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 已提交
114
		const registry = Registry.as<IStatusbarRegistry>(Extensions.Statusbar);
E
Erich Gamma 已提交
115

B
Benjamin Pasero 已提交
116 117
		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 已提交
118

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

		this.toDispose.push(...descriptors.map(descriptor => {
B
Benjamin Pasero 已提交
122 123
			const item = this.instantiationService.createInstance(descriptor.syncDescriptor);
			const el = this.doCreateStatusItem(descriptor.alignment, descriptor.priority);
E
Erich Gamma 已提交
124

B
Benjamin Pasero 已提交
125
			const dispose = item.render(el);
E
Erich Gamma 已提交
126 127 128 129 130 131 132 133
			this.statusItemsContainer.append(el);

			return dispose;
		}));

		return this.statusItemsContainer;
	}

134
	protected updateStyles(): void {
135 136
		super.updateStyles();

137 138 139 140
		const container = this.getContainer();

		container.style('color', this.getColor(STATUS_BAR_FOREGROUND));
		container.style('background-color', this.getColor(this.contextService.hasWorkspace() ? STATUS_BAR_BACKGROUND : STATUS_BAR_NO_FOLDER_BACKGROUND));
B
Benjamin Pasero 已提交
141

142 143 144 145
		const hcBorder = this.getColor(highContrastBorder);
		container.style('border-top-width', hcBorder ? '1px' : null);
		container.style('border-top-style', hcBorder ? 'solid' : null);
		container.style('border-top-color', hcBorder);
146 147
	}

E
Erich Gamma 已提交
148
	private doCreateStatusItem(alignment: StatusbarAlignment, priority: number = 0): HTMLElement {
B
Benjamin Pasero 已提交
149
		const el = document.createElement('div');
E
Erich Gamma 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163
		dom.addClass(el, 'statusbar-item');

		if (alignment === StatusbarAlignment.RIGHT) {
			dom.addClass(el, 'right');
		} else {
			dom.addClass(el, 'left');
		}

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

		return el;
	}

164 165 166 167 168 169 170 171
	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(() => {
172
			statusDispose = this.addEntry({ text: message }, StatusbarAlignment.LEFT, -Number.MAX_VALUE /* far right on left hand side */);
173 174 175 176 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
			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 已提交
202
	public dispose(): void {
J
Joao Moreno 已提交
203
		this.toDispose = dispose(this.toDispose);
E
Erich Gamma 已提交
204 205 206 207 208

		super.dispose();
	}
}

209
let manageExtensionAction: ManageExtensionAction;
E
Erich Gamma 已提交
210 211 212 213 214
class StatusBarEntryItem implements IStatusbarItem {
	private entry: IStatusbarEntry;

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

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

	public render(el: HTMLElement): IDisposable {
230
		let toDispose: IDisposable[] = [];
E
Erich Gamma 已提交
231 232 233 234 235 236 237 238 239 240 241 242
		dom.addClass(el, 'statusbar-entry');

		// Text Container
		let textContainer: HTMLElement;
		if (this.entry.command) {
			textContainer = document.createElement('a');

			$(textContainer).on('click', () => this.executeCommand(this.entry.command), toDispose);
		} else {
			textContainer = document.createElement('span');
		}

243 244
		// Label
		new OcticonLabel(textContainer).text = this.entry.text;
E
Erich Gamma 已提交
245 246 247 248 249 250 251 252 253 254 255

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

		// Color
		if (this.entry.color) {
			$(textContainer).color(this.entry.color);
		}

256 257 258 259 260 261 262 263 264 265 266 267 268
		// Context Menu
		if (this.entry.extensionId) {
			$(textContainer).on('contextmenu', e => {
				dom.EventHelper.stop(e, true);

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

E
Erich Gamma 已提交
269 270 271 272
		el.appendChild(textContainer);

		return {
			dispose: () => {
J
Joao Moreno 已提交
273
				toDispose = dispose(toDispose);
E
Erich Gamma 已提交
274 275 276 277 278 279 280
			}
		};
	}

	private executeCommand(id: string) {

		// Lookup built in commands
B
Benjamin Pasero 已提交
281
		const builtInActionDescriptor = Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions).getWorkbenchAction(id);
E
Erich Gamma 已提交
282
		if (builtInActionDescriptor) {
B
Benjamin Pasero 已提交
283
			const action = this.instantiationService.createInstance(builtInActionDescriptor.syncDescriptor);
E
Erich Gamma 已提交
284 285 286

			if (action.enabled) {
				this.telemetryService.publicLog('workbenchActionExecuted', { id: action.id, from: 'status bar' });
A
Alex Dima 已提交
287
				(action.run() || TPromise.as(null)).done(() => {
E
Erich Gamma 已提交
288 289 290
					action.dispose();
				}, (err) => this.messageService.show(Severity.Error, toErrorMessage(err)));
			} else {
B
wording  
Benjamin Pasero 已提交
291
				this.messageService.show(Severity.Warning, nls.localize('canNotRun', "Command '{0}' is currently not enabled and can not be run.", action.label || id));
E
Erich Gamma 已提交
292
			}
A
Alex Dima 已提交
293 294

			return;
E
Erich Gamma 已提交
295 296
		}

A
Alex Dima 已提交
297
		// Maintain old behaviour of always focusing the editor here
B
Benjamin Pasero 已提交
298 299
		const activeEditor = this.editorService.getActiveEditor();
		const codeEditor = getCodeEditor(activeEditor);
A
Alex Dima 已提交
300 301
		if (codeEditor) {
			codeEditor.focus();
E
Erich Gamma 已提交
302
		}
A
Alex Dima 已提交
303 304 305

		// Fallback to the command service for any other case
		this.commandService.executeCommand(id).done(undefined, err => this.messageService.show(Severity.Error, toErrorMessage(err)));
E
Erich Gamma 已提交
306
	}
A
Alex Dima 已提交
307
}
308 309 310 311 312 313 314 315 316 317 318 319

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 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
}

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}; }`);
	}

	const statusBarInfoItemBackground = theme.getColor(STATUS_BAR_INFO_ITEM_BACKGROUND);
	if (statusBarInfoItemBackground) {
		collector.addRule(`.monaco-workbench > .part.statusbar > .statusbar-item .status-bar-info { background-color: ${statusBarInfoItemBackground}; }`);
	}

	const statusBarInfoItemHoverBackground = theme.getColor(STATUS_BAR_INFO_ITEM_HOVER_BACKGROUND);
	if (statusBarInfoItemHoverBackground) {
		collector.addRule(`.monaco-workbench > .part.statusbar > .statusbar-item a.status-bar-info:hover:not([disabled]):not(.disabled) { background-color: ${statusBarInfoItemHoverBackground}; }`);
	}
});