diff --git a/packages/core/src/webui/ui-config.ts b/packages/core/src/webui/ui-config.ts index 56b72d89..bea79ba0 100644 --- a/packages/core/src/webui/ui-config.ts +++ b/packages/core/src/webui/ui-config.ts @@ -303,16 +303,20 @@ export function normalizeAppearance(value: unknown, imageState: ServerImageState }; } +export function normalizeLayout(value: unknown): UiLayout { + const layout = isObject(value) ? value : {}; + return { + overviewBlocks: normalizeLayoutItems(layout.overviewBlocks, DEFAULT_OVERVIEW_BLOCKS), + navItems: normalizeLayoutItems(layout.navItems, DEFAULT_NAV_ITEMS), + }; +} + export function normalizeUiConfig(value: unknown, imageState: ServerImageState = DEFAULT_IMAGE_STATE): UiConfig { const v = isObject(value) ? value : {}; - const layout = isObject(v.layout) ? v.layout : {}; return { version: UI_CONFIG_VERSION, appearance: normalizeAppearance(v.appearance, imageState), - layout: { - overviewBlocks: normalizeLayoutItems(layout.overviewBlocks, DEFAULT_OVERVIEW_BLOCKS), - navItems: normalizeLayoutItems(layout.navItems, DEFAULT_NAV_ITEMS), - }, + layout: normalizeLayout(v.layout), }; } @@ -379,9 +383,19 @@ export function loadUiConfig(): UiConfig { */ export function saveUiConfig(incoming: unknown): UiConfig { const current = loadUiConfig(); - // On save, the current on-disk state is the truth for the server-managed - // image fields — a client payload cannot forge `hasImage` etc. - const next = normalizeUiConfig(incoming, current.appearance.background); + const v = isObject(incoming) ? incoming : {}; + // Section-level merge: a payload may carry just `appearance` (the theme + // editor) or just `layout` (the layout editor) without clobbering the + // other. A missing/non-object section keeps the current on-disk value. + // The current on-disk state is the truth for the server-managed image + // fields — a client payload cannot forge `hasImage` etc. + const next: UiConfig = { + version: UI_CONFIG_VERSION, + appearance: isObject(v.appearance) + ? normalizeAppearance(v.appearance, current.appearance.background) + : current.appearance, + layout: isObject(v.layout) ? normalizeLayout(v.layout) : current.layout, + }; atomicWrite(next); cached = next; return next; diff --git a/packages/webui/index.html b/packages/webui/index.html index b9498433..94ed0935 100644 --- a/packages/webui/index.html +++ b/packages/webui/index.html @@ -10,6 +10,36 @@ SnowLuma 控制台 + + diff --git a/packages/webui/src/components/layout/main-layout.tsx b/packages/webui/src/components/layout/main-layout.tsx index 22e71261..0006f9dc 100644 --- a/packages/webui/src/components/layout/main-layout.tsx +++ b/packages/webui/src/components/layout/main-layout.tsx @@ -6,6 +6,7 @@ import { ScrollArea } from '@/components/ui/scroll-area'; import { Sidebar } from '@/components/layout/sidebar'; import { TopBar } from '@/components/layout/top-bar'; import { useMediaQuery } from '@/hooks/use-media-query'; +import { useTheme } from '@/contexts/ThemeContext'; import { cn } from '@/lib/utils'; interface MainLayoutProps { @@ -16,9 +17,16 @@ interface MainLayoutProps { export function MainLayout({ status, onLogout, children }: MainLayoutProps) { const isMobile = !useMediaQuery('(min-width: 768px)'); + const { appearance } = useTheme(); const [collapsed, setCollapsed] = useState(() => { - return localStorage.getItem('snowluma_sidebar_collapsed') === '1'; + // An explicit per-browser toggle wins; otherwise fall back to the + // operator's `sidebarDefaultCollapsed` appearance preference. + const explicit = localStorage.getItem('snowluma_sidebar_collapsed'); + if (explicit === '1') return true; + if (explicit === '0') return false; + return appearance.sidebarDefaultCollapsed; }); + const customBg = appearance.background.type !== 'none'; const [mobileOpen, setMobileOpen] = useState(false); const pathname = useRouterState({ select: (s) => s.location.pathname }); @@ -27,7 +35,7 @@ export function MainLayout({ status, onLogout, children }: MainLayoutProps) { }, [collapsed]); return ( -
+
{/* Desktop sidebar */} {!isMobile && ( +
{/* Sky gradient backdrop */}
= { trace: 'text-muted-foreground/60', @@ -22,15 +23,9 @@ const levelClass: Record = { const LEVELS: LogLevel[] = ['trace', 'debug', 'info', 'success', 'warn', 'error']; -/** Show a locale time when the backend sends an ISO timestamp (it does); - * fall back to the raw string otherwise so we never render "Invalid Date". */ -function formatTime(t: string): string { - const d = new Date(t); - return Number.isNaN(d.getTime()) ? t : d.toLocaleTimeString(); -} - export function LogsPage() { const api = useApi(); + const { formatClock } = useTheme(); const [logs, setLogs] = useState([]); const [streamStatus, setStreamStatus] = useState('连接中'); const [paused, setPaused] = useState(false); @@ -220,7 +215,7 @@ export function LogsPage() { the full width instead of a ~40px column that breaks every few characters. On sm+ it's the dense single row. */}
- {formatTime(log.time)} + {formatClock(log.time)} {log.level.toUpperCase()} diff --git a/packages/webui/src/components/pages/overview-page.tsx b/packages/webui/src/components/pages/overview-page.tsx index e0da68bc..ba3e1718 100644 --- a/packages/webui/src/components/pages/overview-page.tsx +++ b/packages/webui/src/components/pages/overview-page.tsx @@ -26,6 +26,7 @@ import type { AccountConnections, AdapterStatus, AdapterStatusLevel, LogEntry } import { useApi } from '@/lib/api'; import { useAppState } from '@/contexts/AppStateContext'; import { useSession } from '@/contexts/SessionContext'; +import { useTheme } from '@/contexts/ThemeContext'; function qqAvatarUrl(uin: string) { return `/avatar/${encodeURIComponent(uin)}`; @@ -390,13 +391,9 @@ function ConnectionsCard({ connections }: { connections: AccountConnections[] }) // ─────────────── recent alerts ─────────────── -function alertClock(t: string): string { - const d = new Date(t); - return Number.isNaN(d.getTime()) ? t : d.toLocaleTimeString(); -} - function RecentAlertsCard() { const api = useApi(); + const { formatClock } = useTheme(); const [alerts, setAlerts] = useState([]); useEffect(() => { @@ -446,7 +443,7 @@ function RecentAlertsCard() {
{alerts.map((a) => (
- {alertClock(a.time)} + {formatClock(a.time)} {a.level.toUpperCase()} diff --git a/packages/webui/src/components/pages/settings-page.tsx b/packages/webui/src/components/pages/settings-page.tsx index 0aba09b3..18b31f5b 100644 --- a/packages/webui/src/components/pages/settings-page.tsx +++ b/packages/webui/src/components/pages/settings-page.tsx @@ -1,39 +1,41 @@ -import { useState, type ReactNode } from 'react'; +import { useRef, useState, type ReactNode } from 'react'; import { motion } from 'motion/react'; -import { Bug, Check, Download, ExternalLink, Github, Info, KeyRound, Loader2, Monitor, Moon, Palette, RefreshCw, ShieldCheck, Sparkles, Star, Sun, Tag } from 'lucide-react'; +import { + Accessibility, Bug, Check, Clock, Download, ExternalLink, Github, Image as ImageIcon, + Info, KeyRound, Loader2, Monitor, Moon, Palette, RefreshCw, ShieldCheck, + Sparkles, Star, Sun, Tag, Upload, Trash2, +} from 'lucide-react'; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Label } from '@/components/ui/label'; import { Separator } from '@/components/ui/separator'; +import { ToggleSwitch } from '@/components/ui/toggle-switch'; import { ACCENTS, + FONT_MONO_OPTIONS, + FONT_SANS_OPTIONS, + GRADIENT_OPTIONS, POLL_INTERVAL_OPTIONS, RADIUS_OPTIONS, + UI_SCALE, useTheme, - type AccentColor, + type AccentScope, + type BackgroundType, + type DarkIntensity, type Density, + type SidebarStyle, type ThemeMode, + type TimeFormat, } from '@/contexts/ThemeContext'; import { ChangePasswordDialog } from '@/components/change-password-dialog'; import { useAppState } from '@/contexts/AppStateContext'; import { cn } from '@/lib/utils'; -const MODE_OPTIONS: { value: ThemeMode; label: string; icon: typeof Sun }[] = [ - { value: 'light', label: '浅色', icon: Sun }, - { value: 'dark', label: '深色', icon: Moon }, - { value: 'system', label: '跟随系统', icon: Monitor }, -]; - -const DENSITY_OPTIONS: { value: Density; label: string; description: string }[] = [ - { value: 'cozy', label: '舒适', description: '默认间距,阅读更轻松' }, - { value: 'compact', label: '紧凑', description: '更小的字号与行距,单屏放更多内容' }, -]; - type SettingsTab = 'appearance' | 'data' | 'account' | 'about'; const TABS: { key: SettingsTab; label: string; icon: typeof Sun }[] = [ { key: 'appearance', label: '外观', icon: Palette }, - { key: 'data', label: '数据刷新', icon: RefreshCw }, + { key: 'data', label: '数据与格式', icon: RefreshCw }, { key: 'account', label: '账号安全', icon: ShieldCheck }, { key: 'about', label: '关于', icon: Info }, ]; @@ -89,178 +91,454 @@ function MiniTabs({ tab, onChange }: { tab: SettingsTab; onChange: (t: SettingsT ); } +// ─────────────── shared little controls ─────────────── + +interface Opt { value: T; label: string; icon?: typeof Sun } + +function Segmented({ + value, options, onChange, +}: { value: T; options: Opt[]; onChange: (v: T) => void }) { + return ( +
+ {options.map((opt) => { + const active = value === opt.value; + const Icon = opt.icon; + return ( + + ); + })} +
+ ); +} + +function Field({ label, hint, children }: { label: string; hint?: string; children: ReactNode }) { + return ( +
+
+ + {hint &&

{hint}

} +
+ {children} +
+ ); +} + +function ToggleRow({ label, hint, value, onChange }: { label: string; hint?: string; value: boolean; onChange: (v: boolean) => void }) { + return ( +
+
+

{label}

+ {hint &&

{hint}

} +
+ +
+ ); +} + // ─────────────── 外观 ─────────────── +const MODE_OPTIONS: Opt[] = [ + { value: 'light', label: '浅色', icon: Sun }, + { value: 'dark', label: '深色', icon: Moon }, + { value: 'system', label: '跟随系统', icon: Monitor }, +]; + +const DARK_INTENSITY_OPTIONS: Opt[] = [ + { value: 'soft', label: '柔和' }, + { value: 'black', label: '纯黑 (OLED)' }, +]; + +const SCOPE_OPTIONS: Opt[] = [ + { value: 'global', label: '全局' }, + { value: 'sidebar', label: '仅侧栏' }, +]; + +const SIDEBAR_STYLE_OPTIONS: Opt[] = [ + { value: 'follow', label: '跟随背景' }, + { value: 'panel', label: '浅色面板' }, + { value: 'accent', label: '强调色' }, +]; + +const BG_TYPE_OPTIONS: Opt[] = [ + { value: 'none', label: '无' }, + { value: 'solid', label: '纯色' }, + { value: 'gradient', label: '渐变' }, + { value: 'image', label: '图片' }, +]; + +const DENSITY_OPTIONS: Opt[] = [ + { value: 'cozy', label: '舒适' }, + { value: 'compact', label: '紧凑' }, +]; + function AppearancePanel() { - const { mode, setMode, accent, setAccent, radius, setRadius, density, setDensity } = useTheme(); + const { appearance, setAppearance, uploadBackground, removeBackground } = useTheme(); + const a = appearance; + + return ( +
+ {/* 主题 */} + + + 主题 + 明暗模式与深色风格。所有改动立即生效并保存到服务器,跨设备同步。 + + + + setAppearance({ mode })} /> + + + + setAppearance({ darkIntensity })} /> + + + + + {/* 强调色 */} + + + 强调色 + 按钮、链接与高亮使用的主色调。 + + + +
+ {ACCENTS.map((spec) => { + const active = a.accentMode === 'preset' && a.accentPreset === spec.id; + return ( + + ); + })} + {/* custom colour */} + +
+
+ + + setAppearance({ accentScope })} /> + + + + setAppearance({ sidebarStyle })} /> + +
+
+ + {/* 背景 */} + + + {/* 排版与界面 */} + + + 排版与界面 + 字体、圆角、缩放与密度。 + + +
+ + ({ value: f.id, label: f.label }))} onChange={(fontSans) => setAppearance({ fontSans })} /> + + + ({ value: f.id, label: f.label }))} onChange={(fontMono) => setAppearance({ fontMono })} /> + +
+ + + ({ value: r.value, label: `${r.label}(${r.value}rem)` }))} + onChange={(radius) => setAppearance({ radius })} + /> + + + +
+ {Math.round(UI_SCALE.min * 100)}% + setAppearance({ uiScale: Number(e.target.value) })} + className="h-2 flex-1 cursor-pointer accent-primary" + aria-label="界面缩放" + /> + {Math.round(UI_SCALE.max * 100)}% +
+
+ + + setAppearance({ density })} /> + +
+
+ + {/* 无障碍与侧栏 */} + + + 无障碍与侧栏 + 动效、对比度与侧栏默认状态。 + + + setAppearance({ reduceMotion })} + /> + + setAppearance({ highContrast })} + /> + + setAppearance({ sidebarDefaultCollapsed })} + /> + + +
+ ); +} + +function BackgroundCard({ + appearance: a, setAppearance, uploadBackground, removeBackground, +}: { + appearance: ReturnType['appearance']; + setAppearance: ReturnType['setAppearance']; + uploadBackground: ReturnType['uploadBackground']; + removeBackground: ReturnType['removeBackground']; +}) { + const fileRef = useRef(null); + const [uploading, setUploading] = useState(false); + const [busy, setBusy] = useState(false); + const [err, setErr] = useState(null); + + const onFile = async (e: React.ChangeEvent) => { + const file = e.target.files?.[0]; + if (fileRef.current) fileRef.current.value = ''; + if (!file) return; + setUploading(true); + setErr(null); + try { + await uploadBackground(file); + } catch (ex) { + setErr(ex instanceof Error ? ex.message : '上传失败'); + } finally { + setUploading(false); + } + }; + + const onRemove = async () => { + setBusy(true); + setErr(null); + try { + await removeBackground(); + } catch { + setErr('删除失败'); + } finally { + setBusy(false); + } + }; + return ( - 外观 - 调整界面的明暗模式、强调色、圆角与密度。所有改动会立即应用并保存到本地。 + 背景 + 纯色、渐变或自定义壁纸,会显示在卡片之间与登录页。 - {/* Mode */} -
- -
- {MODE_OPTIONS.map((opt) => { - const Icon = opt.icon; - const active = mode === opt.value; - return ( - - ); - })} + + setAppearance({ background: { type } })} /> + + + {a.background.type === 'solid' && ( + +
+ + {a.background.color} +
+
+ )} + + {a.background.type === 'gradient' && ( + +
+ {GRADIENT_OPTIONS.map((g) => { + const active = a.background.gradient === g.id; + return ( +
+
+ )} + + {a.background.type === 'image' && ( +
+ +
+ + + {a.background.hasImage && ( + + )} +
+
+ + {a.background.hasImage && ( + <> +
+ 背景预览 +
+ + setAppearance({ background: { imageOpacity: Number(e.target.value) } })} + className="h-2 w-full cursor-pointer accent-primary" + aria-label="遮罩强度" + /> + + + setAppearance({ background: { imageBlur: Number(e.target.value) } })} + className="h-2 w-full cursor-pointer accent-primary" + aria-label="背景模糊" + /> + + + )}
-
+ )} - - - {/* Accent color */} -
- -
- {ACCENTS.map((spec) => { - const active = accent === spec.id; - return ( - - ); - })} -
-

当前:{ACCENTS.find((a) => a.id === accent)?.label}

-
- - - - {/* Radius */} -
- -
- {RADIUS_OPTIONS.map((opt) => { - const active = Math.abs(radius - opt.value) < 1e-6; - return ( - - ); - })} -
-
- - - - {/* Density */} -
- -
- {DENSITY_OPTIONS.map((opt) => { - const active = density === opt.value; - return ( - - ); - })} -
-
+ {err &&

{err}

} ); } -// ─────────────── 数据刷新 ─────────────── +// ─────────────── 数据与格式 ─────────────── + +const TIME_FORMAT_OPTIONS: Opt[] = [ + { value: '24h', label: '24 小时制' }, + { value: '12h', label: '12 小时制 (AM/PM)' }, +]; function DataPanel() { - const { pollInterval, setPollInterval } = useTheme(); + const { appearance, setAppearance } = useTheme(); return ( - - - 数据刷新 - 控制总览页轮询刷新 QQ 列表、进程列表与系统状态的间隔。 - - -
- -
- {POLL_INTERVAL_OPTIONS.map((opt) => { - const active = pollInterval === opt.value; - return ( - - ); - })} -
-

- 当前生效值:{pollInterval === 0 ? '已暂停轮询' : `${pollInterval} 毫秒`} -

-
-
-
+
+ + + 数据刷新 + 总览页轮询刷新 QQ 列表、进程列表与系统状态的间隔。 + + + + ({ value: o.value, label: o.label }))} + onChange={(pollInterval) => setAppearance({ pollInterval })} + /> + + + + + + + 时间格式 + 日志与告警等时间戳的显示方式。 + + + + setAppearance({ timeFormat })} /> + + + +
); } diff --git a/packages/webui/src/contexts/ThemeContext.tsx b/packages/webui/src/contexts/ThemeContext.tsx index 2d984519..44331434 100644 --- a/packages/webui/src/contexts/ThemeContext.tsx +++ b/packages/webui/src/contexts/ThemeContext.tsx @@ -1,40 +1,50 @@ -import { createContext, useContext, useEffect, useMemo, useState, type ReactNode } from 'react'; +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'; -export type ThemeMode = 'light' | 'dark' | 'system'; +// 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, +} from '@/types'; + +/** A partial appearance update; `background` may itself be a partial patch. */ +export type AppearancePatch = Partial> & { background?: Partial }; + +// ─── Frontend catalogues (the server stores only ids; the visuals live here) ─── export type AccentColor = 'sky' | 'blue' | 'violet' | 'rose' | 'emerald' | 'amber' | 'orange'; -export type Density = 'cozy' | 'compact'; export interface AccentSpec { id: AccentColor; label: string; swatch: string; - light: { primary: string; ring: string; sidebarPrimary: string; sidebarRing: string }; - dark: { primary: string; ring: string; sidebarPrimary: string; sidebarRing: string }; + light: { primary: string; ring: string }; + dark: { primary: string; ring: string }; } export const ACCENTS: AccentSpec[] = [ { id: 'sky', label: '天蓝', swatch: '#38bdf8', - light: { primary: 'oklch(68.5% 0.155 230)', ring: 'oklch(68.5% 0.155 230)', sidebarPrimary: 'oklch(68.5% 0.155 230)', sidebarRing: 'oklch(68.5% 0.155 230)' }, - dark: { primary: 'oklch(75% 0.14 230)', ring: 'oklch(75% 0.14 230)', sidebarPrimary: 'oklch(75% 0.14 230)', sidebarRing: 'oklch(75% 0.14 230)' } }, + light: { primary: 'oklch(68.5% 0.155 230)', ring: 'oklch(68.5% 0.155 230)' }, + dark: { primary: 'oklch(75% 0.14 230)', ring: 'oklch(75% 0.14 230)' } }, { id: 'blue', label: '靛蓝', swatch: '#3b82f6', - light: { primary: 'oklch(60% 0.18 258)', ring: 'oklch(60% 0.18 258)', sidebarPrimary: 'oklch(60% 0.18 258)', sidebarRing: 'oklch(60% 0.18 258)' }, - dark: { primary: 'oklch(70% 0.16 258)', ring: 'oklch(70% 0.16 258)', sidebarPrimary: 'oklch(70% 0.16 258)', sidebarRing: 'oklch(70% 0.16 258)' } }, + light: { primary: 'oklch(60% 0.18 258)', ring: 'oklch(60% 0.18 258)' }, + dark: { primary: 'oklch(70% 0.16 258)', ring: 'oklch(70% 0.16 258)' } }, { id: 'violet', label: '紫罗兰', swatch: '#8b5cf6', - light: { primary: 'oklch(60% 0.2 290)', ring: 'oklch(60% 0.2 290)', sidebarPrimary: 'oklch(60% 0.2 290)', sidebarRing: 'oklch(60% 0.2 290)' }, - dark: { primary: 'oklch(72% 0.17 290)', ring: 'oklch(72% 0.17 290)', sidebarPrimary: 'oklch(72% 0.17 290)', sidebarRing: 'oklch(72% 0.17 290)' } }, + light: { primary: 'oklch(60% 0.2 290)', ring: 'oklch(60% 0.2 290)' }, + dark: { primary: 'oklch(72% 0.17 290)', ring: 'oklch(72% 0.17 290)' } }, { id: 'rose', label: '玫瑰', swatch: '#f43f5e', - light: { primary: 'oklch(63% 0.21 18)', ring: 'oklch(63% 0.21 18)', sidebarPrimary: 'oklch(63% 0.21 18)', sidebarRing: 'oklch(63% 0.21 18)' }, - dark: { primary: 'oklch(72% 0.18 18)', ring: 'oklch(72% 0.18 18)', sidebarPrimary: 'oklch(72% 0.18 18)', sidebarRing: 'oklch(72% 0.18 18)' } }, + light: { primary: 'oklch(63% 0.21 18)', ring: 'oklch(63% 0.21 18)' }, + dark: { primary: 'oklch(72% 0.18 18)', ring: 'oklch(72% 0.18 18)' } }, { id: 'emerald', label: '翡翠', swatch: '#10b981', - light: { primary: 'oklch(64% 0.16 162)', ring: 'oklch(64% 0.16 162)', sidebarPrimary: 'oklch(64% 0.16 162)', sidebarRing: 'oklch(64% 0.16 162)' }, - dark: { primary: 'oklch(74% 0.15 162)', ring: 'oklch(74% 0.15 162)', sidebarPrimary: 'oklch(74% 0.15 162)', sidebarRing: 'oklch(74% 0.15 162)' } }, + light: { primary: 'oklch(64% 0.16 162)', ring: 'oklch(64% 0.16 162)' }, + dark: { primary: 'oklch(74% 0.15 162)', ring: 'oklch(74% 0.15 162)' } }, { id: 'amber', label: '琥珀', swatch: '#f59e0b', - light: { primary: 'oklch(72% 0.17 70)', ring: 'oklch(72% 0.17 70)', sidebarPrimary: 'oklch(72% 0.17 70)', sidebarRing: 'oklch(72% 0.17 70)' }, - dark: { primary: 'oklch(78% 0.16 70)', ring: 'oklch(78% 0.16 70)', sidebarPrimary: 'oklch(78% 0.16 70)', sidebarRing: 'oklch(78% 0.16 70)' } }, + light: { primary: 'oklch(72% 0.17 70)', ring: 'oklch(72% 0.17 70)' }, + dark: { primary: 'oklch(78% 0.16 70)', ring: 'oklch(78% 0.16 70)' } }, { id: 'orange', label: '夕橙', swatch: '#f97316', - light: { primary: 'oklch(67% 0.2 45)', ring: 'oklch(67% 0.2 45)', sidebarPrimary: 'oklch(67% 0.2 45)', sidebarRing: 'oklch(67% 0.2 45)' }, - dark: { primary: 'oklch(74% 0.18 45)', ring: 'oklch(74% 0.18 45)', sidebarPrimary: 'oklch(74% 0.18 45)', sidebarRing: 'oklch(74% 0.18 45)' } }, + light: { primary: 'oklch(67% 0.2 45)', ring: 'oklch(67% 0.2 45)' }, + dark: { primary: 'oklch(74% 0.18 45)', ring: 'oklch(74% 0.18 45)' } }, ]; export const RADIUS_OPTIONS = [ @@ -52,68 +62,341 @@ export const POLL_INTERVAL_OPTIONS = [ { value: 0, label: '已暂停' }, ] as const; +export interface FontSpec { id: string; label: string; stack: string } + +export const FONT_SANS_OPTIONS: FontSpec[] = [ + { id: 'default', label: '默认 (Inter)', stack: "'Inter', 'Noto Sans SC', system-ui, -apple-system, sans-serif" }, + { id: 'system', label: '系统界面', stack: "system-ui, -apple-system, 'Segoe UI', 'Noto Sans SC', sans-serif" }, + { id: 'rounded', label: '圆润', stack: "'Varela Round', 'Quicksand', 'Noto Sans SC', system-ui, sans-serif" }, + { id: 'serif', label: '衬线', stack: "Georgia, 'Songti SC', 'Noto Serif SC', serif" }, +]; + +export const FONT_MONO_OPTIONS: FontSpec[] = [ + { id: 'default', label: '默认 (JetBrains)', stack: "'JetBrains Mono', 'Fira Code', ui-monospace, monospace" }, + { id: 'system', label: '系统等宽', stack: "ui-monospace, 'SF Mono', Menlo, Consolas, monospace" }, +]; + +export interface GradientSpec { id: string; label: string; css: string } + +export const GRADIENT_OPTIONS: GradientSpec[] = [ + { id: 'aurora', label: '极光', css: 'linear-gradient(135deg, #1e3a8a 0%, #0ea5e9 50%, #22d3ee 100%)' }, + { id: 'sunset', label: '日落', css: 'linear-gradient(135deg, #f97316 0%, #db2777 50%, #7c3aed 100%)' }, + { id: 'forest', label: '森野', css: 'linear-gradient(135deg, #064e3b 0%, #10b981 55%, #84cc16 100%)' }, + { id: 'dusk', label: '暮色', css: 'linear-gradient(160deg, #0f172a 0%, #334155 55%, #64748b 100%)' }, + { id: 'rose', label: '霞光', css: 'linear-gradient(135deg, #9f1239 0%, #fb7185 55%, #fda4af 100%)' }, +]; + +export const UI_SCALE = { min: 0.9, max: 1.2, step: 0.05 } as const; + +// ─── Defaults (mirror core/src/webui/ui-config.ts) ───────────────────────── + +export const DEFAULT_APPEARANCE: UiAppearance = { + mode: 'system', + accentMode: 'preset', + accentPreset: 'sky', + accentCustom: '#38bdf8', + accentScope: 'global', + darkIntensity: 'soft', + sidebarStyle: 'follow', + background: { type: 'none', color: '#0ea5e9', gradient: 'none', imageOpacity: 0.15, imageBlur: 0, hasImage: false, imageMime: '', imageVersion: 0 }, + fontSans: 'default', + fontMono: 'default', + uiScale: 1, + radius: 0.75, + density: 'cozy', + reduceMotion: false, + highContrast: false, + sidebarDefaultCollapsed: false, + timeFormat: '24h', + pollInterval: 3000, +}; + +const LS_CACHE = 'snowluma_ui_appearance'; +const LS_MIGRATED = 'snowluma_ui_migrated'; +const TOKEN_KEY = 'snowluma_token'; + +// ─── Colour helpers (custom hex accent → readable foreground) ────────────── + +function hexToRgb(hex: string): { r: number; g: number; b: number } | null { + const m = hex.trim().replace('#', ''); + if (m.length === 3) { + const r = parseInt(m[0] + m[0], 16), g = parseInt(m[1] + m[1], 16), b = parseInt(m[2] + m[2], 16); + return Number.isNaN(r + g + b) ? null : { r, g, b }; + } + if (m.length === 6 || m.length === 8) { + const r = parseInt(m.slice(0, 2), 16), g = parseInt(m.slice(2, 4), 16), b = parseInt(m.slice(4, 6), 16); + return Number.isNaN(r + g + b) ? null : { r, g, b }; + } + return null; +} + +/** Pick black or white text for a given accent so labels stay legible. */ +function readableForeground(hex: string): string { + const rgb = hexToRgb(hex); + if (!rgb) return '#ffffff'; + // Relative luminance (sRGB approximation). + const lum = (0.2126 * rgb.r + 0.7152 * rgb.g + 0.0722 * rgb.b) / 255; + return lum > 0.6 ? '#0b0d12' : '#ffffff'; +} + +const HEX_RE = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/; + +/** A hex colour safe to interpolate into CSS text, else the fallback. Guards + * the localStorage cache (writable by anyone with local access) from + * injecting arbitrary CSS through the accent