From 1d7b188885998552b4fd371ba76b666243cd743c Mon Sep 17 00:00:00 2001 From: viarotel Date: Tue, 31 Oct 2023 17:33:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=8E=A8=20=E4=BF=AE=E5=A4=8D=20macO?= =?UTF-8?q?S=20=E8=AE=BE=E7=BD=AE=E4=B8=BB=E9=A2=98=E8=B7=9F=E9=9A=8F?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E5=90=8E=20=E6=9F=90=E4=BA=9B=E6=83=85?= =?UTF-8?q?=E5=86=B5=E4=B8=8B=E5=BE=AA=E7=8E=AF=E8=A7=A6=E5=8F=91=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E6=AD=BB=E5=BE=AA=E7=8E=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/theme/index.js | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/store/theme/index.js b/src/store/theme/index.js index 10a34a06..58033ef4 100644 --- a/src/store/theme/index.js +++ b/src/store/theme/index.js @@ -12,8 +12,10 @@ const systemTheme = (key, value) => { export const useThemeStore = defineStore({ id: 'app-theme', state() { + const themeValue = window.appStore.get('common.theme') || 'system' return { - value: window.appStore.get('common.theme') || 'system', + value: themeValue, + isDark: false, } }, actions: { @@ -23,11 +25,14 @@ export const useThemeStore = defineStore({ this.update(this.value) }, - update(value) { + async update(value) { this.value = value - systemTheme('update', value) - this.updateHtml(value) - return true + + this.isDark = await systemTheme('isDark') + + await systemTheme('update', value) + + await this.updateHtml(value) }, async updateHtml(value) { @@ -42,8 +47,7 @@ export const useThemeStore = defineStore({ } if (value === 'system') { - const isDark = await systemTheme('isDark') - updateClass(isDark ? 'dark' : 'light') + updateClass(this.isDark ? 'dark' : 'light') return } @@ -53,10 +57,18 @@ export const useThemeStore = defineStore({ }) /** 监听系统主题色变化 */ -systemTheme('change', ({ value }) => { - console.log('systemTheme.change.value', value) +systemTheme('change', async ({ isDark, value }) => { + if (value !== 'system') { + return + } + const themeStore = useThemeStore() - if (value === 'system') { - themeStore.update(value) + + if (themeStore.isDark === isDark) { + return } + + console.log('systemTheme.change.isDark', isDark) + + themeStore.update(value) })