Z ZPI 文档 v0.3.8
介绍页

源码

packages/desktop/src/renderer/appearance.ts

typescript30 行查看原文
import type { InterfacePreferences } from "../shared/bridge.ts";
import { defaultPreferences } from "../shared/config.ts";
import { useStore } from "./store.ts";

export function startAppearance(): () => void {
  const media = matchMedia("(prefers-color-scheme: dark)");
  let preferences: InterfacePreferences = useStore.getState().settings?.interface ?? defaultPreferences;
  const apply = () => {
    const root = document.documentElement;
    root.dataset.theme =
      preferences.theme === "system" ? (media.matches ? "dark" : "light") : preferences.theme;
    root.style.setProperty("--ui-font-size", `${preferences.fontSize}px`);
  };
  const systemChanged = () => {
    if (preferences.theme === "system") apply();
  };
  apply();
  media.addEventListener("change", systemChanged);
  const unsubscribe = useStore.subscribe((state) => {
    const next = state.settings?.interface ?? defaultPreferences;
    if (next.theme === preferences.theme && next.fontSize === preferences.fontSize) return;
    preferences = next;
    apply();
  });
  return () => {
    unsubscribe();
    media.removeEventListener("change", systemChanged);
  };
}