mainThreadStatusBar.ts 2.9 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/workbench/services/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';
B
Benjamin Pasero 已提交
10
import { dispose } from 'vs/base/common/lifecycle';
11
import { Command } from 'vs/editor/common/modes';
12
import { IAccessibilityInformation } from 'vs/platform/accessibility/common/accessibility';
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
	$setEntry(id: number, statusId: string, statusName: string, text: string, tooltip: string | undefined, command: Command | undefined, color: string | ThemeColor | undefined, alignment: MainThreadStatusBarAlignment, priority: number | undefined, accessibilityInformation: IAccessibilityInformation): void {
30
		// if there are icons in the text use the tooltip for the aria label
31 32 33 34 35 36
		let ariaLabel: string;
		if (accessibilityInformation) {
			ariaLabel = accessibilityInformation.label;
		} else {
			ariaLabel = text.indexOf('$(') === -1 ? text : tooltip || text;
		}
37
		const entry: IStatusbarEntry = { text, tooltip, command, color, ariaLabel };
B
Benjamin Pasero 已提交
38

J
Johannes Rieken 已提交
39 40 41 42
		if (typeof priority === 'undefined') {
			priority = 0;
		}

B
Benjamin Pasero 已提交
43 44 45 46 47 48 49
		// 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;
		}
50

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

B
Benjamin Pasero 已提交
56 57 58 59
		// Otherwise update
		else {
			existingEntry.accessor.update(entry);
		}
60 61
	}

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