-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind_helper.go
75 lines (63 loc) · 2.14 KB
/
bind_helper.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
package xwidget
import (
"sync"
"sync/atomic"
"fyne.io/fyne/v2/data/binding"
)
// basicBinder stores a DataItem and a function to be called when it changes.
// It provides a convenient way to replace data and callback independently.
type basicBinder struct {
callback atomic.Value // func(binding.DataItem)
dataListenerPairLock sync.RWMutex
dataListenerPair annotatedListener // access guarded by dataListenerPairLock
}
// Bind replaces the data item whose changes are tracked by the callback function.
func (binder *basicBinder) Bind(data binding.DataItem) {
listener := binding.NewDataListener(func() { // NB: listener captures `data` but always calls the up-to-date callback
f := binder.callback.Load()
if f != nil {
f.(func(binding.DataItem))(data)
}
})
data.AddListener(listener)
listenerInfo := annotatedListener{
data: data,
listener: listener,
}
binder.dataListenerPairLock.Lock()
binder.unbindLocked()
binder.dataListenerPair = listenerInfo
binder.dataListenerPairLock.Unlock()
}
// CallWithData passes the currently bound data item as an argument to the
// provided function.
func (binder *basicBinder) CallWithData(f func(data binding.DataItem)) {
binder.dataListenerPairLock.RLock()
data := binder.dataListenerPair.data
binder.dataListenerPairLock.RUnlock()
f(data)
}
// SetCallback replaces the function to be called when the data changes.
func (binder *basicBinder) SetCallback(f func(data binding.DataItem)) {
binder.callback.Store(f)
}
// Unbind requests the callback to be no longer called when the previously bound
// data item changes.
func (binder *basicBinder) Unbind() {
binder.dataListenerPairLock.Lock()
binder.unbindLocked()
binder.dataListenerPairLock.Unlock()
}
// unbindLocked expects the caller to hold dataListenerPairLock.
func (binder *basicBinder) unbindLocked() {
previousListener := binder.dataListenerPair
binder.dataListenerPair = annotatedListener{nil, nil}
if previousListener.listener == nil || previousListener.data == nil {
return
}
previousListener.data.RemoveListener(previousListener.listener)
}
type annotatedListener struct {
data binding.DataItem
listener binding.DataListener
}