• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fyne-io / fyne / 13089192946

01 Feb 2025 01:43PM UTC coverage: 62.645% (+0.01%) from 62.635%
13089192946

push

github

web-flow
Merge pull request #5484 from Jacalz/binding-file-structure-rework

68 of 70 new or added lines in 2 files covered. (97.14%)

4 existing lines in 1 file now uncovered.

24907 of 39759 relevant lines covered (62.64%)

839.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.5
/data/binding/trees.go
1
package binding
2

3
import (
4
        "bytes"
5

6
        "fyne.io/fyne/v2"
7
)
8

9
// DataTreeRootID const is the value used as ID for the root of any tree binding.
10
const DataTreeRootID = ""
11

12
// DataTree is the base interface for all bindable data trees.
13
//
14
// Since: 2.4
15
type DataTree interface {
16
        DataItem
17
        GetItem(id string) (DataItem, error)
18
        ChildIDs(string) []string
19
}
20

21
// BoolTree supports binding a tree of bool values.
22
//
23
// Since: 2.4
24
type BoolTree interface {
25
        DataTree
26

27
        Append(parent, id string, value bool) error
28
        Get() (map[string][]string, map[string]bool, error)
29
        GetValue(id string) (bool, error)
30
        Prepend(parent, id string, value bool) error
31
        Remove(id string) error
32
        Set(ids map[string][]string, values map[string]bool) error
33
        SetValue(id string, value bool) error
34
}
35

36
// ExternalBoolTree supports binding a tree of bool values from an external variable.
37
//
38
// Since: 2.4
39
type ExternalBoolTree interface {
40
        BoolTree
41

42
        Reload() error
43
}
44

45
// NewBoolTree returns a bindable tree of bool values.
46
//
47
// Since: 2.4
48
func NewBoolTree() BoolTree {
×
49
        return newTreeComparable[bool]()
×
50
}
×
51

52
// BindBoolTree returns a bound tree of bool values, based on the contents of the passed values.
53
// The ids map specifies how each item relates to its parent (with id ""), with the values being in the v map.
54
// If your code changes the content of the maps this refers to you should call Reload() to inform the bindings.
55
//
56
// Since: 2.4
57
func BindBoolTree(ids *map[string][]string, v *map[string]bool) ExternalBoolTree {
×
58
        return bindTreeComparable(ids, v)
×
59
}
×
60

61
// BytesTree supports binding a tree of []byte values.
62
//
63
// Since: 2.4
64
type BytesTree interface {
65
        DataTree
66

67
        Append(parent, id string, value []byte) error
68
        Get() (map[string][]string, map[string][]byte, error)
69
        GetValue(id string) ([]byte, error)
70
        Prepend(parent, id string, value []byte) error
71
        Remove(id string) error
72
        Set(ids map[string][]string, values map[string][]byte) error
73
        SetValue(id string, value []byte) error
74
}
75

76
// ExternalBytesTree supports binding a tree of []byte values from an external variable.
77
//
78
// Since: 2.4
79
type ExternalBytesTree interface {
80
        BytesTree
81

82
        Reload() error
83
}
84

85
// NewBytesTree returns a bindable tree of []byte values.
86
//
87
// Since: 2.4
88
func NewBytesTree() BytesTree {
×
89
        return newTree(bytes.Equal)
×
90
}
×
91

92
// BindBytesTree returns a bound tree of []byte values, based on the contents of the passed values.
93
// The ids map specifies how each item relates to its parent (with id ""), with the values being in the v map.
94
// If your code changes the content of the maps this refers to you should call Reload() to inform the bindings.
95
//
96
// Since: 2.4
97
func BindBytesTree(ids *map[string][]string, v *map[string][]byte) ExternalBytesTree {
×
98
        return bindTree(ids, v, bytes.Equal)
×
99
}
×
100

101
// FloatTree supports binding a tree of float64 values.
102
//
103
// Since: 2.4
104
type FloatTree interface {
105
        DataTree
106

107
        Append(parent, id string, value float64) error
108
        Get() (map[string][]string, map[string]float64, error)
109
        GetValue(id string) (float64, error)
110
        Prepend(parent, id string, value float64) error
111
        Remove(id string) error
112
        Set(ids map[string][]string, values map[string]float64) error
113
        SetValue(id string, value float64) error
114
}
115

116
// ExternalFloatTree supports binding a tree of float64 values from an external variable.
117
//
118
// Since: 2.4
119
type ExternalFloatTree interface {
120
        FloatTree
121

122
        Reload() error
123
}
124

125
// NewFloatTree returns a bindable tree of float64 values.
126
//
127
// Since: 2.4
128
func NewFloatTree() FloatTree {
1✔
129
        return newTreeComparable[float64]()
1✔
130
}
1✔
131

132
// BindFloatTree returns a bound tree of float64 values, based on the contents of the passed values.
133
// The ids map specifies how each item relates to its parent (with id ""), with the values being in the v map.
134
// If your code changes the content of the maps this refers to you should call Reload() to inform the bindings.
135
//
136
// Since: 2.4
137
func BindFloatTree(ids *map[string][]string, v *map[string]float64) ExternalFloatTree {
2✔
138
        return bindTreeComparable(ids, v)
2✔
139
}
2✔
140

141
// IntTree supports binding a tree of int values.
142
//
143
// Since: 2.4
144
type IntTree interface {
145
        DataTree
146

147
        Append(parent, id string, value int) error
148
        Get() (map[string][]string, map[string]int, error)
149
        GetValue(id string) (int, error)
150
        Prepend(parent, id string, value int) error
151
        Remove(id string) error
152
        Set(ids map[string][]string, values map[string]int) error
153
        SetValue(id string, value int) error
154
}
155

156
// ExternalIntTree supports binding a tree of int values from an external variable.
157
//
158
// Since: 2.4
159
type ExternalIntTree interface {
160
        IntTree
161

162
        Reload() error
163
}
164

165
// NewIntTree returns a bindable tree of int values.
166
//
167
// Since: 2.4
168
func NewIntTree() IntTree {
×
169
        return newTreeComparable[int]()
×
170
}
×
171

172
// BindIntTree returns a bound tree of int values, based on the contents of the passed values.
173
// The ids map specifies how each item relates to its parent (with id ""), with the values being in the v map.
174
// If your code changes the content of the maps this refers to you should call Reload() to inform the bindings.
175
//
176
// Since: 2.4
177
func BindIntTree(ids *map[string][]string, v *map[string]int) ExternalIntTree {
×
178
        return bindTreeComparable(ids, v)
×
179
}
×
180

181
// RuneTree supports binding a tree of rune values.
182
//
183
// Since: 2.4
184
type RuneTree interface {
185
        DataTree
186

187
        Append(parent, id string, value rune) error
188
        Get() (map[string][]string, map[string]rune, error)
189
        GetValue(id string) (rune, error)
190
        Prepend(parent, id string, value rune) error
191
        Remove(id string) error
192
        Set(ids map[string][]string, values map[string]rune) error
193
        SetValue(id string, value rune) error
194
}
195

196
// ExternalRuneTree supports binding a tree of rune values from an external variable.
197
//
198
// Since: 2.4
199
type ExternalRuneTree interface {
200
        RuneTree
201

202
        Reload() error
203
}
204

205
// NewRuneTree returns a bindable tree of rune values.
206
//
207
// Since: 2.4
208
func NewRuneTree() RuneTree {
×
209
        return newTreeComparable[rune]()
×
210
}
×
211

212
// BindRuneTree returns a bound tree of rune values, based on the contents of the passed values.
213
// The ids map specifies how each item relates to its parent (with id ""), with the values being in the v map.
214
// If your code changes the content of the maps this refers to you should call Reload() to inform the bindings.
215
//
216
// Since: 2.4
217
func BindRuneTree(ids *map[string][]string, v *map[string]rune) ExternalRuneTree {
×
218
        return bindTreeComparable(ids, v)
×
219
}
×
220

221
// StringTree supports binding a tree of string values.
222
//
223
// Since: 2.4
224
type StringTree interface {
225
        DataTree
226

227
        Append(parent, id string, value string) error
228
        Get() (map[string][]string, map[string]string, error)
229
        GetValue(id string) (string, error)
230
        Prepend(parent, id string, value string) error
231
        Remove(id string) error
232
        Set(ids map[string][]string, values map[string]string) error
233
        SetValue(id string, value string) error
234
}
235

236
// ExternalStringTree supports binding a tree of string values from an external variable.
237
//
238
// Since: 2.4
239
type ExternalStringTree interface {
240
        StringTree
241

242
        Reload() error
243
}
244

245
// NewStringTree returns a bindable tree of string values.
246
//
247
// Since: 2.4
248
func NewStringTree() StringTree {
4✔
249
        return newTreeComparable[string]()
4✔
250
}
4✔
251

252
// BindStringTree returns a bound tree of string values, based on the contents of the passed values.
253
// The ids map specifies how each item relates to its parent (with id ""), with the values being in the v map.
254
// If your code changes the content of the maps this refers to you should call Reload() to inform the bindings.
255
//
256
// Since: 2.4
257
func BindStringTree(ids *map[string][]string, v *map[string]string) ExternalStringTree {
1✔
258
        return bindTreeComparable(ids, v)
1✔
259
}
1✔
260

261
// UntypedTree supports binding a tree of any values.
262
//
263
// Since: 2.5
264
type UntypedTree interface {
265
        DataTree
266

267
        Append(parent, id string, value any) error
268
        Get() (map[string][]string, map[string]any, error)
269
        GetValue(id string) (any, error)
270
        Prepend(parent, id string, value any) error
271
        Remove(id string) error
272
        Set(ids map[string][]string, values map[string]any) error
273
        SetValue(id string, value any) error
274
}
275

276
// ExternalUntypedTree supports binding a tree of any values from an external variable.
277
//
278
// Since: 2.5
279
type ExternalUntypedTree interface {
280
        UntypedTree
281

282
        Reload() error
283
}
284

285
// NewUntypedTree returns a bindable tree of any values.
286
//
287
// Since: 2.5
288
func NewUntypedTree() UntypedTree {
×
289
        return newTree(func(a1, a2 any) bool { return a1 == a2 })
×
290
}
291

292
// BindUntypedTree returns a bound tree of any values, based on the contents of the passed values.
293
// The ids map specifies how each item relates to its parent (with id ""), with the values being in the v map.
294
// If your code changes the content of the maps this refers to you should call Reload() to inform the bindings.
295
//
296
// Since: 2.4
297
func BindUntypedTree(ids *map[string][]string, v *map[string]any) ExternalUntypedTree {
×
298
        return bindTree(ids, v, func(a1, a2 any) bool { return a1 == a2 })
×
299
}
300

301
// URITree supports binding a tree of fyne.URI values.
302
//
303
// Since: 2.4
304
type URITree interface {
305
        DataTree
306

307
        Append(parent, id string, value fyne.URI) error
308
        Get() (map[string][]string, map[string]fyne.URI, error)
309
        GetValue(id string) (fyne.URI, error)
310
        Prepend(parent, id string, value fyne.URI) error
311
        Remove(id string) error
312
        Set(ids map[string][]string, values map[string]fyne.URI) error
313
        SetValue(id string, value fyne.URI) error
314
}
315

316
// ExternalURITree supports binding a tree of fyne.URI values from an external variable.
317
//
318
// Since: 2.4
319
type ExternalURITree interface {
320
        URITree
321

322
        Reload() error
323
}
324

325
// NewURITree returns a bindable tree of fyne.URI values.
326
//
327
// Since: 2.4
328
func NewURITree() URITree {
×
329
        return newTree(compareURI)
×
330
}
×
331

332
// BindURITree returns a bound tree of fyne.URI values, based on the contents of the passed values.
333
// The ids map specifies how each item relates to its parent (with id ""), with the values being in the v map.
334
// If your code changes the content of the maps this refers to you should call Reload() to inform the bindings.
335
//
336
// Since: 2.4
337
func BindURITree(ids *map[string][]string, v *map[string]fyne.URI) ExternalURITree {
×
338
        return bindTree(ids, v, compareURI)
×
339
}
×
340

341
type treeBase struct {
342
        base
343

344
        ids   map[string][]string
345
        items map[string]DataItem
346
}
347

348
// GetItem returns the DataItem at the specified id.
349
func (t *treeBase) GetItem(id string) (DataItem, error) {
6✔
350
        t.lock.RLock()
6✔
351
        defer t.lock.RUnlock()
6✔
352

6✔
353
        if item, ok := t.items[id]; ok {
11✔
354
                return item, nil
5✔
355
        }
5✔
356

357
        return nil, errOutOfBounds
1✔
358
}
359

360
// ChildIDs returns the ordered IDs of items in this data tree that are children of the specified ID.
361
func (t *treeBase) ChildIDs(id string) []string {
15✔
362
        t.lock.RLock()
15✔
363
        defer t.lock.RUnlock()
15✔
364

15✔
365
        if ids, ok := t.ids[id]; ok {
26✔
366
                return ids
11✔
367
        }
11✔
368

369
        return []string{}
4✔
370
}
371

372
func (t *treeBase) appendItem(i DataItem, id, parent string) {
23✔
373
        t.items[id] = i
23✔
374
        ids, ok := t.ids[parent]
23✔
375
        if !ok {
30✔
376
                ids = make([]string, 0)
7✔
377
        }
7✔
378

379
        for _, in := range ids {
45✔
380
                if in == id {
33✔
381
                        return
11✔
382
                }
11✔
383
        }
384
        t.ids[parent] = append(ids, id)
12✔
385
}
386

387
func (t *treeBase) deleteItem(id, parent string) {
8✔
388
        delete(t.items, id)
8✔
389

8✔
390
        ids, ok := t.ids[parent]
8✔
391
        if !ok {
8✔
NEW
392
                return
×
NEW
393
        }
×
394

395
        off := -1
8✔
396
        for i, id2 := range ids {
18✔
397
                if id2 == id {
12✔
398
                        off = i
2✔
399
                        break
2✔
400
                }
401
        }
402
        if off == -1 {
14✔
403
                return
6✔
404
        }
6✔
405
        t.ids[parent] = append(ids[:off], ids[off+1:]...)
2✔
406
}
407

408
func parentIDFor(id string, ids map[string][]string) string {
20✔
409
        for parent, list := range ids {
43✔
410
                for _, child := range list {
54✔
411
                        if child == id {
44✔
412
                                return parent
13✔
413
                        }
13✔
414
                }
415
        }
416

417
        return ""
7✔
418
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc