Skip to content

Expose data-vscode-theme-id attribute in webviews, fix #149661#154635

Merged
mjbvz merged 4 commits intomicrosoft:mainfrom
pingren:fix/expose-theme-id-webview
Sep 23, 2022
Merged

Expose data-vscode-theme-id attribute in webviews, fix #149661#154635
mjbvz merged 4 commits intomicrosoft:mainfrom
pingren:fix/expose-theme-id-webview

Conversation

@pingren
Copy link
Contributor

@pingren pingren commented Jul 9, 2022

This PR fixes #149661

Context

Changes

  • deprecate data-vscode-theme-name, rename it to data-vscode-theme-label
  • add data-vscode-theme-id

Original PR

Adds a data attribute to webviews with the themeId. Extension could write theme specific CSS for webviews. Unlike themeName, themeId will not change if changing the display language. themeName is preserved to prevent breaking changes.

Before (the display language changed to zh-cn)

CleanShot 2022-07-09 at 23 27 08@2x

After

CleanShot 2022-07-09 at 23 25 36@2x

@ghost
Copy link

ghost commented Jul 9, 2022

CLA assistant check
All CLA requirements met.

Copy link
Collaborator

@mjbvz mjbvz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding a new class, how about always using the default english name for data-vscode-theme-name. That way existing users don't have to adopt anything

@pingren
Copy link
Contributor Author

pingren commented Jul 10, 2022

@mjbvz I agree it's better not let existing users adopt anything. However, after some diggings around the code, I realized that

theme label is defined in

"label": "%themeLabel%",

and could be overwritten by i18n extension in

https://github.com/microsoft/vscode-loc/blob/fb987ff21ae002c918fc0e7039642ae345f279b6/i18n/vscode-language-pack-zh-hans/translations/extensions/theme-red.i18n.json#L14

When ExtensionsRegistry loading theme extensions, themeLabel will be localized right away, and ThemeService is not aware of its original English string.

Keeping and passing a copy of English themeLabel is overkill. Therefore, adding a data-vscode-theme-id should be the way to address the issue with minimal efforts.

@mjbvz
Copy link
Collaborator

mjbvz commented Jul 12, 2022

Introducing this new attribute both leaves the current broken behavior in place and introduces a new thing for users/extension authors to adopt. Not a fan

@aeschli Any ideas on reading the default English value of a theme name?

@aeschli
Copy link
Contributor

aeschli commented Jul 12, 2022

Yes, I don't think we don't know the original English label in the ThemeService as the translation happens in a layer below it.
It would be good if we can deprecate data-vscode-theme-name. Themes actually have a label not a name.
Let's search how many extensions use it. We can then also see how it is used: either for label (for presentation) vs the id (to set it or for theme specific colors)

@pingren
Copy link
Contributor Author

pingren commented Jul 12, 2022

I found themes using data-vscode-theme-name from https://github.com/search?q=data-vscode-theme-name&type=code.
Most of them are used to for theme specific colors or styles. I don't know if there are better ways to get exact usage stats.

@aeschli
Copy link
Contributor

aeschli commented Jul 12, 2022

Searching the top 6000 extensions on the Marketplace so far only rangav.vscode-thunder-client uses data-vscode-theme-name

@aeschli
Copy link
Contributor

aeschli commented Jul 19, 2022

After searching 37'800 extensions, I found data-vscode-theme-name at the following spaces

blueplanet-ciena.ciena-blueplanet/resource-type-graph/main.91fb18098d9d6cca2a82.js
cweijan.vscode-database-client2/out/webview/js/app.js
cweijan.vscode-database-client2/out/webview/js/query.js
cweijan.vscode-dm-client/out/webview/js/app.js
cweijan.vscode-es-client2/out/webview/js/app.js
cweijan.vscode-es-client2/out/webview/js/query.js
cweijan.vscode-mysql-client2/out/webview/js/app.js
cweijan.vscode-mysql-client2/out/webview/js/query.js
cweijan.vscode-postgresql-client2/out/webview/js/app.js
cweijan.vscode-postgresql-client2/out/webview/js/query.js
cweijan.vscode-redis-client/out/webview/js/app.js
cweijan.vscode-redis-client/out/webview/js/query.js
cweijan.vscode-ssh/out/webview/js/app.js
cweijan.vscode-ssh/out/webview/js/query.js
eliostruyf.vscode-theming-devtools/dist/webview.js
LigaAI.liga/out/web-dist/assets/i18n.js
Lightrun.lightrunplugin-saas/webpack/build/configViewer.js
Lightrun.lightrunplugin-saas/webpack/build/logs.js
rangav.vscode-thunder-client/dist/extension.js
rebornix.vscode-code-renderer/out/client/index.js
YuTengjing.scripting-listener/dist/web/webview.js

My suggestion would be to deprecate data-vscode-theme-name (remove it from documentation, but leave it as is for the next half year), add data-vscode-theme-id and data-vscode-theme-label and inform the extensions above.

@pingren
Copy link
Contributor Author

pingren commented Jul 21, 2022

Thank you for your research!
I will change the PR to deprecate data-vscode-theme-name and add data-vscode-theme-id and data-vscode-theme-label.
I could submit another PR to https://github.com/microsoft/vscode-docs about these changes if you want.

And I am just curious. How did you search for source code of extensions on the Marketplace?

@aeschli
Copy link
Contributor

aeschli commented Jul 21, 2022

@pingren An update to the docs would be fantastic.
We have an internal tool that downloads a set of extensions from the marketplace so we can search them

@pingren pingren force-pushed the fix/expose-theme-id-webview branch from c6ffba7 to b7560ee Compare July 24, 2022 11:08
pingren added a commit to pingren/vscode-docs that referenced this pull request Jul 24, 2022
@pingren pingren force-pushed the fix/expose-theme-id-webview branch from b7560ee to 7ab7b48 Compare September 5, 2022 04:55
@pingren pingren requested a review from mjbvz September 5, 2022 04:56
@pingren
Copy link
Contributor Author

pingren commented Sep 5, 2022

@aeschli PR Updated. Please take a look when you have a moment.

@aeschli
Copy link
Contributor

aeschli commented Sep 5, 2022

I'd rather not introduce the id to platform/common/IColorTheme.

Instead you can do the following:
In the WebviewThemeDataProvider use IWorkbenchThemeService instead of IThemeService

Then the theme you get with this._themeService.getColorTheme() is of type IWorkbenchColorTheme and you can access the property id

@pingren pingren force-pushed the fix/expose-theme-id-webview branch from 7ab7b48 to 29fbfb5 Compare September 13, 2022 02:57
@pingren
Copy link
Contributor Author

pingren commented Sep 13, 2022

Updated: use IWorkbenchThemeService instead of IThemeService

@aeschli
Copy link
Contributor

aeschli commented Sep 13, 2022

Thanks @pingren. Can you also remove the change in src/vs/workbench/contrib/terminal/test/common/terminalColorRegistry.test.ts?

@pingren
Copy link
Contributor Author

pingren commented Sep 15, 2022

Thank you! Sorry for that.

Updated: Remove the change in src/vs/workbench/contrib/terminal/test/common/terminalColorRegistry.test.ts

aeschli
aeschli previously approved these changes Sep 15, 2022
@mjbvz mjbvz added this to the September 2022 milestone Sep 22, 2022

const activeTheme = ApiThemeClassName.fromTheme(theme);
this._cachedWebViewThemeData = { styles, activeTheme, themeLabel: theme.label, };
this._cachedWebViewThemeData = { styles, activeTheme, themeLabel: theme.label, themeId: theme.settingsId };
Copy link
Collaborator

@mjbvz mjbvz Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aeschli Can you please confirm that settingsId is a good value to use here? I switched to use this since theme.id is not friendly to consume. Here's some example values for Dark+:

  • theme.idvs-dark vscode-theme-defaults-themes-dark_plus-json
  • theme.settingsIdDefault Dark+

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry for not realizing this. settingsId is what we want (it's unique and it's what can be used in the settings)

@mjbvz mjbvz enabled auto-merge (squash) September 23, 2022 17:59
@mjbvz mjbvz merged commit e42b88d into microsoft:main Sep 23, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Nov 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Webview CSS: data-vscode-theme-name attribute changes when the language changes

3 participants