|
| 1 | +""" |
| 2 | +Theme management for ChiX Editor |
| 3 | +""" |
| 4 | + |
| 5 | +import customtkinter as ctk |
| 6 | +import json |
| 7 | +import os |
| 8 | + |
| 9 | +# Define color schemes |
| 10 | +THEMES = { |
| 11 | + # VS Code Dark+ inspired theme |
| 12 | + "vscode_dark": { |
| 13 | + "bg_primary": "#1e1e1e", |
| 14 | + "bg_secondary": "#252526", |
| 15 | + "bg_tertiary": "#2d2d30", |
| 16 | + "bg_hover": "#3e3e42", |
| 17 | + "fg_primary": "#d4d4d4", |
| 18 | + "fg_secondary": "#9e9e9e", |
| 19 | + "accent_primary": "#007acc", |
| 20 | + "accent_secondary": "#3794ff", |
| 21 | + "error": "#f44747", |
| 22 | + "warning": "#ff8800", |
| 23 | + "success": "#35af74", |
| 24 | + "string": "#ce9178", |
| 25 | + "keyword": "#569cd6", |
| 26 | + "comment": "#6A9955", |
| 27 | + "function": "#dcdcaa", |
| 28 | + "type": "#4ec9b0", |
| 29 | + "number": "#b5cea8", |
| 30 | + "operator": "#d4d4d4", |
| 31 | + "variable": "#9cdcfe", |
| 32 | + "class": "#4ec9b0", |
| 33 | + "parameter": "#9cdcfe", |
| 34 | + "line_highlight": "#2a2d2e", |
| 35 | + "selection": "#264f78", |
| 36 | + }, |
| 37 | + |
| 38 | + # Xcode Dark theme (inspired by Xcode's dark mode) |
| 39 | + "xcode_dark": { |
| 40 | + "bg_primary": "#292a30", |
| 41 | + "bg_secondary": "#1e1e1e", |
| 42 | + "bg_tertiary": "#252528", |
| 43 | + "bg_hover": "#3a3a3c", |
| 44 | + "fg_primary": "#dfdfe0", |
| 45 | + "fg_secondary": "#9d9d9f", |
| 46 | + "accent_primary": "#4f87ff", |
| 47 | + "accent_secondary": "#6699ff", |
| 48 | + "error": "#ff3b30", |
| 49 | + "warning": "#ff9500", |
| 50 | + "success": "#34c759", |
| 51 | + "string": "#fc6a5d", |
| 52 | + "keyword": "#fc5fa3", |
| 53 | + "comment": "#6c7986", |
| 54 | + "function": "#a167e6", |
| 55 | + "type": "#67b7a4", |
| 56 | + "number": "#d0bf69", |
| 57 | + "operator": "#a9b7c6", |
| 58 | + "variable": "#41a1c0", |
| 59 | + "class": "#5dd8ff", |
| 60 | + "parameter": "#8ec4e6", |
| 61 | + "line_highlight": "#2f3239", |
| 62 | + "selection": "#3f638b", |
| 63 | + }, |
| 64 | + |
| 65 | + # Xcode Light theme (inspired by Xcode's light mode) |
| 66 | + "xcode_light": { |
| 67 | + "bg_primary": "#ffffff", |
| 68 | + "bg_secondary": "#f4f4f4", |
| 69 | + "bg_tertiary": "#e8e8e8", |
| 70 | + "bg_hover": "#d9d9d9", |
| 71 | + "fg_primary": "#262626", |
| 72 | + "fg_secondary": "#5b5b5b", |
| 73 | + "accent_primary": "#0f68a0", |
| 74 | + "accent_secondary": "#3a87b4", |
| 75 | + "error": "#d12f1b", |
| 76 | + "warning": "#cf8500", |
| 77 | + "success": "#23882b", |
| 78 | + "string": "#c41a16", |
| 79 | + "keyword": "#9b2393", |
| 80 | + "comment": "#536579", |
| 81 | + "function": "#6c36a9", |
| 82 | + "type": "#0f68a0", |
| 83 | + "number": "#1c00cf", |
| 84 | + "operator": "#000000", |
| 85 | + "variable": "#326d74", |
| 86 | + "class": "#0f68a0", |
| 87 | + "parameter": "#2e6399", |
| 88 | + "line_highlight": "#ecf5ff", |
| 89 | + "selection": "#b2d7ff", |
| 90 | + }, |
| 91 | + |
| 92 | + # Dark theme with blue accents |
| 93 | + "dark_blue": { |
| 94 | + "bg_primary": "#1a1a1a", |
| 95 | + "bg_secondary": "#252525", |
| 96 | + "bg_tertiary": "#2d2d2d", |
| 97 | + "bg_hover": "#3e3e42", |
| 98 | + "fg_primary": "#d4d4d4", |
| 99 | + "fg_secondary": "#9e9e9e", |
| 100 | + "accent_primary": "#1e90ff", |
| 101 | + "accent_secondary": "#5b9bd5", |
| 102 | + "error": "#f44747", |
| 103 | + "warning": "#ff8800", |
| 104 | + "success": "#35af74", |
| 105 | + "string": "#ce9178", |
| 106 | + "keyword": "#569cd6", |
| 107 | + "comment": "#6A9955", |
| 108 | + "function": "#dcdcaa", |
| 109 | + "type": "#4ec9b0", |
| 110 | + "number": "#b5cea8", |
| 111 | + "operator": "#d4d4d4", |
| 112 | + "variable": "#9cdcfe", |
| 113 | + "class": "#4ec9b0", |
| 114 | + "parameter": "#9cdcfe", |
| 115 | + "line_highlight": "#2a2d2e", |
| 116 | + "selection": "#264f78", |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +# Current theme - can be changed at runtime |
| 121 | +current_theme = "xcode_dark" |
| 122 | + |
| 123 | +def get_theme(): |
| 124 | + """Get the current theme colors""" |
| 125 | + return THEMES[current_theme] |
| 126 | + |
| 127 | +def get_color(color_key): |
| 128 | + """Get a specific color from the current theme""" |
| 129 | + theme = get_theme() |
| 130 | + return theme.get(color_key, "#ffffff") |
| 131 | + |
| 132 | +def set_theme(theme_name): |
| 133 | + """Set the active theme""" |
| 134 | + global current_theme |
| 135 | + if theme_name in THEMES: |
| 136 | + current_theme = theme_name |
| 137 | + return True |
| 138 | + return False |
| 139 | + |
| 140 | +def cycle_theme(): |
| 141 | + """Cycle through available themes""" |
| 142 | + global current_theme |
| 143 | + theme_keys = list(THEMES.keys()) |
| 144 | + current_index = theme_keys.index(current_theme) |
| 145 | + next_index = (current_index + 1) % len(theme_keys) |
| 146 | + current_theme = theme_keys[next_index] |
| 147 | + return current_theme |
| 148 | + |
| 149 | +def setup_theme(): |
| 150 | + """Set up the initial theme configuration""" |
| 151 | + # Set appearance mode |
| 152 | + ctk.set_appearance_mode("dark") |
| 153 | + ctk.set_default_color_theme("blue") |
| 154 | + |
| 155 | + try: |
| 156 | + # Customize button appearance |
| 157 | + ctk.ThemeManager.theme["CTkButton"]["corner_radius"] = 4 |
| 158 | + ctk.ThemeManager.theme["CTkButton"]["border_width"] = 0 |
| 159 | + |
| 160 | + # Apply theme colors |
| 161 | + theme = get_theme() |
| 162 | + |
| 163 | + # Apply to standard widgets |
| 164 | + ctk.ThemeManager.theme["CTkFrame"]["fg_color"] = [theme["bg_secondary"], theme["bg_secondary"]] |
| 165 | + ctk.ThemeManager.theme["CTkButton"]["fg_color"] = [theme["accent_primary"], theme["accent_primary"]] |
| 166 | + ctk.ThemeManager.theme["CTkButton"]["hover_color"] = [theme["accent_secondary"], theme["accent_secondary"]] |
| 167 | + ctk.ThemeManager.theme["CTkButton"]["text_color"] = [theme["fg_primary"], theme["fg_primary"]] |
| 168 | + |
| 169 | + # Apply to text-based widgets |
| 170 | + ctk.ThemeManager.theme["CTkTextbox"]["fg_color"] = [theme["bg_primary"], theme["bg_primary"]] |
| 171 | + ctk.ThemeManager.theme["CTkTextbox"]["text_color"] = [theme["fg_primary"], theme["fg_primary"]] |
| 172 | + |
| 173 | + # Apply to entry widgets |
| 174 | + ctk.ThemeManager.theme["CTkEntry"]["fg_color"] = [theme["bg_tertiary"], theme["bg_tertiary"]] |
| 175 | + ctk.ThemeManager.theme["CTkEntry"]["text_color"] = [theme["fg_primary"], theme["fg_primary"]] |
| 176 | + |
| 177 | + except (KeyError, AttributeError) as e: |
| 178 | + print(f"Warning: Error setting up theme: {e}") |
| 179 | + pass |
| 180 | + |
| 181 | +def save_theme_preferences(file_path="theme_prefs.json"): |
| 182 | + """Save theme preferences to a file""" |
| 183 | + prefs = { |
| 184 | + "theme": current_theme, |
| 185 | + } |
| 186 | + |
| 187 | + try: |
| 188 | + with open(file_path, 'w') as f: |
| 189 | + json.dump(prefs, f) |
| 190 | + except Exception as e: |
| 191 | + print(f"Error saving theme preferences: {e}") |
| 192 | + |
| 193 | +def load_theme_preferences(file_path="theme_prefs.json"): |
| 194 | + """Load theme preferences from a file""" |
| 195 | + global current_theme |
| 196 | + |
| 197 | + try: |
| 198 | + if os.path.exists(file_path): |
| 199 | + with open(file_path, 'r') as f: |
| 200 | + prefs = json.load(f) |
| 201 | + if "theme" in prefs and prefs["theme"] in THEMES: |
| 202 | + current_theme = prefs["theme"] |
| 203 | + except Exception as e: |
| 204 | + print(f"Error loading theme preferences: {e}") |
0 commit comments