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
maruu committed Nov 8, 2023
1 parent e250829 commit 1765685
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
30 changes: 24 additions & 6 deletions widget/toolbar.go
Expand Up @@ -17,27 +17,45 @@ type ToolbarItem interface {
type ToolbarAction struct {
Icon fyne.Resource
OnActivated func() `json:"-"`
button *Button
}

// ToolbarObject gets a button to render this ToolbarAction
func (t *ToolbarAction) ToolbarObject() fyne.CanvasObject {
button := NewButtonWithIcon("", t.Icon, t.OnActivated)
button.Importance = LowImportance

return button
t.button.OnTapped = t.OnActivated
return t.button
}

// SetIcon updates the icon on a ToolbarItem
//
// Since: 2.2
func (t *ToolbarAction) SetIcon(icon fyne.Resource) {
t.Icon = icon
t.ToolbarObject().Refresh()
t.button.SetIcon(t.Icon)
t.button.Refresh()
}

// Enable this ToolbarAction, updating any style or features appropriately.
func (t *ToolbarAction) Enable() {
t.button.Enable()
}

// Disable this ToolbarAction so that it cannot be interacted with, updating any style appropriately.
func (t *ToolbarAction) Disable() {
t.button.Disable()
}

// Disabled returns true if this ToolbarAction is currently disabled or false if it can currently be interacted with.
func (t *ToolbarAction) Disabled() bool {
return t.button.Disabled()
}

// NewToolbarAction returns a new push button style ToolbarItem
func NewToolbarAction(icon fyne.Resource, onActivated func()) *ToolbarAction {
return &ToolbarAction{icon, onActivated}
button := NewButtonWithIcon("", icon, onActivated)
button.Importance = LowImportance

return &ToolbarAction{Icon:icon, OnActivated: onActivated, button: button}
}

// ToolbarSpacer is a blank, stretchable space for a toolbar.
Expand Down
17 changes: 17 additions & 0 deletions widget/toolbar_test.go
Expand Up @@ -90,3 +90,20 @@ type toolbarLabel struct {
func (t *toolbarLabel) ToolbarObject() fyne.CanvasObject {
return t.Label
}

func TestToolbarAction_Disable(t *testing.T) {
testIcon := theme.InfoIcon()
toolbarAction := NewToolbarAction(testIcon, nil)
toolbarAction.Disable()
assert.NotEqual(t, false, toolbarAction.Disabled())
assert.Equal(t, true, toolbarAction.Disabled())
}

func TestToolbarAction_Enable(t *testing.T) {
testIcon := theme.InfoIcon()
toolbarAction := NewToolbarAction(testIcon, nil)
toolbarAction.Disable()
toolbarAction.Enable()
assert.NotEqual(t, true, toolbarAction.Disabled())
assert.Equal(t, false, toolbarAction.Disabled())
}

0 comments on commit 1765685

Please sign in to comment.