mainThreadStatusBar.ts 2.6 KB
Newer Older
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.
 *--------------------------------------------------------------------------------------------*/

6
import { IStatusbarService, StatusbarAlignment as MainThreadStatusBarAlignment, IStatusbarEntryAccessor, IStatusbarEntry } from 'vs/platform/statusbar/common/statusbar';
7
import { MainThreadStatusBarShape, MainContext, IExtHostContext } from '../common/extHost.protocol';
B
Benjamin Pasero 已提交
8
import { ThemeColor } from 'vs/platform/theme/common/themeService';
9
import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers';
10
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
B
Benjamin Pasero 已提交
11
import { dispose } from 'vs/base/common/lifecycle';
12
import { localize } from 'vs/nls';
13

14
@extHostNamedCustomer(MainContext.MainThreadStatusBar)
15
export class MainThreadStatusBar implements MainThreadStatusBarShape {
16

B
Benjamin Pasero 已提交
17
	private readonly entries: Map<number, { accessor: IStatusbarEntryAccessor, alignment: MainThreadStatusBarAlignment, priority: number }> = new Map();
18 19

	constructor(
B
Benjamin Pasero 已提交
20 21 22
		_extHostContext: IExtHostContext,
		@IStatusbarService private readonly statusbarService: IStatusbarService
	) { }
23 24

	dispose(): void {
B
Benjamin Pasero 已提交
25 26
		this.entries.forEach(entry => entry.accessor.dispose());
		this.entries.clear();
27 28
	}

29 30
	$setEntry(id: number, extension: IExtensionDescription, text: string, tooltip: string, command: string, color: string | ThemeColor, alignment: MainThreadStatusBarAlignment, priority: number): void {
		const entry: IStatusbarEntry = {
31 32
			id: extension.identifier.value,
			name: localize('extensionLabel', "{0} (Extension)", extension.displayName || extension.name),
33 34 35 36 37 38
			text,
			tooltip,
			command,
			color,
			extensionId: extension.identifier
		};
B
Benjamin Pasero 已提交
39 40 41 42 43 44 45 46

		// Reset existing entry if alignment or priority changed
		let existingEntry = this.entries.get(id);
		if (existingEntry && (existingEntry.alignment !== alignment || existingEntry.priority !== priority)) {
			dispose(existingEntry.accessor);
			this.entries.delete(id);
			existingEntry = undefined;
		}
47

B
Benjamin Pasero 已提交
48 49 50 51
		// Create new entry if not existing
		if (!existingEntry) {
			this.entries.set(id, { accessor: this.statusbarService.addEntry(entry, alignment, priority), alignment, priority });
		}
52

B
Benjamin Pasero 已提交
53 54 55 56
		// Otherwise update
		else {
			existingEntry.accessor.update(entry);
		}
57 58
	}

A
Alex Dima 已提交
59
	$dispose(id: number) {
B
Benjamin Pasero 已提交
60 61 62 63
		const entry = this.entries.get(id);
		if (entry) {
			dispose(entry.accessor);
			this.entries.delete(id);
64 65 66
		}
	}
}