Skip to content

Commit

Permalink
Merge pull request #4347 from maruu/2306-disable-toolbar-actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz authored Feb 1, 2024
2 parents 3a34b21 + 62610f6 commit 34502cd
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
33 changes: 28 additions & 5 deletions widget/toolbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,50 @@ 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
// synchronize properties
t.button.Icon = t.Icon
t.button.OnTapped = t.OnActivated

return button
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)
}

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

// Disable this ToolbarAction so that it cannot be interacted with, updating any style appropriately.
//
// Since: 2.5
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.
//
// Since: 2.5
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}
return &ToolbarAction{Icon: icon, OnActivated: onActivated}
}

// ToolbarSpacer is a blank, stretchable space for a toolbar.
Expand Down
44 changes: 44 additions & 0 deletions widget/toolbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,47 @@ 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())
}

func TestToolbarAction_UpdateOnActivated(t *testing.T) {
activated := false

testIcon := theme.InfoIcon()
toolbarAction := NewToolbarAction(testIcon, func() { activated = true })

test.Tap(toolbarAction.ToolbarObject().(*Button))

assert.True(t, activated)

activated = false

// verify that changes are synchronized as well
toolbarAction.OnActivated = func() {}

test.Tap(toolbarAction.ToolbarObject().(*Button))

assert.False(t, activated)
}

func TestToolbarAction_DefaultCreation(t *testing.T) {
testIcon := theme.InfoIcon()
toolbarAction := ToolbarAction{Icon: testIcon}
obj := toolbarAction.ToolbarObject()
assert.Equal(t, testIcon, obj.(*Button).Icon)
}

0 comments on commit 34502cd

Please sign in to comment.