forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio_group.go
227 lines (195 loc) · 5.08 KB
/
radio_group.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package widget
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/internal/widget"
)
// RadioGroup widget has a list of text labels and checks check icons next to each.
// Changing the selection (only one can be selected) will trigger the changed func.
//
// Since: 1.4
type RadioGroup struct {
DisableableWidget
Horizontal bool
Required bool
OnChanged func(string) `json:"-"`
Options []string
Selected string
items []*radioItem
}
var _ fyne.Widget = (*RadioGroup)(nil)
// NewRadioGroup creates a new radio group widget with the set options and change handler
//
// Since: 1.4
func NewRadioGroup(options []string, changed func(string)) *RadioGroup {
r := &RadioGroup{
Options: options,
OnChanged: changed,
}
r.ExtendBaseWidget(r)
r.update()
return r
}
// Append adds a new option to the end of a RadioGroup widget.
func (r *RadioGroup) Append(option string) {
r.Options = append(r.Options, option)
r.Refresh()
}
// CreateRenderer is a private method to Fyne which links this widget to its renderer
func (r *RadioGroup) CreateRenderer() fyne.WidgetRenderer {
r.ExtendBaseWidget(r)
r.propertyLock.Lock()
defer r.propertyLock.Unlock()
r.update()
objects := make([]fyne.CanvasObject, len(r.items))
for i, item := range r.items {
objects[i] = item
}
return &radioGroupRenderer{widget.NewBaseRenderer(objects), r.items, r}
}
// MinSize returns the size that this widget should not shrink below
func (r *RadioGroup) MinSize() fyne.Size {
r.ExtendBaseWidget(r)
return r.BaseWidget.MinSize()
}
// Refresh causes this widget to be redrawn in it's current state.
//
// Implements: fyne.CanvasObject
func (r *RadioGroup) Refresh() {
r.propertyLock.Lock()
r.update()
r.propertyLock.Unlock()
r.BaseWidget.Refresh()
}
// SetSelected sets the radio option, it can be used to set a default option.
func (r *RadioGroup) SetSelected(option string) {
if r.Selected == option {
return
}
r.Selected = option
if r.OnChanged != nil {
r.OnChanged(option)
}
r.Refresh()
}
func (r *RadioGroup) itemTapped(item *radioItem) {
if r.Disabled() {
return
}
if r.Selected == item.Label {
if r.Required {
return
}
r.Selected = ""
item.SetSelected(false)
} else {
r.Selected = item.Label
item.SetSelected(true)
}
if r.OnChanged != nil {
r.OnChanged(r.Selected)
}
r.Refresh()
}
func (r *RadioGroup) update() {
r.Options = removeDuplicates(r.Options)
if len(r.items) < len(r.Options) {
for i := len(r.items); i < len(r.Options); i++ {
item := newRadioItem(r.Options[i], r.itemTapped)
r.items = append(r.items, item)
}
} else if len(r.items) > len(r.Options) {
r.items = r.items[:len(r.Options)]
}
for i, item := range r.items {
item.Label = r.Options[i]
item.Selected = item.Label == r.Selected
item.DisableableWidget.disabled = r.disabled
item.Refresh()
}
}
type radioGroupRenderer struct {
widget.BaseRenderer
items []*radioItem
radio *RadioGroup
}
// Layout the components of the radio widget
func (r *radioGroupRenderer) Layout(_ fyne.Size) {
count := 1
if r.items != nil && len(r.items) > 0 {
count = len(r.items)
}
var itemHeight, itemWidth float32
minSize := r.radio.MinSize()
if r.radio.Horizontal {
itemHeight = minSize.Height
itemWidth = minSize.Width / float32(count)
} else {
itemHeight = minSize.Height / float32(count)
itemWidth = minSize.Width
}
itemSize := fyne.NewSize(itemWidth, itemHeight)
x, y := float32(0), float32(0)
for _, item := range r.items {
item.Resize(itemSize)
item.Move(fyne.NewPos(x, y))
if r.radio.Horizontal {
x += itemWidth
} else {
y += itemHeight
}
}
}
// MinSize calculates the minimum size of a radio item.
// This is based on the contained text, the radio icon and a standard amount of padding
// between each item.
func (r *radioGroupRenderer) MinSize() fyne.Size {
width := float32(0)
height := float32(0)
for _, item := range r.items {
itemMin := item.MinSize()
width = fyne.Max(width, itemMin.Width)
height = fyne.Max(height, itemMin.Height)
}
if r.radio.Horizontal {
width = width * float32(len(r.items))
} else {
height = height * float32(len(r.items))
}
return fyne.NewSize(width, height)
}
func (r *radioGroupRenderer) Refresh() {
r.updateItems()
canvas.Refresh(r.radio.super())
}
func (r *radioGroupRenderer) updateItems() {
if len(r.items) < len(r.radio.Options) {
for i := len(r.items); i < len(r.radio.Options); i++ {
item := newRadioItem(r.radio.Options[i], r.radio.itemTapped)
r.SetObjects(append(r.Objects(), item))
r.items = append(r.items, item)
}
r.Layout(r.radio.Size())
} else if len(r.items) > len(r.radio.Options) {
total := len(r.radio.Options)
r.items = r.items[:total]
r.SetObjects(r.Objects()[:total])
}
for i, item := range r.items {
item.Label = r.radio.Options[i]
item.Selected = item.Label == r.radio.Selected
item.disabled = r.radio.disabled
item.Refresh()
}
}
func removeDuplicates(options []string) []string {
var result []string
found := make(map[string]bool)
for _, option := range options {
if _, ok := found[option]; !ok {
found[option] = true
result = append(result, option)
}
}
return result
}