forked from marcetin/parallelcoin
/
buttonlayout.go
106 lines (93 loc) · 2.48 KB
/
buttonlayout.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
package gui
import (
"image/color"
"gioui.org/f32"
l "gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/unit"
"github.com/p9c/pod/pkg/gui/f32color"
)
type ButtonLayout struct {
*Window
background color.NRGBA
cornerRadius unit.Value
button *Clickable
w l.Widget
corners int
}
// ButtonLayout creates a button with a background and another widget over top
func (w *Window) ButtonLayout(button *Clickable) *ButtonLayout {
return &ButtonLayout{
Window: w,
button: button,
background: w.Colors.GetNRGBAFromName("ButtonBg"),
cornerRadius: w.TextSize.Scale(0.25),
}
}
// Corners sets which corners have the radius of rounding
func (b *ButtonLayout) Corners(corners int) *ButtonLayout {
b.corners = corners
return b
}
// Background sets the background color of the button
func (b *ButtonLayout) Background(color string) *ButtonLayout {
b.background = b.Theme.Colors.GetNRGBAFromName(color)
return b
}
// CornerRadius sets the radius of the corners of the button
func (b *ButtonLayout) CornerRadius(radius float32) *ButtonLayout {
b.cornerRadius = b.Theme.TextSize.Scale(radius)
return b
}
// Embed a widget in the button
func (b *ButtonLayout) Embed(w l.Widget) *ButtonLayout {
b.w = w
return b
}
func (b *ButtonLayout) SetClick(fn func()) *ButtonLayout {
b.button.SetClick(fn)
return b
}
func (b *ButtonLayout) SetCancel(fn func()) *ButtonLayout {
b.button.SetCancel(fn)
return b
}
func (b *ButtonLayout) SetPress(fn func()) *ButtonLayout {
b.button.SetPress(fn)
return b
}
// Fn is the function that draws the button and its child widget
func (b *ButtonLayout) Fn(gtx l.Context) l.Dimensions {
min := gtx.Constraints.Min
return b.Stack().Alignment(l.Center).
Expanded(
func(gtx l.Context) l.Dimensions {
rr := float32(gtx.Px(b.cornerRadius))
clip.RRect{
Rect: f32.Rectangle{Max: f32.Point{
X: float32(gtx.Constraints.Min.X),
Y: float32(gtx.Constraints.Min.Y),
}},
NW: ifDir(rr, b.corners&NW),
NE: ifDir(rr, b.corners&NE),
SW: ifDir(rr, b.corners&SW),
SE: ifDir(rr, b.corners&SE),
}.Add(gtx.Ops)
background := b.background
if gtx.Queue == nil {
background = f32color.MulAlpha(b.background, 150)
}
dims := Fill(gtx, background)
for _, c := range b.button.History() {
drawInk(gtx, c)
}
return dims
}).
Stacked(
func(gtx l.Context) l.Dimensions {
gtx.Constraints.Min = min
return l.Center.Layout(gtx, b.w)
}).
Expanded(b.button.Fn).
Fn(gtx)
}