feat(theme): add custom UI color palettes

This commit is contained in:
Domye authored and GitHub committed 2026-08-16 11:59:28 +08:00
1 parent c81466d0db
commit 8996da7d9a
15 files changed
+618 -37

No files matched your search

+79 -2
View File
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { buildSqlCompletionThemeRules, resolveCustomThemeBackgrounds, resolveEditorTheme } from "@/lib/editor/editorThemes";
import type { AppThemePalette } from "@/lib/app/appTheme";
import { buildSqlCompletionThemeRules, editorDiagnosticColors, editorThemeAppearanceFor, resolveCustomThemeBackgrounds, resolveEditorTheme } from "@/lib/editor/editorThemes";
import { DEFAULT_APP_CUSTOM_UI_COLORS, wcagContrastRatio, type AppThemePalette } from "@/lib/app/appTheme";
import type { EditorTheme } from "@/stores/settingsStore";
describe("resolveEditorTheme", () => {
@@ -69,6 +69,83 @@ describe("custom editor theme backgrounds", () => {
gutterBackground: "#181825",
});
});
it("routes the custom UI palette to verified dark/light editor themes by appearance", () => {
expect(resolveEditorTheme("app", "dark", "custom")).toBe("one-dark");
expect(resolveEditorTheme("app", "light", "custom")).toBe("vscode-light");
expect(resolveEditorTheme("one-dark", "light", "custom")).toBe("one-dark");
});
it("derives the follow-app editor appearance from the custom background at light/dark extremes", () => {
const darkBg = { ...DEFAULT_APP_CUSTOM_UI_COLORS, background: "#000000" };
const lightBg = { ...DEFAULT_APP_CUSTOM_UI_COLORS, background: "#ffffff" };
expect(editorThemeAppearanceFor("light", "custom", darkBg)).toBe("dark");
expect(editorThemeAppearanceFor("dark", "custom", lightBg)).toBe("light");
// Setting off or fixed palettes keep the previous mode-based behavior unchanged.
expect(editorThemeAppearanceFor("dark", "pearl")).toBe("dark");
expect(editorThemeAppearanceFor("light", "cobalt")).toBe("light");
expect(editorThemeAppearanceFor("light", "custom", undefined)).toBe("light");
});
it("keeps every major semantic token readable on the editor themes the custom palette routes to", () => {
// Token colors are the shipped values of the curated themes (one-dark from
// @codemirror/theme-one-dark, vscode-light from @uiw/codemirror-theme-vscode).
// The floors are WCAG AA large-text (3.0) for dimmed comments and a 4.0
// readable floor for body tokens; one-dark's variable sits at ~4.4 by design.
const curated: Array<{ bg: string; fg: string; comment: string; tokens: Record<string, string> }> = [
{
bg: "#282c34",
fg: "#abb2bf",
comment: "#7d8799",
tokens: {
keyword: "#c678dd",
string: "#98c379",
number: "#d19a66",
function: "#61afef",
type: "#e5c07b",
variable: "#e06c75",
operator: "#56b6c2",
},
},
{
bg: "#ffffff",
fg: "#383a42",
comment: "#008000",
tokens: {
keyword: "#383a42",
string: "#a31515",
number: "#383a42",
function: "#383a42",
type: "#383a42",
variable: "#383a42",
operator: "#383a42",
},
},
];
for (const theme of curated) {
for (const [name, color] of Object.entries(theme.tokens)) {
expect(wcagContrastRatio(color, theme.bg), `${name} (${color}) on ${theme.bg}`).toBeGreaterThanOrEqual(4.0);
}
expect(wcagContrastRatio(theme.comment, theme.bg), `comment on ${theme.bg}`).toBeGreaterThanOrEqual(3.0);
}
});
it("uses light diagnostic markers on dark editors and dark markers on light editors", () => {
const dark = editorDiagnosticColors("dark");
const light = editorDiagnosticColors("light");
expect(wcagContrastRatio(dark.error, "#282c34")).toBeGreaterThanOrEqual(3.0);
expect(wcagContrastRatio(dark.warning, "#282c34")).toBeGreaterThanOrEqual(3.0);
expect(wcagContrastRatio(light.error, "#ffffff")).toBeGreaterThanOrEqual(3.0);
expect(wcagContrastRatio(light.warning, "#ffffff")).toBeGreaterThanOrEqual(3.0);
});
it("keeps selected text readable against the curated themes' selection backgrounds", () => {
// Selection backgrounds are part of the same verified themes; selected text
// (the theme foreground) must stay legible inside the selection highlight.
expect(wcagContrastRatio("#abb2bf", "#3E4451")).toBeGreaterThanOrEqual(3.0); // one-dark selection
expect(wcagContrastRatio("#383a42", "#add6ff")).toBeGreaterThanOrEqual(3.0); // vscode-light selection
});
});
describe("SQL completion theme", () => {
+63
View File
@@ -0,0 +1,63 @@
import { describe, expect, it } from "vitest";
import { APP_CUSTOM_UI_DERIVED_VAR_NAMES, DEFAULT_APP_CUSTOM_UI_COLORS, DEFAULT_APP_CUSTOM_UI_COLORS_DARK, appCustomUiColorValue, customUiAppearance, deriveCustomUiColors, hexRelativeLuminance, mixHex, normalizeAppCustomUiColors, readableTextOn, wcagContrastRatio } from "@/lib/app/appTheme";
describe("appTheme custom UI colors", () => {
it("falls back to defaults for missing or invalid values and keeps valid hex", () => {
expect(normalizeAppCustomUiColors(null)).toEqual(DEFAULT_APP_CUSTOM_UI_COLORS);
expect(normalizeAppCustomUiColors({ background: "red", primary: "#2b63b7" }).background).toBe(DEFAULT_APP_CUSTOM_UI_COLORS.background);
expect(normalizeAppCustomUiColors({ background: "red", primary: "#2b63b7" }).primary).toBe("#2b63b7");
expect(normalizeAppCustomUiColors({ background: "#fff" }).background).toBe(DEFAULT_APP_CUSTOM_UI_COLORS.background);
});
it("converts hex to space-separated rgb and comma-separated triplet", () => {
expect(appCustomUiColorValue("#2b63b7")).toEqual({ color: "rgb(43 99 183)", rgbTriplet: "43, 99, 183" });
});
it("computes WCAG luminance and contrast ratio", () => {
expect(hexRelativeLuminance("#000000")).toBeCloseTo(0, 5);
expect(hexRelativeLuminance("#ffffff")).toBeCloseTo(1, 5);
expect(wcagContrastRatio("#000000", "#ffffff")).toBeCloseTo(21, 5);
expect(wcagContrastRatio("#000000", "#000000")).toBeCloseTo(1, 5);
});
it("ships distinct dark defaults and normalizes them like the light set", () => {
expect(DEFAULT_APP_CUSTOM_UI_COLORS_DARK.background).toBe("#131416");
expect(DEFAULT_APP_CUSTOM_UI_COLORS_DARK.background).not.toBe(DEFAULT_APP_CUSTOM_UI_COLORS.background);
expect(normalizeAppCustomUiColors(DEFAULT_APP_CUSTOM_UI_COLORS_DARK)).toEqual(DEFAULT_APP_CUSTOM_UI_COLORS_DARK);
});
it("derives light/dark appearance from the custom background luminance at both extremes", () => {
expect(customUiAppearance({ ...DEFAULT_APP_CUSTOM_UI_COLORS, background: "#000000" })).toBe("dark");
expect(customUiAppearance({ ...DEFAULT_APP_CUSTOM_UI_COLORS, background: "#ffffff" })).toBe("light");
expect(customUiAppearance({ ...DEFAULT_APP_CUSTOM_UI_COLORS, background: "#123456" })).toBe("dark");
expect(customUiAppearance({ ...DEFAULT_APP_CUSTOM_UI_COLORS, background: "#e8f0fa" })).toBe("light");
});
it("mixes two hex colors toward the second by weight", () => {
expect(mixHex("#000000", "#ffffff", 0.5)).toBe("rgb(128 128 128)");
expect(mixHex("#ffffff", "#000000", 1)).toBe("rgb(0 0 0)");
expect(mixHex("#ffffff", "#000000", 0)).toBe("rgb(255 255 255)");
});
it("derives every surface with its paired foreground and never uses color-mix", () => {
const derived = deriveCustomUiColors({ ...DEFAULT_APP_CUSTOM_UI_COLORS, primary: "#2b63b7" });
expect(Object.keys(derived).sort()).toEqual([...APP_CUSTOM_UI_DERIVED_VAR_NAMES].sort());
// Mid-blue primary takes white text for contrast.
expect(derived["--primary-foreground"]).toBe("rgb(255 255 255)");
expect(derived["--sidebar-primary"]).toBe("rgb(43 99 183)");
expect(derived["--muted-foreground"]).toBe("rgb(152 152 152)");
for (const value of Object.values(derived)) {
expect(value).not.toContain("color-mix");
expect(value).not.toContain("NaN");
}
});
it("keeps paired foreground/background combinations readable across primary extremes", () => {
for (const primary of ["#111111", "#eeeeee", "#808080", "#2b63b7", "#c0392b"]) {
const derived = deriveCustomUiColors({ ...DEFAULT_APP_CUSTOM_UI_COLORS, primary });
expect(wcagContrastRatio(derived["--primary-foreground"], derived["--sidebar-primary"]), `primary ${primary}`).toBeGreaterThanOrEqual(3.5);
}
expect(readableTextOn("#000000")).toBe("#ffffff");
expect(readableTextOn("#ffffff")).toBe("#0a0a0a");
});
});
+214 -2
View File
@@ -2,13 +2,155 @@ import type { Theme } from "@tauri-apps/api/window";
export const APP_THEME_STORAGE_KEY = "dbx-theme";
export const APP_THEME_PALETTE_STORAGE_KEY = "dbx-theme-palette";
export const APP_CUSTOM_UI_STORAGE_KEY = "dbx-theme-custom-ui";
export const APP_CUSTOM_UI_DARK_STORAGE_KEY = "dbx-theme-custom-ui-dark";
export const APP_CORNER_STYLE_STORAGE_KEY = "dbx-corner-style";
export type AppThemeMode = "light" | "dark" | "system";
export type AppThemeAppearance = "light" | "dark";
export type AppThemePalette = "pearl" | "mist" | "graphite" | "cobalt" | "sage" | "amber" | "blush" | "vscode" | "idea" | "xcode" | "jetbrains" | "cursor" | "claude";
export type AppThemePalette = "pearl" | "mist" | "graphite" | "cobalt" | "sage" | "amber" | "blush" | "vscode" | "idea" | "xcode" | "jetbrains" | "cursor" | "claude" | "custom";
export type AppCornerStyle = "none" | "small" | "large";
export interface AppCustomUiColors {
background: string;
foreground: string;
primary: string;
border: string;
sidebar: string;
}
export const DEFAULT_APP_CUSTOM_UI_COLORS: AppCustomUiColors = {
background: "#ffffff",
foreground: "#0a0a0a",
primary: "#171717",
border: "#e5e5e5",
sidebar: "#fafafa",
};
export const DEFAULT_APP_CUSTOM_UI_COLORS_DARK: AppCustomUiColors = {
background: "#131416",
foreground: "#d7d7db",
primary: "#d0d0d6",
border: "#6e6e72",
sidebar: "#19191c",
};
export interface AppCustomUiColorDef {
key: keyof AppCustomUiColors;
varName: string;
rgbVarName?: string;
labelKey: string;
}
export const APP_CUSTOM_UI_COLOR_DEFS: readonly AppCustomUiColorDef[] = [
{ key: "background", varName: "--background", labelKey: "settings.customUiBackground" },
{ key: "foreground", varName: "--foreground", labelKey: "settings.customUiForeground" },
{ key: "primary", varName: "--primary", rgbVarName: "--dbx-primary-rgb", labelKey: "settings.customUiPrimary" },
{ key: "border", varName: "--border", labelKey: "settings.customUiBorder" },
{ key: "sidebar", varName: "--sidebar", labelKey: "settings.customUiSidebar" },
];
// Every surface and its paired foreground is derived from the chosen colors as
// concrete rgb values (no color-mix dependency, so legacy WebViews work). The
// pairings are the readability contract: text colors are computed for the
// surface they sit on (primary/sidebar-primary foregrounds pick black or white
// by WCAG contrast).
export const APP_CUSTOM_UI_DERIVED_VAR_NAMES: readonly string[] = [
"--card",
"--card-foreground",
"--popover",
"--popover-foreground",
"--secondary",
"--secondary-foreground",
"--muted",
"--muted-foreground",
"--accent",
"--accent-foreground",
"--primary-foreground",
"--sidebar-foreground",
"--sidebar-accent",
"--sidebar-accent-foreground",
"--sidebar-primary",
"--sidebar-primary-foreground",
"--sidebar-border",
"--sidebar-ring",
"--input",
"--ring",
"--dbx-chrome",
"--dbx-chrome-muted",
"--dbx-content",
"--dbx-editor-toolbar",
"--dbx-gutter",
"--dbx-sidebar-header",
];
export function mixHex(a: string, b: string, weight: number): string {
const pa = parseInt(a.slice(1), 16);
const pb = parseInt(b.slice(1), 16);
const ar = (pa >> 16) & 255;
const ag = (pa >> 8) & 255;
const ab = pa & 255;
const br = (pb >> 16) & 255;
const bg2 = (pb >> 8) & 255;
const bb = pb & 255;
const r = Math.round(ar + (br - ar) * weight);
const g = Math.round(ag + (bg2 - ag) * weight);
const bl = Math.round(ab + (bb - ab) * weight);
return `rgb(${r} ${g} ${bl})`;
}
/** Pick white or near-black text for the best contrast on the given background. */
export function readableTextOn(hex: string): string {
return wcagContrastRatio(hex, "#ffffff") >= wcagContrastRatio(hex, "#0a0a0a") ? "#ffffff" : "#0a0a0a";
}
function rgbValue(hex: string): string {
return appCustomUiColorValue(hex).color;
}
export function deriveCustomUiColors(colors: AppCustomUiColors): Record<string, string> {
const { background, foreground, primary, border, sidebar } = colors;
const card = mixHex(background, foreground, 0.03);
const secondary = mixHex(background, foreground, 0.06);
const accent = mixHex(background, foreground, 0.12);
const primaryFg = readableTextOn(primary);
// Chrome surfaces are near-background tints so foreground text stays readable
// even when the user pairs an inverted (dark bg + light fg) custom palette.
const chrome = mixHex(background, foreground, 0.02);
const chromeMuted = mixHex(background, foreground, 0.035);
const toolbar = mixHex(background, foreground, 0.02);
const gutter = mixHex(background, foreground, 0.035);
const sidebarHeader = mixHex(sidebar, foreground, 0.02);
return {
"--card": card,
"--card-foreground": rgbValue(foreground),
"--popover": card,
"--popover-foreground": rgbValue(foreground),
"--secondary": secondary,
"--secondary-foreground": rgbValue(foreground),
"--muted": secondary,
"--muted-foreground": mixHex(foreground, background, 0.58),
"--accent": accent,
"--accent-foreground": rgbValue(foreground),
"--primary-foreground": rgbValue(primaryFg),
"--sidebar-foreground": rgbValue(foreground),
"--sidebar-accent": mixHex(sidebar, foreground, 0.06),
"--sidebar-accent-foreground": rgbValue(foreground),
"--sidebar-primary": rgbValue(primary),
"--sidebar-primary-foreground": rgbValue(primaryFg),
"--sidebar-border": rgbValue(border),
"--sidebar-ring": rgbValue(primary),
"--input": rgbValue(border),
"--ring": rgbValue(primary),
"--dbx-chrome": chrome,
"--dbx-chrome-muted": chromeMuted,
"--dbx-content": rgbValue(background),
"--dbx-editor-toolbar": toolbar,
"--dbx-gutter": gutter,
"--dbx-sidebar-header": sidebarHeader,
};
}
export type AppThemePaletteOption = {
value: AppThemePalette;
labelKey: string;
@@ -30,6 +172,7 @@ export const APP_THEME_PALETTES: AppThemePaletteOption[] = [
{ value: "jetbrains", labelKey: "settings.themePaletteJetbrains", className: "theme-jetbrains", previewColor: "#7b61ff" },
{ value: "cursor", labelKey: "settings.themePaletteCursor", className: "theme-cursor", previewColor: "#6ba4ff" },
{ value: "claude", labelKey: "settings.themePaletteClaude", className: "theme-claude", previewColor: "#c47a50" },
{ value: "custom", labelKey: "settings.themePaletteCustom", className: null, previewColor: "#171717" },
];
export const APP_THEME_PALETTE_CLASS_NAMES = APP_THEME_PALETTES.map((palette) => palette.className).filter((className): className is string => Boolean(className));
@@ -43,10 +186,79 @@ export function normalizeAppThemeMode(value: string | null): AppThemeMode {
}
export function normalizeAppThemePalette(value: string | null): AppThemePalette {
if (value === "mist" || value === "graphite" || value === "cobalt" || value === "sage" || value === "amber" || value === "blush" || value === "vscode" || value === "idea" || value === "xcode" || value === "jetbrains" || value === "cursor" || value === "claude" || value === "pearl") return value;
if (
value === "mist" ||
value === "graphite" ||
value === "cobalt" ||
value === "sage" ||
value === "amber" ||
value === "blush" ||
value === "vscode" ||
value === "idea" ||
value === "xcode" ||
value === "jetbrains" ||
value === "cursor" ||
value === "claude" ||
value === "custom" ||
value === "pearl"
)
return value;
return "pearl";
}
export function normalizeAppCustomUiColors(value: unknown): AppCustomUiColors {
const source = (value && typeof value === "object" ? value : {}) as Record<string, unknown>;
const out = {} as AppCustomUiColors;
for (const key of Object.keys(DEFAULT_APP_CUSTOM_UI_COLORS) as (keyof AppCustomUiColors)[]) {
const candidate = source[key];
out[key] = typeof candidate === "string" && /^#[0-9a-fA-F]{6}$/.test(candidate) ? candidate : DEFAULT_APP_CUSTOM_UI_COLORS[key];
}
return out;
}
export function appCustomUiColorValue(hex: string): { color: string; rgbTriplet: string } {
const value = hex.replace("#", "");
const r = parseInt(value.slice(0, 2), 16);
const g = parseInt(value.slice(2, 4), 16);
const b = parseInt(value.slice(4, 6), 16);
return { color: `rgb(${r} ${g} ${b})`, rgbTriplet: `${r}, ${g}, ${b}` };
}
function rgbChannels(value: string): [number, number, number] {
const trimmed = value.trim();
const rgbMatch = /^rgb\((\d+)\s+(\d+)\s+(\d+)\)$/.exec(trimmed);
if (rgbMatch) return [Number(rgbMatch[1]), Number(rgbMatch[2]), Number(rgbMatch[3])];
const hex = trimmed.replace("#", "");
return [parseInt(hex.slice(0, 2), 16), parseInt(hex.slice(2, 4), 16), parseInt(hex.slice(4, 6), 16)];
}
/** WCAG 2.x relative luminance of a color, 0 (black) .. 1 (white). Accepts #rrggbb or rgb(r g b). */
export function hexRelativeLuminance(color: string): number {
const [r8, g8, b8] = rgbChannels(color);
const toLinear = (channel: number): number => {
const c = channel / 255;
return c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
};
return 0.2126 * toLinear(r8) + 0.7152 * toLinear(g8) + 0.0722 * toLinear(b8);
}
/** WCAG 2.x contrast ratio between two colors, 1 .. 21. Accepts #rrggbb or rgb(r g b). */
export function wcagContrastRatio(a: string, b: string): number {
const lighter = Math.max(hexRelativeLuminance(a), hexRelativeLuminance(b));
const darker = Math.min(hexRelativeLuminance(a), hexRelativeLuminance(b));
return (lighter + 0.05) / (darker + 0.05);
}
/**
* Decides the effective light/dark appearance for a custom UI palette from the
* chosen background's luminance, so surfaces driven by the app palette (such as
* the "Follow app theme" editor) stay readable no matter how dark or light the
* background is.
*/
export function customUiAppearance(colors: AppCustomUiColors): AppThemeAppearance {
return hexRelativeLuminance(colors.background) < 0.2 ? "dark" : "light";
}
export function normalizeAppCornerStyle(value: string | null): AppCornerStyle {
if (value === "none" || value === "small") return value;
return "large";
+28 -1
View File
@@ -1,6 +1,6 @@
import type { Extension } from "@codemirror/state";
import type { EditorTheme, CustomThemeColors } from "@/stores/settingsStore";
import type { AppThemeAppearance, AppThemePalette } from "@/lib/app/appTheme";
import { customUiAppearance, type AppCustomUiColors, type AppThemeAppearance, type AppThemePalette } from "@/lib/app/appTheme";
import { HighlightStyle, syntaxHighlighting } from "@codemirror/language";
import { tags } from "@lezer/highlight";
@@ -658,6 +658,12 @@ export function resolveEditorTheme(theme: EditorTheme, appAppearance: AppThemeAp
return appAppearance === "dark" ? "cursor-dark" : "cursor-light";
case "claude":
return appAppearance === "dark" ? "claude-dark" : "claude-light";
case "custom":
// The custom UI palette routes the follow-app editor to verified,
// self-consistent themes. The caller derives appAppearance from the
// actual custom background luminance, so dark backgrounds pick the
// dark theme and light backgrounds the light theme.
return appAppearance === "dark" ? "one-dark" : "vscode-light";
default:
return appAppearance === "dark" ? "one-dark" : "vscode-light";
}
@@ -665,6 +671,27 @@ export function resolveEditorTheme(theme: EditorTheme, appAppearance: AppThemeAp
return theme;
}
/**
* Effective appearance for the "Follow app theme" editor: with the custom UI
* palette the mode's light/dark flag is replaced by the custom background's
* luminance, keeping tokens, selection, cursor, search matches and diagnostics
* readable on arbitrarily dark/light custom backgrounds. Other palettes keep
* the previous appearance unchanged.
*/
export function editorThemeAppearanceFor(appAppearance: AppThemeAppearance, appPalette: AppThemePalette, customUiColors?: AppCustomUiColors): AppThemeAppearance {
if (appPalette === "custom" && customUiColors) return customUiAppearance(customUiColors);
return appAppearance;
}
/**
* Diagnostic marker colors for the editor surface, chosen from the resolved
* editor appearance so error/warning underlines stay legible on the actual
* editor background instead of using app-level warning/destructive tokens.
*/
export function editorDiagnosticColors(appearance: AppThemeAppearance): { error: string; warning: string } {
return appearance === "dark" ? { error: "#f87171", warning: "#fbbf24" } : { error: "#dc2626", warning: "#b45309" };
}
/** Load a CodeMirror theme extension by theme name. */
export async function loadEditorTheme(theme: EditorTheme, appAppearance: AppThemeAppearance = "dark", customColors?: CustomThemeColors, appPalette: AppThemePalette = "pearl"): Promise<Extension> {
const resolvedTheme = resolveEditorTheme(theme, appAppearance, appPalette);