feat: 面板主题色增加跟随浏览器 (#3107)

Refs #3075
pull/3114/head
ssongliu 2023-11-29 22:06:08 +08:00 committed by GitHub
parent fa9d855523
commit ed89f8fdd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 2 deletions

View File

@ -8,6 +8,10 @@ export const useTheme = () => {
const themeConfig = computed(() => globalStore.themeConfig); const themeConfig = computed(() => globalStore.themeConfig);
const switchDark = () => { const switchDark = () => {
if (themeConfig.value.theme === 'auto') {
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)');
themeConfig.value.theme = prefersDark.matches ? 'dark' : 'light';
}
const body = document.documentElement as HTMLElement; const body = document.documentElement as HTMLElement;
if (themeConfig.value.theme === 'dark') body.setAttribute('class', 'dark'); if (themeConfig.value.theme === 'dark') body.setAttribute('class', 'dark');
else body.setAttribute('class', ''); else body.setAttribute('class', '');

View File

@ -1162,6 +1162,7 @@ const message = {
theme: 'Theme', theme: 'Theme',
dark: 'Dark', dark: 'Dark',
light: 'Light', light: 'Light',
auto: 'Follow System',
language: 'Language', language: 'Language',
languageHelper: languageHelper:
'By default, it follows the browser language. This parameter takes effect only on the current browser', 'By default, it follows the browser language. This parameter takes effect only on the current browser',

View File

@ -1106,6 +1106,7 @@ const message = {
componentSize: '', componentSize: '',
dark: '', dark: '',
light: '', light: '',
auto: '',
language: '', language: '',
languageHelper: '', languageHelper: '',
sessionTimeout: '', sessionTimeout: '',

View File

@ -1107,6 +1107,7 @@ const message = {
componentSize: '', componentSize: '',
dark: '', dark: '',
light: '', light: '',
auto: '',
language: '', language: '',
languageHelper: '', languageHelper: '',
sessionTimeout: '', sessionTimeout: '',

View File

@ -73,6 +73,15 @@ const loadDataFromDB = async () => {
switchDark(); switchDark();
}; };
const updateDarkMode = async (event: MediaQueryListEvent) => {
const res = await getSettingInfo();
if (res.data.theme !== 'auto') {
return;
}
globalStore.setThemeConfig({ ...themeConfig.value, theme: event.matches ? 'dark' : 'light' });
switchDark();
};
const loadStatus = async () => { const loadStatus = async () => {
loading.value = globalStore.isLoading; loading.value = globalStore.isLoading;
loadinText.value = globalStore.loadingText; loadinText.value = globalStore.loadingText;
@ -101,6 +110,17 @@ onBeforeUnmount(() => {
onMounted(() => { onMounted(() => {
loadStatus(); loadStatus();
loadDataFromDB(); loadDataFromDB();
const mqList = window.matchMedia('(prefers-color-scheme: dark)');
if (mqList.addEventListener) {
mqList.addEventListener('change', (e) => {
updateDarkMode(e);
});
} else if (mqList.addListener) {
mqList.addListener((e) => {
updateDarkMode(e);
});
}
}); });
</script> </script>

View File

@ -29,13 +29,14 @@
<el-form-item :label="$t('setting.theme')" prop="theme"> <el-form-item :label="$t('setting.theme')" prop="theme">
<el-radio-group @change="onSave('Theme', form.theme)" v-model="form.theme"> <el-radio-group @change="onSave('Theme', form.theme)" v-model="form.theme">
<el-radio-button label="light"> <el-radio-button label="light">
<el-icon><Sunny /></el-icon>
<span>{{ $t('setting.light') }}</span> <span>{{ $t('setting.light') }}</span>
</el-radio-button> </el-radio-button>
<el-radio-button label="dark"> <el-radio-button label="dark">
<el-icon><Moon /></el-icon>
<span>{{ $t('setting.dark') }}</span> <span>{{ $t('setting.dark') }}</span>
</el-radio-button> </el-radio-button>
<el-radio-button label="auto">
<span>{{ $t('setting.auto') }}</span>
</el-radio-button>
</el-radio-group> </el-radio-group>
</el-form-item> </el-form-item>