Skip to content

Commit

Permalink
Change state to boolean.
Browse files Browse the repository at this point in the history
In methods and functions, state replaced by a boolean called `on`.
  • Loading branch information
jimorc committed Oct 28, 2024
1 parent 4bccdbd commit 75b5862
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 49 deletions.
12 changes: 6 additions & 6 deletions cmd/twostatetoolbaraction_demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ func main() {
w := a.NewWindow("Two State Demo")

twoState0 := xwidget.NewTwoStateToolbarAction(nil,
nil, func(state xwidget.TwoStateState) {
fmt.Println(state)
nil, func(on bool) {
fmt.Println(on)
})
sep := widget.NewToolbarSeparator()
tb := widget.NewToolbar(twoState0, sep)

toggleButton := widget.NewButton("Toggle State", func() {
state := twoState0.GetState()
twoState0.SetState(!state)
on := twoState0.GetOn()
twoState0.SetOn(!on)
})
offIconButton := widget.NewButton("Set OffIcon", func() {
twoState0.SetOffStateIcon(theme.MediaPlayIcon())
twoState0.SetOffIcon(theme.MediaPlayIcon())
})
onIconButton := widget.NewButton("Set OnIcon", func() {
twoState0.SetState1Icon(theme.MediaPauseIcon())
twoState0.SetOnIcon(theme.MediaStopIcon())
})
vc := container.NewVBox(toggleButton, offIconButton, onIconButton)
c := container.NewBorder(tb, vc, nil, nil)
Expand Down
53 changes: 25 additions & 28 deletions widget/twostatetoolbaraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ import (
"fyne.io/fyne/v2/widget"
)

// TwoStateState defines the type for the state of a TwoStateToolbarAction.
type TwoStateState bool

const (
OffState TwoStateState = false
OnState TwoStateState = true
)

// TwoStateToolbarAction is a push button style of ToolbarItem that displays a different
// icon depending on its state
// icon depending on its state.
//
// state is a boolean indicating off and on. The actual meaning of the boolean depends on how it is used. For
// example, in a media play app, false might indicate that the medium file is not being played, and true might
// indicate that the file is being played.
// Similarly, the two states could be used to indicate that a panel is being hidden or shown.
type TwoStateToolbarAction struct {
state TwoStateState
on bool
offIcon fyne.Resource
onIcon fyne.Resource
OnActivated func(TwoStateState) `json:"-"`
OnActivated func(bool) `json:"-"`

button widget.Button
}
Expand All @@ -28,37 +25,37 @@ type TwoStateToolbarAction struct {
// a different icon for each of its two states
func NewTwoStateToolbarAction(offStateIcon fyne.Resource,
onStateIcon fyne.Resource,
onTapped func(TwoStateState)) *TwoStateToolbarAction {
onTapped func(bool)) *TwoStateToolbarAction {
t := &TwoStateToolbarAction{offIcon: offStateIcon, onIcon: onStateIcon, OnActivated: onTapped}
t.button.SetIcon(t.offIcon)
t.button.OnTapped = t.activated
return t
}

// GetState returns the current state of the toolbaraction
func (t *TwoStateToolbarAction) GetState() TwoStateState {
return t.state
// GetOn returns the current state of the toolbaraction
func (t *TwoStateToolbarAction) GetOn() bool {
return t.on
}

// SetState sets the state of the toolbaraction
func (t *TwoStateToolbarAction) SetState(state TwoStateState) {
t.state = state
// SetOn sets the state of the toolbaraction
func (t *TwoStateToolbarAction) SetOn(on bool) {
t.on = on
if t.OnActivated != nil {
t.OnActivated(t.state)
t.OnActivated(t.on)
}
t.setButtonIcon()
t.button.Refresh()
}

// SetOffStateIcon sets the icon that is displayed when the state is OffState
func (t *TwoStateToolbarAction) SetOffStateIcon(icon fyne.Resource) {
// SetOffIcon sets the icon that is displayed when the state is false
func (t *TwoStateToolbarAction) SetOffIcon(icon fyne.Resource) {
t.offIcon = icon
t.setButtonIcon()
t.button.Refresh()
}

// SetOnStateIcon sets the icon that is displayed when the state is OnState
func (t *TwoStateToolbarAction) SetOnStateIcon(icon fyne.Resource) {
// SetOnIcon sets the icon that is displayed when the state is true
func (t *TwoStateToolbarAction) SetOnIcon(icon fyne.Resource) {
t.onIcon = icon
t.setButtonIcon()
t.button.Refresh()
Expand All @@ -75,20 +72,20 @@ func (t *TwoStateToolbarAction) ToolbarObject() fyne.CanvasObject {
}

func (t *TwoStateToolbarAction) activated() {
if t.state == OffState {
t.state = OnState
if !t.on {
t.on = true
} else {
t.state = OffState
t.on = false
}
t.setButtonIcon()
if t.OnActivated != nil {
t.OnActivated(t.state)
t.OnActivated(t.on)
}
t.button.Refresh()
}

func (t *TwoStateToolbarAction) setButtonIcon() {
if t.state == OffState {
if !t.on {
t.button.Icon = t.offIcon
} else {
t.button.Icon = t.onIcon
Expand Down
30 changes: 15 additions & 15 deletions widget/twostatetoolbaraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func TestNewTwoStateToolbarAction(t *testing.T) {
action := NewTwoStateToolbarAction(theme.MediaPlayIcon(),
theme.MediaPauseIcon(),
func(_ TwoStateState) {})
func(_ bool) {})
assert.Equal(t, theme.MediaPlayIcon().Name(), action.offIcon.Name())
assert.Equal(t, theme.MediaPauseIcon().Name(), action.onIcon.Name())
assert.Equal(t, action.offIcon.Name(), action.button.Icon.Name())
Expand All @@ -22,7 +22,7 @@ func TestNewTwoStateToolbarAction(t *testing.T) {
func TestTwoStateToolbarAction_Activated(t *testing.T) {
action := NewTwoStateToolbarAction(theme.MediaPlayIcon(),
theme.MediaPauseIcon(),
func(_ TwoStateState) {})
func(_ bool) {})
require.Equal(t, action.offIcon.Name(), action.button.Icon.Name())
action.button.Tapped(nil)
assert.Equal(t, action.onIcon.Name(), action.button.Icon.Name())
Expand All @@ -32,7 +32,7 @@ func TestTwoStateToolbarAction_Tapped(t *testing.T) {
test.NewApp()
action := NewTwoStateToolbarAction(theme.MediaPlayIcon(),
theme.MediaPauseIcon(),
func(_ TwoStateState) {})
func(_ bool) {})
tb := widget.NewToolbar(action)
w := test.NewWindow(tb)
defer w.Close()
Expand All @@ -42,46 +42,46 @@ func TestTwoStateToolbarAction_Tapped(t *testing.T) {
}

func TestTwoStateToolbarAction_GetSetState(t *testing.T) {
var ts TwoStateState
playState := OffState
var ts bool
playState := false
test.NewApp()
action := NewTwoStateToolbarAction(theme.MediaPlayIcon(),
theme.MediaPauseIcon(),
func(state TwoStateState) {
ts = state
func(on bool) {
ts = on
})
tb := widget.NewToolbar(action)
w := test.NewWindow(tb)
defer w.Close()
assert.Equal(t, playState, action.GetState())
action.SetState(OnState)
assert.Equal(t, OnState, action.GetState())
assert.Equal(t, OnState, ts)
assert.Equal(t, playState, action.GetOn())
action.SetOn(true)
assert.Equal(t, true, action.GetOn())
assert.Equal(t, true, ts)
test.AssertRendersToImage(t, "twostatetoolbaraction/onstate.png", w.Canvas())
}

func TestTwoStateToolbarAction_SetOffStateIcon(t *testing.T) {
test.NewApp()
action := NewTwoStateToolbarAction(nil,
theme.MediaPauseIcon(),
func(state TwoStateState) {})
func(staone bool) {})
tb := widget.NewToolbar(action)
w := test.NewWindow(tb)
defer w.Close()

action.SetOffStateIcon(theme.MediaPlayIcon())
action.SetOffIcon(theme.MediaPlayIcon())
assert.Equal(t, theme.MediaPlayIcon().Name(), action.offIcon.Name())
}

func TestTwoStateToolbarAction_SetOnStateIcon(t *testing.T) {
test.NewApp()
action := NewTwoStateToolbarAction(theme.MediaPlayIcon(),
nil,
func(state TwoStateState) {})
func(on bool) {})
tb := widget.NewToolbar(action)
w := test.NewWindow(tb)
defer w.Close()

action.SetOnStateIcon(theme.MediaPauseIcon())
action.SetOnIcon(theme.MediaPauseIcon())
assert.Equal(t, theme.MediaPauseIcon().Name(), action.onIcon.Name())
}

0 comments on commit 75b5862

Please sign in to comment.