|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:ui'; |
| 3 | + |
| 4 | +import 'package:shadcn_flutter/shadcn_flutter.dart' show ColorScheme; |
| 5 | +import 'package:querya_desktop/core/theme/parser/color_parser.dart'; |
| 6 | +import 'package:querya_desktop/core/theme/parser/querya_theme_manifest.dart'; |
| 7 | +import 'package:querya_desktop/core/theme/parser/vscode_theme_manifest.dart'; |
| 8 | +import 'package:querya_desktop/core/theme/querya_theme.dart'; |
| 9 | +import 'package:querya_desktop/core/theme/theme_metadata.dart'; |
| 10 | + |
| 11 | +/// One editable color in the theme editor MVP. |
| 12 | +class ThemeEditorColorField { |
| 13 | + const ThemeEditorColorField({ |
| 14 | + required this.section, |
| 15 | + required this.key, |
| 16 | + required this.label, |
| 17 | + }); |
| 18 | + |
| 19 | + final String section; |
| 20 | + final String key; |
| 21 | + final String label; |
| 22 | +} |
| 23 | + |
| 24 | +/// MVP color fields for Preferences theme editor (TP-F3). |
| 25 | +const themeEditorMvpColorFields = [ |
| 26 | + ThemeEditorColorField( |
| 27 | + section: 'shadcn_colors', |
| 28 | + key: 'primary', |
| 29 | + label: 'Primary', |
| 30 | + ), |
| 31 | + ThemeEditorColorField( |
| 32 | + section: 'shadcn_colors', |
| 33 | + key: 'background', |
| 34 | + label: 'Background', |
| 35 | + ), |
| 36 | + ThemeEditorColorField( |
| 37 | + section: 'shadcn_colors', |
| 38 | + key: 'foreground', |
| 39 | + label: 'Foreground', |
| 40 | + ), |
| 41 | + ThemeEditorColorField( |
| 42 | + section: 'shadcn_colors', |
| 43 | + key: 'card', |
| 44 | + label: 'Card', |
| 45 | + ), |
| 46 | + ThemeEditorColorField( |
| 47 | + section: 'shadcn_colors', |
| 48 | + key: 'border', |
| 49 | + label: 'Border', |
| 50 | + ), |
| 51 | + ThemeEditorColorField( |
| 52 | + section: 'editor_colors', |
| 53 | + key: 'background', |
| 54 | + label: 'Editor background', |
| 55 | + ), |
| 56 | + ThemeEditorColorField( |
| 57 | + section: 'editor_colors', |
| 58 | + key: 'foreground', |
| 59 | + label: 'Editor foreground', |
| 60 | + ), |
| 61 | + ThemeEditorColorField( |
| 62 | + section: 'editor_colors', |
| 63 | + key: 'selection', |
| 64 | + label: 'Editor selection', |
| 65 | + ), |
| 66 | + ThemeEditorColorField( |
| 67 | + section: 'editor_colors', |
| 68 | + key: 'canvas', |
| 69 | + label: 'Workbench canvas', |
| 70 | + ), |
| 71 | + ThemeEditorColorField( |
| 72 | + section: 'editor_colors', |
| 73 | + key: 'sidebarBackground', |
| 74 | + label: 'Sidebar background', |
| 75 | + ), |
| 76 | +]; |
| 77 | + |
| 78 | +/// Mutable draft for editing and exporting `querya.theme.v1`. |
| 79 | +class ThemeEditorDraft { |
| 80 | + ThemeEditorDraft({ |
| 81 | + required this.id, |
| 82 | + required this.name, |
| 83 | + required this.type, |
| 84 | + required Map<String, String> shadcnColors, |
| 85 | + required Map<String, String> editorColors, |
| 86 | + List<TokenColorRule> tokenColors = const [], |
| 87 | + this.description, |
| 88 | + this.author, |
| 89 | + this.version, |
| 90 | + this.homepage, |
| 91 | + this.license, |
| 92 | + this.preview, |
| 93 | + List<String> tags = const [], |
| 94 | + this.readOnlySource = false, |
| 95 | + }) : shadcnColors = Map<String, String>.from(shadcnColors), |
| 96 | + editorColors = Map<String, String>.from(editorColors), |
| 97 | + tokenColors = List<TokenColorRule>.from(tokenColors), |
| 98 | + tags = List<String>.from(tags); |
| 99 | + |
| 100 | + String id; |
| 101 | + String name; |
| 102 | + QueryaThemeType type; |
| 103 | + final Map<String, String> shadcnColors; |
| 104 | + final Map<String, String> editorColors; |
| 105 | + final List<TokenColorRule> tokenColors; |
| 106 | + String? description; |
| 107 | + String? author; |
| 108 | + String? version; |
| 109 | + String? homepage; |
| 110 | + String? license; |
| 111 | + String? preview; |
| 112 | + final List<String> tags; |
| 113 | + |
| 114 | + /// Built-in / asset themes are exported as copies only. |
| 115 | + final bool readOnlySource; |
| 116 | + |
| 117 | + factory ThemeEditorDraft.fromManifest( |
| 118 | + QueryaThemeManifest manifest, { |
| 119 | + bool readOnlySource = false, |
| 120 | + }) { |
| 121 | + return ThemeEditorDraft( |
| 122 | + id: manifest.id, |
| 123 | + name: manifest.name, |
| 124 | + type: manifest.type, |
| 125 | + shadcnColors: manifest.shadcnColors, |
| 126 | + editorColors: manifest.editorColors, |
| 127 | + tokenColors: manifest.tokenColors, |
| 128 | + description: manifest.description, |
| 129 | + author: manifest.author, |
| 130 | + version: manifest.version, |
| 131 | + homepage: manifest.homepage, |
| 132 | + license: manifest.license, |
| 133 | + preview: manifest.preview, |
| 134 | + tags: manifest.tags, |
| 135 | + readOnlySource: readOnlySource, |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + factory ThemeEditorDraft.fromQueryaTheme({ |
| 140 | + required String id, |
| 141 | + required String name, |
| 142 | + required bool isDark, |
| 143 | + required QueryaTheme theme, |
| 144 | + ThemeMetadata? metadata, |
| 145 | + bool readOnlySource = false, |
| 146 | + }) { |
| 147 | + final scheme = theme.colorScheme; |
| 148 | + final editor = theme.editor; |
| 149 | + final workbench = theme.workbench; |
| 150 | + |
| 151 | + return ThemeEditorDraft( |
| 152 | + id: id, |
| 153 | + name: name, |
| 154 | + type: isDark ? QueryaThemeType.dark : QueryaThemeType.light, |
| 155 | + shadcnColors: { |
| 156 | + for (final field in themeEditorMvpColorFields) |
| 157 | + if (field.section == 'shadcn_colors') |
| 158 | + field.key: _colorForShadcnField(field.key, scheme), |
| 159 | + }, |
| 160 | + editorColors: { |
| 161 | + 'background': formatVsCodeColor(editor.background), |
| 162 | + 'foreground': formatVsCodeColor(editor.foreground), |
| 163 | + 'selection': formatVsCodeColor(editor.selection), |
| 164 | + 'canvas': formatVsCodeColor(workbench.canvas), |
| 165 | + 'sidebarBackground': formatVsCodeColor(workbench.sidebarBackground), |
| 166 | + }, |
| 167 | + tokenColors: theme.tokenColors, |
| 168 | + description: metadata?.description, |
| 169 | + author: metadata?.author, |
| 170 | + version: metadata?.version, |
| 171 | + homepage: metadata?.homepage, |
| 172 | + license: metadata?.license, |
| 173 | + preview: metadata?.preview, |
| 174 | + tags: metadata?.tags ?? const [], |
| 175 | + readOnlySource: readOnlySource, |
| 176 | + ); |
| 177 | + } |
| 178 | + |
| 179 | + static String _colorForShadcnField(String key, ColorScheme scheme) { |
| 180 | + final color = switch (key) { |
| 181 | + 'primary' => scheme.primary, |
| 182 | + 'background' => scheme.background, |
| 183 | + 'foreground' => scheme.foreground, |
| 184 | + 'card' => scheme.card, |
| 185 | + 'border' => scheme.border, |
| 186 | + _ => scheme.primary, |
| 187 | + }; |
| 188 | + return formatVsCodeColor(color); |
| 189 | + } |
| 190 | + |
| 191 | + String? colorHex(ThemeEditorColorField field) { |
| 192 | + final map = field.section == 'shadcn_colors' ? shadcnColors : editorColors; |
| 193 | + return map[field.key]; |
| 194 | + } |
| 195 | + |
| 196 | + void setColorHex(ThemeEditorColorField field, String hex) { |
| 197 | + final normalized = hex.trim(); |
| 198 | + parseQueryaThemeColor(normalized); |
| 199 | + final map = field.section == 'shadcn_colors' ? shadcnColors : editorColors; |
| 200 | + map[field.key] = formatVsCodeColor(parseQueryaThemeColor(normalized)); |
| 201 | + } |
| 202 | + |
| 203 | + void setColor(ThemeEditorColorField field, Color color) { |
| 204 | + final map = field.section == 'shadcn_colors' ? shadcnColors : editorColors; |
| 205 | + map[field.key] = formatVsCodeColor(color); |
| 206 | + } |
| 207 | + |
| 208 | + QueryaThemeManifest toManifest() { |
| 209 | + return QueryaThemeManifest( |
| 210 | + schema: queryaThemeSchemaV1, |
| 211 | + id: id, |
| 212 | + name: name, |
| 213 | + type: type, |
| 214 | + shadcnColors: Map.unmodifiable(shadcnColors), |
| 215 | + editorColors: Map.unmodifiable(editorColors), |
| 216 | + tokenColors: List.unmodifiable(tokenColors), |
| 217 | + description: description, |
| 218 | + author: author, |
| 219 | + version: version, |
| 220 | + homepage: homepage, |
| 221 | + license: license, |
| 222 | + preview: preview, |
| 223 | + tags: List.unmodifiable(tags), |
| 224 | + ); |
| 225 | + } |
| 226 | + |
| 227 | + /// JSON export with a unique id when saving a built-in/read-only source. |
| 228 | + ThemeEditorDraft forExport({String? exportId}) { |
| 229 | + if (!readOnlySource && exportId == null) return this; |
| 230 | + final nextId = exportId ?? '$id-edited'; |
| 231 | + return ThemeEditorDraft( |
| 232 | + id: nextId, |
| 233 | + name: '$name (edited)', |
| 234 | + type: type, |
| 235 | + shadcnColors: shadcnColors, |
| 236 | + editorColors: editorColors, |
| 237 | + tokenColors: tokenColors, |
| 238 | + description: description, |
| 239 | + author: author, |
| 240 | + version: version ?? '1.0.0', |
| 241 | + homepage: homepage, |
| 242 | + license: license, |
| 243 | + preview: preview, |
| 244 | + tags: tags, |
| 245 | + readOnlySource: false, |
| 246 | + ); |
| 247 | + } |
| 248 | + |
| 249 | + String toExportJsonString() { |
| 250 | + return const JsonEncoder.withIndent(' ').convert(toManifest().toJson()); |
| 251 | + } |
| 252 | +} |
0 commit comments