summaryrefslogtreecommitdiffhomepage
path: root/packages/tui/internal/theme/manager.go
blob: cb6c539914cd7becde01bf84f870e0d90cf10473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package theme

import (
	"fmt"
	"log/slog"
	"slices"
	"strings"
	"sync"

	"github.com/alecthomas/chroma/v2/styles"
)

// Manager handles theme registration, selection, and retrieval.
// It maintains a registry of available themes and tracks the currently active theme.
type Manager struct {
	themes      map[string]Theme
	currentName string
	mu          sync.RWMutex
}

// Global instance of the theme manager
var globalManager = &Manager{
	themes:      make(map[string]Theme),
	currentName: "",
}

// Default theme instance for custom theme defaulting
var defaultThemeColors = NewOpenCodeTheme()

// RegisterTheme adds a new theme to the registry.
// If this is the first theme registered, it becomes the default.
func RegisterTheme(name string, theme Theme) {
	globalManager.mu.Lock()
	defer globalManager.mu.Unlock()

	globalManager.themes[name] = theme

	// If this is the first theme, make it the default
	if globalManager.currentName == "" {
		globalManager.currentName = name
	}
}

// SetTheme changes the active theme to the one with the specified name.
// Returns an error if the theme doesn't exist.
func SetTheme(name string) error {
	globalManager.mu.Lock()
	defer globalManager.mu.Unlock()
	delete(styles.Registry, "charm")

	// Handle custom theme
	// if name == "custom" {
	// 	cfg := config.Get()
	// 	if cfg == nil || cfg.TUI.CustomTheme == nil || len(cfg.TUI.CustomTheme) == 0 {
	// 		return fmt.Errorf("custom theme selected but no custom theme colors defined in config")
	// 	}
	//
	// 	customTheme, err := LoadCustomTheme(cfg.TUI.CustomTheme)
	// 	if err != nil {
	// 		return fmt.Errorf("failed to load custom theme: %w", err)
	// 	}
	//
	// 	// Register the custom theme
	// 	globalManager.themes["custom"] = customTheme

	if _, exists := globalManager.themes[name]; !exists {
		return fmt.Errorf("theme '%s' not found", name)
	}

	globalManager.currentName = name

	return nil
}

// CurrentTheme returns the currently active theme.
// If no theme is set, it returns nil.
func CurrentTheme() Theme {
	globalManager.mu.RLock()
	defer globalManager.mu.RUnlock()

	if globalManager.currentName == "" {
		return nil
	}

	return globalManager.themes[globalManager.currentName]
}

// CurrentThemeName returns the name of the currently active theme.
func CurrentThemeName() string {
	globalManager.mu.RLock()
	defer globalManager.mu.RUnlock()

	return globalManager.currentName
}

// AvailableThemes returns a list of all registered theme names.
func AvailableThemes() []string {
	globalManager.mu.RLock()
	defer globalManager.mu.RUnlock()

	names := make([]string, 0, len(globalManager.themes))
	for name := range globalManager.themes {
		names = append(names, name)
	}
	slices.SortFunc(names, func(a, b string) int {
		if a == "opencode" {
			return -1
		} else if b == "opencode" {
			return 1
		}
		return strings.Compare(a, b)
	})
	return names
}

// GetTheme returns a specific theme by name.
// Returns nil if the theme doesn't exist.
func GetTheme(name string) Theme {
	globalManager.mu.RLock()
	defer globalManager.mu.RUnlock()

	return globalManager.themes[name]
}

// LoadCustomTheme creates a new theme instance based on the custom theme colors
// defined in the configuration. It uses the default OpenCode theme as a base
// and overrides colors that are specified in the customTheme map.
func LoadCustomTheme(customTheme map[string]any) (Theme, error) {
	// Create a new theme based on the default OpenCode theme
	theme := NewOpenCodeTheme()

	// Process each color in the custom theme map
	for key, value := range customTheme {
		adaptiveColor, err := ParseAdaptiveColor(value)
		if err != nil {
			slog.Warn("Invalid color definition in custom theme", "key", key, "error", err)
			continue // Skip this color but continue processing others
		}

		// Set the color in the theme based on the key
		switch strings.ToLower(key) {
		case "primary":
			theme.PrimaryColor = adaptiveColor
		case "secondary":
			theme.SecondaryColor = adaptiveColor
		case "accent":
			theme.AccentColor = adaptiveColor
		case "error":
			theme.ErrorColor = adaptiveColor
		case "warning":
			theme.WarningColor = adaptiveColor
		case "success":
			theme.SuccessColor = adaptiveColor
		case "info":
			theme.InfoColor = adaptiveColor
		case "text":
			theme.TextColor = adaptiveColor
		case "textmuted":
			theme.TextMutedColor = adaptiveColor
		case "textemphasized":
			theme.TextEmphasizedColor = adaptiveColor
		case "background":
			theme.BackgroundColor = adaptiveColor
		case "backgroundsecondary":
			theme.BackgroundSecondaryColor = adaptiveColor
		case "backgrounddarker":
			theme.BackgroundDarkerColor = adaptiveColor
		case "bordernormal":
			theme.BorderNormalColor = adaptiveColor
		case "borderfocused":
			theme.BorderFocusedColor = adaptiveColor
		case "borderdim":
			theme.BorderDimColor = adaptiveColor
		case "diffadded":
			theme.DiffAddedColor = adaptiveColor
		case "diffremoved":
			theme.DiffRemovedColor = adaptiveColor
		case "diffcontext":
			theme.DiffContextColor = adaptiveColor
		case "diffhunkheader":
			theme.DiffHunkHeaderColor = adaptiveColor
		case "diffhighlightadded":
			theme.DiffHighlightAddedColor = adaptiveColor
		case "diffhighlightremoved":
			theme.DiffHighlightRemovedColor = adaptiveColor
		case "diffaddedbg":
			theme.DiffAddedBgColor = adaptiveColor
		case "diffremovedbg":
			theme.DiffRemovedBgColor = adaptiveColor
		case "diffcontextbg":
			theme.DiffContextBgColor = adaptiveColor
		case "difflinenumber":
			theme.DiffLineNumberColor = adaptiveColor
		case "diffaddedlinenumberbg":
			theme.DiffAddedLineNumberBgColor = adaptiveColor
		case "diffremovedlinenumberbg":
			theme.DiffRemovedLineNumberBgColor = adaptiveColor
		case "syntaxcomment":
			theme.SyntaxCommentColor = adaptiveColor
		case "syntaxkeyword":
			theme.SyntaxKeywordColor = adaptiveColor
		case "syntaxfunction":
			theme.SyntaxFunctionColor = adaptiveColor
		case "syntaxvariable":
			theme.SyntaxVariableColor = adaptiveColor
		case "syntaxstring":
			theme.SyntaxStringColor = adaptiveColor
		case "syntaxnumber":
			theme.SyntaxNumberColor = adaptiveColor
		case "syntaxtype":
			theme.SyntaxTypeColor = adaptiveColor
		case "syntaxoperator":
			theme.SyntaxOperatorColor = adaptiveColor
		case "syntaxpunctuation":
			theme.SyntaxPunctuationColor = adaptiveColor
		case "markdowntext":
			theme.MarkdownTextColor = adaptiveColor
		case "markdownheading":
			theme.MarkdownHeadingColor = adaptiveColor
		case "markdownlink":
			theme.MarkdownLinkColor = adaptiveColor
		case "markdownlinktext":
			theme.MarkdownLinkTextColor = adaptiveColor
		case "markdowncode":
			theme.MarkdownCodeColor = adaptiveColor
		case "markdownblockquote":
			theme.MarkdownBlockQuoteColor = adaptiveColor
		case "markdownemph":
			theme.MarkdownEmphColor = adaptiveColor
		case "markdownstrong":
			theme.MarkdownStrongColor = adaptiveColor
		case "markdownhorizontalrule":
			theme.MarkdownHorizontalRuleColor = adaptiveColor
		case "markdownlistitem":
			theme.MarkdownListItemColor = adaptiveColor
		case "markdownlistitemenum":
			theme.MarkdownListEnumerationColor = adaptiveColor
		case "markdownimage":
			theme.MarkdownImageColor = adaptiveColor
		case "markdownimagetext":
			theme.MarkdownImageTextColor = adaptiveColor
		case "markdowncodeblock":
			theme.MarkdownCodeBlockColor = adaptiveColor
		case "markdownlistenumeration":
			theme.MarkdownListEnumerationColor = adaptiveColor
		default:
			slog.Warn("Unknown color key in custom theme", "key", key)
		}
	}

	return theme, nil
}