@@ -17,13 +17,13 @@ import { Registry } from 'vs/platform/registry/common/platform';
17
17
import { asCssVariableName , ColorIdentifier , Extensions , IColorRegistry } from 'vs/platform/theme/common/colorRegistry' ;
18
18
import { Extensions as ThemingExtensions , ICssStyleCollector , IFileIconTheme , IProductIconTheme , IThemingRegistry , ITokenStyle } from 'vs/platform/theme/common/themeService' ;
19
19
import { IDisposable , Disposable } from 'vs/base/common/lifecycle' ;
20
- import { ColorScheme , isDark } from 'vs/platform/theme/common/theme' ;
20
+ import { ColorScheme , isDark , isHighContrast } from 'vs/platform/theme/common/theme' ;
21
21
import { getIconsStyleSheet , UnthemedProductIconTheme } from 'vs/platform/theme/browser/iconsStyleSheet' ;
22
22
23
- const VS_THEME_NAME = 'vs' ;
24
- const VS_DARK_THEME_NAME = 'vs-dark' ;
25
- const HC_BLACK_THEME_NAME = 'hc-black' ;
26
- const HC_LIGHT_THEME_NAME = 'hc-light' ;
23
+ export const VS_LIGHT_THEME_NAME = 'vs' ;
24
+ export const VS_DARK_THEME_NAME = 'vs-dark' ;
25
+ export const HC_BLACK_THEME_NAME = 'hc-black' ;
26
+ export const HC_LIGHT_THEME_NAME = 'hc-light' ;
27
27
28
28
const colorRegistry = Registry . as < IColorRegistry > ( Extensions . ColorContribution ) ;
29
29
const themingRegistry = Registry . as < IThemingRegistry > ( ThemingExtensions . ThemingContribution ) ;
@@ -118,7 +118,7 @@ class StandaloneTheme implements IStandaloneTheme {
118
118
119
119
public get type ( ) : ColorScheme {
120
120
switch ( this . base ) {
121
- case VS_THEME_NAME : return ColorScheme . LIGHT ;
121
+ case VS_LIGHT_THEME_NAME : return ColorScheme . LIGHT ;
122
122
case HC_BLACK_THEME_NAME : return ColorScheme . HIGH_CONTRAST_DARK ;
123
123
case HC_LIGHT_THEME_NAME : return ColorScheme . HIGH_CONTRAST_LIGHT ;
124
124
default : return ColorScheme . DARK ;
@@ -182,7 +182,7 @@ class StandaloneTheme implements IStandaloneTheme {
182
182
183
183
function isBuiltinTheme ( themeName : string ) : themeName is BuiltinTheme {
184
184
return (
185
- themeName === VS_THEME_NAME
185
+ themeName === VS_LIGHT_THEME_NAME
186
186
|| themeName === VS_DARK_THEME_NAME
187
187
|| themeName === HC_BLACK_THEME_NAME
188
188
|| themeName === HC_LIGHT_THEME_NAME
@@ -191,7 +191,7 @@ function isBuiltinTheme(themeName: string): themeName is BuiltinTheme {
191
191
192
192
function getBuiltinRules ( builtinTheme : BuiltinTheme ) : IStandaloneThemeData {
193
193
switch ( builtinTheme ) {
194
- case VS_THEME_NAME :
194
+ case VS_LIGHT_THEME_NAME :
195
195
return vs ;
196
196
case VS_DARK_THEME_NAME :
197
197
return vs_dark ;
@@ -229,7 +229,6 @@ export class StandaloneThemeService extends Disposable implements IStandaloneThe
229
229
private _globalStyleElement : HTMLStyleElement | null ;
230
230
private _styleElements : HTMLStyleElement [ ] ;
231
231
private _colorMapOverride : Color [ ] | null ;
232
- private _desiredTheme ! : IStandaloneTheme ;
233
232
private _theme ! : IStandaloneTheme ;
234
233
235
234
private _builtInProductIconTheme = new UnthemedProductIconTheme ( ) ;
@@ -240,7 +239,7 @@ export class StandaloneThemeService extends Disposable implements IStandaloneThe
240
239
this . _autoDetectHighContrast = true ;
241
240
242
241
this . _knownThemes = new Map < string , StandaloneTheme > ( ) ;
243
- this . _knownThemes . set ( VS_THEME_NAME , newBuiltInTheme ( VS_THEME_NAME ) ) ;
242
+ this . _knownThemes . set ( VS_LIGHT_THEME_NAME , newBuiltInTheme ( VS_LIGHT_THEME_NAME ) ) ;
244
243
this . _knownThemes . set ( VS_DARK_THEME_NAME , newBuiltInTheme ( VS_DARK_THEME_NAME ) ) ;
245
244
this . _knownThemes . set ( HC_BLACK_THEME_NAME , newBuiltInTheme ( HC_BLACK_THEME_NAME ) ) ;
246
245
this . _knownThemes . set ( HC_LIGHT_THEME_NAME , newBuiltInTheme ( HC_LIGHT_THEME_NAME ) ) ;
@@ -253,15 +252,16 @@ export class StandaloneThemeService extends Disposable implements IStandaloneThe
253
252
this . _globalStyleElement = null ;
254
253
this . _styleElements = [ ] ;
255
254
this . _colorMapOverride = null ;
256
- this . setTheme ( VS_THEME_NAME ) ;
255
+ this . setTheme ( VS_LIGHT_THEME_NAME ) ;
256
+ this . _onOSSchemeChanged ( ) ;
257
257
258
258
iconsStyleSheet . onDidChange ( ( ) => {
259
259
this . _codiconCSS = iconsStyleSheet . getCSS ( ) ;
260
260
this . _updateCSS ( ) ;
261
261
} ) ;
262
262
263
263
addMatchMediaChangeListener ( '(forced-colors: active)' , ( ) => {
264
- this . _updateActualTheme ( ) ;
264
+ this . _onOSSchemeChanged ( ) ;
265
265
} ) ;
266
266
}
267
267
@@ -331,41 +331,43 @@ export class StandaloneThemeService extends Disposable implements IStandaloneThe
331
331
}
332
332
333
333
public setTheme ( themeName : string ) : void {
334
- let theme : StandaloneTheme ;
334
+ let theme : StandaloneTheme | undefined ;
335
335
if ( this . _knownThemes . has ( themeName ) ) {
336
- theme = this . _knownThemes . get ( themeName ) ! ;
336
+ theme = this . _knownThemes . get ( themeName ) ;
337
337
} else {
338
- theme = this . _knownThemes . get ( VS_THEME_NAME ) ! ;
338
+ theme = this . _knownThemes . get ( VS_LIGHT_THEME_NAME ) ;
339
339
}
340
- this . _desiredTheme = theme ;
341
- this . _updateActualTheme ( ) ;
340
+ this . _updateActualTheme ( theme ) ;
342
341
}
343
342
344
- private getHighContrastTheme ( ) {
345
- if ( isDark ( this . _desiredTheme . type ) ) {
346
- return HC_BLACK_THEME_NAME ;
347
- } else {
348
- return HC_LIGHT_THEME_NAME ;
349
- }
350
- }
351
-
352
- private _updateActualTheme ( ) : void {
353
- const theme = (
354
- this . _autoDetectHighContrast && window . matchMedia ( `(forced-colors: active)` ) . matches
355
- ? this . _knownThemes . get ( this . getHighContrastTheme ( ) ) !
356
- : this . _desiredTheme
357
- ) ;
358
- if ( this . _theme === theme ) {
343
+ private _updateActualTheme ( desiredTheme : IStandaloneTheme | undefined ) : void {
344
+ if ( ! desiredTheme || this . _theme === desiredTheme ) {
359
345
// Nothing to do
360
346
return ;
361
347
}
362
- this . _theme = theme ;
348
+ this . _theme = desiredTheme ;
363
349
this . _updateThemeOrColorMap ( ) ;
364
350
}
365
351
352
+ private _onOSSchemeChanged ( ) {
353
+ if ( this . _autoDetectHighContrast ) {
354
+ const wantsHighContrast = window . matchMedia ( `(forced-colors: active)` ) . matches ;
355
+ if ( wantsHighContrast !== isHighContrast ( this . _theme . type ) ) {
356
+ // switch to high contrast or non-high contrast but stick to dark or light
357
+ let newThemeName ;
358
+ if ( isDark ( this . _theme . type ) ) {
359
+ newThemeName = wantsHighContrast ? HC_BLACK_THEME_NAME : VS_DARK_THEME_NAME ;
360
+ } else {
361
+ newThemeName = wantsHighContrast ? HC_LIGHT_THEME_NAME : VS_LIGHT_THEME_NAME ;
362
+ }
363
+ this . _updateActualTheme ( this . _knownThemes . get ( newThemeName ) ) ;
364
+ }
365
+ }
366
+ }
367
+
366
368
public setAutoDetectHighContrast ( autoDetectHighContrast : boolean ) : void {
367
369
this . _autoDetectHighContrast = autoDetectHighContrast ;
368
- this . _updateActualTheme ( ) ;
370
+ this . _onOSSchemeChanged ( ) ;
369
371
}
370
372
371
373
private _updateThemeOrColorMap ( ) : void {
0 commit comments