Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

joh/ancient fox #184337

Merged
merged 2 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion extensions/vscode-api-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"enabledApiProposals": [
"authSession",
"contribViewsRemote",
"contribStatusBarItems",
"customEditorMove",
"diffCommand",
"documentFiltersExclusive",
Expand Down Expand Up @@ -192,7 +193,14 @@
}
]
}
]
],
"statusBarItems": {
"id": "myStaticItem",
"alignment": "right",
"priority": 17,
"name": "My Static Item",
"text": "Hello $(globe)"
}
},
"scripts": {
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./",
Expand Down
22 changes: 22 additions & 0 deletions extensions/vscode-api-tests/src/singlefolder-tests/window.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1036,4 +1036,26 @@ suite('vscode API - window', () => {
statusBarEntryWithId.name = 'Test Name';
assert.strictEqual(statusBarEntryWithId.name, 'Test Name');
});

test('createStatusBar - static', async function () {

const item = window.createStatusBarItem('myStaticItem');

assert.strictEqual(item.alignment, StatusBarAlignment.Right);
assert.strictEqual(item.priority, 17);
assert.strictEqual(item.name, 'My Static Item');
assert.strictEqual(item.text, 'Hello $(globe)');

item.dispose();
});

test('createStatusBar - static, CANNOT change some props', async function () {

const item = window.createStatusBarItem('myStaticItem', StatusBarAlignment.Left, 12);

assert.strictEqual(item.alignment, StatusBarAlignment.Right);
assert.strictEqual(item.priority, 17);

item.dispose();
});
});
2 changes: 2 additions & 0 deletions src/vs/base/common/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ export function combinedDisposable(...disposables: IDisposable[]): IDisposable {

/**
* Turn a function that implements dispose into an {@link IDisposable}.
*
* @param fn Clean up function, guaranteed to be called only **once**.
*/
export function toDisposable(fn: () => void): IDisposable {
const self = trackDisposable({
Expand Down
17 changes: 7 additions & 10 deletions src/vs/workbench/api/browser/mainThreadStatusBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { MainThreadStatusBarShape, MainContext, ExtHostContext, StatusBarItemDto } from '../common/extHost.protocol';
import { ThemeColor } from 'vs/base/common/themables';
import { extHostNamedCustomer, IExtHostContext } from 'vs/workbench/services/extensions/common/extHostCustomers';
import { DisposableMap } from 'vs/base/common/lifecycle';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { Command } from 'vs/editor/common/languages';
import { IAccessibilityInformation } from 'vs/platform/accessibility/common/accessibility';
import { IMarkdownString } from 'vs/base/common/htmlContent';
Expand All @@ -16,7 +16,7 @@ import { IStatusbarEntry, StatusbarAlignment } from 'vs/workbench/services/statu
@extHostNamedCustomer(MainContext.MainThreadStatusBar)
export class MainThreadStatusBar implements MainThreadStatusBarShape {

private readonly entries = new DisposableMap<string>();
private readonly _store = new DisposableStore();

constructor(
extHostContext: IExtHostContext,
Expand All @@ -32,11 +32,11 @@ export class MainThreadStatusBar implements MainThreadStatusBarShape {

proxy.$acceptStaticEntries(entries);

statusbarService.onDidChange(e => {
this._store.add(statusbarService.onDidChange(e => {
if (e.added) {
proxy.$acceptStaticEntries([asDto(e.added[0], e.added[1])]);
}
});
}));

function asDto(entryId: string, item: { entry: IStatusbarEntry; alignment: StatusbarAlignment; priority: number }): StatusBarItemDto {
return {
Expand All @@ -51,17 +51,14 @@ export class MainThreadStatusBar implements MainThreadStatusBarShape {
}

dispose(): void {
this.entries.dispose();
this._store.dispose();
}

$setEntry(entryId: string, id: string, extensionId: string | undefined, name: string, text: string, tooltip: IMarkdownString | string | undefined, command: Command | undefined, color: string | ThemeColor | undefined, backgroundColor: string | ThemeColor | undefined, alignLeft: boolean, priority: number | undefined, accessibilityInformation: IAccessibilityInformation | undefined): void {
const dispo = this.statusbarService.setOrUpdateEntry(entryId, id, extensionId, name, text, tooltip, command, color, backgroundColor, alignLeft, priority, accessibilityInformation);
if (!this.entries.has(entryId)) {
this.entries.set(entryId, dispo);
}
this.statusbarService.setOrUpdateEntry(entryId, id, extensionId, name, text, tooltip, command, color, backgroundColor, alignLeft, priority, accessibilityInformation);
}

$disposeEntry(entryId: string) {
this.entries.deleteAndDispose(entryId);
this.statusbarService.unsetEntry(entryId);
}
}
39 changes: 24 additions & 15 deletions src/vs/workbench/api/browser/statusBarExtensionPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export interface IExtensionStatusBarItemService {

onDidChange: Event<IExtensionStatusBarItemChangeEvent>;

setOrUpdateEntry(id: string, statusId: string, extensionId: string | undefined, name: string, text: string, tooltip: IMarkdownString | string | undefined, command: Command | undefined, color: string | ThemeColor | undefined, backgroundColor: string | ThemeColor | undefined, alignLeft: boolean, priority: number | undefined, accessibilityInformation: IAccessibilityInformation | undefined): IDisposable;
setOrUpdateEntry(id: string, statusId: string, extensionId: string | undefined, name: string, text: string, tooltip: IMarkdownString | string | undefined, command: Command | undefined, color: string | ThemeColor | undefined, backgroundColor: string | ThemeColor | undefined, alignLeft: boolean, priority: number | undefined, accessibilityInformation: IAccessibilityInformation | undefined): void;

unsetEntry(id: string): void;

getEntries(): Iterable<ExtensionStatusBarEntry>;
}
Expand All @@ -53,7 +55,7 @@ class ExtensionStatusBarItemService implements IExtensionStatusBarItemService {

declare readonly _serviceBrand: undefined;

private readonly _entries: Map<string, { accessor: IStatusbarEntryAccessor; entry: IStatusbarEntry; alignment: MainThreadStatusBarAlignment; priority: number }> = new Map();
private readonly _entries: Map<string, { accessor: IStatusbarEntryAccessor; entry: IStatusbarEntry; alignment: MainThreadStatusBarAlignment; priority: number; disposable: IDisposable }> = new Map();

private readonly _onDidChange = new Emitter<IExtensionStatusBarItemChangeEvent>();
readonly onDidChange: Event<IExtensionStatusBarItemChangeEvent> = this._onDidChange.event;
Expand All @@ -66,7 +68,11 @@ class ExtensionStatusBarItemService implements IExtensionStatusBarItemService {
this._onDidChange.dispose();
}

setOrUpdateEntry(entryId: string, id: string, extensionId: string | undefined, name: string, text: string, tooltip: IMarkdownString | string | undefined, command: Command | undefined, color: string | ThemeColor | undefined, backgroundColor: string | ThemeColor | undefined, alignLeft: boolean, priority: number | undefined, accessibilityInformation: IAccessibilityInformation | undefined): IDisposable {
setOrUpdateEntry(entryId: string,
id: string, extensionId: string | undefined, name: string, text: string, tooltip: IMarkdownString | string | undefined,
command: Command | undefined, color: string | ThemeColor | undefined, backgroundColor: string | ThemeColor | undefined,
alignLeft: boolean, priority: number | undefined, accessibilityInformation: IAccessibilityInformation | undefined
): void {
// if there are icons in the text use the tooltip for the aria label
let ariaLabel: string;
let role: string | undefined = undefined;
Expand Down Expand Up @@ -109,11 +115,17 @@ class ExtensionStatusBarItemService implements IExtensionStatusBarItemService {
entryPriority = priority;
}

const accessor = this._statusbarService.addEntry(entry, id, alignment, entryPriority);
this._entries.set(entryId, {
accessor: this._statusbarService.addEntry(entry, id, alignment, entryPriority),
accessor,
entry,
alignment,
priority
priority,
disposable: toDisposable(() => {
accessor.dispose();
this._entries.delete(entryId);
this._onDidChange.fire({ removed: entryId });
})
});

this._onDidChange.fire({ added: [entryId, { entry, alignment, priority }] });
Expand All @@ -123,15 +135,10 @@ class ExtensionStatusBarItemService implements IExtensionStatusBarItemService {
existingEntry.accessor.update(entry);
existingEntry.entry = entry;
}
}

return toDisposable(() => {
const entry = this._entries.get(entryId);
if (entry) {
entry.accessor.dispose();
this._entries.delete(entryId);
this._onDidChange.fire({ removed: entryId });
}
});
unsetEntry(entryId: string): void {
this._entries.get(entryId)?.disposable.dispose();
}

getEntries(): Iterable<[string, { entry: IStatusbarEntry; alignment: MainThreadStatusBarAlignment; priority: number }]> {
Expand Down Expand Up @@ -236,7 +243,7 @@ export class StatusBarItemsExtensionPoint {

const fullItemId = asStatusBarItemIdentifier(entry.description.identifier, candidate.id);

contributions.add(statusBarItemsService.setOrUpdateEntry(
statusBarItemsService.setOrUpdateEntry(
fullItemId,
fullItemId,
ExtensionIdentifier.toKey(entry.description.identifier),
Expand All @@ -248,7 +255,9 @@ export class StatusBarItemsExtensionPoint {
candidate.alignment === 'left',
candidate.priority,
undefined
));
);

contributions.add(toDisposable(() => statusBarItemsService.unsetEntry(fullItemId)));
}
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/api/common/extHostStatusBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ export class ExtHostStatusBarEntry implements vscode.StatusBarItem {
// this can only happen when an item was contributed by an extension
const item = staticItems.get(this._entryId);
if (item) {
alignment = item.alignLeft ? ExtHostStatusBarAlignment.Left : ExtHostStatusBarAlignment.Right;
priority = item.priority;
this._visible = true;
this._alignment = item.alignLeft ? ExtHostStatusBarAlignment.Left : ExtHostStatusBarAlignment.Right;
this._priority = item.priority;
this.name = item.name;
this.text = item.text;
this.command = item.command;
Expand Down