Skip to content

Commit

Permalink
Add the ability to disable ToolbarActions fyne-io#2306
Browse files Browse the repository at this point in the history
  • Loading branch information
pravin772 committed Dec 31, 2022
1 parent 8c25095 commit 9502efa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
15 changes: 13 additions & 2 deletions widget/toolbar.go
Expand Up @@ -15,6 +15,7 @@ type ToolbarItem interface {

// ToolbarAction is push button style of ToolbarItem
type ToolbarAction struct {
DisableableWidget
Icon fyne.Resource
OnActivated func() `json:"-"`
}
Expand All @@ -23,7 +24,7 @@ type ToolbarAction struct {
func (t *ToolbarAction) ToolbarObject() fyne.CanvasObject {
button := NewButtonWithIcon("", t.Icon, t.OnActivated)
button.Importance = LowImportance

button.disabled = t.disabled
return button
}

Expand All @@ -37,7 +38,17 @@ func (t *ToolbarAction) SetIcon(icon fyne.Resource) {

// NewToolbarAction returns a new push button style ToolbarItem
func NewToolbarAction(icon fyne.Resource, onActivated func()) *ToolbarAction {
return &ToolbarAction{icon, onActivated}
toolbarAction := &ToolbarAction{
Icon: icon,
OnActivated: onActivated,
}
return toolbarAction
}

// SetDisabled updated the disabled property of ToolbarAction
func (t *ToolbarAction) SetDisabled(disabled bool) {
t.disabled = disabled
t.ToolbarObject().Refresh()
}

// ToolbarSpacer is a blank, stretchable space for a toolbar.
Expand Down
8 changes: 8 additions & 0 deletions widget/toolbar_test.go
Expand Up @@ -83,6 +83,14 @@ func TestToolbar_SetIcon(t *testing.T) {
assert.Equal(t, newIcon, toolbarItem.Icon)
}

func TestToolbar_SetDisabled(t *testing.T) {
testIcon := theme.FyneLogo()
toolbarItem := NewToolbarAction(testIcon, nil)
toolbarItem.SetDisabled(true)
assert.NotEqual(t, false, toolbarItem.disabled)
assert.Equal(t, true, toolbarItem.disabled)
}

type toolbarLabel struct {
*Label
}
Expand Down

0 comments on commit 9502efa

Please sign in to comment.