-
Notifications
You must be signed in to change notification settings - Fork 5
/
theme-editor.go
310 lines (280 loc) · 7.27 KB
/
theme-editor.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
package main
import (
"image/color"
"log"
"gioui.org/f32"
"gioui.org/font/gofont"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget/material"
"git.sr.ht/~whereswaldon/sprig/core"
"git.sr.ht/~whereswaldon/sprig/icons"
sprigTheme "git.sr.ht/~whereswaldon/sprig/widget/theme"
"gioui.org/x/colorpicker"
materials "gioui.org/x/component"
)
type ThemeEditorView struct {
manager ViewManager
core.App
PrimaryDefault colorpicker.State
PrimaryDark colorpicker.State
PrimaryLight colorpicker.State
SecondaryDefault colorpicker.State
SecondaryDark colorpicker.State
SecondaryLight colorpicker.State
BackgroundDefault colorpicker.State
BackgroundDark colorpicker.State
BackgroundLight colorpicker.State
TextColor colorpicker.State
HintColor colorpicker.State
InvertedTextColor colorpicker.State
ColorsList layout.List
listElems []colorListElement
AncestorMux colorpicker.MuxState
DescendantMux colorpicker.MuxState
SelectedMux colorpicker.MuxState
SiblingMux colorpicker.MuxState
NonselectedMux colorpicker.MuxState
MuxList layout.List
muxListElems []muxListElement
*sprigTheme.Theme
widgetTheme *material.Theme
}
type colorListElement struct {
*colorpicker.State
Label string
TargetColors []*color.NRGBA
}
type muxListElement struct {
*colorpicker.MuxState
Label string
TargetColor **color.NRGBA
}
var _ View = &ThemeEditorView{}
func NewThemeEditorView(app core.App) View {
th := material.NewTheme()
th.Shaper = text.NewShaper(text.WithCollection(gofont.Collection()))
c := &ThemeEditorView{
App: app,
widgetTheme: th,
}
c.ConfigurePickersFor(app.Theme().Current())
return c
}
func (c *ThemeEditorView) ConfigurePickersFor(th *sprigTheme.Theme) {
c.PrimaryDefault.SetColor(th.Primary.Default.Bg)
c.PrimaryDark.SetColor(th.Primary.Dark.Bg)
c.PrimaryLight.SetColor(th.Primary.Light.Bg)
c.SecondaryDefault.SetColor(th.Secondary.Default.Bg)
c.SecondaryDark.SetColor(th.Secondary.Dark.Bg)
c.SecondaryLight.SetColor(th.Secondary.Light.Bg)
c.BackgroundDefault.SetColor(th.Background.Default.Bg)
c.BackgroundDark.SetColor(th.Background.Dark.Bg)
c.BackgroundLight.SetColor(th.Background.Light.Bg)
c.ColorsList.Axis = layout.Vertical
c.listElems = []colorListElement{
{
Label: "Primary",
TargetColors: []*color.NRGBA{
&th.Primary.Default.Bg,
&th.Theme.Palette.Bg,
},
State: &c.PrimaryDefault,
},
{
Label: "Primary Light",
TargetColors: []*color.NRGBA{
&th.Primary.Light.Bg,
},
State: &c.PrimaryLight,
},
{
Label: "Primary Dark",
TargetColors: []*color.NRGBA{
&th.Primary.Dark.Bg,
},
State: &c.PrimaryDark,
},
{
Label: "Secondary",
TargetColors: []*color.NRGBA{
&th.Secondary.Default.Bg,
},
State: &c.SecondaryDefault,
},
{
Label: "Secondary Light",
TargetColors: []*color.NRGBA{
&th.Secondary.Light.Bg,
},
State: &c.SecondaryLight,
},
{
Label: "Secondary Dark",
TargetColors: []*color.NRGBA{
&th.Secondary.Dark.Bg,
},
State: &c.SecondaryDark,
},
{
Label: "Background",
TargetColors: []*color.NRGBA{
&th.Background.Default.Bg,
},
State: &c.BackgroundDefault,
},
{
Label: "Background Light",
TargetColors: []*color.NRGBA{
&th.Background.Light.Bg,
},
State: &c.BackgroundLight,
},
{
Label: "Background Dark",
TargetColors: []*color.NRGBA{
&th.Background.Dark.Bg,
},
State: &c.BackgroundDark,
},
}
muxOptions := []colorpicker.MuxOption{}
for _, elem := range c.listElems {
if len(elem.TargetColors) < 1 || elem.TargetColors[0] == nil {
continue
}
elem.SetColor(*elem.TargetColors[0])
muxOptions = append(muxOptions, colorpicker.MuxOption{
Label: elem.Label,
Value: elem.TargetColors[0],
})
}
c.muxListElems = []muxListElement{
{
Label: "Ancestors",
MuxState: &c.AncestorMux,
TargetColor: &th.Ancestors,
},
{
Label: "Descendants",
MuxState: &c.DescendantMux,
TargetColor: &th.Descendants,
},
{
Label: "Selected",
MuxState: &c.SelectedMux,
TargetColor: &th.Selected,
},
{
Label: "Siblings",
MuxState: &c.SiblingMux,
TargetColor: &th.Siblings,
},
{
Label: "Unselected",
MuxState: &c.NonselectedMux,
TargetColor: &th.Unselected,
},
}
for _, mux := range c.muxListElems {
*mux.MuxState = colorpicker.NewMuxState(muxOptions...)
}
}
func (c *ThemeEditorView) BecomeVisible() {
c.ConfigurePickersFor(c.App.Theme().Current())
}
func (c *ThemeEditorView) HandleIntent(intent Intent) {}
func (c *ThemeEditorView) NavItem() *materials.NavItem {
return &materials.NavItem{
Name: "Theme",
Icon: icons.CancelReplyIcon,
}
}
func (c *ThemeEditorView) AppBarData() (bool, string, []materials.AppBarAction, []materials.OverflowAction) {
return true, "Theme", []materials.AppBarAction{}, []materials.OverflowAction{}
}
func (c *ThemeEditorView) HandleClipboard(contents string) {
}
func (c *ThemeEditorView) Update(gtx layout.Context) {
for i, elem := range c.listElems {
if elem.Changed() {
for _, target := range elem.TargetColors {
*target = elem.Color()
}
op.InvalidateOp{}.Add(gtx.Ops)
log.Printf("picker %d changed", i)
}
}
for _, elem := range c.muxListElems {
if elem.Update(gtx) {
*elem.TargetColor = elem.Color()
op.InvalidateOp{}.Add(gtx.Ops)
log.Printf("mux changed")
}
}
}
func (c *ThemeEditorView) Layout(gtx layout.Context) layout.Dimensions {
return c.layoutPickers(gtx)
}
func (c *ThemeEditorView) layoutPickers(gtx layout.Context) layout.Dimensions {
return c.ColorsList.Layout(gtx, len(c.listElems)+1, func(gtx C, index int) D {
if index == len(c.listElems) {
return c.layoutMuxes(gtx)
}
return layout.Stack{}.Layout(gtx,
layout.Expanded(func(gtx C) D {
return sprigTheme.Rect{
Color: color.NRGBA{A: 255},
Size: f32.Point{
X: float32(gtx.Constraints.Min.X),
Y: float32(gtx.Constraints.Min.Y),
},
}.Layout(gtx)
}),
layout.Stacked(func(gtx C) D {
return layout.UniformInset(unit.Dp(3)).Layout(gtx, func(gtx C) D {
return layout.Stack{}.Layout(gtx,
layout.Expanded(func(gtx C) D {
return sprigTheme.Rect{
Color: color.NRGBA{R: 255, G: 255, B: 255, A: 255},
Size: f32.Point{
X: float32(gtx.Constraints.Min.X),
Y: float32(gtx.Constraints.Min.Y),
},
}.Layout(gtx)
}),
layout.Stacked(func(gtx C) D {
elem := c.listElems[index]
dims := colorpicker.Picker(c.widgetTheme, elem.State, elem.Label).Layout(gtx)
return dims
}),
)
})
}),
)
})
}
func (c *ThemeEditorView) layoutMuxes(gtx layout.Context) layout.Dimensions {
return layout.Stack{}.Layout(gtx,
layout.Expanded(func(gtx C) D {
return sprigTheme.Rect{
Color: color.NRGBA{R: 255, G: 255, B: 255, A: 255},
Size: f32.Point{
X: float32(gtx.Constraints.Min.X),
Y: float32(gtx.Constraints.Min.Y),
},
}.Layout(gtx)
}),
layout.Stacked(func(gtx C) D {
return c.MuxList.Layout(gtx, len(c.muxListElems), func(gtx C, index int) D {
element := c.muxListElems[index]
return colorpicker.Mux(c.widgetTheme, element.MuxState, element.Label).Layout(gtx)
})
}),
)
}
func (c *ThemeEditorView) SetManager(mgr ViewManager) {
c.manager = mgr
}