1
1
import React from 'react'
2
- import { BrowserStorageItem , THEMES } from '../constants'
2
+ import { BrowserStorageItem , Theme , THEMES , THEME_MATCH_MEDIA_DARK } from '../constants'
3
3
import { localStorageService , themeService } from '../services'
4
4
5
5
interface Props {
@@ -10,6 +10,7 @@ const THEME_NAMES = THEMES.map(({ value }) => value)
10
10
11
11
export const defaultState = {
12
12
theme : THEME_NAMES [ 0 ] ,
13
+ usingSystemTheme : localStorageService . get ( BrowserStorageItem . theme ) === Theme . System ,
13
14
changeTheme : ( themeValue : any ) => {
14
15
themeService . applyTheme ( themeValue )
15
16
} ,
@@ -25,28 +26,39 @@ export class ThemeProvider extends React.Component<Props> {
25
26
const theme = ! storedThemeValue || ! THEME_NAMES . includes ( storedThemeValue )
26
27
? defaultState . theme
27
28
: storedThemeValue
29
+ const usingSystemTheme = theme === Theme . System
28
30
29
31
themeService . applyTheme ( theme )
30
32
31
33
this . state = {
32
- theme,
34
+ theme : theme === Theme . System ? this . getSystemTheme ( ) : theme ,
35
+ usingSystemTheme,
33
36
}
34
37
}
35
38
39
+ getSystemTheme = ( ) => window . matchMedia && window . matchMedia ( THEME_MATCH_MEDIA_DARK ) . matches ? Theme . Dark : Theme . Light
40
+
36
41
changeTheme = ( themeValue : any ) => {
37
- this . setState ( { theme : themeValue } , ( ) => {
42
+ let actualTheme = themeValue
43
+ if ( themeValue === Theme . System ) {
44
+ actualTheme = this . getSystemTheme ( )
45
+ }
46
+ window . app ?. ipc ?. invoke ?.( 'theme:change' , themeValue )
47
+
48
+ this . setState ( { theme : actualTheme , usingSystemTheme : themeValue === Theme . System } , ( ) => {
38
49
themeService . applyTheme ( themeValue )
39
50
} )
40
51
}
41
52
42
53
render ( ) {
43
54
const { children } = this . props
44
- const { theme } : any = this . state
55
+ const { theme, usingSystemTheme } : any = this . state
45
56
46
57
return (
47
58
< ThemeContext . Provider
48
59
value = { {
49
60
theme,
61
+ usingSystemTheme,
50
62
changeTheme : this . changeTheme ,
51
63
} }
52
64
>
0 commit comments