@@ -44,6 +44,7 @@ export interface CoreSiteSpaceUsage {
4444export class CoreSettingsHelper {
4545 protected logger ;
4646 protected syncPromises = { } ;
47+ protected colorSchemes : string [ ] = [ ] ;
4748
4849 constructor ( loggerProvider : CoreLoggerProvider ,
4950 protected appProvider : CoreAppProvider ,
@@ -342,14 +343,7 @@ export class CoreSettingsHelper {
342343 if ( ! ! CoreConfigConstants . forceColorScheme ) {
343344 this . setColorScheme ( CoreConfigConstants . forceColorScheme ) ;
344345 } else {
345- let defaultColorScheme = 'light' ;
346-
347- if ( window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches ||
348- window . matchMedia ( '(prefers-color-scheme: light)' ) . matches ) {
349- defaultColorScheme = 'auto' ;
350- }
351-
352- this . configProvider . get ( CoreConstants . SETTINGS_COLOR_SCHEME , defaultColorScheme ) . then ( ( scheme ) => {
346+ this . configProvider . get ( CoreConstants . SETTINGS_COLOR_SCHEME , 'light' ) . then ( ( scheme ) => {
353347 this . setColorScheme ( scheme ) ;
354348 } ) ;
355349 }
@@ -388,6 +382,40 @@ export class CoreSettingsHelper {
388382 document . documentElement . style . fontSize = fontSize + '%' ;
389383 }
390384
385+ /**
386+ * Get system allowed color schemes.
387+ *
388+ * @return Allowed color schemes.
389+ */
390+ getAllowedColorSchemes ( ) : string [ ] {
391+ if ( this . colorSchemes . length > 0 ) {
392+ return this . colorSchemes ;
393+ }
394+
395+ if ( ! CoreConfigConstants . forceColorScheme ) {
396+ this . colorSchemes . push ( 'light' ) ;
397+ this . colorSchemes . push ( 'dark' ) ;
398+ if ( window . matchMedia ( '(prefers-color-scheme)' ) . media !== 'not all' ) {
399+ this . colorSchemes . push ( 'auto' ) ;
400+ }
401+ } else {
402+ this . colorSchemes = [ CoreConfigConstants . forceColorScheme ] ;
403+ }
404+
405+ return this . colorSchemes ;
406+ }
407+
408+ /**
409+ * Toggle Dark on auto mode.
410+ *
411+ * @param dark If dark scheme should be set or removed.
412+ */
413+ protected toggleDarkTheme ( dark : boolean ) : void {
414+ if ( document . body . classList . contains ( 'scheme-auto' ) ) {
415+ document . body . classList . toggle ( 'scheme-dark' , dark ) ;
416+ }
417+ }
418+
391419 /**
392420 * Set body color scheme.
393421 *
@@ -397,6 +425,10 @@ export class CoreSettingsHelper {
397425 document . body . classList . remove ( 'scheme-light' ) ;
398426 document . body . classList . remove ( 'scheme-dark' ) ;
399427 document . body . classList . remove ( 'scheme-auto' ) ;
428+
429+ const colorSchemes = this . getAllowedColorSchemes ( ) ;
430+
431+ colorScheme = colorSchemes . indexOf ( colorScheme ) >= 0 ? colorScheme : colorSchemes [ 0 ] ;
400432 document . body . classList . add ( 'scheme-' + colorScheme ) ;
401433 }
402434}
0 commit comments