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

Add the ability to disable ToolbarActions #2306 #4347

Merged
merged 12 commits into from
Feb 1, 2024
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)
}
Loading