Skip to content

Commit

Permalink
fix: 🎨 修复 macOS 设置主题跟随系统后 某些情况下循环触发导致死循环的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Oct 31, 2023
1 parent d948c58 commit 1d7b188
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/store/theme/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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) {
Expand All @@ -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
}

Expand All @@ -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)
})

0 comments on commit 1d7b188

Please sign in to comment.