-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
check_group.go
266 lines (235 loc) · 5.89 KB
/
check_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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package widget
import (
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/internal/widget"
)
// CheckGroup widget has a list of text labels and checkbox icons next to each.
// Changing the selection (any number can be selected) will trigger the changed func.
//
// Since: 2.1
type CheckGroup struct {
DisableableWidget
Horizontal bool
Required bool
OnChanged func([]string) `json:"-"`
Options []string
Selected []string
items []*Check
}
var _ fyne.Widget = (*CheckGroup)(nil)
// NewCheckGroup creates a new check group widget with the set options and change handler
//
// Since: 2.1
func NewCheckGroup(options []string, changed func([]string)) *CheckGroup {
r := &CheckGroup{
DisableableWidget: DisableableWidget{},
Options: options,
OnChanged: changed,
}
r.ExtendBaseWidget(r)
r.update()
return r
}
// Append adds a new option to the end of a CheckGroup widget.
func (r *CheckGroup) 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 *CheckGroup) 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 &checkGroupRenderer{widget.NewBaseRenderer(objects), r.items, r}
}
// MinSize returns the size that this widget should not shrink below
func (r *CheckGroup) 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 *CheckGroup) Refresh() {
r.propertyLock.Lock()
r.update()
r.propertyLock.Unlock()
r.BaseWidget.Refresh()
}
// Remove removes the first occurrence of the specified option found from a CheckGroup widget.
// Return true if an option was removed.
//
// Since: 2.3
func (r *CheckGroup) Remove(option string) bool {
for i, o := range r.Options {
if strings.EqualFold(option, o) {
r.Options = append(r.Options[:i], r.Options[i+1:]...)
for j, s := range r.Selected {
if strings.EqualFold(option, s) {
r.Selected = append(r.Selected[:j], r.Selected[j+1:]...)
break
}
}
r.Refresh()
return true
}
}
return false
}
// SetSelected sets the checked options, it can be used to set a default option.
func (r *CheckGroup) SetSelected(options []string) {
//if r.Selected == options {
// return
//}
r.Selected = options
if r.OnChanged != nil {
r.OnChanged(options)
}
r.Refresh()
}
func (r *CheckGroup) itemTapped(item *Check) {
if r.Disabled() {
return
}
contains := false
for i, s := range r.Selected {
if s == item.Text {
contains = true
if len(r.Selected) <= 1 {
if r.Required {
item.SetChecked(true)
return
}
r.Selected = nil
} else {
r.Selected = append(r.Selected[:i], r.Selected[i+1:]...)
}
break
}
}
if !contains {
r.Selected = append(r.Selected, item.Text)
}
if r.OnChanged != nil {
r.OnChanged(r.Selected)
}
r.Refresh()
}
func (r *CheckGroup) update() {
r.Options = removeDuplicates(r.Options)
if len(r.items) < len(r.Options) {
for i := len(r.items); i < len(r.Options); i++ {
var item *Check
item = NewCheck(r.Options[i], func(bool) {
r.itemTapped(item)
})
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 {
contains := false
for _, s := range r.Selected {
if s == item.Text {
contains = true
break
}
}
item.Text = r.Options[i]
item.Checked = contains
item.DisableableWidget.disabled = r.disabled
item.Refresh()
}
}
type checkGroupRenderer struct {
widget.BaseRenderer
items []*Check
checks *CheckGroup
}
// Layout the components of the checks widget
func (r *checkGroupRenderer) Layout(_ fyne.Size) {
count := 1
if r.items != nil && len(r.items) > 0 {
count = len(r.items)
}
var itemHeight, itemWidth float32
minSize := r.checks.MinSize()
if r.checks.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.checks.Horizontal {
x += itemWidth
} else {
y += itemHeight
}
}
}
// MinSize calculates the minimum size of a checks item.
// This is based on the contained text, the checks icon and a standard amount of padding
// between each item.
func (r *checkGroupRenderer) MinSize() fyne.Size {
width := float32(0)
height := float32(0)
for _, item := range r.items {
itemMin := item.MinSize()
if r.checks.Horizontal {
height = fyne.Max(height, itemMin.Height)
width += itemMin.Width
} else {
width = fyne.Max(width, itemMin.Width)
height += itemMin.Height
}
}
return fyne.NewSize(width, height)
}
func (r *checkGroupRenderer) Refresh() {
r.updateItems()
canvas.Refresh(r.checks.super())
}
func (r *checkGroupRenderer) updateItems() {
if len(r.items) < len(r.checks.Options) {
for i := len(r.items); i < len(r.checks.Options); i++ {
var item *Check
item = NewCheck(r.checks.Options[i], func(bool) {
r.checks.itemTapped(item)
})
r.SetObjects(append(r.Objects(), item))
r.items = append(r.items, item)
}
r.Layout(r.checks.Size())
} else if len(r.items) > len(r.checks.Options) {
total := len(r.checks.Options)
r.items = r.items[:total]
r.SetObjects(r.Objects()[:total])
}
for i, item := range r.items {
contains := false
for _, s := range r.checks.Selected {
if s == item.Text {
contains = true
break
}
}
item.Text = r.checks.Options[i]
item.Checked = contains
item.disabled = r.checks.disabled
item.Refresh()
}
}