Skip to content

Commit 210167b

Browse files
authored
Expose theme name in webviews (microsoft#98128)
* Expose theme name in webviews Fixes microsoft#97663 Adds a data attribute to webviews with the theme name. This is lets extensions write theme specific css for webviews * Change name to `vscode-theme-name` and add `vscode-theme-kind` vscode-theme-kind is generic kind: light, dark, high contrast. We already expose this as a classname but for conistency also want to have it as an attribute
1 parent 2d25399 commit 210167b

File tree

6 files changed

+16
-4
lines changed

6 files changed

+16
-4
lines changed

src/vs/editor/standalone/test/browser/standaloneLanguages.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ suite('TokenizationSupport2Adapter', () => {
4242
}
4343
public getColorTheme(): IStandaloneTheme {
4444
return {
45+
label: 'mock',
46+
4547
tokenTheme: new MockTokenTheme(),
4648

4749
themeName: LIGHT,

src/vs/platform/theme/common/themeService.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ export interface ITokenStyle {
8787
}
8888

8989
export interface IColorTheme {
90+
9091
readonly type: ThemeType;
9192

93+
readonly label: string;
94+
9295
/**
9396
* Resolves the color of the given color identifier. If the theme does not
9497
* specify the color, the default color is returned unless <code>useDefault</code> is set to false.

src/vs/platform/theme/test/common/testThemeService.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { Color } from 'vs/base/common/color';
99

1010
export class TestColorTheme implements IColorTheme {
1111

12+
public readonly label = 'test';
13+
1214
constructor(private colors: { [id: string]: string; } = {}, public type = DARK) {
1315
}
1416

src/vs/workbench/contrib/webview/browser/baseWebviewElement.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ export abstract class BaseWebview<T extends HTMLElement> extends Disposable {
270270
}
271271

272272
protected style(): void {
273-
const { styles, activeTheme } = this.webviewThemeDataProvider.getWebviewThemeData();
274-
this._send('styles', { styles, activeTheme });
273+
const { styles, activeTheme, themeLabel } = this.webviewThemeDataProvider.getWebviewThemeData();
274+
this._send('styles', { styles, activeTheme, themeName: themeLabel });
275275
}
276276

277277
protected handleFocusChange(isFocused: boolean): void {

src/vs/workbench/contrib/webview/browser/pre/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179
let pendingMessages = [];
180180

181181
const initData = {
182-
initialScrollProgress: undefined
182+
initialScrollProgress: undefined,
183183
};
184184

185185

@@ -195,6 +195,9 @@
195195
if (body) {
196196
body.classList.remove('vscode-light', 'vscode-dark', 'vscode-high-contrast');
197197
body.classList.add(initData.activeTheme);
198+
199+
body.dataset.vscodeThemeKind = initData.activeTheme;
200+
body.dataset.vscodeThemeName = initData.themeName || '';
198201
}
199202

200203
if (initData.styles) {
@@ -383,6 +386,7 @@
383386
host.onMessage('styles', (_event, data) => {
384387
initData.styles = data.styles;
385388
initData.activeTheme = data.activeTheme;
389+
initData.themeName = data.themeName;
386390

387391
const target = getActiveFrame();
388392
if (!target) {

src/vs/workbench/contrib/webview/common/themeing.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Emitter } from 'vs/base/common/event';
1313

1414
interface WebviewThemeData {
1515
readonly activeTheme: string;
16+
readonly themeLabel: string;
1617
readonly styles: { readonly [key: string]: string | number; };
1718
}
1819

@@ -73,7 +74,7 @@ export class WebviewThemeDataProvider extends Disposable {
7374
};
7475

7576
const activeTheme = ApiThemeClassName.fromTheme(theme);
76-
return { styles, activeTheme };
77+
return { styles, activeTheme, themeLabel: theme.label, };
7778
}
7879

7980
private reset() {

0 commit comments

Comments
 (0)