Skip to content

Commit

Permalink
Add title API for tree views (#80623)
Browse files Browse the repository at this point in the history
* First pass at set view title

* Change to get/set instead of setTitle

and get the name of the view from the extension contributions
  • Loading branch information
alexr00 authored Sep 11, 2019
1 parent 9aab28d commit 462a8be
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/vs/vscode.proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,10 @@ declare module 'vscode' {
*/
message?: string;

/**
* The name of the tree view. It is set from the extension package.json and can be changed later.
*/
title?: string;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/vs/workbench/api/browser/mainThreadTreeViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ export class MainThreadTreeViews extends Disposable implements MainThreadTreeVie
}
}

$setTitle(treeViewId: string, title: string): void {
const viewer = this.getTreeView(treeViewId);
if (viewer) {
viewer.title = title;
}
}

private async reveal(treeView: ITreeView, dataProvider: TreeViewDataProvider, itemIn: ITreeItem, parentChain: ITreeItem[], options: IRevealOptions): Promise<void> {
options = options ? options : { select: false, focus: false };
const select = isUndefinedOrNull(options.select) ? false : options.select;
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export interface MainThreadTreeViewsShape extends IDisposable {
$refresh(treeViewId: string, itemsToRefresh?: { [treeItemHandle: string]: ITreeItem }): Promise<void>;
$reveal(treeViewId: string, treeItem: ITreeItem, parentChain: ITreeItem[], options: IRevealOptions): Promise<void>;
$setMessage(treeViewId: string, message: string): void;
$setTitle(treeViewId: string, title: string): void;
}

export interface MainThreadDownloadServiceShape extends IDisposable {
Expand Down
24 changes: 24 additions & 0 deletions src/vs/workbench/api/common/extHostTreeViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export class ExtHostTreeViews implements ExtHostTreeViewsShape {
checkProposedApiEnabled(extension);
treeView.message = message;
},
get title() { return treeView.title; },
set title(title: string) {
checkProposedApiEnabled(extension);
treeView.title = title;
},
reveal: (element: T, options?: IRevealOptions): Promise<void> => {
return treeView.reveal(element, options);
},
Expand Down Expand Up @@ -195,6 +200,15 @@ class ExtHostTreeView<T> extends Disposable {

constructor(private viewId: string, options: vscode.TreeViewOptions<T>, private proxy: MainThreadTreeViewsShape, private commands: CommandsConverter, private logService: ILogService, private extension: IExtensionDescription) {
super();
if (extension.contributes && extension.contributes.views) {
for (const location in extension.contributes.views) {
for (const view of extension.contributes.views[location]) {
if (view.id === viewId) {
this._title = view.name;
}
}
}
}
this.dataProvider = options.treeDataProvider;
this.proxy.$registerTreeViewDataProvider(viewId, { showCollapseAll: !!options.showCollapseAll, canSelectMany: !!options.canSelectMany });
if (this.dataProvider.onDidChangeTreeData) {
Expand Down Expand Up @@ -274,6 +288,16 @@ class ExtHostTreeView<T> extends Disposable {
this._onDidChangeData.fire({ message: true, element: false });
}

private _title: string = '';
get title(): string {
return this._title;
}

set title(title: string) {
this._title = title;
this.proxy.$setTitle(this.viewId, title);
}

setExpanded(treeItemHandle: TreeItemHandle, expanded: boolean): void {
const element = this.getExtensionElement(treeItemHandle);
if (element) {
Expand Down
17 changes: 15 additions & 2 deletions src/vs/workbench/browser/parts/views/customView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class CustomTreeViewPanel extends ViewletPanel {
const { treeView } = (<ITreeViewDescriptor>Registry.as<IViewsRegistry>(Extensions.ViewsRegistry).getView(options.id));
this.treeView = treeView;
this._register(this.treeView.onDidChangeActions(() => this.updateActions(), this));
this._register(this.treeView.onDidChangeTitle((newTitle) => this.updateTitle(newTitle)));
this._register(toDisposable(() => this.treeView.setVisibility(false)));
this._register(this.onDidChangeBodyVisibility(() => this.updateTreeVisibility()));
this.updateTreeVisibility();
Expand Down Expand Up @@ -190,9 +191,12 @@ export class CustomTreeView extends Disposable implements ITreeView {
private readonly _onDidChangeActions: Emitter<void> = this._register(new Emitter<void>());
readonly onDidChangeActions: Event<void> = this._onDidChangeActions.event;

private readonly _onDidChangeTitle: Emitter<string> = this._register(new Emitter<string>());
readonly onDidChangeTitle: Event<string> = this._onDidChangeTitle.event;

constructor(
private id: string,
private title: string,
private _title: string,
private viewContainer: ViewContainer,
@IExtensionService private readonly extensionService: IExtensionService,
@IWorkbenchThemeService private readonly themeService: IWorkbenchThemeService,
Expand Down Expand Up @@ -262,6 +266,15 @@ export class CustomTreeView extends Disposable implements ITreeView {
this.updateMessage();
}

get title(): string {
return this._title;
}

set title(name: string) {
this._title = name;
this._onDidChangeTitle.fire(this._title);
}

get canSelectMany(): boolean {
return this._canSelectMany;
}
Expand Down Expand Up @@ -379,7 +392,7 @@ export class CustomTreeView extends Disposable implements ITreeView {
return element.tooltip ? element.tooltip : element.label ? element.label.label : '';
}
},
ariaLabel: this.title,
ariaLabel: this._title,
keyboardNavigationLabelProvider: {
getKeyboardNavigationLabel: (item: ITreeItem) => {
return item.label ? item.label.label : (item.resourceUri ? basename(URI.revive(item.resourceUri)) : undefined);
Expand Down
4 changes: 4 additions & 0 deletions src/vs/workbench/common/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ export interface ITreeView extends IDisposable {

message?: string;

title: string;

readonly visible: boolean;

readonly onDidExpandItem: Event<ITreeItem>;
Expand All @@ -326,6 +328,8 @@ export interface ITreeView extends IDisposable {

readonly onDidChangeActions: Event<void>;

readonly onDidChangeTitle: Event<string>;

refresh(treeItems?: ITreeItem[]): Promise<void>;

setVisibility(visible: boolean): void;
Expand Down

0 comments on commit 462a8be

Please sign in to comment.