checkable.go raw
1 package gel
2
3 import (
4 "image"
5
6 "github.com/p9c/gio/io/pointer"
7 l "github.com/p9c/gio/layout"
8 "github.com/p9c/gio/op/paint"
9 "github.com/p9c/gio/text"
10 "github.com/p9c/gio/unit"
11 "golang.org/x/exp/shiny/materialdesign/icons"
12 )
13
14 type Checkable struct {
15 *Window
16 label string
17 color string
18 font text.Font
19 textSize unit.Value
20 iconColor string
21 size unit.Value
22 checkedStateIcon *[]byte
23 uncheckedStateIcon *[]byte
24 shaper text.Shaper
25 checked bool
26 }
27
28 // Checkable creates a checkbox type widget
29 func (w *Window) Checkable() *Checkable {
30 font := "bariol regular"
31 var f text.Font
32 if fon, e := w.Theme.collection.Font(font); !E.Chk(e) {
33 f = fon
34 }
35 // for i := range w.collection {
36 // if w.collection[i].Font.Typeface == text.Typeface(font) {
37 // f = w.collection[i].Font
38 // break
39 // }
40 // }
41 return &Checkable{
42 Window: w,
43 label: "checkable",
44 color: "Primary",
45 font: f,
46 textSize: w.TextSize.Scale(14.0 / 16.0),
47 iconColor: "Primary",
48 size: w.TextSize.Scale(1.5),
49 checkedStateIcon: &icons.ToggleCheckBox,
50 uncheckedStateIcon: &icons.ToggleCheckBoxOutlineBlank,
51 shaper: w.shaper,
52 }
53 }
54
55 // Label sets the label on the checkbox
56 func (c *Checkable) Label(txt string) *Checkable {
57 c.label = txt
58 return c
59 }
60
61 // Color sets the color of the checkbox label
62 func (c *Checkable) Color(color string) *Checkable {
63 c.color = color
64 return c
65 }
66
67 // Font sets the font used on the label
68 func (c *Checkable) Font(font string) *Checkable {
69 if fon, e := c.Theme.collection.Font(font); !E.Chk(e) {
70 c.font = fon
71 }
72 return c
73 }
74
75 // TextScale sets the size of the font relative to the base text size
76 func (c *Checkable) TextScale(scale float32) *Checkable {
77 c.textSize = c.Theme.TextSize.Scale(scale)
78 return c
79 }
80
81 // IconColor sets the color of the icon
82 func (c *Checkable) IconColor(color string) *Checkable {
83 c.iconColor = color
84 return c
85 }
86
87 // Scale sets the size of the checkbox icon relative to the base font size
88 func (c *Checkable) Scale(size float32) *Checkable {
89 c.size = c.Theme.TextSize.Scale(size)
90 return c
91 }
92
93 // CheckedStateIcon loads the icon for the checked state
94 func (c *Checkable) CheckedStateIcon(ic *[]byte) *Checkable {
95 c.checkedStateIcon = ic
96 return c
97 }
98
99 // UncheckedStateIcon loads the icon for the unchecked state
100 func (c *Checkable) UncheckedStateIcon(ic *[]byte) *Checkable {
101 c.uncheckedStateIcon = ic
102 return c
103 }
104
105 // Fn renders the checkbox widget
106 func (c *Checkable) Fn(gtx l.Context, checked bool) l.Dimensions {
107 var icon *Icon
108 if checked {
109 icon = c.Icon().
110 Color(c.iconColor).
111 Src(c.checkedStateIcon)
112 } else {
113 icon = c.Icon().
114 Color(c.iconColor).
115 Src(c.uncheckedStateIcon)
116 }
117 icon.size = c.size
118 // D.S(icon)
119 dims :=
120 c.Theme.Flex(). // AlignBaseline().
121 Rigid(
122 // c.Theme.ButtonInset(0.25,
123 func(gtx l.Context) l.Dimensions {
124 size := gtx.Px(c.size)
125 // icon.color = c.iconColor
126 // TODO: maybe make a special code for raw colors to do this kind of alpha
127 // or add a parameter to apply it
128 // if gtx.Queue == nil {
129 // icon.color = f32color.MulAlpha(c.Theme.Colors.Get(icon.color), 150)
130 // }
131 icon.Fn(gtx)
132 return l.Dimensions{
133 Size: image.Point{X: size, Y: size},
134 }
135 },
136 // ).Fn,
137 ).
138 Rigid(
139 // c.Theme.ButtonInset(0.25,
140 func(gtx l.Context) l.Dimensions {
141 paint.ColorOp{Color: c.Theme.Colors.GetNRGBAFromName(c.color)}.Add(gtx.Ops)
142 return c.Caption(c.label).Color(c.color).Fn(gtx)
143 // return widget.Label{}.Layout(gtx, c.shaper, c.font, c.textSize, c.label)
144 },
145 // ).Fn,
146 ).
147 Fn(gtx)
148 pointer.Rect(image.Rectangle{Max: dims.Size}).Add(gtx.Ops)
149 return dims
150 }
151