forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme.go
373 lines (332 loc) · 10.1 KB
/
theme.go
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Package theme defines how a Fyne app should look when rendered.
package theme // import "fyne.io/fyne/v2/theme"
import (
"image/color"
"os"
"strings"
"fyne.io/fyne/v2"
)
const (
// VariantDark is the version of a theme that satisfies a user preference for a light look.
//
// Since: 2.0
VariantDark fyne.ThemeVariant = 0
// VariantLight is the version of a theme that satisfies a user preference for a dark look.
//
// Since: 2.0
VariantLight fyne.ThemeVariant = 1
// potential for adding theme types such as high visibility or monochrome...
variantNameUserPreference fyne.ThemeVariant = 2 // locally used in builtinTheme for backward compatibility
)
// DarkTheme defines the built-in dark theme colors and sizes.
//
// Deprecated: This method ignores user preference and should not be used, it will be removed in v3.0.
func DarkTheme() fyne.Theme {
theme := &builtinTheme{variant: VariantDark}
theme.initFonts()
return theme
}
// DefaultTheme returns a built-in theme that can adapt to the user preference of light or dark colors.
//
// Since: 2.0
func DefaultTheme() fyne.Theme {
if defaultTheme == nil {
defaultTheme = setupDefaultTheme()
}
return defaultTheme
}
// LightTheme defines the built-in light theme colors and sizes.
//
// Deprecated: This method ignores user preference and should not be used, it will be removed in v3.0.
func LightTheme() fyne.Theme {
theme := &builtinTheme{variant: VariantLight}
theme.initFonts()
return theme
}
var (
defaultTheme fyne.Theme
)
type builtinTheme struct {
variant fyne.ThemeVariant
regular, bold, italic, boldItalic, monospace, symbol fyne.Resource
}
func (t *builtinTheme) initFonts() {
t.regular = regular
t.bold = bold
t.italic = italic
t.boldItalic = bolditalic
t.monospace = monospace
t.symbol = symbol
font := os.Getenv("FYNE_FONT")
if font != "" {
t.regular = loadCustomFont(font, "Regular", regular)
if t.regular == regular { // failed to load
t.bold = loadCustomFont(font, "Bold", bold)
t.italic = loadCustomFont(font, "Italic", italic)
t.boldItalic = loadCustomFont(font, "BoldItalic", bolditalic)
} else { // first custom font loaded, fall back to that
t.bold = loadCustomFont(font, "Bold", t.regular)
t.italic = loadCustomFont(font, "Italic", t.regular)
t.boldItalic = loadCustomFont(font, "BoldItalic", t.regular)
}
}
font = os.Getenv("FYNE_FONT_MONOSPACE")
if font != "" {
t.monospace = loadCustomFont(font, "Regular", monospace)
}
font = os.Getenv("FYNE_FONT_SYMBOL")
if font != "" {
t.symbol = loadCustomFont(font, "Regular", symbol)
}
}
func (t *builtinTheme) Color(n fyne.ThemeColorName, v fyne.ThemeVariant) color.Color {
if t.variant != variantNameUserPreference {
v = t.variant
}
primary := fyne.CurrentApp().Settings().PrimaryColor()
if n == ColorNamePrimary || n == ColorNameHyperlink {
return primaryColorNamed(primary)
} else if n == ColorNameFocus {
return focusColorNamed(primary)
} else if n == ColorNameSelection {
return selectionColorNamed(primary)
}
if v == VariantLight {
return lightPaletColorNamed(n)
}
return darkPaletColorNamed(n)
}
func (t *builtinTheme) Font(style fyne.TextStyle) fyne.Resource {
if style.Monospace {
return t.monospace
}
if style.Bold {
if style.Italic {
return t.boldItalic
}
return t.bold
}
if style.Italic {
return t.italic
}
if style.Symbol {
return t.symbol
}
return t.regular
}
func (t *builtinTheme) Size(s fyne.ThemeSizeName) float32 {
switch s {
case SizeNameSeparatorThickness:
return 1
case SizeNameInlineIcon:
return 20
case SizeNameInnerPadding:
return 8
case SizeNameLineSpacing:
return 4
case SizeNamePadding:
return 4
case SizeNameScrollBar:
return 16
case SizeNameScrollBarSmall:
return 3
case SizeNameText:
return 14
case SizeNameHeadingText:
return 24
case SizeNameSubHeadingText:
return 18
case SizeNameCaptionText:
return 11
case SizeNameInputBorder:
return 1
case SizeNameInputRadius:
return 5
case SizeNameSelectionRadius:
return 3
default:
return 0
}
}
func current() fyne.Theme {
app := fyne.CurrentApp()
if app == nil {
return DarkTheme()
}
currentTheme := app.Settings().Theme()
if currentTheme == nil {
return DarkTheme()
}
return currentTheme
}
func currentVariant() fyne.ThemeVariant {
if std, ok := current().(*builtinTheme); ok {
if std.variant != variantNameUserPreference {
return std.variant // override if using the old LightTheme() or DarkTheme() constructor
}
}
return fyne.CurrentApp().Settings().ThemeVariant()
}
func darkPaletColorNamed(name fyne.ThemeColorName) color.Color {
switch name {
case ColorNameBackground:
return color.NRGBA{R: 0x17, G: 0x17, B: 0x18, A: 0xff}
case ColorNameButton:
return color.NRGBA{R: 0x28, G: 0x29, B: 0x2e, A: 0xff}
case ColorNameDisabled:
return color.NRGBA{R: 0x39, G: 0x39, B: 0x3a, A: 0xff}
case ColorNameDisabledButton:
return color.NRGBA{R: 0x28, G: 0x29, B: 0x2e, A: 0xff}
case ColorNameError:
return errorColor
case ColorNameForeground:
return color.NRGBA{R: 0xf3, G: 0xf3, B: 0xf3, A: 0xff}
case ColorNameHover:
return color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0x0f}
case ColorNameHeaderBackground:
return color.NRGBA{R: 0x1b, G: 0x1b, B: 0x1b, A: 0xff}
case ColorNameInputBackground:
return color.NRGBA{R: 0x20, G: 0x20, B: 0x23, A: 0xff}
case ColorNameInputBorder:
return color.NRGBA{R: 0x39, G: 0x39, B: 0x3a, A: 0xff}
case ColorNameMenuBackground:
return color.NRGBA{R: 0x28, G: 0x29, B: 0x2e, A: 0xff}
case ColorNameOverlayBackground:
return color.NRGBA{R: 0x18, G: 0x1d, B: 0x25, A: 0xff}
case ColorNamePlaceHolder:
return color.NRGBA{R: 0xb2, G: 0xb2, B: 0xb2, A: 0xff}
case ColorNamePressed:
return color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0x66}
case ColorNameScrollBar:
return color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0x99}
case ColorNameSeparator:
return color.NRGBA{R: 0x0, G: 0x0, B: 0x0, A: 0xff}
case ColorNameShadow:
return color.NRGBA{A: 0x66}
case ColorNameSuccess:
return successColor
case ColorNameWarning:
return warningColor
}
return color.Transparent
}
func focusColorNamed(name string) color.NRGBA {
switch name {
case ColorRed:
return color.NRGBA{R: 0xf4, G: 0x43, B: 0x36, A: 0x7f}
case ColorOrange:
return color.NRGBA{R: 0xff, G: 0x98, B: 0x00, A: 0x7f}
case ColorYellow:
return color.NRGBA{R: 0xff, G: 0xeb, B: 0x3b, A: 0x7f}
case ColorGreen:
return color.NRGBA{R: 0x8b, G: 0xc3, B: 0x4a, A: 0x7f}
case ColorPurple:
return color.NRGBA{R: 0x9c, G: 0x27, B: 0xb0, A: 0x7f}
case ColorBrown:
return color.NRGBA{R: 0x79, G: 0x55, B: 0x48, A: 0x7f}
case ColorGray:
return color.NRGBA{R: 0x9e, G: 0x9e, B: 0x9e, A: 0x7f}
}
// We return the value for ColorBlue for every other value.
// There is no need to have it in the switch above.
return color.NRGBA{R: 0x00, G: 0x6C, B: 0xff, A: 0x2a}
}
func lightPaletColorNamed(name fyne.ThemeColorName) color.Color {
switch name {
case ColorNameBackground:
return color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
case ColorNameButton:
return color.NRGBA{R: 0xf5, G: 0xf5, B: 0xf5, A: 0xff}
case ColorNameDisabled:
return color.NRGBA{R: 0xe3, G: 0xe3, B: 0xe3, A: 0xff}
case ColorNameDisabledButton:
return color.NRGBA{R: 0xf5, G: 0xf5, B: 0xf5, A: 0xff}
case ColorNameError:
return errorColor
case ColorNameForeground:
return color.NRGBA{R: 0x56, G: 0x56, B: 0x56, A: 0xff}
case ColorNameHover:
return color.NRGBA{A: 0x0f}
case ColorNameHeaderBackground:
return color.NRGBA{R: 0xf9, G: 0xf9, B: 0xf9, A: 0xff}
case ColorNameInputBackground:
return color.NRGBA{R: 0xf3, G: 0xf3, B: 0xf3, A: 0xff}
case ColorNameInputBorder:
return color.NRGBA{R: 0xe3, G: 0xe3, B: 0xe3, A: 0xff}
case ColorNameMenuBackground:
return color.NRGBA{R: 0xf5, G: 0xf5, B: 0xf5, A: 0xff}
case ColorNameOverlayBackground:
return color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
case ColorNamePlaceHolder:
return color.NRGBA{R: 0x88, G: 0x88, B: 0x88, A: 0xff}
case ColorNamePressed:
return color.NRGBA{A: 0x19}
case ColorNameScrollBar:
return color.NRGBA{A: 0x99}
case ColorNameSeparator:
return color.NRGBA{R: 0xe3, G: 0xe3, B: 0xe3, A: 0xff}
case ColorNameShadow:
return color.NRGBA{A: 0x33}
case ColorNameSuccess:
return successColor
case ColorNameWarning:
return warningColor
}
return color.Transparent
}
func loadCustomFont(env, variant string, fallback fyne.Resource) fyne.Resource {
variantPath := strings.Replace(env, "Regular", variant, -1)
res, err := fyne.LoadResourceFromPath(variantPath)
if err != nil {
fyne.LogError("Error loading specified font", err)
return fallback
}
return res
}
func primaryColorNamed(name string) color.NRGBA {
switch name {
case ColorRed:
return color.NRGBA{R: 0xf4, G: 0x43, B: 0x36, A: 0xff}
case ColorOrange:
return color.NRGBA{R: 0xff, G: 0x98, B: 0x00, A: 0xff}
case ColorYellow:
return color.NRGBA{R: 0xff, G: 0xeb, B: 0x3b, A: 0xff}
case ColorGreen:
return color.NRGBA{R: 0x8b, G: 0xc3, B: 0x4a, A: 0xff}
case ColorPurple:
return color.NRGBA{R: 0x9c, G: 0x27, B: 0xb0, A: 0xff}
case ColorBrown:
return color.NRGBA{R: 0x79, G: 0x55, B: 0x48, A: 0xff}
case ColorGray:
return color.NRGBA{R: 0x9e, G: 0x9e, B: 0x9e, A: 0xff}
}
// We return the value for ColorBlue for every other value.
// There is no need to have it in the switch above.
return color.NRGBA{R: 0x29, G: 0x6f, B: 0xf6, A: 0xff}
}
func selectionColorNamed(name string) color.NRGBA {
switch name {
case ColorRed:
return color.NRGBA{R: 0xf4, G: 0x43, B: 0x36, A: 0x3f}
case ColorOrange:
return color.NRGBA{R: 0xff, G: 0x98, B: 0x00, A: 0x3f}
case ColorYellow:
return color.NRGBA{R: 0xff, G: 0xeb, B: 0x3b, A: 0x3f}
case ColorGreen:
return color.NRGBA{R: 0x8b, G: 0xc3, B: 0x4a, A: 0x3f}
case ColorPurple:
return color.NRGBA{R: 0x9c, G: 0x27, B: 0xb0, A: 0x3f}
case ColorBrown:
return color.NRGBA{R: 0x79, G: 0x55, B: 0x48, A: 0x3f}
case ColorGray:
return color.NRGBA{R: 0x9e, G: 0x9e, B: 0x9e, A: 0x3f}
}
// We return the value for ColorBlue for every other value.
// There is no need to have it in the switch above.
return color.NRGBA{R: 0x00, G: 0x6C, B: 0xff, A: 0x40}
}
func setupDefaultTheme() fyne.Theme {
theme := &builtinTheme{variant: variantNameUserPreference}
theme.initFonts()
return theme
}