From e93a17de78ee4326534bc50edb89b75a81df1133 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sat, 13 Jun 2026 23:42:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(webui):=20add=20Catppuccin=20Latte/Frapp?= =?UTF-8?q?=C3=A9/Macchiato/Mocha=20palettes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- packages/core/src/webui/ui-config.ts | 7 ++ packages/webui/index.html | 16 ++- .../src/components/pages/settings-page.tsx | 63 ++++++++-- .../webui/src/components/theme-toggle.tsx | 8 +- packages/webui/src/contexts/ThemeContext.tsx | 115 +++++++++++++++++- packages/webui/src/types.ts | 2 + 6 files changed, 191 insertions(+), 20 deletions(-) diff --git a/packages/core/src/webui/ui-config.ts b/packages/core/src/webui/ui-config.ts index d8bec0c4..2df5f209 100644 --- a/packages/core/src/webui/ui-config.ts +++ b/packages/core/src/webui/ui-config.ts @@ -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(v.accentScope, ['sidebar', 'global'], DEFAULT_APPEARANCE.accentScope), darkIntensity: oneOf(v.darkIntensity, ['soft', 'black'], DEFAULT_APPEARANCE.darkIntensity), + palette: oneOf(v.palette, ['default', 'catppuccin-latte', 'catppuccin-frappe', 'catppuccin-macchiato', 'catppuccin-mocha'], DEFAULT_APPEARANCE.palette), sidebarStyle: oneOf(v.sidebarStyle, ['follow', 'panel', 'accent'], DEFAULT_APPEARANCE.sidebarStyle), background: normalizeBackground(v.background, imageState), fontSans: idOr(v.fontSans, DEFAULT_APPEARANCE.fontSans), diff --git a/packages/webui/index.html b/packages/webui/index.html index 94ed0935..91fc953d 100644 --- a/packages/webui/index.html +++ b/packages/webui/index.html @@ -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'); diff --git a/packages/webui/src/components/pages/settings-page.tsx b/packages/webui/src/components/pages/settings-page.tsx index 0dee1940..6b59aaaf 100644 --- a/packages/webui/src/components/pages/settings-page.tsx +++ b/packages/webui/src/components/pages/settings-page.tsx @@ -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(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({ - value, options, onChange, -}: { value: T; options: Opt[]; onChange: (v: T) => void }) { + value, options, onChange, disabled, +}: { value: T; options: Opt[]; onChange: (v: T) => void; disabled?: boolean }) { const labelledBy = useContext(RowLabelContext); return ( -
+
{options.map((opt) => { const active = value === opt.value; const Icon = opt.icon; @@ -132,9 +139,10 @@ function Segmented({ 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[] = [ 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 (
- - - setAppearance({ mode })} /> + + +
+ {PALETTE_OPTIONS.map((p) => { + const active = a.palette === p.id; + return ( + + ); + })} +
- - setAppearance({ darkIntensity })} /> + + setAppearance({ mode })} disabled={paletteFixed} /> + + + setAppearance({ darkIntensity })} disabled={paletteFixed} />
diff --git a/packages/webui/src/components/theme-toggle.tsx b/packages/webui/src/components/theme-toggle.tsx index 4ba50491..8dda003f 100644 --- a/packages/webui/src/components/theme-toggle.tsx +++ b/packages/webui/src/components/theme-toggle.tsx @@ -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 (
; + +const CATPPUCCIN: Record, 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 // 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(() => { diff --git a/packages/webui/src/types.ts b/packages/webui/src/types.ts index a4a941eb..f391bc7a 100644 --- a/packages/webui/src/types.ts +++ b/packages/webui/src/types.ts @@ -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;