Allow using Command in StatusBarItem (#92046)

Fixes #22353

StatusBarItem is one of the few places in our API where we only allow extensions to give us a command as a `string` instead of as `Command` object. This change updates the API to also allow passing in a `vscode.Command` (which also allows arguments!)
This commit is contained in:
Matt Bierner
2020-03-09 12:57:33 -07:00
committed by GitHub
parent 0b3e54edb1
commit 44d4fe895d
8 changed files with 59 additions and 33 deletions

View File

@@ -5,11 +5,13 @@
import { StatusbarAlignment as MainThreadStatusBarAlignment } from 'vs/workbench/services/statusbar/common/statusbar';
import { StatusBarAlignment as ExtHostStatusBarAlignment, Disposable, ThemeColor } from './extHostTypes';
import { StatusBarItem, StatusBarAlignment } from 'vscode';
import { MainContext, MainThreadStatusBarShape, IMainContext } from './extHost.protocol';
import type * as vscode from 'vscode';
import { MainContext, MainThreadStatusBarShape, IMainContext, ICommandDto } from './extHost.protocol';
import { localize } from 'vs/nls';
import { CommandsConverter } from 'vs/workbench/api/common/extHostCommands';
import { DisposableStore } from 'vs/base/common/lifecycle';
export class ExtHostStatusBarEntry implements StatusBarItem {
export class ExtHostStatusBarEntry implements vscode.StatusBarItem {
private static ID_GEN = 0;
private _id: number;
@@ -24,14 +26,20 @@ export class ExtHostStatusBarEntry implements StatusBarItem {
private _text: string = '';
private _tooltip?: string;
private _color?: string | ThemeColor;
private _command?: string;
private readonly _internalCommandRegistration = new DisposableStore();
private _command?: {
readonly fromApi: string | vscode.Command,
readonly internal: ICommandDto,
};
private _timeoutHandle: any;
private _proxy: MainThreadStatusBarShape;
private _commands: CommandsConverter;
constructor(proxy: MainThreadStatusBarShape, id: string, name: string, alignment: ExtHostStatusBarAlignment = ExtHostStatusBarAlignment.Left, priority?: number) {
constructor(proxy: MainThreadStatusBarShape, commands: CommandsConverter, id: string, name: string, alignment: ExtHostStatusBarAlignment = ExtHostStatusBarAlignment.Left, priority?: number) {
this._id = ExtHostStatusBarEntry.ID_GEN++;
this._proxy = proxy;
this._commands = commands;
this._statusId = id;
this._statusName = name;
this._alignment = alignment;
@@ -42,7 +50,7 @@ export class ExtHostStatusBarEntry implements StatusBarItem {
return this._id;
}
public get alignment(): StatusBarAlignment {
public get alignment(): vscode.StatusBarAlignment {
return this._alignment;
}
@@ -62,8 +70,8 @@ export class ExtHostStatusBarEntry implements StatusBarItem {
return this._color;
}
public get command(): string | undefined {
return this._command;
public get command(): string | vscode.Command | undefined {
return this._command?.fromApi;
}
public set text(text: string) {
@@ -81,8 +89,25 @@ export class ExtHostStatusBarEntry implements StatusBarItem {
this.update();
}
public set command(command: string | undefined) {
this._command = command;
public set command(command: string | vscode.Command | undefined) {
if (this._command?.fromApi === command) {
return;
}
this._internalCommandRegistration.clear();
if (typeof command === 'string') {
this._command = {
fromApi: command,
internal: this._commands.toInternal({ title: '', command }, this._internalCommandRegistration),
};
} else if (command) {
this._command = {
fromApi: command,
internal: this._commands.toInternal(command, this._internalCommandRegistration),
};
} else {
this._command = undefined;
}
this.update();
}
@@ -109,7 +134,7 @@ export class ExtHostStatusBarEntry implements StatusBarItem {
this._timeoutHandle = undefined;
// Set to status bar
this._proxy.$setEntry(this.id, this._statusId, this._statusName, this.text, this.tooltip, this.command, this.color,
this._proxy.$setEntry(this.id, this._statusId, this._statusName, this.text, this.tooltip, this._command?.internal, this.color,
this._alignment === ExtHostStatusBarAlignment.Left ? MainThreadStatusBarAlignment.LEFT : MainThreadStatusBarAlignment.RIGHT,
this._priority);
}, 0);
@@ -123,7 +148,7 @@ export class ExtHostStatusBarEntry implements StatusBarItem {
class StatusBarMessage {
private _item: StatusBarItem;
private _item: vscode.StatusBarItem;
private _messages: { message: string }[] = [];
constructor(statusBar: ExtHostStatusBar) {
@@ -161,16 +186,18 @@ class StatusBarMessage {
export class ExtHostStatusBar {
private _proxy: MainThreadStatusBarShape;
private readonly _proxy: MainThreadStatusBarShape;
private readonly _commands: CommandsConverter;
private _statusMessage: StatusBarMessage;
constructor(mainContext: IMainContext) {
constructor(mainContext: IMainContext, commands: CommandsConverter) {
this._proxy = mainContext.getProxy(MainContext.MainThreadStatusBar);
this._commands = commands;
this._statusMessage = new StatusBarMessage(this);
}
createStatusBarEntry(id: string, name: string, alignment?: ExtHostStatusBarAlignment, priority?: number): StatusBarItem {
return new ExtHostStatusBarEntry(this._proxy, id, name, alignment, priority);
createStatusBarEntry(id: string, name: string, alignment?: ExtHostStatusBarAlignment, priority?: number): vscode.StatusBarItem {
return new ExtHostStatusBarEntry(this._proxy, this._commands, id, name, alignment, priority);
}
setStatusBarMessage(text: string, timeoutOrThenable?: number | Thenable<any>): Disposable {