Skip to content

Commit

Permalink
Merge pull request #1558 from andydotxyz/fix/1553
Browse files Browse the repository at this point in the history
Fix nil reference in disabled buttons
  • Loading branch information
andydotxyz committed Nov 16, 2020
2 parents 2ad27f5 + fefe218 commit 20d79d9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
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

0 comments on commit 20d79d9

Please sign in to comment.