feat: follow system theme (#403)

pull/401/head
star knight 2024-11-26 10:25:48 +08:00 committed by GitHub
parent 6a5edf44bc
commit 82089563e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 14 deletions

View File

@ -1,6 +1,7 @@
<script>
import createMenus from '@/view/router/menu'
import zhCN from 'ant-design-vue/lib/locale-provider/zh_CN'
import { colorTheme } from './composables/theme'
export default {
name: 'App',
@ -16,12 +17,20 @@ export default {
},
computed: {
themeClass () {
return `theme-${this.config.app.theme}`
return `theme-${colorTheme.value}`
},
theme () {
return this.config.app.theme
return colorTheme.value
},
},
mounted () {
let theme = this.config.app.theme
if (this.config.app.theme === 'system') {
theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
}
colorTheme.value = theme
},
created () {
this.menus = createMenus(this)
this.config = this.$global.config

View File

@ -0,0 +1,3 @@
import { ref } from 'vue'
export const colorTheme = ref('dark')

View File

@ -1,6 +1,7 @@
<script>
import { ipcRenderer } from 'electron'
import Plugin from '../mixins/plugin'
import { colorTheme } from '../composables/theme'
export default {
name: 'Setting',
@ -10,19 +11,12 @@ export default {
key: 'app',
removeUserConfigLoading: false,
reloadLoading: false,
themeBackup: null,
urlBackup: null,
personalUrlBackup: null,
}
},
created () {
},
mounted () {
},
methods: {
ready (config) {
this.themeBackup = config.app.theme
this.urlBackup = config.app.remoteConfig.url
this.personalUrlBackup = config.app.remoteConfig.personalUrl
},
@ -187,15 +181,17 @@ export default {
async applyAfter () {
let reloadLazy = 10
let theme = this.config.app.theme
if (theme === 'system') {
theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
}
colorTheme.value = theme
//
if (this.config.app.remoteConfig.url !== this.urlBackup || this.config.app.remoteConfig.personalUrl !== this.personalUrlBackup) {
await this.$api.config.downloadRemoteConfig()
await this.reloadConfigAndRestart()
reloadLazy = 300
}
//
if (this.config.app.theme !== this.themeBackup) {
setTimeout(() => window.location.reload(), reloadLazy)
}
@ -264,7 +260,7 @@ export default {
this.$confirm({
title: '确定要恢复出厂设置吗?',
width: 610,
content: (h) => (
content: h => (
<div class="restore-factory-settings">
<hr />
<p>
@ -376,6 +372,9 @@ export default {
<a-radio-button value="dark" title="dark">
暗色
</a-radio-button>
<a-radio-button value="system" title="system">
跟随系统
</a-radio-button>
</a-radio-group>
</a-form-item>
<a-form-item label="首页提示" :label-col="labelCol" :wrapper-col="wrapperCol">