Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash in label with binding #2131

Merged
merged 2 commits into from Apr 7, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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() {
}
fpabl0 marked this conversation as resolved.
Show resolved Hide resolved

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 {
fpabl0 marked this conversation as resolved.
Show resolved Hide resolved
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