Files
gao-customer-h5/src/Root.vue
T
2026-11-20 14:28:44 +08:00

87 lines
2.4 KiB
Vue

<template>
<router-view />
</template>
<script setup lang="ts">
import { Capacitor, SystemBars, SystemBarsStyle } from "@capacitor/core";
import { useGlobalUIStore } from "@/store/globalUI";
import { useSettingStore } from "@/store/settingStore";
const globalUIStore = useGlobalUIStore();
const settingStore = useSettingStore();
const darkMediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
const prefersDark = ref(darkMediaQuery.matches);
const resolvedThemeMode = computed<"light" | "dark">(() => {
if (globalUIStore.themeMode === "system") {
return prefersDark.value ? "dark" : "light";
}
return globalUIStore.themeMode;
});
async function syncSystemBars(themeMode: "light" | "dark"): Promise<void> {
if (!Capacitor.isNativePlatform()) {
return;
}
await SystemBars.setStyle({
// DARK 表示深色系统栏配浅色图标,所以深色主题要用它
style: themeMode === "dark" ? SystemBarsStyle.Dark : SystemBarsStyle.Light
});
}
function handleSystemThemeChange(event: MediaQueryListEvent): void {
prefersDark.value = event.matches;
}
async function unlockSoundOnUserGesture(): Promise<void> {
await settingStore.unlockSoundPlayers();
}
watch(
() => resolvedThemeMode.value,
(themeMode) => {
const rootElement = document.documentElement;
rootElement.classList.toggle("theme-dark", themeMode === "dark");
rootElement.dataset.theme = themeMode;
const favicon = document.querySelector<HTMLLinkElement>("link[rel='icon']");
if (favicon) favicon.href = themeMode === "dark" ? "/icons/brand-dark.svg" : "/icons/brand.svg";
void syncSystemBars(themeMode).catch(() => undefined);
},
{ immediate: true }
);
watch(
() => globalUIStore.bodyBackground,
(bodyBackground) => {
document.body.style.background = bodyBackground;
},
{ immediate: true }
);
watch(
() => globalUIStore.backdropBlur,
(enabled) => {
if (enabled) {
document.documentElement.style.removeProperty("--tui-backdrop-blur");
} else {
document.documentElement.style.setProperty("--tui-backdrop-blur", "0px");
}
},
{ immediate: true }
);
onMounted(() => {
settingStore.loadSoundPlayers();
darkMediaQuery.addEventListener("change", handleSystemThemeChange);
window.addEventListener("pointerdown", unlockSoundOnUserGesture, { once: true });
});
onUnmounted(() => {
darkMediaQuery.removeEventListener("change", handleSystemThemeChange);
window.removeEventListener("pointerdown", unlockSoundOnUserGesture);
});
</script>