-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclickable.go
78 lines (69 loc) · 1.77 KB
/
clickable.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
package ux
import (
"image"
"image/color"
"gioui.org/io/input"
"gioui.org/io/semantic"
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/widget"
)
type Clickable struct {
bgColor color.NRGBA
bgColorHover color.NRGBA
clickable widget.Clickable
onClick func()
widget layout.Widget
}
func NewClickable() *Clickable {
return &Clickable{}
}
func (c *Clickable) SetBgColor(bgColor color.NRGBA) {
c.bgColor = bgColor
}
func (c *Clickable) SetBgColorHover(bgColorHover color.NRGBA) {
c.bgColorHover = bgColorHover
}
func (c *Clickable) SetWidget(widget layout.Widget) *Clickable {
c.widget = widget
return c
}
func (c *Clickable) SetOnClick(onClick func()) {
c.onClick = onClick
}
func (c *Clickable) Layout(gtx layout.Context) layout.Dimensions {
if c.bgColor == (color.NRGBA{}) {
c.bgColor = th.Color.DefaultBgGrayColor
}
if c.bgColorHover == (color.NRGBA{}) {
c.bgColorHover = th.Color.HoveredBorderBlueColor
}
for c.clickable.Clicked(gtx) {
if c.onClick != nil {
c.onClick()
}
}
return c.clickable.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
semantic.Button.Add(gtx.Ops)
return layout.Background{}.Layout(gtx,
func(gtx layout.Context) layout.Dimensions {
rect := clip.UniformRRect(image.Rectangle{Max: image.Point{
X: gtx.Constraints.Min.X,
Y: gtx.Constraints.Min.Y,
}}, gtx.Dp(th.Size.DefaultElementRadiusSize))
defer rect.Push(gtx.Ops).Pop()
if gtx.Source == (input.Source{}) {
paint.Fill(gtx.Ops, Disabled(c.bgColorHover))
} else if c.clickable.Hovered() {
// paint.Fill(gtx.Ops, c.bgColorHover)
}
if gtx.Focused(c.clickable) {
paint.Fill(gtx.Ops, c.bgColorHover)
}
return layout.Dimensions{Size: gtx.Constraints.Min}
},
c.widget,
)
})
}