feat(webui): add Catppuccin Latte/Frappé/Macchiato/Mocha palettes

Add a "配色方案" picker offering the four Catppuccin flavors as full color
schemes alongside the default sky theme.

- Schema: new `palette` field on UiAppearance (core + client mirror),
  normalized to a 5-value whitelist defaulting to 'default'.
- Apply: each flavor's base/surface tokens are injected into the shared
  theme stylesheet over :root (Latte) or .dark (the three dark flavors);
  accent (--primary/--ring) stays separate so it composes. A flavor fixes
  light/dark (Latte→light, rest→dark), overriding `mode`, and forces
  dark-intensity to soft so the OLED "black" rule can't fight the palette.
  sidebar-style + high-contrast still compose (higher specificity).
- Pre-paint bootstrap is palette-aware (no light↔dark flash on reload).
- Settings: swatch-preview picker; mode + dark-intensity controls disable
  while a flavor is active. The top-bar ThemeToggle hides then (it would be
  inert under a fixed-scheme palette).
This commit is contained in:
DrSmoothl committed 2026-06-13 23:42:16 +08:00
1 parent de598242ab
commit e93a17de78
6 files changed
+191 -20

No files matched your search

+7
View File
@@ -43,6 +43,9 @@ export type SidebarStyle = 'follow' | 'panel' | 'accent';
export type BackgroundType = 'none' | 'solid' | 'gradient' | 'image';
export type Density = 'cozy' | 'compact';
export type TimeFormat = '12h' | '24h';
/** Full color scheme. 'default' keeps the sky theme (driven by `mode`); the
* Catppuccin flavors are complete palettes that also fix light/dark. */
export type Palette = 'default' | 'catppuccin-latte' | 'catppuccin-frappe' | 'catppuccin-macchiato' | 'catppuccin-mocha';
export interface UiBackground {
type: BackgroundType;
@@ -70,6 +73,8 @@ export interface UiAppearance {
accentCustom: string;
accentScope: AccentScope;
darkIntensity: DarkIntensity;
/** Full color scheme; 'default' = the built-in sky theme. */
palette: Palette;
sidebarStyle: SidebarStyle;
background: UiBackground;
/** Sans font preset id (frontend catalogue). */
@@ -179,6 +184,7 @@ const DEFAULT_APPEARANCE: UiAppearance = {
accentCustom: '#38bdf8',
accentScope: 'global',
darkIntensity: 'soft',
palette: 'default',
sidebarStyle: 'follow',
background: DEFAULT_BACKGROUND,
fontSans: 'default',
@@ -385,6 +391,7 @@ export function normalizeAppearance(value: unknown, imageState: ServerImageState
accentCustom: hexOr(v.accentCustom, DEFAULT_APPEARANCE.accentCustom),
accentScope: oneOf<AccentScope>(v.accentScope, ['sidebar', 'global'], DEFAULT_APPEARANCE.accentScope),
darkIntensity: oneOf<DarkIntensity>(v.darkIntensity, ['soft', 'black'], DEFAULT_APPEARANCE.darkIntensity),
palette: oneOf<Palette>(v.palette, ['default', 'catppuccin-latte', 'catppuccin-frappe', 'catppuccin-macchiato', 'catppuccin-mocha'], DEFAULT_APPEARANCE.palette),
sidebarStyle: oneOf<SidebarStyle>(v.sidebarStyle, ['follow', 'panel', 'accent'], DEFAULT_APPEARANCE.sidebarStyle),
background: normalizeBackground(v.background, imageState),
fontSans: idOr(v.fontSans, DEFAULT_APPEARANCE.fontSans),
+12 -4
View File
@@ -23,15 +23,23 @@
if (!a || typeof a !== 'object') return;
var root = document.documentElement;
var mode = a.mode;
var resolved = mode === 'light' || mode === 'dark'
? mode
: (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
var pal = a.palette;
// A Catppuccin flavor fixes light/dark; Latte = light, the rest dark.
var isCpn = typeof pal === 'string' && pal.indexOf('catppuccin-') === 0;
var resolved = pal === 'catppuccin-latte' ? 'light'
: isCpn ? 'dark'
: (mode === 'light' || mode === 'dark'
? mode
: (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'));
root.classList.remove('light', 'dark');
root.classList.add(resolved);
root.setAttribute('data-theme', resolved);
root.style.colorScheme = resolved;
if (isCpn) root.setAttribute('data-palette', pal);
if (a.density === 'compact' || a.density === 'cozy') root.setAttribute('data-density', a.density);
if (a.darkIntensity === 'black' || a.darkIntensity === 'soft') root.setAttribute('data-dark-intensity', a.darkIntensity);
// Catppuccin forces soft so the OLED "black" rule can't fight the palette.
var intensity = isCpn ? 'soft' : a.darkIntensity;
if (intensity === 'black' || intensity === 'soft') root.setAttribute('data-dark-intensity', intensity);
if (a.sidebarStyle === 'follow' || a.sidebarStyle === 'panel' || a.sidebarStyle === 'accent') root.setAttribute('data-sidebar-style', a.sidebarStyle);
root.setAttribute('data-contrast', a.highContrast ? 'high' : 'normal');
root.setAttribute('data-reduce-motion', a.reduceMotion ? '1' : '0');
@@ -14,6 +14,8 @@ import {
FONT_MONO_OPTIONS,
FONT_SANS_OPTIONS,
GRADIENT_OPTIONS,
PALETTE_OPTIONS,
paletteResolved,
POLL_INTERVAL_OPTIONS,
RADIUS_OPTIONS,
UI_SCALE,
@@ -118,11 +120,16 @@ const RowLabelContext = createContext<string | undefined>(undefined);
// iOS/macOS segmented control: a recessed track with a raised pill on the
// selected segment. Single-select → exposed as a radiogroup. Wraps gracefully.
function Segmented<T extends string | number>({
value, options, onChange,
}: { value: T; options: Opt<T>[]; onChange: (v: T) => void }) {
value, options, onChange, disabled,
}: { value: T; options: Opt<T>[]; onChange: (v: T) => void; disabled?: boolean }) {
const labelledBy = useContext(RowLabelContext);
return (
<div role="radiogroup" aria-labelledby={labelledBy} className="inline-flex flex-wrap items-center gap-1 rounded-lg bg-muted/60 p-1">
<div
role="radiogroup"
aria-labelledby={labelledBy}
aria-disabled={disabled || undefined}
className={cn('inline-flex flex-wrap items-center gap-1 rounded-lg bg-muted/60 p-1', disabled && 'opacity-50')}
>
{options.map((opt) => {
const active = value === opt.value;
const Icon = opt.icon;
@@ -132,9 +139,10 @@ function Segmented<T extends string | number>({
type="button"
role="radio"
aria-checked={active}
disabled={disabled}
onClick={() => onChange(opt.value)}
className={cn(
'inline-flex items-center gap-1.5 rounded-md px-3 py-1.5 text-sm transition-all cursor-pointer outline-none focus-visible:ring-[3px] focus-visible:ring-ring/40',
'inline-flex items-center gap-1.5 rounded-md px-3 py-1.5 text-sm transition-all cursor-pointer outline-none focus-visible:ring-[3px] focus-visible:ring-ring/40 disabled:cursor-not-allowed',
active ? 'bg-card font-semibold text-foreground shadow-sm ring-1 ring-border' : 'font-medium text-muted-foreground hover:text-foreground',
)}
>
@@ -240,15 +248,52 @@ const DENSITY_OPTIONS: Opt<Density>[] = [
function AppearancePanel() {
const { appearance, setAppearance, uploadBackground, removeBackground } = useTheme();
const a = appearance;
// A Catppuccin flavor fixes its own light/dark + darkness, so the mode and
// intensity controls are inert while one is active.
const paletteFixed = paletteResolved(a.palette) !== null;
return (
<div className="flex flex-col gap-5">
<Group title="主题" icon={Sun} description="明暗模式与深色风格。所有改动立即生效并保存到服务器,跨设备同步。">
<SettingRow label="显示模式">
<Segmented value={a.mode} options={MODE_OPTIONS} onChange={(mode) => setAppearance({ mode })} />
<Group title="主题" icon={Sun} description="配色方案、明暗模式与深色风格。所有改动立即生效并保存到服务器,跨设备同步。">
<SettingRow label="配色方案" hint="“默认”跟随下方明暗模式;Catppuccin 为整套配色,会自行决定明暗。" layout="stack">
<div className="flex flex-wrap gap-2.5" role="radiogroup" aria-label="配色方案">
{PALETTE_OPTIONS.map((p) => {
const active = a.palette === p.id;
return (
<button
key={p.id}
type="button"
role="radio"
aria-checked={active}
aria-label={p.label}
onClick={() => setAppearance({ palette: p.id })}
className={cn(
'flex w-[5.5rem] flex-col items-center gap-1.5 rounded-xl border p-1.5 transition-all cursor-pointer outline-none focus-visible:ring-[3px] focus-visible:ring-ring/40',
active ? 'border-primary ring-2 ring-primary/30' : 'border-border hover:border-foreground/30',
)}
>
<span className="flex h-11 w-full items-center justify-center rounded-lg" style={{ backgroundColor: p.preview.bg }}>
<span className="flex gap-1 rounded-md px-1.5 py-1 shadow-sm" style={{ backgroundColor: p.preview.surface }}>
{p.preview.dots.map((d, i) => (
<span key={i} className="size-1.5 rounded-full" style={{ backgroundColor: d }} />
))}
</span>
</span>
<span className="text-[11px] font-medium">{p.label}</span>
</button>
);
})}
</div>
</SettingRow>
<SettingRow label="深色强度" hint="“纯黑”将深色背景压到接近纯黑,适合 OLED 屏幕(仅深色模式下可见)。" layout="stack">
<Segmented value={a.darkIntensity} options={DARK_INTENSITY_OPTIONS} onChange={(darkIntensity) => setAppearance({ darkIntensity })} />
<SettingRow label="显示模式" hint={paletteFixed ? '当前由 Catppuccin 配色决定明暗。' : undefined}>
<Segmented value={a.mode} options={MODE_OPTIONS} onChange={(mode) => setAppearance({ mode })} disabled={paletteFixed} />
</SettingRow>
<SettingRow
label="深色强度"
hint={paletteFixed ? 'Catppuccin 配色自带明暗,无需调节。' : '“纯黑”将深色背景压到接近纯黑,适合 OLED 屏幕(仅深色模式下可见)。'}
layout="stack"
>
<Segmented value={a.darkIntensity} options={DARK_INTENSITY_OPTIONS} onChange={(darkIntensity) => setAppearance({ darkIntensity })} disabled={paletteFixed} />
</SettingRow>
</Group>
@@ -1,6 +1,6 @@
import { Monitor, Moon, Sun } from 'lucide-react';
import { motion } from 'motion/react';
import { useTheme, type ThemeMode } from '@/contexts/ThemeContext';
import { paletteResolved, useTheme, type ThemeMode } from '@/contexts/ThemeContext';
import { cn } from '@/lib/utils';
const OPTIONS: { mode: ThemeMode; icon: typeof Sun; label: string }[] = [
@@ -10,7 +10,11 @@ const OPTIONS: { mode: ThemeMode; icon: typeof Sun; label: string }[] = [
];
export function ThemeToggle({ className }: { className?: string }) {
const { mode, setMode } = useTheme();
const { appearance, mode, setMode } = useTheme();
// A Catppuccin flavor fixes light/dark, so this quick toggle would be inert
// (and its pill would desync from the forced scheme) — hide it. Light/dark is
// then changed by switching the palette back to “默认” in settings.
if (paletteResolved(appearance.palette)) return null;
return (
<div
className={cn(
+110 -5
View File
@@ -1,11 +1,11 @@
import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from 'react';
import { MotionConfig } from 'motion/react';
import type { ThemeMode, UiAppearance, UiBackground } from '@/types';
import type { Palette, ThemeMode, UiAppearance, UiBackground } from '@/types';
// Re-export the appearance value types so consumers (settings page etc.) can
// import them straight from the context module.
export type {
AccentMode, AccentScope, BackgroundType, DarkIntensity, Density, SidebarStyle, ThemeMode, TimeFormat, UiAppearance,
AccentMode, AccentScope, BackgroundType, DarkIntensity, Density, Palette, SidebarStyle, ThemeMode, TimeFormat, UiAppearance,
} from '@/types';
/** A partial appearance update; `background` may itself be a partial patch. */
@@ -97,6 +97,7 @@ export const DEFAULT_APPEARANCE: UiAppearance = {
accentCustom: '#38bdf8',
accentScope: 'global',
darkIntensity: 'soft',
palette: 'default',
sidebarStyle: 'follow',
background: { type: 'none', color: '#0ea5e9', gradient: 'none', imageOpacity: 0.15, imageBlur: 0, hasImage: false, imageMime: '', imageVersion: 0 },
fontSans: 'default',
@@ -187,6 +188,102 @@ function accentVarsCss(a: UiAppearance): string {
return `:root{${light}}\n.dark{${dark}}`;
}
// ─── Catppuccin palettes (full surface/base token sets) ────────────────────
// Official flavor hexes mapped onto our token names. Accent (--primary/--ring)
// is intentionally left to the accent system so the two compose. Latte is the
// light flavor; the other three are dark. Applied via the injected theme
// stylesheet over the base :root/.dark (see applyAppearance), so darkIntensity
// is forced off for these (their darkness is part of the palette).
type PaletteVars = Record<string, string>;
const CATPPUCCIN: Record<Exclude<Palette, 'default'>, PaletteVars> = {
'catppuccin-latte': {
'--background': '#eff1f5', '--foreground': '#4c4f69',
'--card': '#ffffff', '--card-foreground': '#4c4f69',
'--popover': '#ffffff', '--popover-foreground': '#4c4f69',
'--secondary': '#e6e9ef', '--secondary-foreground': '#4c4f69',
'--muted': '#e6e9ef', '--muted-foreground': '#6c6f85',
'--accent': '#dce0e8', '--accent-foreground': '#4c4f69',
'--destructive': '#d20f39', '--destructive-foreground': '#ffffff',
'--success': '#40a02b', '--warning': '#df8e1d',
'--border': '#ccd0da', '--input': '#ccd0da',
'--sidebar': '#e6e9ef', '--sidebar-foreground': '#4c4f69',
'--sidebar-accent': '#dce0e8', '--sidebar-accent-foreground': '#4c4f69', '--sidebar-border': '#ccd0da',
},
'catppuccin-frappe': {
'--background': '#303446', '--foreground': '#c6d0f5',
'--card': '#414559', '--card-foreground': '#c6d0f5',
'--popover': '#414559', '--popover-foreground': '#c6d0f5',
'--secondary': '#414559', '--secondary-foreground': '#c6d0f5',
'--muted': '#414559', '--muted-foreground': '#a5adce',
'--accent': '#51576d', '--accent-foreground': '#c6d0f5',
'--destructive': '#e78284', '--destructive-foreground': '#232634',
'--success': '#a6d189', '--warning': '#e5c890',
'--border': '#51576d', '--input': '#51576d',
'--sidebar': '#292c3c', '--sidebar-foreground': '#c6d0f5',
'--sidebar-accent': '#414559', '--sidebar-accent-foreground': '#c6d0f5', '--sidebar-border': '#414559',
},
'catppuccin-macchiato': {
'--background': '#24273a', '--foreground': '#cad3f5',
'--card': '#363a4f', '--card-foreground': '#cad3f5',
'--popover': '#363a4f', '--popover-foreground': '#cad3f5',
'--secondary': '#363a4f', '--secondary-foreground': '#cad3f5',
'--muted': '#363a4f', '--muted-foreground': '#a5adcb',
'--accent': '#494d64', '--accent-foreground': '#cad3f5',
'--destructive': '#ed8796', '--destructive-foreground': '#181926',
'--success': '#a6da95', '--warning': '#eed49f',
'--border': '#494d64', '--input': '#494d64',
'--sidebar': '#1e2030', '--sidebar-foreground': '#cad3f5',
'--sidebar-accent': '#363a4f', '--sidebar-accent-foreground': '#cad3f5', '--sidebar-border': '#363a4f',
},
'catppuccin-mocha': {
'--background': '#1e1e2e', '--foreground': '#cdd6f4',
'--card': '#313244', '--card-foreground': '#cdd6f4',
'--popover': '#313244', '--popover-foreground': '#cdd6f4',
'--secondary': '#313244', '--secondary-foreground': '#cdd6f4',
'--muted': '#313244', '--muted-foreground': '#a6adc8',
'--accent': '#45475a', '--accent-foreground': '#cdd6f4',
'--destructive': '#f38ba8', '--destructive-foreground': '#11111b',
'--success': '#a6e3a1', '--warning': '#f9e2af',
'--border': '#45475a', '--input': '#45475a',
'--sidebar': '#181825', '--sidebar-foreground': '#cdd6f4',
'--sidebar-accent': '#313244', '--sidebar-accent-foreground': '#cdd6f4', '--sidebar-border': '#313244',
},
};
/** A Catppuccin flavor is light (Latte) or dark (the rest). */
export function paletteResolved(p: Palette): 'light' | 'dark' | null {
if (p === 'catppuccin-latte') return 'light';
if (p.startsWith('catppuccin-')) return 'dark';
return null;
}
export interface PaletteSpec {
id: Palette;
label: string;
/** Picker preview: surface tile + three signature accent dots. */
preview: { bg: string; surface: string; dots: [string, string, string] };
}
export const PALETTE_OPTIONS: PaletteSpec[] = [
{ id: 'default', label: '默认', preview: { bg: '#f1f5f9', surface: '#ffffff', dots: ['#0ea5e9', '#10b981', '#f43f5e'] } },
{ id: 'catppuccin-latte', label: 'Latte', preview: { bg: '#eff1f5', surface: '#ffffff', dots: ['#8839ef', '#40a02b', '#d20f39'] } },
{ id: 'catppuccin-frappe', label: 'Frappé', preview: { bg: '#303446', surface: '#414559', dots: ['#ca9ee6', '#a6d189', '#e78284'] } },
{ id: 'catppuccin-macchiato', label: 'Macchiato', preview: { bg: '#24273a', surface: '#363a4f', dots: ['#c6a0f6', '#a6da95', '#ed8796'] } },
{ id: 'catppuccin-mocha', label: 'Mocha', preview: { bg: '#1e1e2e', surface: '#313244', dots: ['#cba6f7', '#a6e3a1', '#f38ba8'] } },
];
function paletteVarsCss(a: UiAppearance): string {
if (a.palette === 'default') return '';
const vars = CATPPUCCIN[a.palette];
if (!vars) return '';
const body = Object.entries(vars).map(([k, v]) => `${k}:${v};`).join('');
// Latte is light → :root; the dark flavors → .dark (resolved is forced to
// match in the provider, so the selector always matches the active scheme).
const selector = a.palette === 'catppuccin-latte' ? ':root' : '.dark';
return `${selector}{${body}}`;
}
function fontStack(options: FontSpec[], id: string): string {
return (options.find((f) => f.id === id) ?? options[0]).stack;
}
@@ -200,7 +297,11 @@ function applyAppearance(a: UiAppearance, resolved: 'light' | 'dark'): void {
root.style.colorScheme = resolved;
root.setAttribute('data-density', a.density);
root.setAttribute('data-dark-intensity', a.darkIntensity);
// A Catppuccin flavor owns its own darkness, so the OLED "black" override
// must not fight it — force a neutral intensity (it has no CSS rule) so the
// injected palette wins over base .dark.
root.setAttribute('data-dark-intensity', a.palette === 'default' ? a.darkIntensity : 'soft');
root.setAttribute('data-palette', a.palette);
root.setAttribute('data-sidebar-style', a.sidebarStyle);
root.setAttribute('data-contrast', a.highContrast ? 'high' : 'normal');
root.setAttribute('data-reduce-motion', a.reduceMotion ? '1' : '0');
@@ -223,7 +324,9 @@ function applyAppearance(a: UiAppearance, resolved: 'light' | 'dark'): void {
el.id = styleId;
document.head.appendChild(el);
}
el.textContent = accentVarsCss(a);
// Palette (base/surface tokens) first, then accent (--primary/--ring) — a
// disjoint var set, layered after so it composes with any flavor.
el.textContent = `${paletteVarsCss(a)}\n${accentVarsCss(a)}`;
// Custom CSS — appended AFTER the accent block so it is the last style in
// <head> and can override anything. Skipped under ?safe-mode=1 (escape
@@ -446,7 +549,9 @@ export function ThemeProvider({ children }: { children: ReactNode }) {
// the mutators themselves so consecutive calls in one tick still compose.
const appearanceRef = useRef(appearance);
const resolved: 'light' | 'dark' = appearance.mode === 'system' ? systemTheme : appearance.mode;
// A Catppuccin flavor fixes light/dark; otherwise honor mode (+ system).
const resolved: 'light' | 'dark' =
paletteResolved(appearance.palette) ?? (appearance.mode === 'system' ? systemTheme : appearance.mode);
// Track the OS theme for `mode: 'system'`.
useEffect(() => {
+2
View File
@@ -171,6 +171,7 @@ export type SidebarStyle = 'follow' | 'panel' | 'accent';
export type BackgroundType = 'none' | 'solid' | 'gradient' | 'image';
export type Density = 'cozy' | 'compact';
export type TimeFormat = '12h' | '24h';
export type Palette = 'default' | 'catppuccin-latte' | 'catppuccin-frappe' | 'catppuccin-macchiato' | 'catppuccin-mocha';
export interface UiBackground {
type: BackgroundType;
@@ -192,6 +193,7 @@ export interface UiAppearance {
accentCustom: string;
accentScope: AccentScope;
darkIntensity: DarkIntensity;
palette: Palette;
sidebarStyle: SidebarStyle;
background: UiBackground;
fontSans: string;