mirror of
https://github.com/jxxghp/MoviePilot-Frontend.git
synced 2026-09-05 23:56:42 +08:00
兼容处理不支持黑白配色的老旧设备
This commit is contained in:
@@ -3,6 +3,7 @@ import { ref } from 'vue'
|
|||||||
import { useTheme } from 'vuetify'
|
import { useTheme } from 'vuetify'
|
||||||
import type { ThemeSwitcherTheme } from '@layouts/types'
|
import type { ThemeSwitcherTheme } from '@layouts/types'
|
||||||
import api from '@/api'
|
import api from '@/api'
|
||||||
|
import { checkPrefersColorSchemeIsDark } from '@/@core/utils'
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
themes: ThemeSwitcherTheme[]
|
themes: ThemeSwitcherTheme[]
|
||||||
@@ -22,7 +23,7 @@ const {
|
|||||||
)
|
)
|
||||||
|
|
||||||
function updateTheme() {
|
function updateTheme() {
|
||||||
const autoTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
|
const autoTheme = checkPrefersColorSchemeIsDark() ? 'dark' : 'light'
|
||||||
const theme = currentThemeName.value === 'auto' ? autoTheme : currentThemeName.value
|
const theme = currentThemeName.value === 'auto' ? autoTheme : currentThemeName.value
|
||||||
globalTheme.name.value = theme
|
globalTheme.name.value = theme
|
||||||
savedTheme.value = theme
|
savedTheme.value = theme
|
||||||
@@ -32,7 +33,11 @@ function updateTheme() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 监听系统主题变化
|
// 监听系统主题变化
|
||||||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateTheme)
|
try {
|
||||||
|
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateTheme)
|
||||||
|
} catch(e) {
|
||||||
|
console.error('当前设备不支持监听系统主题变化')
|
||||||
|
}
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => currentThemeName.value,
|
() => currentThemeName.value,
|
||||||
|
|||||||
@@ -118,3 +118,12 @@ export function isNullOrEmptyObject(obj: any): boolean {
|
|||||||
// 然后判断是否为空对象
|
// 然后判断是否为空对象
|
||||||
return !!(typeof obj === 'object' && Object.keys(obj).length === 0)
|
return !!(typeof obj === 'object' && Object.keys(obj).length === 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 判断系统配置色是否是黑暗的
|
||||||
|
export function checkPrefersColorSchemeIsDark(): boolean {
|
||||||
|
try {
|
||||||
|
return window.matchMedia('(prefers-color-scheme: dark)').matches
|
||||||
|
} catch (e) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+2
-1
@@ -3,6 +3,7 @@ import { useToast } from 'vue-toast-notification'
|
|||||||
import { useTheme } from 'vuetify'
|
import { useTheme } from 'vuetify'
|
||||||
import api from '@/api'
|
import api from '@/api'
|
||||||
import store from './store'
|
import store from './store'
|
||||||
|
import { checkPrefersColorSchemeIsDark } from '@/@core/utils'
|
||||||
|
|
||||||
const { global: globalTheme } = useTheme()
|
const { global: globalTheme } = useTheme()
|
||||||
|
|
||||||
@@ -21,7 +22,7 @@ async function fetchThemeConfig() {
|
|||||||
// 设置主题
|
// 设置主题
|
||||||
async function setTheme() {
|
async function setTheme() {
|
||||||
let themeValue = await fetchThemeConfig() || localStorage.getItem('theme') || 'light'
|
let themeValue = await fetchThemeConfig() || localStorage.getItem('theme') || 'light'
|
||||||
const autoTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
|
const autoTheme = checkPrefersColorSchemeIsDark() ? 'dark' : 'light'
|
||||||
globalTheme.name.value = themeValue === 'auto' ? autoTheme : themeValue
|
globalTheme.name.value = themeValue === 'auto' ? autoTheme : themeValue
|
||||||
// 修改载入时背景色
|
// 修改载入时背景色
|
||||||
localStorage.setItem('materio-initial-loader-bg', globalTheme.current.value.colors.background)
|
localStorage.setItem('materio-initial-loader-bg', globalTheme.current.value.colors.background)
|
||||||
|
|||||||
+2
-1
@@ -7,6 +7,7 @@ import api from '@/api'
|
|||||||
import router from '@/router'
|
import router from '@/router'
|
||||||
import logo from '@images/logo.png'
|
import logo from '@images/logo.png'
|
||||||
import { useTheme } from 'vuetify'
|
import { useTheme } from 'vuetify'
|
||||||
|
import { checkPrefersColorSchemeIsDark } from '@/@core/utils'
|
||||||
|
|
||||||
const { global: globalTheme } = useTheme()
|
const { global: globalTheme } = useTheme()
|
||||||
|
|
||||||
@@ -100,7 +101,7 @@ async function fetchThemeConfig() {
|
|||||||
// 设置主题
|
// 设置主题
|
||||||
async function setTheme() {
|
async function setTheme() {
|
||||||
let themeValue = await fetchThemeConfig() || localStorage.getItem('theme') || 'light'
|
let themeValue = await fetchThemeConfig() || localStorage.getItem('theme') || 'light'
|
||||||
const autoTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
|
const autoTheme = checkPrefersColorSchemeIsDark() ? 'dark' : 'light'
|
||||||
globalTheme.name.value = themeValue === 'auto' ? autoTheme : themeValue
|
globalTheme.name.value = themeValue === 'auto' ? autoTheme : themeValue
|
||||||
// 修改载入时背景色
|
// 修改载入时背景色
|
||||||
localStorage.setItem('materio-initial-loader-bg', globalTheme.current.value.colors.background)
|
localStorage.setItem('materio-initial-loader-bg', globalTheme.current.value.colors.background)
|
||||||
|
|||||||
Reference in New Issue
Block a user