From 15b138cdd7bc2f672c1e90aaea237bb0968e0b45 Mon Sep 17 00:00:00 2001 From: chenchun Date: Wed, 2 Sep 2026 22:47:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=BB=98=E8=AE=A4=E4=B8=BB=E9=A2=98?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=20system=20=E8=B7=9F=E9=9A=8F=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=EF=BC=8CBGM=20=E9=BB=98=E8=AE=A4=E5=BC=80=E5=90=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 主题默认值 light → system(首次访问自动跟随 prefers-color-scheme, 与原版默认 light 不同,为产品决策) - 音效默认值 off → on(localStorage 无记录即开启,显式关闭才停; 首次真实播放仍受自动播放策略限制,由 useBgm 手势恢复逻辑兜底) --- lib/shellMedia.tsx | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/shellMedia.tsx b/lib/shellMedia.tsx index e5637f1..489d63c 100644 --- a/lib/shellMedia.tsx +++ b/lib/shellMedia.tsx @@ -8,7 +8,8 @@ import { useEffect, useSyncExternalStore } from "react"; * sound store (module 23525): * * - theme ∈ {light, dark, system}, persisted to localStorage "theme" - * (original key, kept verbatim for parity); default "light". + * (original key, kept verbatim for parity); default "system" — follow the + * device (auto), per product decision (original defaulted to "light"). * - resolvedTheme = theme === "system" * ? (matchMedia("(prefers-color-scheme: dark)").matches ? dark : light) * : theme; @@ -16,7 +17,8 @@ import { useEffect, useSyncExternalStore } from "react"; * CSS tokens flip via the .dark block in globals.css. * - "system" follows OS changes live (original matchMedia change listener). * - soundEnabled persisted to localStorage "sound" ("on"/"off", original - * keys). + * keys); default ON — BGM starts (autoplay policies defer actual playback + * to the first user gesture, handled in useBgm's recovery effect). * * Implemented as a module-singleton external store (useSyncExternalStore) * instead of the original context: the header and the canvas components @@ -38,9 +40,9 @@ type ShellMediaState = { }; let state: ShellMediaState = { - theme: "light", + theme: "system", resolvedTheme: "light", - soundEnabled: false, + soundEnabled: true, }; const listeners = new Set<() => void>(); @@ -90,16 +92,18 @@ function hydrate() { if (hydrated || typeof window === "undefined") return; hydrated = true; const stored = readStored(THEME_KEY); + // 无有效存储 → 默认 system(自动跟随设备) const theme: ThemeSetting = SETTINGS.includes(stored as ThemeSetting) ? (stored as ThemeSetting) - : "light"; + : "system"; mediaQuery = window.matchMedia("(prefers-color-scheme: dark)"); mediaQuery.addEventListener("change", onSystemChange); state = { ...state, theme, resolvedTheme: resolve(theme) }; applyDocument(theme); state = { ...state, - soundEnabled: readStored(SOUND_KEY) === "on", + // 无有效存储 → 默认开启 BGM;显式 "off" 才关闭 + soundEnabled: readStored(SOUND_KEY) !== "off", }; } @@ -142,12 +146,14 @@ function subscribe(listener: () => void) { const getSnapshot = () => state; // Hydration-safe SSR snapshot: must equal what the server rendered -// (light/paused defaults). React uses this for the hydration render, then -// re-renders with the live snapshot once the client store hydrates. +// (system/light defaults per product decision; sound defaults ON — the +// audio only starts after the first user gesture, see useBgm). React uses +// this for the hydration render, then re-renders with the live snapshot +// once the client store hydrates. const SERVER_SNAPSHOT: ShellMediaState = { - theme: "light", + theme: "system", resolvedTheme: "light", - soundEnabled: false, + soundEnabled: true, }; function getServerSnapshot(): ShellMediaState {