Skip to content

Commit 103e2e5

Browse files
committed
fix instantly reflect system auto-themes to app themes
1 parent ae3e579 commit 103e2e5

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/renderer/components/layouts/ThemeContainer.tsx

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
/** @jsxImportSource @emotion/react */
2-
import { useMemo } from 'react';
3-
import { darkScrollbar } from '@mui/material';
2+
import { useEffect, useMemo } from 'react';
3+
import { darkScrollbar, useMediaQuery, CssBaseline } from '@mui/material';
44
import { ThemeProvider as MuiThemeProvider, createTheme } from '@mui/material/styles';
55
import { css, Global, ThemeProvider } from '@emotion/react';
66
import { connect } from 'react-redux';
7+
import { bindActionCreators } from 'redux';
8+
import * as configActions from '@/renderer/store/modules/config';
79

8-
const ThemeContainer = ({ children, config }) => {
9-
const darkMode = config.isDarkTheme;
10+
const ThemeContainer = ({ children, config, ConfigActions }) => {
11+
const prefersDarkMode = useMediaQuery('(prefers-color-scheme: dark)');
12+
const darkMode = useMemo((): boolean => {
13+
if (config.appConfigTheme === 'auto') {
14+
return prefersDarkMode;
15+
}
16+
return config.isDarkTheme;
17+
}, [config.appConfigTheme, config.isDarkTheme, prefersDarkMode]);
1018
const muiTheme = useMemo(
1119
() =>
1220
createTheme({
@@ -56,6 +64,12 @@ const ThemeContainer = ({ children, config }) => {
5664
[config.isDarkTheme],
5765
);
5866

67+
useEffect(() => {
68+
if (config.appConfigTheme === 'auto') {
69+
ConfigActions.setConfig({ isDarkTheme: prefersDarkMode });
70+
}
71+
}, [prefersDarkMode]);
72+
5973
return (
6074
<MuiThemeProvider theme={muiTheme}>
6175
<Global
@@ -65,6 +79,7 @@ const ThemeContainer = ({ children, config }) => {
6579
}
6680
`}
6781
/>
82+
<CssBaseline />
6883
<ThemeProvider theme={muiTheme}>{children}</ThemeProvider>
6984
</MuiThemeProvider>
7085
);
@@ -74,4 +89,8 @@ const mapStateToProps = (state) => ({
7489
config: state.config,
7590
});
7691

77-
export default connect(mapStateToProps)(ThemeContainer);
92+
const mapDispatchToProps = (dispatch) => ({
93+
ConfigActions: bindActionCreators({ ...configActions }, dispatch),
94+
});
95+
96+
export default connect(mapStateToProps, mapDispatchToProps)(ThemeContainer);

0 commit comments

Comments
 (0)