/
layout.go
211 lines (202 loc) · 6.67 KB
/
layout.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
package counter
import (
"gioui.org/layout"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/gioapp/gel/helper"
"github.com/gioapp/gel/theme"
"image"
)
type DuoUIcounterStyle struct {
increase *iconBtn
decrease *iconBtn
reset *iconBtn
input material.EditorStyle
Font text.Font
TextSize unit.Value
TxColor string
BgColor string
BtnBgColor string
shaper text.Shaper
c *DuoUIcounter
}
func DuoUIcounterSt(t *theme.DuoUItheme, cc *DuoUIcounter) DuoUIcounterStyle {
return DuoUIcounterStyle{
// ToDo Replace theme's buttons with counter exclusive buttons, set icons for increase/decrease
increase: iconButton(t.T, t.Icons["counterPlusIcon"], cc.CounterIncrease, t.Colors["Primary"]),
decrease: iconButton(t.T, t.Icons["counterMinusIcon"], cc.CounterDecrease, t.Colors["Primary"]),
input: material.Editor(t.T, cc.CounterInput, "0"),
//pageFunction: pageFunction,
Font: text.Font{
Typeface: t.Fonts["Primary"],
},
TxColor: t.Colors["Light"],
BgColor: t.Colors["Dark"],
BtnBgColor: t.Colors["Primary"],
TextSize: unit.Dp(float32(18)),
shaper: t.Shaper,
c: cc,
}
}
func (c DuoUIcounterStyle) Layout(g layout.Context, th *material.Theme, label, value string) func(gtx layout.Context) layout.Dimensions {
return func(gtx layout.Context) layout.Dimensions {
c.c.CounterInput.SetText(value)
hmin := g.Constraints.Min.X
vmin := g.Constraints.Min.Y
//txColor := c.TxColor
bgColor := c.BgColor
return layout.Stack{Alignment: layout.Center}.Layout(g,
layout.Expanded(func(gtx layout.Context) layout.Dimensions {
//rr := float32(g.Px(unit.Dp(0)))
//clip.Rect{
//Rect: f32.Rectangle{Max: f32.Point{
// X: float32(g.Constraints.Min.X),
// Y: float32(g.Constraints.Min.Y),
//}},
//NE: rr, NW: rr, SE: rr, SW: rr,
//}.Op(g.Ops).Add(g.Ops)
return helper.Fill(g, helper.HexARGB(bgColor))
}),
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
g.Constraints.Min.X = hmin
g.Constraints.Min.Y = vmin
return layout.Center.Layout(g, func(gtx layout.Context) layout.Dimensions {
return layout.Flex{
Spacing: layout.SpaceAround,
Alignment: layout.Middle,
}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
for c.c.CounterDecrease.Clicked() {
c.c.Decrease()
c.c.PageFunction()
}
return c.decrease.Layout(gtx)
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Center.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Inset{
Top: unit.Dp(0),
Right: unit.Dp(16),
Bottom: unit.Dp(0),
Left: unit.Dp(16),
}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Flex{
Axis: layout.Vertical,
Spacing: layout.SpaceAround,
Alignment: layout.Middle,
}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
//paint.ColorOp{Color: HexARGB(c.TxColor)}.Add(gtx.Ops)
//return widget.Label{
// Alignment: text.Middle,
//}.Layout(gtx, c.shaper, c.Font, unit.Dp(8), label)
return material.Caption(th, label).Layout(gtx)
//return layout.Dimensions{}
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
c.input.Font.Typeface = c.Font.Typeface
c.input.Color = helper.HexARGB(c.TxColor)
for _, e := range c.c.CounterInput.Events() {
switch e.(type) {
case widget.ChangeEvent:
//if i, err := strconv.Atoi(c.c.CounterInput.Text()); err == nil {
// c.c.Value = i
//}
}
}
return c.input.Layout(gtx)
paint.ColorOp{Color: helper.HexARGB(c.TxColor)}.Add(gtx.Ops)
return material.Body1(th, value).Layout(gtx)
//{
// Alignment: text.Middle,
//}.Layout(gtx, c.shaper, c.Font, unit.Dp(12), value)
//return layout.Dimensions{}
}))
})
})
}),
//layout.Flexed(0.2, func() {
// for cc.CounterReset.Clicked(gtx) {
// cc.Reset()
// c.pageFunction()
// }
// c.reset.Layout(gtx, cc.CounterReset)
//}),
//layout.Flexed(0.2, func(gtx layout.Context)layout.Dimensions{
//
// //paint.ColorOp{Color: HexARGB(c.TxColor)}.Add(gtx.Ops)
// //return widget.Label{
// // Alignment: text.Middle,
// //}.Layout(gtx, c.shaper, c.Font, unit.Dp(8), label)
//}),
//layout.Rigid(func(gtx layout.Context)layout.Dimensions{
// c.input.Font.Typeface = c.Font.Typeface
// c.input.Color = HexARGB(c.TxColor)
// for _, e := range cc.CounterInput.Events() {
// switch e.(type) {
// case widget.ChangeEvent:
// if i, err := strconv.Atoi(cc.CounterInput.Text()); err == nil {
// cc.Value = i
// }
// }
// }
// return c.input.Layout(gtx)
// // paint.ColorOp{Color: HexARGB(c.TxColor)}.Add(gtx.Ops)
// // gel.Label{
// // Alignment: text.Middle,
// // }.Layout(gtx, c.shaper, c.Font, unit.Dp(12), value)
//}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
for c.c.CounterIncrease.Clicked() {
c.c.Increase()
c.c.PageFunction()
}
return c.increase.Layout(gtx)
}))
})
}),
)
}
}
type iconBtn struct {
theme *material.Theme
button *widget.Clickable
icon *widget.Icon
word string
bg string
}
func iconButton(t *material.Theme, i *widget.Icon, c *widget.Clickable, bg string) *iconBtn {
return &iconBtn{
theme: t,
button: c,
icon: i,
bg: bg,
}
}
func (b iconBtn) Layout(gtx layout.Context) layout.Dimensions {
btn := material.ButtonLayout(b.theme, b.button)
btn.CornerRadius = unit.Dp(0)
btn.Background = helper.HexARGB(b.bg)
return btn.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.UniformInset(unit.Dp(8)).Layout(gtx, func(gtx layout.Context) layout.Dimensions {
iconAndLabel := layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle}
layIcon := layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Inset{}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
var d layout.Dimensions
if b.icon != nil {
size := gtx.Px(unit.Dp(24))
b.icon.Layout(gtx, unit.Px(float32(size)))
d = layout.Dimensions{
Size: image.Point{X: size, Y: size},
}
}
return d
})
})
return iconAndLabel.Layout(gtx, layIcon)
})
})
}