提交 f3caf387 编写于 作者: M Matt Bierner

Also serialize and restore webview icons

上级 8899e43f
......@@ -2,7 +2,6 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as dom from 'vs/base/browser/dom';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import * as map from 'vs/base/common/map';
import URI, { UriComponents } from 'vs/base/common/uri';
......@@ -30,39 +29,6 @@ export class MainThreadWebviews implements MainThreadWebviewsShape, WebviewReviv
private static revivalPool = 0;
private static _styleElement?: HTMLStyleElement;
private static _icons = new Map<number, { light: URI, dark: URI }>();
private static updateStyleElement(
webview: WebviewEditorInput,
iconPath: { light: URI, dark: URI } | undefined
) {
const id = webview.getId();
if (!this._styleElement) {
this._styleElement = dom.createStyleSheet();
this._styleElement.className = 'webview-icons';
}
if (!iconPath) {
this._icons.delete(id);
} else {
this._icons.set(id, iconPath);
}
const cssRules: string[] = [];
this._icons.forEach((value, key) => {
const webviewSelector = `.show-file-icons .webview-${key}-name-file-icon::before`;
if (URI.isUri(value)) {
cssRules.push(`${webviewSelector} { content: ""; background-image: url(${value.toString()}); }`);
} else {
cssRules.push(`${webviewSelector} { content: ""; background-image: url(${value.light.toString()}); }`);
cssRules.push(`.vs-dark ${webviewSelector} { content: ""; background-image: url(${value.dark.toString()}); }`);
}
});
this._styleElement.innerHTML = cssRules.join('\n');
}
private _toDispose: IDisposable[] = [];
private readonly _proxy: ExtHostWebviewsShape;
......@@ -132,7 +98,7 @@ export class MainThreadWebviews implements MainThreadWebviewsShape, WebviewReviv
public $setIconPath(handle: WebviewPanelHandle, value: { light: UriComponents, dark: UriComponents } | undefined): void {
const webview = this.getWebview(handle);
MainThreadWebviews.updateStyleElement(webview, reviveWebviewIcon(value));
webview.iconPath = reviveWebviewIcon(value);
}
public $setHtml(handle: WebviewPanelHandle, value: string): void {
......@@ -225,10 +191,6 @@ export class MainThreadWebviews implements MainThreadWebviewsShape, WebviewReviv
onMessage: message => this._proxy.$onMessage(handle, message),
onDispose: () => {
const cleanUp = () => {
const webview = this._webviews.get(handle);
if (webview) {
MainThreadWebviews.updateStyleElement(webview, undefined);
}
this._webviews.delete(handle);
};
this._proxy.$onDidDisposeWebviewPanel(handle).then(
......
......@@ -2,7 +2,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as dom from 'vs/base/browser/dom';
import { Emitter } from 'vs/base/common/event';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import URI from 'vs/base/common/uri';
......@@ -14,12 +14,47 @@ import * as vscode from 'vscode';
import { WebviewEvents, WebviewInputOptions, WebviewReviver } from './webviewEditorService';
import { WebviewElement } from './webviewElement';
export class WebviewEditorInput extends EditorInput {
private static handlePool = 0;
private static _styleElement?: HTMLStyleElement;
private static _icons = new Map<number, { light: URI, dark: URI }>();
private static updateStyleElement(
id: number,
iconPath: { light: URI, dark: URI } | undefined
) {
if (!this._styleElement) {
this._styleElement = dom.createStyleSheet();
this._styleElement.className = 'webview-icons';
}
if (!iconPath) {
this._icons.delete(id);
} else {
this._icons.set(id, iconPath);
}
const cssRules: string[] = [];
this._icons.forEach((value, key) => {
const webviewSelector = `.show-file-icons .webview-${key}-name-file-icon::before`;
if (URI.isUri(value)) {
cssRules.push(`${webviewSelector} { content: ""; background-image: url(${value.toString()}); }`);
} else {
cssRules.push(`${webviewSelector} { content: ""; background-image: url(${value.light.toString()}); }`);
cssRules.push(`.vs-dark ${webviewSelector} { content: ""; background-image: url(${value.dark.toString()}); }`);
}
});
this._styleElement.innerHTML = cssRules.join('\n');
}
public static readonly typeId = 'workbench.editors.webviewInput';
private _name: string;
private _iconPath?: { light: URI, dark: URI };
private _options: WebviewInputOptions;
private _html: string = '';
private _currentWebviewHtml: string = '';
......@@ -109,6 +144,15 @@ export class WebviewEditorInput extends EditorInput {
this._onDidChangeLabel.fire();
}
public get iconPath() {
return this._iconPath;
}
public set iconPath(value: { light: URI, dark: URI } | undefined) {
this._iconPath = value;
WebviewEditorInput.updateStyleElement(this._id, value);
}
public matches(other: IEditorInput): boolean {
return other && other === this;
}
......
......@@ -15,6 +15,7 @@ interface SerializedWebview {
readonly options: WebviewInputOptions;
readonly extensionLocation: string;
readonly state: any;
readonly iconPath: { light: string, dark: string } | undefined;
}
export class WebviewEditorInputFactory implements IEditorInputFactory {
......@@ -43,7 +44,8 @@ export class WebviewEditorInputFactory implements IEditorInputFactory {
title: input.getName(),
options: input.options,
extensionLocation: input.extensionLocation.toString(),
state: input.state
state: input.state,
iconPath: input.iconPath ? { light: input.iconPath.light.toString(), dark: input.iconPath.dark.toString(), } : undefined,
};
return JSON.stringify(data);
}
......@@ -54,6 +56,7 @@ export class WebviewEditorInputFactory implements IEditorInputFactory {
): WebviewEditorInput {
const data: SerializedWebview = JSON.parse(serializedEditorInput);
const extensionLocation = URI.parse(data.extensionLocation);
return this._webviewService.reviveWebview(data.viewType, data.title, data.state, data.options, extensionLocation);
const iconPath = data.iconPath ? { light: URI.parse(data.iconPath.light), dark: URI.parse(data.iconPath.dark) } : undefined;
return this._webviewService.reviveWebview(data.viewType, data.title, iconPath, data.state, data.options, extensionLocation);
}
}
......@@ -36,6 +36,7 @@ export interface IWebviewEditorService {
reviveWebview(
viewType: string,
title: string,
iconPath: { light: URI, dark: URI } | undefined,
state: any,
options: WebviewInputOptions,
extensionLocation: URI
......@@ -126,6 +127,7 @@ export class WebviewEditorService implements IWebviewEditorService {
reviveWebview(
viewType: string,
title: string,
iconPath: { light: URI, dark: URI } | undefined,
state: any,
options: WebviewInputOptions,
extensionLocation: URI
......@@ -148,7 +150,7 @@ export class WebviewEditorService implements IWebviewEditorService {
});
}
});
webviewInput.iconPath = iconPath;
return webviewInput;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册