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 nil reference in disabled buttons #1558

Merged
merged 1 commit into from Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 15 additions & 25 deletions widget/button.go
Expand Up @@ -2,7 +2,6 @@ package widget

import (
"image/color"
"strings"
"time"

"fyne.io/fyne"
Expand Down Expand Up @@ -70,7 +69,6 @@ type Button struct {
Icon fyne.Resource
// Specify how prominent the button should be, High will highlight the button and Low will remove some decoration.
Importance ButtonImportance
disabledIcon fyne.Resource
Alignment ButtonAlign
IconPlacement ButtonIconPlacement

Expand All @@ -95,10 +93,9 @@ func NewButton(label string, tapped func()) *Button {
// NewButtonWithIcon creates a new button widget with the specified label, themed icon and tap handler
func NewButtonWithIcon(label string, icon fyne.Resource, tapped func()) *Button {
button := &Button{
Text: label,
Icon: icon,
disabledIcon: theme.NewDisabledResource(icon),
OnTapped: tapped,
Text: label,
Icon: icon,
OnTapped: tapped,
}

button.ExtendBaseWidget(button)
Expand Down Expand Up @@ -167,12 +164,6 @@ func (b *Button) MouseOut() {
func (b *Button) SetIcon(icon fyne.Resource) {
b.Icon = icon

if icon != nil {
b.disabledIcon = theme.NewDisabledResource(icon)
} else {
b.disabledIcon = nil
}

b.Refresh()
}

Expand All @@ -185,6 +176,10 @@ func (b *Button) SetText(text string) {

// Tapped is called when a pointer tapped event is captured and triggers any tap handler
func (b *Button) Tapped(*fyne.PointEvent) {
if b.Disabled() {
return
}

b.tapped = true
defer func() { // TODO move to a real animation
time.Sleep(time.Millisecond * buttonTapDuration)
Expand All @@ -193,7 +188,7 @@ func (b *Button) Tapped(*fyne.PointEvent) {
}()
b.Refresh()

if b.OnTapped != nil && !b.Disabled() {
if b.OnTapped != nil {
b.OnTapped()
}
}
Expand Down Expand Up @@ -291,20 +286,15 @@ func (b *buttonRenderer) Refresh() {
if b.icon == nil {
b.icon = canvas.NewImageFromResource(b.button.Icon)
b.icon.FillMode = canvas.ImageFillContain
b.SetObjects(append(b.Objects(), b.icon))
b.SetObjects([]fyne.CanvasObject{b.bg, b.label, b.icon})
}

if b.button.Disabled() {
b.icon.Resource = theme.NewDisabledResource(b.button.Icon)
} else {
if b.button.Disabled() {
// if the icon has changed, create a new disabled version
// if we could be sure that button.Icon is only ever set through the button.SetIcon method, we could remove this
if !strings.HasSuffix(b.button.disabledIcon.Name(), b.button.Icon.Name()) {
b.icon.Resource = theme.NewDisabledResource(b.button.Icon)
} else {
b.icon.Resource = b.button.disabledIcon
}
} else {
b.icon.Resource = b.button.Icon
}
b.icon.Resource = b.button.Icon
}
b.icon.Refresh()
b.icon.Show()
} else if b.icon != nil {
b.icon.Hide()
Expand Down
5 changes: 4 additions & 1 deletion widget/select_entry.go
Expand Up @@ -88,10 +88,13 @@ func (e *SelectEntry) SetOptions(options []string) {

if e.ActionItem == nil {
e.ActionItem = e.setupDropDown()
if e.Disabled() {
e.ActionItem.(fyne.Disableable).Disable()
}
}
}

func (e *SelectEntry) setupDropDown() fyne.CanvasObject {
func (e *SelectEntry) setupDropDown() *Button {
dropDownButton := NewButton("", func() {
c := fyne.CurrentApp().Driver().CanvasForObject(e.super())

Expand Down