From ec8016f7d1f29ce0ce682a38c7aac41d9ed4ecc0 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Wed, 9 Sep 2020 21:51:22 -0700 Subject: [PATCH] Cache webview view title across reloads Fixes #105867 --- .../api/browser/mainThreadWebviewViews.ts | 2 +- .../workbench/api/common/extHost.protocol.ts | 2 +- .../api/common/extHostWebviewView.ts | 5 +++- .../webviewView/browser/webviewViewPane.ts | 30 +++++++++++++++---- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/vs/workbench/api/browser/mainThreadWebviewViews.ts b/src/vs/workbench/api/browser/mainThreadWebviewViews.ts index eae46c714d0..d43016bcd07 100644 --- a/src/vs/workbench/api/browser/mainThreadWebviewViews.ts +++ b/src/vs/workbench/api/browser/mainThreadWebviewViews.ts @@ -78,7 +78,7 @@ export class MainThreadWebviewsViews extends Disposable implements extHostProtoc }); try { - await this._proxy.$resolveWebviewView(handle, viewType, state, cancellation); + await this._proxy.$resolveWebviewView(handle, viewType, webviewView.title, state, cancellation); } catch (error) { onUnexpectedError(error); webviewView.webview.html = this.mainThreadWebviews.getWebviewResolvedFailedContent(viewType); diff --git a/src/vs/workbench/api/common/extHost.protocol.ts b/src/vs/workbench/api/common/extHost.protocol.ts index 2f84c7625b1..dd896025d43 100644 --- a/src/vs/workbench/api/common/extHost.protocol.ts +++ b/src/vs/workbench/api/common/extHost.protocol.ts @@ -686,7 +686,7 @@ export interface ExtHostCustomEditorsShape { } export interface ExtHostWebviewViewsShape { - $resolveWebviewView(webviewHandle: WebviewHandle, viewType: string, state: any, cancellation: CancellationToken): Promise; + $resolveWebviewView(webviewHandle: WebviewHandle, viewType: string, title: string | undefined, state: any, cancellation: CancellationToken): Promise; $onDidChangeWebviewViewVisibility(webviewHandle: WebviewHandle, visible: boolean): void; diff --git a/src/vs/workbench/api/common/extHostWebviewView.ts b/src/vs/workbench/api/common/extHostWebviewView.ts index dcb76f3afd3..5122096832e 100644 --- a/src/vs/workbench/api/common/extHostWebviewView.ts +++ b/src/vs/workbench/api/common/extHostWebviewView.ts @@ -29,12 +29,14 @@ class ExtHostWebviewView extends Disposable implements vscode.WebviewView { handle: extHostProtocol.WebviewHandle, proxy: extHostProtocol.MainThreadWebviewViewsShape, viewType: string, + title: string | undefined, webview: ExtHostWebview, isVisible: boolean, ) { super(); this.#viewType = viewType; + this.#title = title; this.#handle = handle; this.#proxy = proxy; this.#webview = webview; @@ -153,6 +155,7 @@ export class ExtHostWebviewViews implements extHostProtocol.ExtHostWebviewViewsS async $resolveWebviewView( webviewHandle: string, viewType: string, + title: string | undefined, state: any, cancellation: CancellationToken, ): Promise { @@ -164,7 +167,7 @@ export class ExtHostWebviewViews implements extHostProtocol.ExtHostWebviewViewsS const { provider, extension } = entry; const webview = this._extHostWebview.createNewWebview(webviewHandle, { /* todo */ }, extension); - const revivedView = new ExtHostWebviewView(webviewHandle, this._proxy, viewType, webview, true); + const revivedView = new ExtHostWebviewView(webviewHandle, this._proxy, viewType, title, webview, true); this._webviewViews.set(webviewHandle, revivedView); diff --git a/src/vs/workbench/contrib/webviewView/browser/webviewViewPane.ts b/src/vs/workbench/contrib/webviewView/browser/webviewViewPane.ts index ad5ba7e2796..8e2849173f7 100644 --- a/src/vs/workbench/contrib/webviewView/browser/webviewViewPane.ts +++ b/src/vs/workbench/contrib/webviewView/browser/webviewViewPane.ts @@ -28,7 +28,10 @@ import { IExtensionService } from 'vs/workbench/services/extensions/common/exten declare const ResizeObserver: any; -const webviewStateKey = 'webviewState'; +const storageKeys = { + webviewState: 'webviewState', + title: 'title' +} as const; export class WebviewViewPane extends ViewPane { @@ -38,6 +41,9 @@ export class WebviewViewPane extends ViewPane { private _container?: HTMLElement; private _resizeObserver?: any; + private readonly defaultTitle: string; + private setTitle: string | undefined; + private readonly memento: Memento; private readonly viewState: MementoObject; @@ -60,10 +66,16 @@ export class WebviewViewPane extends ViewPane { @IViewsService private readonly viewService: IViewsService, ) { super({ ...options, titleMenuId: MenuId.ViewTitle }, keybindingService, contextMenuService, configurationService, contextKeyService, viewDescriptorService, instantiationService, openerService, themeService, telemetryService); + this.defaultTitle = this.title; this.memento = new Memento(`webviewView.${this.id}`, storageService); this.viewState = this.memento.getMemento(StorageScope.WORKSPACE); + const storedTitle = this.viewState[storageKeys.title]; + if (typeof storedTitle === 'string') { + this.updateTitle(storedTitle); + } + this._register(this.onDidChangeBodyVisibility(() => this.updateTreeVisibility())); this.updateTreeVisibility(); } @@ -108,8 +120,9 @@ export class WebviewViewPane extends ViewPane { public saveState() { if (this._webview) { - this.viewState[webviewStateKey] = this._webview.state; + this.viewState[storageKeys.webviewState] = this._webview.state; } + this.viewState[storageKeys.title] = this.setTitle; this.memento.saveMemento(); super.saveState(); @@ -142,7 +155,7 @@ export class WebviewViewPane extends ViewPane { const webviewId = `webviewView-${this.id.replace(/[^a-z0-9]/gi, '-')}`.toLowerCase(); const webview = this.webviewService.createWebviewOverlay(webviewId, {}, {}, undefined); - webview.state = this.viewState['webviewState']; + webview.state = this.viewState[storageKeys.webviewState]; this._webview = webview; this._register(toDisposable(() => { @@ -150,7 +163,7 @@ export class WebviewViewPane extends ViewPane { })); this._register(webview.onDidUpdateState(() => { - this.viewState[webviewStateKey] = webview.state; + this.viewState[storageKeys.webviewState] = webview.state; })); const source = this._register(new CancellationTokenSource()); @@ -164,8 +177,8 @@ export class WebviewViewPane extends ViewPane { onDidChangeVisibility: this.onDidChangeBodyVisibility, onDispose: this.onDispose, - get title() { return self.title; }, - set title(value: string) { self.updateTitle(value); }, + get title(): string | undefined { return self.setTitle; }, + set title(value: string | undefined) { self.updateTitle(value); }, get description(): string | undefined { return self.titleDescription; }, set description(value: string | undefined) { self.updateTitleDescription(value); }, @@ -180,6 +193,11 @@ export class WebviewViewPane extends ViewPane { } } + protected updateTitle(value: string | undefined) { + this.setTitle = value; + super.updateTitle(typeof value === 'string' ? value : this.defaultTitle); + } + private async withProgress(task: () => Promise): Promise { return this.progressService.withProgress({ location: this.id, delay: 500 }, task); } -- GitLab