示例工程 22-theming-and-native-appearance

22-theming-and-native-appearance/app/shared/ipc.ts

typescript · 0.8 KB

export const CH = {
  themeGet: "theme:get",
  themeSet: "theme:set",
  themeChanged: "theme:changed",
} as const;

export type ThemeSource = "system" | "light" | "dark";

export type ThemeState = {
  source: ThemeSource;
  shouldUseDarkColors: boolean;
  platform: string;
};

const SOURCES: ThemeSource[] = ["system", "light", "dark"];

export function isThemeSource(value: unknown): value is ThemeSource {
  return typeof value === "string" && SOURCES.includes(value as ThemeSource);
}

export function assertThemeSet(
  value: unknown,
): asserts value is { source: ThemeSource } {
  if (typeof value !== "object" || value === null) {
    throw new Error("BAD_THEME_REQ");
  }
  const source = (value as { source?: unknown }).source;
  if (!isThemeSource(source)) throw new Error("BAD_THEME_SOURCE");
}