| 1 | import React, { |
| 2 | createContext, |
| 3 | useContext, |
| 4 | useEffect, |
| 5 | useRef, |
| 6 | useState, |
| 7 | } from "react"; |
| 8 | import { api, native, nativeAvailable } from "./api"; |
| 9 | import { |
| 10 | defaultAppearance, |
| 11 | normalizeAppearance, |
| 12 | themeVariables, |
| 13 | } from "./appearance-state.mjs"; |
| 14 | |
| 15 | const AppearanceContext = createContext(null); |
| 16 | export const useAppearance = () => useContext(AppearanceContext); |
| 17 | export function AppearanceProvider({ children }) { |
| 18 | const [appearance, setAppearance] = useState(() => { |
| 19 | try { |
| 20 | return normalizeAppearance( |
| 21 | JSON.parse(localStorage.getItem("cage-appearance")) || |
| 22 | defaultAppearance, |
| 23 | ); |
| 24 | } catch { |
| 25 | return defaultAppearance; |
| 26 | } |
| 27 | }); |
| 28 | const [systemDark, setSystemDark] = useState( |
| 29 | matchMedia("(prefers-color-scheme: dark)").matches, |
| 30 | ); |
| 31 | const [systemReduced, setSystemReduced] = useState( |
| 32 | matchMedia("(prefers-reduced-motion: reduce)").matches, |
| 33 | ); |
| 34 | const [loading, setLoading] = useState(true), |
| 35 | [saving, setSaving] = useState(false), |
| 36 | [error, setError] = useState(""); |
| 37 | const revision = useRef(0), |
| 38 | queue = useRef(Promise.resolve()), |
| 39 | current = useRef(appearance); |
| 40 | const variant = |
| 41 | appearance.mode === "system" |
| 42 | ? systemDark |
| 43 | ? "dark" |
| 44 | : "light" |
| 45 | : appearance.mode; |
| 46 | const theme = appearance[variant + "Theme"]; |
| 47 | current.current = appearance; |
| 48 | useEffect(() => { |
| 49 | let live = true; |
| 50 | const load = () => |
| 51 | api("/appearance") |
| 52 | .then((value) => { |
| 53 | if (live) { |
| 54 | setAppearance(normalizeAppearance(value)); |
| 55 | localStorage.setItem("cage-appearance", JSON.stringify(value)); |
| 56 | } |
| 57 | }) |
| 58 | .catch((e) => { |
| 59 | if (live) setError(e.message); |
| 60 | }) |
| 61 | .finally(() => { |
| 62 | if (live) setLoading(false); |
| 63 | }); |
| 64 | if (location.hash) |
| 65 | window.addEventListener("cage-session-ready", load, { once: true }); |
| 66 | else load(); |
| 67 | const dark = matchMedia("(prefers-color-scheme: dark)"), |
| 68 | reduced = matchMedia("(prefers-reduced-motion: reduce)"); |
| 69 | const onDark = () => setSystemDark(dark.matches), |
| 70 | onReduced = () => setSystemReduced(reduced.matches); |
| 71 | dark.addEventListener("change", onDark); |
| 72 | reduced.addEventListener("change", onReduced); |
| 73 | return () => { |
| 74 | live = false; |
| 75 | window.removeEventListener("cage-session-ready", load); |
| 76 | dark.removeEventListener("change", onDark); |
| 77 | reduced.removeEventListener("change", onReduced); |
| 78 | }; |
| 79 | }, []); |
| 80 | useEffect(() => { |
| 81 | const root = document.documentElement; |
| 82 | const variables = themeVariables(theme, variant); |
| 83 | for (const [key, value] of Object.entries(variables)) |
| 84 | root.style.setProperty(key, value); |
| 85 | root.style.setProperty("--ui-font-size", appearance.uiFontSize + "px"); |
| 86 | root.style.setProperty("--code-font-size", appearance.codeFontSize + "px"); |
| 87 | root.style.setProperty( |
| 88 | "--control-cursor", |
| 89 | appearance.pointerCursors ? "pointer" : "default", |
| 90 | ); |
| 91 | root.dataset.theme = variant; |
| 92 | root.dataset.translucent = String(!theme.opaqueWindows); |
| 93 | root.dataset.fontSmoothing = String(appearance.fontSmoothing); |
| 94 | root.dataset.reducedMotion = String( |
| 95 | appearance.reducedMotion === "on" || |
| 96 | (appearance.reducedMotion === "system" && systemReduced), |
| 97 | ); |
| 98 | root.style.colorScheme = variant; |
| 99 | if (nativeAvailable()) |
| 100 | native("appearance", { |
| 101 | mode: appearance.mode, |
| 102 | variant, |
| 103 | background: variables["--sidebar"], |
| 104 | foreground: variables["--sidebar-ink"], |
| 105 | translucent: !theme.opaqueWindows, |
| 106 | }).catch(() => |
| 107 | setError("外观已应用到工作台,原生窗口未能同步,请重新打开 Cage。"), |
| 108 | ); |
| 109 | }, [appearance, variant, systemReduced]); |
| 110 | const update = (next) => { |
| 111 | next = normalizeAppearance( |
| 112 | typeof next === "function" ? next(current.current) : next, |
| 113 | ); |
| 114 | current.current = next; |
| 115 | setAppearance(next); |
| 116 | setSaving(true); |
| 117 | setError(""); |
| 118 | const id = ++revision.current; |
| 119 | queue.current = queue.current |
| 120 | .catch(() => {}) |
| 121 | .then(async () => { |
| 122 | if (id !== revision.current) return; |
| 123 | try { |
| 124 | await api("/appearance", next); |
| 125 | localStorage.setItem("cage-appearance", JSON.stringify(next)); |
| 126 | if (id === revision.current) setSaving(false); |
| 127 | return true; |
| 128 | } catch (e) { |
| 129 | if (id === revision.current) { |
| 130 | setError(e.message); |
| 131 | setSaving(false); |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | }); |
| 136 | return queue.current; |
| 137 | }; |
| 138 | return ( |
| 139 | <AppearanceContext.Provider |
| 140 | value={{ |
| 141 | appearance, |
| 142 | theme, |
| 143 | variant, |
| 144 | loading, |
| 145 | saving, |
| 146 | error, |
| 147 | update, |
| 148 | retry: () => update(current.current), |
| 149 | }} |
| 150 | > |
| 151 | {children} |
| 152 | </AppearanceContext.Provider> |
| 153 | ); |
| 154 | } |