/
button.go
115 lines (96 loc) · 2.12 KB
/
button.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
package widget
import (
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/widget"
. "github.com/arjenjb/cu"
"image/color"
)
var buttonTextColorPrimary = color.NRGBA{
R: 255,
G: 255,
B: 255,
A: 255,
}
var buttonTextColor = color.NRGBA{
A: 255,
}
var buttonTextColorDisabled = color.NRGBA{
R: 128,
G: 128,
B: 128,
A: 255,
}
type ButtonWidget struct {
label string
clickable *widget.Clickable
theme *Theme
options buttonOptions
}
var tag struct{}
func (b *ButtonWidget) Layout(gtx layout.Context) layout.Dimensions {
fontSize := b.theme.TextSize
if b.options.big {
fontSize = 15
}
gtx.Constraints.Min.X = gtx.Dp(80)
gtx.Constraints.Min.Y = gtx.Dp(32)
var color = buttonTextColor
if b.options.disabled {
color = buttonTextColorDisabled
} else if b.options.primary {
color = buttonTextColorPrimary
}
buttonText := func(gtx layout.Context) layout.Dimensions {
return b.theme.Mh(Scaled(1), func(gtx layout.Context) layout.Dimensions {
colMacro := op.Record(gtx.Ops)
paint.ColorOp{Color: color}.Add(gtx.Ops)
return widget.Label{Alignment: text.Middle}.Layout(gtx, b.theme.Shaper, b.theme.Font.SansSerif, fontSize, b.label, colMacro.Stop())
})(gtx)
}
return ButtonStyle{
CornerRadius: 4,
Button: b.clickable,
Disabled: b.options.disabled,
Primary: b.options.primary,
}.Layout(gtx, buttonText)
}
type ButtonOptions func(options *buttonOptions)
func Big() func(*buttonOptions) {
return func(o *buttonOptions) {
o.big = true
}
}
func Disabled(b bool) func(*buttonOptions) {
return func(o *buttonOptions) {
o.disabled = b
}
}
func Primary() func(*buttonOptions) {
return func(o *buttonOptions) {
o.primary = true
}
}
type buttonOptions struct {
primary bool
big bool
disabled bool
}
func Button(t *Theme, clickable *widget.Clickable, label string, options ...ButtonOptions) *ButtonWidget {
o := buttonOptions{
primary: false,
big: false,
disabled: false,
}
for _, each := range options {
each(&o)
}
return &ButtonWidget{
theme: t,
label: label,
clickable: clickable,
options: o,
}
}