Skip to content

Commit

Permalink
Fix crash in label with binding
Browse files Browse the repository at this point in the history
Moved to a different setup model where we don't recreate listeners all the time.
Fixes fyne-io#2125
  • Loading branch information
andydotxyz committed Apr 2, 2021
1 parent f9cee28 commit 3fa131d
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions widget/label.go
Expand Up @@ -55,18 +55,7 @@ func NewLabelWithStyle(text string, alignment fyne.TextAlign, style fyne.TextSty
func (l *Label) Bind(data binding.String) {
l.Unbind()
l.textSource = data
l.textListener = binding.NewDataListener(func() {
val, err := l.textSource.Get()
if err != nil {
fyne.LogError("Error getting current data value", err)
return
}

l.Text = val
if cache.IsRendered(l) {
l.Refresh()
}
})
l.createListener()
data.AddListener(l.textListener)
}

Expand Down Expand Up @@ -114,10 +103,31 @@ func (l *Label) Unbind() {
}

l.textSource.RemoveListener(l.textListener)
l.textListener = nil
l.textSource = nil
}

func (l *Label) createListener() {
if l.textListener != nil {
return
}

l.textListener = binding.NewDataListener(func() {
if l.textSource == nil {
return
}
val, err := l.textSource.Get()
if err != nil {
fyne.LogError("Error getting current data value", err)
return
}

l.Text = val
if cache.IsRendered(l) {
l.Refresh()
}
})
}

// textAlign tells the rendering textProvider our alignment
func (l *Label) textAlign() fyne.TextAlign {
return l.Alignment
Expand Down

0 comments on commit 3fa131d

Please sign in to comment.