Repository navigation
Expand file tree
/
Copy pathclass.go
More file actions
337 lines (322 loc) · 12.7 KB
/
Copy pathclass.go
File metadata and controls
337 lines (322 loc) · 12.7 KB
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
package tui
import (
"fmt"
"github.com/grindlemire/go-tui/internal/layout"
"github.com/grindlemire/go-tui/internal/tailwind"
)
// WithClass applies Tailwind-style utility classes at runtime, resolving the
// same class set the .gsx compiler accepts for literal class attributes. The
// generator emits it for expression-valued class attributes. Unknown classes
// are ignored, so validation happens only for literals at compile time.
func WithClass(classes string) Option {
return func(e *Element) {
e.applyClasses(classes)
}
}
// SetClass replaces the element's classes. Every property the previous class
// string set is restored to the value it had before that string was applied,
// then the new string is applied, so omitting a class removes it. A property
// that an option or setter changed after the class string was applied is left
// alone, so options set outside the class string keep their effect in either
// order.
func (e *Element) SetClass(classes string) {
if b, a := e.classBase, e.classApplied; b != nil && a != nil {
for _, op := range e.classOps {
classRestore(e, b, a, op)
}
if e.classText && e.textStyle == a.textStyle && e.textStyleSet == a.textStyleSet {
e.textStyle, e.textStyleSet = b.textStyle, b.textStyleSet
}
}
e.classOps, e.classText, e.classBase, e.classApplied = nil, false, nil, nil
e.applyClasses(classes)
e.MarkDirty()
}
func (e *Element) applyClasses(classes string) {
if e.classBase == nil {
e.classBase = e.captureClassBase()
}
result := tailwind.Parse(classes)
ops := result.Ops()
for _, op := range ops {
e.Apply(classOption(op))
}
e.classOps = append(e.classOps, ops...)
if text := result.Text(); len(text) > 0 {
e.Apply(WithTextStyle(classTextStyle(text)))
e.classText = true
}
e.classApplied = e.captureClassBase()
}
// classBase is the state class ops can write. It is captured before the first
// class string touches the element and again after each string is applied.
type classBase struct {
style LayoutStyle
textAlign TextAlign
border Borders
borderStyle Style
background *Style
scrollbarStyle, scrollbarThumbStyle Style
scrollMode ScrollMode
focusable, tabStop bool
overflow OverflowMode
hidden, truncate, scrollbarHidden, noWrap bool
textGradient, bgGradient, borderGradient *Gradient
textStyle Style
textStyleSet bool
}
func (e *Element) captureClassBase() *classBase {
return &classBase{
style: e.style, textAlign: e.textAlign, border: e.border, borderStyle: *e.borderStyleSlot(),
background: e.background, scrollbarStyle: e.scrollbarStyle, scrollbarThumbStyle: e.scrollbarThumbStyle,
scrollMode: e.scrollMode, focusable: e.focusable, tabStop: e.tabStop, overflow: e.overflow,
hidden: e.hidden, truncate: e.truncate, scrollbarHidden: e.scrollbarHidden, noWrap: e.noWrap,
textGradient: e.textGradient, bgGradient: e.bgGradient, borderGradient: e.borderGradient,
textStyle: e.textStyle, textStyleSet: e.textStyleSet,
}
}
// boxFromOptions reports the border and fixed width a set of element options
// would apply, so components that size their content from their own fields
// can pick up class-derived values before rendering. Only a fixed width
// reaches the component; percent, fraction, and auto widths size the root
// element but leave the input viewport and textarea wrap width at their defaults.
func boxFromOptions(opts []Option) (border Borders, width int) {
if len(opts) == 0 {
return Borders{}, 0
}
probe := New(opts...)
if w := probe.LayoutStyle().Width; w.Unit == layout.UnitFixed {
width = int(w.Amount)
}
return probe.Border(), width
}
// classRestore puts back the property op overwrote, from the snapshot taken
// before the class string was applied, but only while the property still
// holds what the class wrote; a later option or setter wins. Kept next to
// classOption so the two switches stay in step;
// TestSetClass_EveryClassResetsToDefault pins it.
func classRestore(e *Element, b, a *classBase, op tailwind.Op) {
switch op.(type) {
case tailwind.Display:
restore(&e.style.Display, b.style.Display, a.style.Display)
case tailwind.Direction:
restore(&e.style.Direction, b.style.Direction, a.style.Direction)
case tailwind.FlexWrap:
restore(&e.style.FlexWrap, b.style.FlexWrap, a.style.FlexWrap)
case tailwind.AlignContent:
restore(&e.style.AlignContent, b.style.AlignContent, a.style.AlignContent)
case tailwind.FlexGrow:
restore(&e.style.FlexGrow, b.style.FlexGrow, a.style.FlexGrow)
case tailwind.FlexShrink:
restore(&e.style.FlexShrink, b.style.FlexShrink, a.style.FlexShrink)
case tailwind.Justify:
restore(&e.style.JustifyContent, b.style.JustifyContent, a.style.JustifyContent)
case tailwind.AlignItems:
restore(&e.style.AlignItems, b.style.AlignItems, a.style.AlignItems)
case tailwind.AlignSelf:
restore(&e.style.AlignSelf, b.style.AlignSelf, a.style.AlignSelf)
case tailwind.TextAlign:
restore(&e.textAlign, b.textAlign, a.textAlign)
case tailwind.Border, tailwind.BorderSides:
restore(&e.border, b.border, a.border)
case tailwind.BorderColor:
restore(e.borderStyleSlot(), b.borderStyle, a.borderStyle)
case tailwind.Background:
restore(&e.background, b.background, a.background)
case tailwind.ScrollbarColor:
restore(&e.scrollbarStyle, b.scrollbarStyle, a.scrollbarStyle)
case tailwind.ScrollbarThumbColor:
restore(&e.scrollbarThumbStyle, b.scrollbarThumbStyle, a.scrollbarThumbStyle)
case tailwind.Scroll:
// Mirrors what WithScrollable writes.
restore(&e.scrollMode, b.scrollMode, a.scrollMode)
restore(&e.focusable, b.focusable, a.focusable)
restore(&e.scrollbarStyle, b.scrollbarStyle, a.scrollbarStyle)
restore(&e.scrollbarThumbStyle, b.scrollbarThumbStyle, a.scrollbarThumbStyle)
case tailwind.OverflowHidden:
restore(&e.overflow, b.overflow, a.overflow)
case tailwind.Focusable:
restore(&e.focusable, b.focusable, a.focusable)
restore(&e.tabStop, b.tabStop, a.tabStop)
case tailwind.Hidden:
restore(&e.hidden, b.hidden, a.hidden)
case tailwind.Truncate:
restore(&e.truncate, b.truncate, a.truncate)
case tailwind.ScrollbarHidden:
restore(&e.scrollbarHidden, b.scrollbarHidden, a.scrollbarHidden)
case tailwind.Wrap:
restore(&e.noWrap, b.noWrap, a.noWrap)
case tailwind.Gap:
restore(&e.style.Gap, b.style.Gap, a.style.Gap)
case tailwind.Padding, tailwind.PaddingEdges:
restore(&e.style.Padding, b.style.Padding, a.style.Padding)
case tailwind.Margin, tailwind.MarginEdges:
restore(&e.style.Margin, b.style.Margin, a.style.Margin)
case tailwind.Width, tailwind.WidthPercent, tailwind.WidthFraction, tailwind.WidthAuto:
restore(&e.style.Width, b.style.Width, a.style.Width)
case tailwind.Height, tailwind.HeightPercent, tailwind.HeightFraction, tailwind.HeightAuto:
restore(&e.style.Height, b.style.Height, a.style.Height)
case tailwind.MinWidth:
restore(&e.style.MinWidth, b.style.MinWidth, a.style.MinWidth)
case tailwind.MaxWidth:
restore(&e.style.MaxWidth, b.style.MaxWidth, a.style.MaxWidth)
case tailwind.MinHeight:
restore(&e.style.MinHeight, b.style.MinHeight, a.style.MinHeight)
case tailwind.MaxHeight:
restore(&e.style.MaxHeight, b.style.MaxHeight, a.style.MaxHeight)
case tailwind.TextGradient:
restore(&e.textGradient, b.textGradient, a.textGradient)
case tailwind.BackgroundGradient:
restore(&e.bgGradient, b.bgGradient, a.bgGradient)
case tailwind.BorderGradient:
restore(&e.borderGradient, b.borderGradient, a.borderGradient)
default:
panic(fmt.Sprintf("tui: unhandled tailwind op %T", op))
}
}
// restore reverts dst to base only if it still holds the class-written value.
func restore[T comparable](dst *T, base, applied T) {
if *dst == applied {
*dst = base
}
}
// classTextStyle chains text modifiers onto a fresh style in class order, so
// a later color class overrides an earlier one just like the compiled form.
func classTextStyle(ops []tailwind.TextOp) Style {
s := NewStyle()
for _, op := range ops {
switch op := op.(type) {
case tailwind.TextAttr:
switch op.Attr {
case tailwind.Bold:
s = s.Bold()
case tailwind.Dim:
s = s.Dim()
case tailwind.Italic:
s = s.Italic()
case tailwind.Underline:
s = s.Underline()
case tailwind.Blink:
s = s.Blink()
case tailwind.Reverse:
s = s.Reverse()
case tailwind.Strikethrough:
s = s.Strikethrough()
}
case tailwind.Foreground:
s = s.Foreground(classColor(op.Color))
}
}
return s
}
// classOption maps one resolved op to the element option it stands for.
func classOption(op tailwind.Op) Option {
switch op := op.(type) {
case tailwind.Display:
if op.Flex {
return WithDisplay(DisplayFlex)
}
return WithDisplay(DisplayBlock)
case tailwind.Direction:
if op.Column {
return WithDirection(Column)
}
return WithDirection(Row)
case tailwind.FlexWrap:
return WithFlexWrap([...]FlexWrap{WrapNone, Wrap, WrapReverse}[op.Mode])
case tailwind.AlignContent:
return WithAlignContent([...]AlignContent{ContentStart, ContentEnd, ContentCenter, ContentStretch, ContentSpaceBetween, ContentSpaceAround}[op.Value])
case tailwind.FlexGrow:
return WithFlexGrow(float64(op.N))
case tailwind.FlexShrink:
return WithFlexShrink(float64(op.N))
case tailwind.Justify:
return WithJustify([...]Justify{JustifyStart, JustifyCenter, JustifyEnd, JustifySpaceBetween, JustifySpaceAround, JustifySpaceEvenly}[op.Value])
case tailwind.AlignItems:
return WithAlign(classAlign(op.Value))
case tailwind.AlignSelf:
return WithAlignSelf(classAlign(op.Value))
case tailwind.TextAlign:
return WithTextAlign([...]TextAlign{TextAlignLeft, TextAlignCenter, TextAlignRight}[op.Value])
case tailwind.Border:
return WithBorder([...]BorderStyle{BorderNone, BorderSingle, BorderDouble, BorderRounded, BorderThick}[op.Style])
case tailwind.BorderSides:
return WithBorderTRBL(BorderStyle(op.Top), BorderStyle(op.Right), BorderStyle(op.Bottom), BorderStyle(op.Left))
case tailwind.BorderColor:
return WithBorderStyle(NewStyle().Foreground(classColor(op.Color)))
case tailwind.Background:
return WithBackground(NewStyle().Background(classColor(op.Color)))
case tailwind.ScrollbarColor:
return WithScrollbarStyle(NewStyle().Foreground(classColor(op.Color)))
case tailwind.ScrollbarThumbColor:
return WithScrollbarThumbStyle(NewStyle().Foreground(classColor(op.Color)))
case tailwind.Scroll:
return WithScrollable([...]ScrollMode{ScrollBoth, ScrollVertical, ScrollHorizontal}[op.Mode])
case tailwind.OverflowHidden:
return WithOverflow(OverflowHidden)
case tailwind.Focusable:
return WithFocusable(true)
case tailwind.Hidden:
return WithHidden(true)
case tailwind.Truncate:
return WithTruncate(true)
case tailwind.ScrollbarHidden:
return WithScrollbarHidden(true)
case tailwind.Wrap:
return WithWrap(op.Enabled)
case tailwind.Gap:
return WithGap(op.N)
case tailwind.Padding:
return WithPadding(op.N)
case tailwind.Margin:
return WithMargin(op.N)
case tailwind.Width:
return WithWidth(op.N)
case tailwind.Height:
return WithHeight(op.N)
case tailwind.MinWidth:
return WithMinWidth(op.N)
case tailwind.MaxWidth:
return WithMaxWidth(op.N)
case tailwind.MinHeight:
return WithMinHeight(op.N)
case tailwind.MaxHeight:
return WithMaxHeight(op.N)
case tailwind.PaddingEdges:
return WithPaddingTRBL(op.Top, op.Right, op.Bottom, op.Left)
case tailwind.MarginEdges:
return WithMarginTRBL(op.Top, op.Right, op.Bottom, op.Left)
case tailwind.WidthPercent:
return WithWidthPercent(op.Percent)
case tailwind.HeightPercent:
return WithHeightPercent(op.Percent)
case tailwind.WidthFraction:
return WithWidthPercent(op.Percent())
case tailwind.HeightFraction:
return WithHeightPercent(op.Percent())
case tailwind.WidthAuto:
return WithWidthAuto()
case tailwind.HeightAuto:
return WithHeightAuto()
case tailwind.TextGradient:
return WithTextGradient(classGradient(op.Start, op.End, op.Direction))
case tailwind.BackgroundGradient:
return WithBackgroundGradient(classGradient(op.Start, op.End, op.Direction))
case tailwind.BorderGradient:
return WithBorderGradient(classGradient(op.Start, op.End, op.Direction))
}
panic(fmt.Sprintf("tui: unhandled tailwind op %T", op))
}
func classAlign(v tailwind.AlignValue) Align {
return [...]Align{AlignStart, AlignCenter, AlignEnd, AlignStretch}[v]
}
func classColor(c tailwind.Color) Color {
if c.IsRGB {
return RGBColor(c.R, c.G, c.B)
}
return ANSIColor(c.Index)
}
func classGradient(start, end tailwind.Color, dir tailwind.GradientDirection) Gradient {
d := [...]GradientDirection{GradientHorizontal, GradientVertical, GradientDiagonalDown, GradientDiagonalUp}[dir]
return NewGradient(classColor(start), classColor(end)).WithDirection(d)
}