forked from fyne-io/fyne
/
button.go
210 lines (168 loc) · 5.25 KB
/
button.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
package widget
import (
"image/color"
"fyne.io/fyne"
"fyne.io/fyne/canvas"
"fyne.io/fyne/theme"
)
type buttonRenderer struct {
icon *canvas.Image
label *canvas.Text
objects []fyne.CanvasObject
button *Button
}
// MinSize calculates the minimum size of a button.
// This is based on the contained text, any icon that is set and a standard
// amount of padding added.
func (b *buttonRenderer) MinSize() fyne.Size {
var min fyne.Size
if b.button.Text != "" {
min = b.label.MinSize().Add(fyne.NewSize(theme.Padding()*4, theme.Padding()*2))
if b.icon != nil {
min = min.Add(fyne.NewSize(theme.IconInlineSize()+theme.Padding(), 0))
}
} else if b.icon != nil {
min = fyne.NewSize(theme.IconInlineSize()+theme.Padding()*2, theme.IconInlineSize()+theme.Padding()*2)
}
return min
}
// Layout the components of the button widget
func (b *buttonRenderer) Layout(size fyne.Size) {
if b.button.Text != "" {
inner := size.Subtract(fyne.NewSize(theme.Padding()*4, theme.Padding()*2))
if b.button.Icon == nil {
b.label.Resize(inner)
b.label.Move(fyne.NewPos(theme.Padding()*2, theme.Padding()))
} else {
offset := fyne.NewSize(theme.IconInlineSize(), 0)
labelSize := inner.Subtract(offset)
b.label.Resize(labelSize)
b.label.Move(fyne.NewPos(theme.IconInlineSize()+theme.Padding()*2, theme.Padding()))
b.icon.Resize(fyne.NewSize(theme.IconInlineSize(), theme.IconInlineSize()))
b.icon.Move(fyne.NewPos(
(size.Width-theme.IconInlineSize()-b.label.MinSize().Width-theme.Padding())/2,
(size.Height-theme.IconInlineSize())/2))
}
} else {
b.icon.Resize(fyne.NewSize(theme.IconInlineSize(), theme.IconInlineSize()))
b.icon.Move(fyne.NewPos(theme.Padding(), theme.Padding()))
}
}
// ApplyTheme is called when the Button may need to update it's look
func (b *buttonRenderer) ApplyTheme() {
b.label.Color = theme.TextColor()
b.Refresh()
}
func (b *buttonRenderer) BackgroundColor() color.Color {
if b.button.Style == PrimaryButton {
return theme.PrimaryColor()
}
return theme.ButtonColor()
}
func (b *buttonRenderer) Refresh() {
b.label.Text = b.button.Text
if b.button.Icon != nil {
if b.icon == nil {
b.icon = canvas.NewImageFromResource(b.button.Icon)
b.objects = append(b.objects, b.icon)
} else {
b.icon.Resource = b.button.Icon
}
b.icon.Hidden = false
} else if b.icon != nil {
b.icon.Hidden = true
}
b.Layout(b.button.Size())
canvas.Refresh(b.button)
}
func (b *buttonRenderer) Objects() []fyne.CanvasObject {
return b.objects
}
func (b *buttonRenderer) Destroy() {
}
// Button widget has a text label and triggers an event func when clicked
type Button struct {
baseWidget
Text string
Style ButtonStyle
Icon fyne.Resource
OnTapped func() `json:"-"`
}
// ButtonStyle determines the behaviour and rendering of a button.
type ButtonStyle int
const (
// DefaultButton is the standard button style
DefaultButton ButtonStyle = iota
// PrimaryButton that should be more prominent to the user
PrimaryButton
)
// Resize sets a new size for a widget.
// Note this should not be used if the widget is being managed by a Layout within a Container.
func (b *Button) Resize(size fyne.Size) {
b.resize(size, b)
}
// Move the widget to a new position, relative to it's parent.
// Note this should not be used if the widget is being managed by a Layout within a Container.
func (b *Button) Move(pos fyne.Position) {
b.move(pos, b)
}
// MinSize returns the smallest size this widget can shrink to
func (b *Button) MinSize() fyne.Size {
return b.minSize(b)
}
// Show this widget, if it was previously hidden
func (b *Button) Show() {
b.show(b)
}
// Hide this widget, if it was previously visible
func (b *Button) Hide() {
b.hide(b)
}
// Tapped is called when a pointer tapped event is captured and triggers any tap handler
func (b *Button) Tapped(*fyne.PointEvent) {
if b.OnTapped != nil {
b.OnTapped()
}
}
// TappedSecondary is called when a secondary pointer tapped event is captured
func (b *Button) TappedSecondary(*fyne.PointEvent) {
}
// CreateRenderer is a private method to Fyne which links this widget to it's renderer
func (b *Button) CreateRenderer() fyne.WidgetRenderer {
var icon *canvas.Image
if b.Icon != nil {
icon = canvas.NewImageFromResource(b.Icon)
}
text := canvas.NewText(b.Text, theme.TextColor())
text.Alignment = fyne.TextAlignCenter
objects := []fyne.CanvasObject{
text,
}
if icon != nil {
objects = append(objects, icon)
}
return &buttonRenderer{icon, text, objects, b}
}
// SetText allows the button label to be changed
func (b *Button) SetText(text string) {
b.Text = text
Refresh(b)
}
// SetIcon updates the icon on a label - pass nil to hide an icon
func (b *Button) SetIcon(icon fyne.Resource) {
b.Icon = icon
Refresh(b)
}
// NewButton creates a new button widget with the set label and tap handler
func NewButton(label string, tapped func()) *Button {
button := &Button{baseWidget{}, label, DefaultButton, nil, tapped}
Renderer(button).Layout(button.MinSize())
return button
}
// NewButtonWithIcon creates a new button widget with the specified label,
// themed icon and tap handler
func NewButtonWithIcon(label string, icon fyne.Resource, tapped func()) *Button {
button := &Button{baseWidget{}, label, DefaultButton, icon, tapped}
Renderer(button).Layout(button.MinSize())
return button
}