Allow to show status bar entries with an id and name (fix #74972)

This commit is contained in:
Benjamin Pasero
2021-05-19 14:03:28 +02:00
parent f23f011f55
commit 159479eb5a
31 changed files with 212 additions and 167 deletions

View File

@@ -10,8 +10,10 @@ import { MainContext, MainThreadStatusBarShape, IMainContext, ICommandDto } from
import { localize } from 'vs/nls';
import { CommandsConverter } from 'vs/workbench/api/common/extHostCommands';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
export class ExtHostStatusBarEntry implements vscode.StatusBarItem {
private static ID_GEN = 0;
private static ALLOWED_BACKGROUND_COLORS = new Map<string, ThemeColor>(
@@ -21,17 +23,20 @@ export class ExtHostStatusBarEntry implements vscode.StatusBarItem {
#proxy: MainThreadStatusBarShape;
#commands: CommandsConverter;
private _id: number;
private _entryId: number;
private _extension?: IExtensionDescription;
private _id?: string;
private _alignment: number;
private _priority?: number;
private _disposed: boolean = false;
private _visible: boolean = false;
private _statusId: string;
private _statusName: string;
private _text: string = '';
private _tooltip?: string;
private _name?: string;
private _color?: string | ThemeColor;
private _backgroundColor?: ThemeColor;
private readonly _internalCommandRegistration = new DisposableStore();
@@ -43,19 +48,23 @@ export class ExtHostStatusBarEntry implements vscode.StatusBarItem {
private _timeoutHandle: any;
private _accessibilityInformation?: vscode.AccessibilityInformation;
constructor(proxy: MainThreadStatusBarShape, commands: CommandsConverter, id: string, name: string, alignment: ExtHostStatusBarAlignment = ExtHostStatusBarAlignment.Left, priority?: number) {
constructor(proxy: MainThreadStatusBarShape, commands: CommandsConverter, extension: IExtensionDescription, id?: string, alignment?: ExtHostStatusBarAlignment, priority?: number);
constructor(proxy: MainThreadStatusBarShape, commands: CommandsConverter, extension: IExtensionDescription | undefined, id: string, alignment?: ExtHostStatusBarAlignment, priority?: number);
constructor(proxy: MainThreadStatusBarShape, commands: CommandsConverter, extension?: IExtensionDescription, id?: string, alignment: ExtHostStatusBarAlignment = ExtHostStatusBarAlignment.Left, priority?: number) {
this.#proxy = proxy;
this.#commands = commands;
this._id = ExtHostStatusBarEntry.ID_GEN++;
this._statusId = id;
this._statusName = name;
this._entryId = ExtHostStatusBarEntry.ID_GEN++;
this._extension = extension;
this._id = id;
this._alignment = alignment;
this._priority = priority;
}
public get id(): number {
return this._id;
public get id(): string {
return this._id ?? this._extension!.identifier.value;
}
public get alignment(): vscode.StatusBarAlignment {
@@ -70,6 +79,10 @@ export class ExtHostStatusBarEntry implements vscode.StatusBarItem {
return this._text;
}
public get name(): string | undefined {
return this._name;
}
public get tooltip(): string | undefined {
return this._tooltip;
}
@@ -95,6 +108,11 @@ export class ExtHostStatusBarEntry implements vscode.StatusBarItem {
this.update();
}
public set name(name: string | undefined) {
this._name = name;
this.update();
}
public set tooltip(tooltip: string | undefined) {
this._tooltip = tooltip;
this.update();
@@ -149,7 +167,7 @@ export class ExtHostStatusBarEntry implements vscode.StatusBarItem {
public hide(): void {
clearTimeout(this._timeoutHandle);
this._visible = false;
this.#proxy.$dispose(this.id);
this.#proxy.$dispose(this._entryId);
}
private update(): void {
@@ -163,6 +181,28 @@ export class ExtHostStatusBarEntry implements vscode.StatusBarItem {
this._timeoutHandle = setTimeout(() => {
this._timeoutHandle = undefined;
// If the id is not set, derive it from the extension identifier,
// otherwise make sure to prefix it with the extension identifier
// to get a more unique value across extensions.
let id: string;
if (this._extension) {
if (this._id) {
id = `${this._extension.identifier.value}.${this._id}`;
} else {
id = this._extension.identifier.value;
}
} else {
id = this._id!;
}
// If the name is not set, derive it from the extension descriptor
let name: string;
if (this._name) {
name = this._name;
} else {
name = localize('extensionLabel', "{0} (Extension)", this._extension!.displayName || this._extension!.name);
}
// If a background color is set, the foreground is determined
let color = this._color;
if (this._backgroundColor) {
@@ -170,7 +210,7 @@ export class ExtHostStatusBarEntry implements vscode.StatusBarItem {
}
// Set to status bar
this.#proxy.$setEntry(this.id, this._statusId, this._statusName, this._text, this._tooltip, this._command?.internal, color,
this.#proxy.$setEntry(this._entryId, id, name, this._text, this._tooltip, this._command?.internal, color,
this._backgroundColor, this._alignment === ExtHostStatusBarAlignment.Left ? MainThreadStatusBarAlignment.LEFT : MainThreadStatusBarAlignment.RIGHT,
this._priority, this._accessibilityInformation);
}, 0);
@@ -188,7 +228,8 @@ class StatusBarMessage {
private _messages: { message: string }[] = [];
constructor(statusBar: ExtHostStatusBar) {
this._item = statusBar.createStatusBarEntry('status.extensionMessage', localize('status.extensionMessage', "Extension Status"), ExtHostStatusBarAlignment.Left, Number.MIN_VALUE);
this._item = statusBar.createStatusBarEntry(undefined, 'status.extensionMessage', ExtHostStatusBarAlignment.Left, Number.MIN_VALUE);
this._item.name = localize('status.extensionMessage', "Extension Status");
}
dispose() {
@@ -232,12 +273,13 @@ export class ExtHostStatusBar {
this._statusMessage = new StatusBarMessage(this);
}
createStatusBarEntry(id: string, name: string, alignment?: ExtHostStatusBarAlignment, priority?: number): vscode.StatusBarItem {
return new ExtHostStatusBarEntry(this._proxy, this._commands, id, name, alignment, priority);
createStatusBarEntry(extension: IExtensionDescription | undefined, id: string, alignment?: ExtHostStatusBarAlignment, priority?: number): vscode.StatusBarItem;
createStatusBarEntry(extension: IExtensionDescription, id?: string, alignment?: ExtHostStatusBarAlignment, priority?: number): vscode.StatusBarItem;
createStatusBarEntry(extension: IExtensionDescription, id: string, alignment?: ExtHostStatusBarAlignment, priority?: number): vscode.StatusBarItem {
return new ExtHostStatusBarEntry(this._proxy, this._commands, extension, id, alignment, priority);
}
setStatusBarMessage(text: string, timeoutOrThenable?: number | Thenable<any>): Disposable {
const d = this._statusMessage.setMessage(text);
let handle: any;