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

fyne-io / fyne / 13153335083

05 Feb 2025 08:36AM UTC coverage: 62.633% (-0.02%) from 62.656%
13153335083

push

github

web-flow
Merge pull request #5491 from Jacalz/compare-uri

5 of 19 new or added lines in 5 files covered. (26.32%)

7 existing lines in 2 files now uncovered.

24913 of 39776 relevant lines covered (62.63%)

838.03 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
        "fyne.io/fyne/v2/storage"
8
)
9

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

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

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

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

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

43
        Reload() error
44
}
45

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

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

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

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

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

83
        Reload() error
84
}
85

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

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

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

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

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

123
        Reload() error
124
}
125

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

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

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

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

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

163
        Reload() error
164
}
165

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

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

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

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

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

203
        Reload() error
204
}
205

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

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

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

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

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

243
        Reload() error
244
}
245

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

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

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

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

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

283
        Reload() error
284
}
285

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

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

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

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

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

323
        Reload() error
324
}
325

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

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

342
type treeBase struct {
343
        base
344

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

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

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

358
        return nil, errOutOfBounds
1✔
359
}
360

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

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

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

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

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

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

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

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

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

418
        return ""
7✔
419
}
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