This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preferences.go
324 lines (276 loc) · 7.45 KB
/
preferences.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package internal
import (
"reflect"
"sync"
"github.com/Anwert/fyne/v2"
)
// InMemoryPreferences provides an implementation of the fyne.Preferences API that is stored in memory.
type InMemoryPreferences struct {
values map[string]interface{}
lock sync.RWMutex
changeListeners []func()
}
// Declare conformity with Preferences interface
var _ fyne.Preferences = (*InMemoryPreferences)(nil)
// AddChangeListener allows code to be notified when some preferences change. This will fire on any update.
// The passed 'listener' should not try to write values.
func (p *InMemoryPreferences) AddChangeListener(listener func()) {
p.lock.Lock()
defer p.lock.Unlock()
p.changeListeners = append(p.changeListeners, listener)
}
// Bool looks up a boolean value for the key
func (p *InMemoryPreferences) Bool(key string) bool {
return p.BoolWithFallback(key, false)
}
func (p *InMemoryPreferences) BoolList(key string) []bool {
return p.BoolListWithFallback(key, []bool{})
}
func (p *InMemoryPreferences) BoolListWithFallback(key string, fallback []bool) []bool {
value, ok := p.get(key)
if !ok {
return fallback
}
valb, ok := value.([]bool)
if !ok {
return fallback
}
return valb
}
// BoolWithFallback looks up a boolean value and returns the given fallback if not found
func (p *InMemoryPreferences) BoolWithFallback(key string, fallback bool) bool {
value, ok := p.get(key)
if !ok {
return fallback
}
valb, ok := value.(bool)
if !ok {
return fallback
}
return valb
}
// ChangeListeners returns the list of listeners registered for this set of preferences.
func (p *InMemoryPreferences) ChangeListeners() []func() {
return p.changeListeners
}
// Float looks up a float64 value for the key
func (p *InMemoryPreferences) Float(key string) float64 {
return p.FloatWithFallback(key, 0.0)
}
func (p *InMemoryPreferences) FloatList(key string) []float64 {
return p.FloatListWithFallback(key, []float64{})
}
func (p *InMemoryPreferences) FloatListWithFallback(key string, fallback []float64) []float64 {
value, ok := p.get(key)
if !ok {
return fallback
}
valf, ok := value.([]float64)
if ok {
return valf
}
vali, ok := value.([]int)
if ok {
flts := make([]float64, len(vali))
for i, f := range vali {
flts[i] = float64(f)
}
return flts
}
return fallback
}
// FloatWithFallback looks up a float64 value and returns the given fallback if not found
func (p *InMemoryPreferences) FloatWithFallback(key string, fallback float64) float64 {
value, ok := p.get(key)
if !ok {
return fallback
}
valf, ok := value.(float64)
if ok {
return valf
}
vali, ok := value.(int)
if ok {
return float64(vali)
}
return fallback
}
// Int looks up an integer value for the key
func (p *InMemoryPreferences) Int(key string) int {
return p.IntWithFallback(key, 0)
}
func (p *InMemoryPreferences) IntList(key string) []int {
return p.IntListWithFallback(key, []int{})
}
func (p *InMemoryPreferences) IntListWithFallback(key string, fallback []int) []int {
value, ok := p.get(key)
if !ok {
return fallback
}
vali, ok := value.([]int)
if ok {
return vali
}
// integers can be de-serialised as floats, so support both
valf, ok := value.([]float64)
if ok {
ints := make([]int, len(valf))
for i, f := range valf {
ints[i] = int(f)
}
return ints
}
return fallback
}
// IntWithFallback looks up an integer value and returns the given fallback if not found
func (p *InMemoryPreferences) IntWithFallback(key string, fallback int) int {
value, ok := p.get(key)
if !ok {
return fallback
}
vali, ok := value.(int)
if ok {
return vali
}
// integers can be de-serialised as floats, so support both
valf, ok := value.(float64)
if !ok {
return fallback
}
return int(valf)
}
// ReadValues provides read access to the underlying value map - for internal use only...
// You should not retain a reference to the map nor write to the values in the callback function
func (p *InMemoryPreferences) ReadValues(fn func(map[string]interface{})) {
p.lock.RLock()
fn(p.values)
p.lock.RUnlock()
}
// RemoveValue deletes a value on the given key
func (p *InMemoryPreferences) RemoveValue(key string) {
p.remove(key)
}
// SetBool saves a boolean value for the given key
func (p *InMemoryPreferences) SetBool(key string, value bool) {
p.set(key, value)
}
func (p *InMemoryPreferences) SetBoolList(key string, value []bool) {
p.set(key, value)
}
// SetFloat saves a float64 value for the given key
func (p *InMemoryPreferences) SetFloat(key string, value float64) {
p.set(key, value)
}
func (p *InMemoryPreferences) SetFloatList(key string, value []float64) {
p.set(key, value)
}
// SetInt saves an integer value for the given key
func (p *InMemoryPreferences) SetInt(key string, value int) {
p.set(key, value)
}
func (p *InMemoryPreferences) SetIntList(key string, value []int) {
p.set(key, value)
}
// SetString saves a string value for the given key
func (p *InMemoryPreferences) SetString(key string, value string) {
p.set(key, value)
}
func (p *InMemoryPreferences) SetStringList(key string, value []string) {
p.set(key, value)
}
// String looks up a string value for the key
func (p *InMemoryPreferences) String(key string) string {
return p.StringWithFallback(key, "")
}
func (p *InMemoryPreferences) StringList(key string) []string {
return p.StringListWithFallback(key, []string{})
}
func (p *InMemoryPreferences) StringListWithFallback(key string, fallback []string) []string {
value, ok := p.get(key)
if !ok {
return fallback
}
vals, ok := value.([]string)
if !ok {
return fallback
}
return vals
}
// StringWithFallback looks up a string value and returns the given fallback if not found
func (p *InMemoryPreferences) StringWithFallback(key, fallback string) string {
value, ok := p.get(key)
if !ok {
return fallback
}
vals, ok := value.(string)
if !ok {
return fallback
}
return vals
}
// WriteValues provides write access to the underlying value map - for internal use only...
// You should not retain a reference to the map passed to the callback function
func (p *InMemoryPreferences) WriteValues(fn func(map[string]interface{})) {
p.lock.Lock()
fn(p.values)
p.lock.Unlock()
p.fireChange()
}
// NewInMemoryPreferences creates a new preferences implementation stored in memory
func NewInMemoryPreferences() *InMemoryPreferences {
return &InMemoryPreferences{values: make(map[string]interface{})}
}
func (p *InMemoryPreferences) fireChange() {
p.lock.RLock()
listeners := p.changeListeners
p.lock.RUnlock()
var wg sync.WaitGroup
for _, l := range listeners {
wg.Add(1)
go func(listener func()) {
defer wg.Done()
listener()
}(l)
}
wg.Wait()
}
func (p *InMemoryPreferences) get(key string) (interface{}, bool) {
p.lock.RLock()
defer p.lock.RUnlock()
v, err := p.values[key]
return v, err
}
func (p *InMemoryPreferences) remove(key string) {
p.lock.Lock()
delete(p.values, key)
p.lock.Unlock()
p.fireChange()
}
func (p *InMemoryPreferences) set(key string, value interface{}) {
p.lock.Lock()
if reflect.TypeOf(value).Kind() == reflect.Slice {
s := reflect.ValueOf(value)
old := reflect.ValueOf(p.values[key])
if p.values[key] != nil && s.Len() == old.Len() {
changed := false
for i := 0; i < s.Len(); i++ {
if s.Index(i).Interface() != old.Index(i).Interface() {
changed = true
break
}
}
if !changed {
p.lock.Unlock()
return
}
}
} else {
if stored, ok := p.values[key]; ok && stored == value {
p.lock.Unlock()
return
}
}
p.values[key] = value
p.lock.Unlock()
p.fireChange()
}