| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "regexp" |
| 10 | "strings" |
| 11 | "unicode/utf8" |
| 12 | |
| 13 | "github.com/pelletier/go-toml/v2" |
| 14 | ) |
| 15 | |
| 16 | type AppearanceFonts struct { |
| 17 | UI string `json:"ui" toml:"ui"` |
| 18 | Code string `json:"code" toml:"code"` |
| 19 | } |
| 20 | type AppearanceTheme struct { |
| 21 | Preset string `json:"preset"` |
| 22 | Accent string `json:"accent" toml:"accent"` |
| 23 | Surface string `json:"surface" toml:"surface"` |
| 24 | Ink string `json:"ink" toml:"ink"` |
| 25 | Contrast int `json:"contrast" toml:"contrast"` |
| 26 | OpaqueWindows bool `json:"opaqueWindows" toml:"opaqueWindows"` |
| 27 | Fonts AppearanceFonts `json:"fonts" toml:"fonts"` |
| 28 | Colors map[string]string `json:"colors,omitempty"` |
| 29 | Terminal map[string]string `json:"terminal,omitempty"` |
| 30 | } |
| 31 | type SavedAppearanceTheme struct { |
| 32 | ID string `json:"id"` |
| 33 | Name string `json:"name"` |
| 34 | Light AppearanceTheme `json:"lightTheme"` |
| 35 | Dark AppearanceTheme `json:"darkTheme"` |
| 36 | } |
| 37 | type Appearance struct { |
| 38 | CustomThemes []SavedAppearanceTheme `json:"customThemes,omitempty"` |
| 39 | Mode string `json:"mode"` |
| 40 | Light AppearanceTheme `json:"lightTheme"` |
| 41 | Dark AppearanceTheme `json:"darkTheme"` |
| 42 | UIFontSize int `json:"uiFontSize"` |
| 43 | CodeFontSize int `json:"codeFontSize"` |
| 44 | PointerCursors bool `json:"pointerCursors"` |
| 45 | FontSmoothing bool `json:"fontSmoothing"` |
| 46 | ReducedMotion string `json:"reducedMotion"` |
| 47 | } |
| 48 | |
| 49 | func defaultAppearance() Appearance { |
| 50 | return Appearance{Mode: "system", Light: AppearanceTheme{Preset: "codex", Accent: "#339cff", Surface: "#ffffff", Ink: "#1a1c1f", Contrast: 45}, Dark: AppearanceTheme{Preset: "codex", Accent: "#339cff", Surface: "#181818", Ink: "#ffffff", Contrast: 60}, UIFontSize: 14, CodeFontSize: 13, FontSmoothing: true, ReducedMotion: "system"} |
| 51 | } |
| 52 | |
| 53 | func (a Appearance) validate() error { |
| 54 | if a.Mode != "system" && a.Mode != "light" && a.Mode != "dark" { |
| 55 | return errors.New("请选择跟随系统、浅色或深色外观") |
| 56 | } |
| 57 | if a.UIFontSize < 12 || a.UIFontSize > 24 || a.CodeFontSize < 10 || a.CodeFontSize > 24 { |
| 58 | return errors.New("界面字号范围为 12–24,代码字号范围为 10–24") |
| 59 | } |
| 60 | if a.ReducedMotion != "system" && a.ReducedMotion != "on" && a.ReducedMotion != "off" { |
| 61 | return errors.New("减少动态效果选项无效") |
| 62 | } |
| 63 | if err := validateAppearanceTheme(a.Light); err != nil { |
| 64 | return err |
| 65 | } |
| 66 | if err := validateAppearanceTheme(a.Dark); err != nil { |
| 67 | return err |
| 68 | } |
| 69 | if len(a.CustomThemes) > 50 { |
| 70 | return errors.New("最多保存 50 个自定义主题,请先删除不需要的主题") |
| 71 | } |
| 72 | ids := make(map[string]bool) |
| 73 | for _, saved := range a.CustomThemes { |
| 74 | if !regexp.MustCompile(`^[a-zA-Z0-9_-]{1,80}$`).MatchString(saved.ID) || ids[saved.ID] { |
| 75 | return errors.New("自定义主题标识无效或重复") |
| 76 | } |
| 77 | ids[saved.ID] = true |
| 78 | if strings.TrimSpace(saved.Name) == "" || utf8.RuneCountInString(saved.Name) > 80 { |
| 79 | return errors.New("请输入 1–80 个字符的主题名称") |
| 80 | } |
| 81 | if err := validateAppearanceTheme(saved.Light); err != nil { |
| 82 | return err |
| 83 | } |
| 84 | if err := validateAppearanceTheme(saved.Dark); err != nil { |
| 85 | return err |
| 86 | } |
| 87 | } |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | var appearanceColorKeys = strings.Fields("sidebar sidebarInk toolbar toolbarInk chrome chromeDark strong muted line input button buttonInk accentInk link focus hover selected selection success warning danger inactive") |
| 92 | var terminalColorKeys = strings.Fields("background foreground cursor cursorAccent selectionBackground black red green yellow blue magenta cyan white brightBlack brightRed brightGreen brightYellow brightBlue brightMagenta brightCyan brightWhite") |
| 93 | var appearanceColor = regexp.MustCompile(`^#[0-9a-fA-F]{6}$`) |
| 94 | |
| 95 | func validateAppearanceTheme(theme AppearanceTheme) error { |
| 96 | if !appearanceColor.MatchString(theme.Accent) || !appearanceColor.MatchString(theme.Surface) || !appearanceColor.MatchString(theme.Ink) { |
| 97 | return errors.New("颜色须为六位十六进制值,例如 #fdf6e3") |
| 98 | } |
| 99 | if theme.Contrast < 0 || theme.Contrast > 100 { |
| 100 | return errors.New("对比度范围为 0–100") |
| 101 | } |
| 102 | if utf8.RuneCountInString(theme.Preset) > 80 { |
| 103 | return errors.New("主题名称过长") |
| 104 | } |
| 105 | for _, font := range []string{theme.Fonts.UI, theme.Fonts.Code} { |
| 106 | if utf8.RuneCountInString(font) > 200 || strings.ContainsAny(font, ";{}<>\r\n") { |
| 107 | return errors.New("字体名称无效") |
| 108 | } |
| 109 | } |
| 110 | for field, colors := range map[string]map[string]string{"colors": theme.Colors, "terminal": theme.Terminal} { |
| 111 | allowed := appearanceColorKeys |
| 112 | if field == "terminal" { |
| 113 | allowed = terminalColorKeys |
| 114 | } |
| 115 | for key, value := range colors { |
| 116 | found := false |
| 117 | for _, candidate := range allowed { |
| 118 | if key == candidate { |
| 119 | found = true |
| 120 | break |
| 121 | } |
| 122 | } |
| 123 | if !found || !appearanceColor.MatchString(value) { |
| 124 | return errors.New("元素颜色无效或包含不支持的颜色项目") |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | return nil |
| 129 | } |
| 130 | |
| 131 | func (s *Server) getAppearance(w http.ResponseWriter, r *http.Request) { |
| 132 | a := defaultAppearance() |
| 133 | b, err := os.ReadFile(filepath.Join(s.profile, "appearance.json")) |
| 134 | if err == nil { |
| 135 | err = json.Unmarshal(b, &a) |
| 136 | } |
| 137 | if err != nil && !os.IsNotExist(err) { |
| 138 | requestError(w, errors.New("无法读取已保存的外观设置")) |
| 139 | return |
| 140 | } |
| 141 | if err = a.validate(); err != nil { |
| 142 | requestError(w, err) |
| 143 | return |
| 144 | } |
| 145 | jsonReply(w, a) |
| 146 | } |
| 147 | |
| 148 | func (s *Server) saveAppearance(w http.ResponseWriter, r *http.Request) { |
| 149 | var a Appearance |
| 150 | if !readJSON(w, r, &a, 512<<10) { |
| 151 | return |
| 152 | } |
| 153 | if err := a.validate(); err != nil { |
| 154 | requestError(w, err) |
| 155 | return |
| 156 | } |
| 157 | |
| 158 | if err := writePrivate(filepath.Join(s.profile, "appearance.json"), a); err != nil { |
| 159 | requestError(w, errors.New("外观设置未能保存,请重试")) |
| 160 | return |
| 161 | } |
| 162 | jsonReply(w, a) |
| 163 | } |
| 164 | |
| 165 | func readCodexAppearance(configPath string) (Appearance, error) { |
| 166 | a := defaultAppearance() |
| 167 | a.CodeFontSize = 12 |
| 168 | var config struct { |
| 169 | Desktop struct { |
| 170 | Mode string `toml:"appearanceTheme"` |
| 171 | Light *AppearanceTheme `toml:"appearanceLightChromeTheme"` |
| 172 | Dark *AppearanceTheme `toml:"appearanceDarkChromeTheme"` |
| 173 | LightPreset string `toml:"appearanceLightCodeThemeId"` |
| 174 | DarkPreset string `toml:"appearanceDarkCodeThemeId"` |
| 175 | UIFontSize int `toml:"sansFontSize"` |
| 176 | CodeFontSize int `toml:"codeFontSize"` |
| 177 | PointerCursors bool `toml:"usePointerCursors"` |
| 178 | FontSmoothing *bool `toml:"useFontSmoothing"` |
| 179 | } `toml:"desktop"` |
| 180 | } |
| 181 | b, err := os.ReadFile(configPath) |
| 182 | if err != nil { |
| 183 | return a, errors.New("未找到本机 Codex 配置") |
| 184 | } |
| 185 | if err = toml.Unmarshal(b, &config); err != nil { |
| 186 | return a, errors.New("无法解析本机 Codex 配置") |
| 187 | } |
| 188 | d := config.Desktop |
| 189 | if d.Mode != "" { |
| 190 | a.Mode = d.Mode |
| 191 | } |
| 192 | if d.Light != nil { |
| 193 | a.Light = *d.Light |
| 194 | } |
| 195 | if d.Dark != nil { |
| 196 | a.Dark = *d.Dark |
| 197 | } |
| 198 | a.Light.Preset = d.LightPreset |
| 199 | if a.Light.Preset == "" { |
| 200 | a.Light.Preset = "codex" |
| 201 | } |
| 202 | a.Dark.Preset = d.DarkPreset |
| 203 | if a.Dark.Preset == "" { |
| 204 | a.Dark.Preset = "codex" |
| 205 | } |
| 206 | if d.UIFontSize != 0 { |
| 207 | a.UIFontSize = d.UIFontSize |
| 208 | } |
| 209 | if d.CodeFontSize != 0 { |
| 210 | a.CodeFontSize = d.CodeFontSize |
| 211 | } |
| 212 | a.PointerCursors = d.PointerCursors |
| 213 | if d.FontSmoothing != nil { |
| 214 | a.FontSmoothing = *d.FontSmoothing |
| 215 | } |
| 216 | |
| 217 | var global struct { |
| 218 | ReducedMotion string `json:"reduced-motion-preference"` |
| 219 | } |
| 220 | if b, err := os.ReadFile(filepath.Join(filepath.Dir(configPath), ".codex-global-state.json")); err == nil && json.Unmarshal(b, &global) == nil && global.ReducedMotion != "" { |
| 221 | a.ReducedMotion = global.ReducedMotion |
| 222 | } |
| 223 | return a, a.validate() |
| 224 | } |
| 225 | |
| 226 | func (s *Server) codexAppearance(w http.ResponseWriter, r *http.Request) { |
| 227 | home, err := os.UserHomeDir() |
| 228 | if err != nil { |
| 229 | requestError(w, errors.New("无法定位本机用户目录")) |
| 230 | return |
| 231 | } |
| 232 | a, err := readCodexAppearance(filepath.Join(home, ".codex", "config.toml")) |
| 233 | if err != nil { |
| 234 | requestError(w, err) |
| 235 | return |
| 236 | } |
| 237 | jsonReply(w, a) |
| 238 | } |