Skip to content

Commit

Permalink
Use atomic.Pointer for preference bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Dec 26, 2023
1 parent 02b1455 commit 77c50aa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 35 deletions.
11 changes: 4 additions & 7 deletions data/binding/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ type prefBound{{ .Name }} struct {
base
key string
p fyne.Preferences
cache atomic.Value // {{ .Type }}
cache atomic.Pointer[{{ .Type }}]
}
// BindPreference{{ .Name }} returns a bindable {{ .Type }} value that is managed by the application preferences.
Expand All @@ -148,7 +148,7 @@ func BindPreference{{ .Name }}(key string, p fyne.Preferences) {{ .Name }} {
func (b *prefBound{{ .Name }}) Get() ({{ .Type }}, error) {
cache := b.p.{{ .Name }}(b.key)
b.cache.Store(cache)
b.cache.Store(&cache)
return cache, nil
}
Expand All @@ -163,11 +163,8 @@ func (b *prefBound{{ .Name }}) Set(v {{ .Type }}) error {
func (b *prefBound{{ .Name }}) checkForChange() {
val := b.cache.Load()
if val != nil {
cache := val.({{ .Type }})
if b.p.{{ .Name }}(b.key) == cache {
return
}
if val != nil && b.p.{{ .Name }}(b.key) == *val {
return
}
b.trigger()
}
Expand Down
44 changes: 16 additions & 28 deletions data/binding/preference.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type prefBoundBool struct {
base
key string
p fyne.Preferences
cache atomic.Value // bool
cache atomic.Pointer[bool]
}

// BindPreferenceBool returns a bindable bool value that is managed by the application preferences.
Expand All @@ -41,7 +41,7 @@ func BindPreferenceBool(key string, p fyne.Preferences) Bool {

func (b *prefBoundBool) Get() (bool, error) {
cache := b.p.Bool(b.key)
b.cache.Store(cache)
b.cache.Store(&cache)
return cache, nil
}

Expand All @@ -56,11 +56,8 @@ func (b *prefBoundBool) Set(v bool) error {

func (b *prefBoundBool) checkForChange() {
val := b.cache.Load()
if val != nil {
cache := val.(bool)
if b.p.Bool(b.key) == cache {
return
}
if val != nil && b.p.Bool(b.key) == *val {
return
}
b.trigger()
}
Expand All @@ -73,7 +70,7 @@ type prefBoundFloat struct {
base
key string
p fyne.Preferences
cache atomic.Value // float64
cache atomic.Pointer[float64]
}

// BindPreferenceFloat returns a bindable float64 value that is managed by the application preferences.
Expand All @@ -99,7 +96,7 @@ func BindPreferenceFloat(key string, p fyne.Preferences) Float {

func (b *prefBoundFloat) Get() (float64, error) {
cache := b.p.Float(b.key)
b.cache.Store(cache)
b.cache.Store(&cache)
return cache, nil
}

Expand All @@ -114,11 +111,8 @@ func (b *prefBoundFloat) Set(v float64) error {

func (b *prefBoundFloat) checkForChange() {
val := b.cache.Load()
if val != nil {
cache := val.(float64)
if b.p.Float(b.key) == cache {
return
}
if val != nil && b.p.Float(b.key) == *val {
return
}
b.trigger()
}
Expand All @@ -131,7 +125,7 @@ type prefBoundInt struct {
base
key string
p fyne.Preferences
cache atomic.Value // int
cache atomic.Pointer[int]
}

// BindPreferenceInt returns a bindable int value that is managed by the application preferences.
Expand All @@ -157,7 +151,7 @@ func BindPreferenceInt(key string, p fyne.Preferences) Int {

func (b *prefBoundInt) Get() (int, error) {
cache := b.p.Int(b.key)
b.cache.Store(cache)
b.cache.Store(&cache)
return cache, nil
}

Expand All @@ -172,11 +166,8 @@ func (b *prefBoundInt) Set(v int) error {

func (b *prefBoundInt) checkForChange() {
val := b.cache.Load()
if val != nil {
cache := val.(int)
if b.p.Int(b.key) == cache {
return
}
if val != nil && b.p.Int(b.key) == *val {
return
}
b.trigger()
}
Expand All @@ -189,7 +180,7 @@ type prefBoundString struct {
base
key string
p fyne.Preferences
cache atomic.Value // string
cache atomic.Pointer[string]
}

// BindPreferenceString returns a bindable string value that is managed by the application preferences.
Expand All @@ -215,7 +206,7 @@ func BindPreferenceString(key string, p fyne.Preferences) String {

func (b *prefBoundString) Get() (string, error) {
cache := b.p.String(b.key)
b.cache.Store(cache)
b.cache.Store(&cache)
return cache, nil
}

Expand All @@ -230,11 +221,8 @@ func (b *prefBoundString) Set(v string) error {

func (b *prefBoundString) checkForChange() {
val := b.cache.Load()
if val != nil {
cache := val.(string)
if b.p.String(b.key) == cache {
return
}
if val != nil && b.p.String(b.key) == *val {
return
}
b.trigger()
}
Expand Down

0 comments on commit 77c50aa

Please sign in to comment.