| 1 | export const systemUIFont = |
| 2 | '-apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", sans-serif'; |
| 3 | export const systemCodeFont = '"SF Mono", Menlo, Consolas, monospace'; |
| 4 | import { presets } from "./appearance-presets.mjs"; |
| 5 | export { |
| 6 | presets, |
| 7 | presetNames, |
| 8 | presetGroups, |
| 9 | presetInfo, |
| 10 | themeCatalog, |
| 11 | applyThemePreset, |
| 12 | } from "./appearance-presets.mjs"; |
| 13 | export const defaultAppearance = { |
| 14 | customThemes: [], |
| 15 | mode: "system", |
| 16 | lightTheme: { |
| 17 | ...presets.light.codex, |
| 18 | preset: "codex", |
| 19 | opaqueWindows: false, |
| 20 | fonts: { ui: "", code: "" }, |
| 21 | }, |
| 22 | darkTheme: { |
| 23 | ...presets.dark.codex, |
| 24 | preset: "codex", |
| 25 | opaqueWindows: false, |
| 26 | fonts: { ui: "", code: "" }, |
| 27 | }, |
| 28 | uiFontSize: 14, |
| 29 | codeFontSize: 13, |
| 30 | pointerCursors: false, |
| 31 | fontSmoothing: true, |
| 32 | reducedMotion: "system", |
| 33 | }; |
| 34 | export function normalizeAppearance(value = {}) { |
| 35 | value = value && typeof value === "object" ? value : {}; |
| 36 | return { |
| 37 | ...defaultAppearance, |
| 38 | ...value, |
| 39 | customThemes: Array.isArray(value.customThemes) ? value.customThemes : [], |
| 40 | lightTheme: { |
| 41 | ...defaultAppearance.lightTheme, |
| 42 | ...value.lightTheme, |
| 43 | colors: { ...value.lightTheme?.colors }, |
| 44 | terminal: { ...value.lightTheme?.terminal }, |
| 45 | fonts: { |
| 46 | ...defaultAppearance.lightTheme.fonts, |
| 47 | ...value.lightTheme?.fonts, |
| 48 | }, |
| 49 | }, |
| 50 | darkTheme: { |
| 51 | ...defaultAppearance.darkTheme, |
| 52 | ...value.darkTheme, |
| 53 | colors: { ...value.darkTheme?.colors }, |
| 54 | terminal: { ...value.darkTheme?.terminal }, |
| 55 | fonts: { |
| 56 | ...defaultAppearance.darkTheme.fonts, |
| 57 | ...value.darkTheme?.fonts, |
| 58 | }, |
| 59 | }, |
| 60 | }; |
| 61 | } |
| 62 | export function mix(a, b, weight) { |
| 63 | const rgb = (hex) => |
| 64 | [1, 3, 5].map((index) => parseInt(hex.slice(index, index + 2), 16)); |
| 65 | const left = rgb(a), |
| 66 | right = rgb(b); |
| 67 | return ( |
| 68 | "#" + |
| 69 | left |
| 70 | .map((value, index) => |
| 71 | Math.round(value + (right[index] - value) * weight) |
| 72 | .toString(16) |
| 73 | .padStart(2, "0"), |
| 74 | ) |
| 75 | .join("") |
| 76 | ); |
| 77 | } |
| 78 | function luminance(hex) { |
| 79 | const [r, g, b] = [1, 3, 5] |
| 80 | .map((index) => parseInt(hex.slice(index, index + 2), 16) / 255) |
| 81 | .map((v) => (v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4)); |
| 82 | return r * 0.2126 + g * 0.7152 + b * 0.0722; |
| 83 | } |
| 84 | export function themeVariables(theme, variant) { |
| 85 | const { surface, ink, accent, contrast } = theme; |
| 86 | const chrome = mix(surface, ink, 0.025 + contrast * 0.00055); |
| 87 | const line = mix(surface, ink, 0.09 + contrast * 0.0013); |
| 88 | const variables = { |
| 89 | "--paper": surface, |
| 90 | "--chrome": chrome, |
| 91 | "--chrome-dark": mix(surface, ink, 0.1), |
| 92 | "--ink": ink, |
| 93 | "--strong": mix(ink, variant === "light" ? "#000000" : "#ffffff", 0.15), |
| 94 | "--muted": mix(ink, surface, 0.15), |
| 95 | "--accent": accent, |
| 96 | "--focus": accent, |
| 97 | "--input": surface, |
| 98 | "--line": line, |
| 99 | "--hover": mix(surface, ink, 0.09), |
| 100 | "--selected": mix(surface, ink, 0.13), |
| 101 | "--selection": mix(surface, accent, 0.32), |
| 102 | "--button": ink, |
| 103 | "--button-ink": surface, |
| 104 | "--link": accent, |
| 105 | "--success": variant === "dark" ? "#70bf56" : "#357c3c", |
| 106 | "--danger": variant === "dark" ? "#f26d78" : "#bb3535", |
| 107 | "--warning": variant === "dark" ? "#e6b450" : "#af7a22", |
| 108 | "--inactive": "#a3a3a3", |
| 109 | "--ui-font-family": theme.fonts.ui || systemUIFont, |
| 110 | "--code-font-family": theme.fonts.code || systemCodeFont, |
| 111 | "--accent-ink": luminance(accent) > 0.4 ? "#111111" : "#ffffff", |
| 112 | }; |
| 113 | for (const [key, value] of Object.entries(theme.colors || {})) { |
| 114 | if (colorTokens[key]) variables[colorTokens[key]] = value; |
| 115 | } |
| 116 | variables["--sidebar"] = theme.colors?.sidebar || variables["--chrome"]; |
| 117 | variables["--sidebar-ink"] = theme.colors?.sidebarInk || ink; |
| 118 | variables["--toolbar"] = theme.colors?.toolbar || surface; |
| 119 | variables["--toolbar-ink"] = theme.colors?.toolbarInk || ink; |
| 120 | |
| 121 | variables["--border"] = variables["--line"]; |
| 122 | variables["--surface"] = variables["--paper"]; |
| 123 | return variables; |
| 124 | } |
| 125 | export function terminalTheme(theme, variant) { |
| 126 | const vars = themeVariables(theme, variant); |
| 127 | return { |
| 128 | background: theme.surface, |
| 129 | foreground: theme.ink, |
| 130 | cursor: theme.ink, |
| 131 | cursorAccent: theme.surface, |
| 132 | selectionBackground: vars["--selection"], |
| 133 | black: variant === "dark" ? "#202020" : "#242424", |
| 134 | red: variant === "dark" ? "#f26d78" : "#c43d3d", |
| 135 | green: variant === "dark" ? "#aad94c" : "#4b7b23", |
| 136 | yellow: theme.accent, |
| 137 | blue: variant === "dark" ? "#59c2ff" : "#206bb1", |
| 138 | magenta: variant === "dark" ? "#d2a6ff" : "#a03e9c", |
| 139 | cyan: variant === "dark" ? "#95e6cb" : "#187b74", |
| 140 | white: variant === "dark" ? "#bfbdb6" : "#dadada", |
| 141 | brightBlack: "#737373", |
| 142 | brightRed: "#fa7784", |
| 143 | brightGreen: "#a4d86a", |
| 144 | brightYellow: "#ffd173", |
| 145 | brightBlue: "#73d0ff", |
| 146 | brightMagenta: "#d4bfff", |
| 147 | brightCyan: "#95e6cb", |
| 148 | brightWhite: "#ffffff", |
| 149 | ...theme.terminal, |
| 150 | }; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | export const colorGroups = [ |
| 155 | { |
| 156 | label: "界面与层级", |
| 157 | entries: [ |
| 158 | ["chrome", "面板背景"], |
| 159 | ["sidebar", "侧栏背景"], |
| 160 | ["sidebarInk", "侧栏文字"], |
| 161 | ["toolbar", "工具栏背景"], |
| 162 | ["toolbarInk", "工具栏文字"], |
| 163 | ["chromeDark", "次级背景"], |
| 164 | ["strong", "标题文字"], |
| 165 | ["muted", "辅助文字"], |
| 166 | ["line", "边框与分隔线"], |
| 167 | ["input", "输入框背景"], |
| 168 | ], |
| 169 | }, |
| 170 | { |
| 171 | label: "按钮与交互", |
| 172 | entries: [ |
| 173 | ["button", "主按钮背景"], |
| 174 | ["buttonInk", "主按钮文字"], |
| 175 | ["accentInk", "强调色上的文字"], |
| 176 | ["link", "链接"], |
| 177 | ["focus", "焦点边框"], |
| 178 | ["hover", "悬停背景"], |
| 179 | ["selected", "选中背景"], |
| 180 | ["selection", "文本选区"], |
| 181 | ], |
| 182 | }, |
| 183 | { |
| 184 | label: "状态提示", |
| 185 | entries: [ |
| 186 | ["success", "成功状态"], |
| 187 | ["warning", "警告状态"], |
| 188 | ["danger", "错误状态"], |
| 189 | ["inactive", "未启动状态"], |
| 190 | ], |
| 191 | }, |
| 192 | ]; |
| 193 | export const colorTokens = Object.fromEntries( |
| 194 | colorGroups.flatMap((group) => |
| 195 | group.entries.map(([key]) => [ |
| 196 | key, |
| 197 | "--" + key.replace(/[A-Z]/g, (c) => "-" + c.toLowerCase()), |
| 198 | ]), |
| 199 | ), |
| 200 | ); |
| 201 | export const terminalColorGroups = [ |
| 202 | { |
| 203 | label: "终端", |
| 204 | entries: [ |
| 205 | ["background", "背景"], |
| 206 | ["foreground", "文字"], |
| 207 | ["cursor", "光标"], |
| 208 | ["cursorAccent", "光标内文字"], |
| 209 | ["selectionBackground", "选区背景"], |
| 210 | ], |
| 211 | }, |
| 212 | { |
| 213 | label: "终端 ANSI 配色", |
| 214 | entries: [ |
| 215 | ["black", "黑色"], |
| 216 | ["red", "红色"], |
| 217 | ["green", "绿色"], |
| 218 | ["yellow", "黄色"], |
| 219 | ["blue", "蓝色"], |
| 220 | ["magenta", "品红"], |
| 221 | ["cyan", "青色"], |
| 222 | ["white", "白色"], |
| 223 | ["brightBlack", "亮黑"], |
| 224 | ["brightRed", "亮红"], |
| 225 | ["brightGreen", "亮绿"], |
| 226 | ["brightYellow", "亮黄"], |
| 227 | ["brightBlue", "亮蓝"], |
| 228 | ["brightMagenta", "亮品红"], |
| 229 | ["brightCyan", "亮青"], |
| 230 | ["brightWhite", "亮白"], |
| 231 | ], |
| 232 | }, |
| 233 | ]; |
| 234 | export const themeFileLimit = 256 * 1024; |
| 235 | export const customThemeLimit = 50; |
| 236 | const hexColor = /^#[0-9a-fA-F]{6}$/; |
| 237 | const object = (value) => |
| 238 | value !== null && typeof value === "object" && !Array.isArray(value); |
| 239 | export function validateTheme(theme) { |
| 240 | if (!object(theme)) throw new Error("主题缺少浅色或深色配置"); |
| 241 | const allowed = [ |
| 242 | "preset", |
| 243 | "accent", |
| 244 | "surface", |
| 245 | "ink", |
| 246 | "contrast", |
| 247 | "opaqueWindows", |
| 248 | "fonts", |
| 249 | "colors", |
| 250 | "terminal", |
| 251 | ]; |
| 252 | if (Object.keys(theme).some((key) => !allowed.includes(key))) |
| 253 | throw new Error("主题包含不支持的设置"); |
| 254 | if ( |
| 255 | ![theme.accent, theme.surface, theme.ink].every( |
| 256 | (value) => typeof value === "string" && hexColor.test(value), |
| 257 | ) |
| 258 | ) |
| 259 | throw new Error("颜色须为六位十六进制值,例如 #fdf6e3"); |
| 260 | if ( |
| 261 | typeof theme.preset !== "string" || |
| 262 | theme.preset.length > 80 || |
| 263 | !Number.isInteger(theme.contrast) || |
| 264 | theme.contrast < 0 || |
| 265 | theme.contrast > 100 || |
| 266 | typeof theme.opaqueWindows !== "boolean" |
| 267 | ) |
| 268 | throw new Error("主题配置无效"); |
| 269 | if ( |
| 270 | !object(theme.fonts) || |
| 271 | Object.keys(theme.fonts).some((key) => !["ui", "code"].includes(key)) || |
| 272 | [theme.fonts.ui, theme.fonts.code].some( |
| 273 | (font) => |
| 274 | typeof font !== "string" || |
| 275 | font.length > 200 || |
| 276 | /[;{}<>\r\n]/.test(font), |
| 277 | ) |
| 278 | ) |
| 279 | throw new Error("字体名称无效"); |
| 280 | for (const [field, keys] of [ |
| 281 | ["colors", Object.keys(colorTokens)], |
| 282 | [ |
| 283 | "terminal", |
| 284 | terminalColorGroups.flatMap((group) => group.entries.map(([key]) => key)), |
| 285 | ], |
| 286 | ]) { |
| 287 | if (theme[field] === undefined) continue; |
| 288 | if ( |
| 289 | !object(theme[field]) || |
| 290 | Object.entries(theme[field]).some( |
| 291 | ([key, value]) => |
| 292 | !keys.includes(key) || |
| 293 | typeof value !== "string" || |
| 294 | !hexColor.test(value), |
| 295 | ) |
| 296 | ) |
| 297 | throw new Error("元素颜色无效或包含不支持的颜色项目"); |
| 298 | } |
| 299 | return structuredClone(theme); |
| 300 | } |
| 301 | export function validateThemeName(name) { |
| 302 | if (typeof name !== "string" || !name.trim() || name.trim().length > 80) |
| 303 | throw new Error("请输入 1–80 个字符的主题名称"); |
| 304 | return name.trim(); |
| 305 | } |
| 306 | export function themeDocument(name, appearance) { |
| 307 | return { |
| 308 | format: "cage-theme", |
| 309 | version: 1, |
| 310 | name: validateThemeName(name), |
| 311 | lightTheme: validateTheme(appearance.lightTheme), |
| 312 | darkTheme: validateTheme(appearance.darkTheme), |
| 313 | }; |
| 314 | } |
| 315 | export function parseThemeDocument(text) { |
| 316 | if (new TextEncoder().encode(text).length > themeFileLimit) |
| 317 | throw new Error("主题文件不能超过 256 KB"); |
| 318 | let data; |
| 319 | try { |
| 320 | data = JSON.parse(text); |
| 321 | } catch { |
| 322 | throw new Error("无法读取主题,请选择有效的 JSON 文件"); |
| 323 | } |
| 324 | if (!object(data) || data.format !== "cage-theme" || data.version !== 1) |
| 325 | throw new Error("不支持的主题格式或版本,请使用 Cage 导出的主题文件"); |
| 326 | if ( |
| 327 | Object.keys(data).some( |
| 328 | (key) => |
| 329 | !["format", "version", "name", "lightTheme", "darkTheme"].includes(key), |
| 330 | ) |
| 331 | ) |
| 332 | throw new Error("主题文件包含不支持的设置"); |
| 333 | return themeDocument(data.name, data); |
| 334 | } |
| 335 | export function sameTheme(left, right) { |
| 336 | const canonical = (theme) => { |
| 337 | const valid = validateTheme(theme); |
| 338 | return JSON.stringify([ |
| 339 | valid.preset, |
| 340 | valid.accent, |
| 341 | valid.surface, |
| 342 | valid.ink, |
| 343 | valid.contrast, |
| 344 | valid.opaqueWindows, |
| 345 | valid.fonts.ui, |
| 346 | valid.fonts.code, |
| 347 | Object.entries(valid.colors || {}).sort(), |
| 348 | Object.entries(valid.terminal || {}).sort(), |
| 349 | ]); |
| 350 | }; |
| 351 | return ( |
| 352 | canonical(left.lightTheme) === canonical(right.lightTheme) && |
| 353 | canonical(left.darkTheme) === canonical(right.darkTheme) |
| 354 | ); |
| 355 | } |