Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

3.6 KB
Raw Blame
/**
 * Follows the editor theme. VS Code stamps the webview body with
 * `vscode-light` / `vscode-dark` / `vscode-high-contrast` /
 * `vscode-high-contrast-light`; the shared UI package derives its entire
 * palette from a `.dark` class on the root element. This module keeps the two
 * in sync and exposes a subscription for code that needs the resolved kind
 * (the next-themes shim, canvas renderers).
 *
 * The two high-contrast kinds additionally stamp an `hc` class, which
 * `theme-bridge.css` uses to widen borders and lift tertiary text. The
 * `native` class (preference "auto") switches neutrals to --vscode-* values. Contrast is
 * read from the editor even when the light/dark preference is pinned: a user on
 * a high-contrast theme needs the contrast whichever ramp they chose.
 */

import type { ThemePreference } from "../../../src/shared/webviewMessages";

export type ThemeKind = "light" | "dark";

const subscribers = new Set<(kind: ThemeKind) => void>();
let current: ThemeKind = "light";
let started = false;
/** Pinned scheme from the Home switcher; "auto" follows the editor. */
let preference: ThemePreference = "auto";

function compute(): ThemeKind {
  const cls = document.body.classList;
  // High-contrast without the -light suffix is the dark HC theme.
  if (cls.contains("vscode-dark")) return "dark";
  if (cls.contains("vscode-high-contrast") && !cls.contains("vscode-high-contrast-light")) {
    return "dark";
  }
  return "light";
}

/** True under either high-contrast kind. VS Code stamps the light one with
 *  both `vscode-high-contrast-light` and, on some builds, the base class. */
function computeHighContrast(): boolean {
  const cls = document.body.classList;
  return cls.contains("vscode-high-contrast") || cls.contains("vscode-high-contrast-light");
}

function apply(kind: ThemeKind): void {
  const root = document.documentElement;
  root.classList.toggle("dark", kind === "dark");
  root.classList.toggle("hc", computeHighContrast());
  // Native editor neutrals apply only while following the editor theme.
  root.classList.toggle("native", preference === "auto");
  if (kind === current) return;
  current = kind;
  for (const cb of subscribers) cb(kind);
}

/** The effective kind under the current preference. */
function resolve(): ThemeKind {
  return preference === "auto" ? compute() : preference;
}

/** Idempotent; called by the mount helper before first render. */
export function initTheme(): void {
  if (started) return;
  started = true;
  apply(resolve());
  const observer = new MutationObserver(() => apply(resolve()));
  observer.observe(document.body, { attributes: true, attributeFilter: ["class"] });
}

/**
 * Applies the persisted preference pushed by the host (init and theme-changed
 * messages). A pinned value wins over the editor theme until set back to auto.
 */
export function setThemePreference(pref: ThemePreference): void {
  preference = pref;
  apply(resolve());
}

export function getThemePreference(): ThemePreference {
  return preference;
}

export function getThemeKind(): ThemeKind {
  return current;
}

/**
 * User-initiated override from an in-view toggle (e.g. the graph's light/dark
 * switch). Applies immediately; the next editor theme change wins again via
 * the body-class observer.
 */
export function setThemeOverride(kind: ThemeKind): void {
  apply(kind);
  // The override is a Repowise-palette scheme; native neutrals would clash with it.
  document.documentElement.classList.remove("native");
}

export function subscribeTheme(cb: (kind: ThemeKind) => void): () => void {
  subscribers.add(cb);
  return () => subscribers.delete(cb);
}