-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckbox.go
90 lines (84 loc) · 2.46 KB
/
checkbox.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
package ux
import (
"image"
"image/color"
"gioui.org/font"
"gioui.org/io/semantic"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
)
type Checkbox struct {
CheckBox *widget.Bool
Label string
Color color.NRGBA
Font font.Font
TextSize unit.Sp
IconColor color.NRGBA
Size unit.Dp
shaper *text.Shaper
checkedStateIcon *widget.Icon
uncheckedStateIcon *widget.Icon
}
func NewCheckBox(checkBox *widget.Bool, label string) Checkbox {
c := Checkbox{
CheckBox: checkBox,
Label: label,
Color: th.Color.DefaultTextWhiteColor,
IconColor: th.Color.BorderLightGrayColor,
TextSize: th.Size.DefaultTextSize,
Size: th.Size.Medium.IconSize,
shaper: th.Shaper,
checkedStateIcon: th.Icon.CheckBoxChecked,
uncheckedStateIcon: th.Icon.CheckBoxUnchecked,
}
c.Font.Typeface = th.Face
return c
}
// SetSize 设置Size
func (c *Checkbox) SetSize(size ElementStyle) {
c.Size = size.IconSize
c.TextSize = size.TextSize
}
// Layout updates the checkBox and displays it.
func (c *Checkbox) Layout(gtx layout.Context) layout.Dimensions {
return c.CheckBox.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
semantic.CheckBox.Add(gtx.Ops)
var icon *widget.Icon
if c.CheckBox.Value {
icon = c.checkedStateIcon
c.IconColor = th.Color.HoveredBorderBlueColor
} else {
icon = c.uncheckedStateIcon
}
if c.CheckBox.Hovered() {
c.IconColor = th.Color.HoveredBorderBlueColor
}
return layout.Flex{Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.UniformInset(2).Layout(gtx, func(gtx layout.Context) layout.Dimensions {
size := gtx.Dp(c.Size)
col := c.IconColor
// if !gtx.Enabled() {
// col = utils.Disabled(col)
// }
gtx.Constraints.Min = image.Point{X: size}
icon.Layout(gtx, col)
return layout.Dimensions{
Size: image.Point{X: size, Y: size},
}
})
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.UniformInset(2).Layout(gtx, func(gtx layout.Context) layout.Dimensions {
colMacro := op.Record(gtx.Ops)
paint.ColorOp{Color: c.Color}.Add(gtx.Ops)
return widget.Label{}.Layout(gtx, c.shaper, c.Font, c.TextSize, c.Label, colMacro.Stop())
})
}),
)
})
}