/
icontextbtn.go
87 lines (80 loc) · 2.36 KB
/
icontextbtn.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
package icontextbtn
import (
"image"
"image/color"
"gioui.org/layout"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"predmer-api/OLD/app/gel/helper"
)
type IconTextButton struct {
Theme *material.Theme
Button *widget.Clickable
Background color.NRGBA
Icon *widget.Icon
IconColor string
IconSize unit.Value
Text string
TextSize unit.Value
CornerRadius unit.Value
Axis layout.Axis
}
func IconTextBtn(t *material.Theme, b *widget.Clickable, i *widget.Icon, is unit.Value, c, w string) IconTextButton {
return IconTextButton{
Theme: t,
Button: b,
Icon: i,
IconColor: c,
IconSize: is,
Text: w,
TextSize: unit.Dp(16),
}
}
func (b IconTextButton) Layout(gtx layout.Context) layout.Dimensions {
bb := material.ButtonLayout(b.Theme, b.Button)
bb.CornerRadius = b.CornerRadius
bb.Background = b.Background
return bb.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.UniformInset(unit.Dp(4)).Layout(gtx, func(gtx layout.Context) layout.Dimensions {
gtx.Constraints.Min.X = gtx.Constraints.Max.X
var razmak float32
if b.Axis != layout.Horizontal {
razmak = 4
}
iconAndLabel := layout.Flex{Axis: b.Axis, Alignment: layout.Middle, Spacing: layout.SpaceBetween}
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(b.IconSize) - 2*gtx.Px(unit.Dp(16))
size := gtx.Px(b.IconSize) - 2*gtx.Px(unit.Dp(16))
b.Icon.Color = helper.HexARGB(b.IconColor)
b.Icon.Layout(gtx, unit.Px(float32(size)))
d = layout.Dimensions{
Size: image.Point{X: size, Y: size},
}
}
return d
})
})
layLabel := layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Inset{Top: unit.Dp(razmak)}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
l := material.Body1(b.Theme, b.Text)
l.TextSize = b.TextSize
l.Alignment = text.Middle
l.Color = b.Theme.Palette.ContrastFg
return l.Layout(gtx)
})
})
layOne := layIcon
layTwo := layLabel
if b.Axis != layout.Vertical {
layOne = layLabel
layTwo = layIcon
}
return iconAndLabel.Layout(gtx, layOne, layTwo)
})
})
}