Skip to content

Commit

Permalink
Add function to change the checked state (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torbilicious authored and andydotxyz committed Feb 11, 2019
1 parent b7b2935 commit 85d74dc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
22 changes: 16 additions & 6 deletions widget/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ type Check struct {
OnChanged func(bool) `json:"-"`
}

// SetChecked sets the the checked state and refreshes widget
func (c *Check) SetChecked(checked bool) {
if checked == c.Checked {
return
}

c.Checked = checked

if c.OnChanged != nil {
c.OnChanged(c.Checked)
}

Refresh(c)
}

// Resize sets a new size for a widget.
// Note this should not be used if the widget is being managed by a Layout within a Container.
func (c *Check) Resize(size fyne.Size) {
Expand Down Expand Up @@ -103,12 +118,7 @@ func (c *Check) Hide() {

// Tapped is called when a pointer tapped event is captured and triggers any change handler
func (c *Check) Tapped(*fyne.PointEvent) {
c.Checked = !c.Checked

if c.OnChanged != nil {
c.OnChanged(c.Checked)
}
Refresh(c)
c.SetChecked(!c.Checked)
}

// TappedSecondary is called when a secondary pointer tapped event is captured
Expand Down
35 changes: 35 additions & 0 deletions widget/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,38 @@ func TestCheckUnChecked(t *testing.T) {

assert.False(t, checked)
}

func TestCheckIsDisabledByDefault(t *testing.T) {
checkedStateFromCallback := false
NewCheck("", func(on bool) {
checkedStateFromCallback = on
})

assert.False(t, checkedStateFromCallback)
}

func TestCheckIsEnabledAfterUpdating(t *testing.T) {
checkedStateFromCallback := false
check := NewCheck("", func(on bool) {
checkedStateFromCallback = on
})

check.SetChecked(true)

assert.True(t, checkedStateFromCallback)
}

func TestCheckStateIsCorrectAfterMultipleUpdates(t *testing.T) {
checkedStateFromCallback := false
check := NewCheck("", func(on bool) {
checkedStateFromCallback = on
})

expectedCheckedState := false
for i := 0; i < 5; i++ {
check.SetChecked(expectedCheckedState)
assert.True(t, checkedStateFromCallback == expectedCheckedState)

expectedCheckedState = !expectedCheckedState
}
}

0 comments on commit 85d74dc

Please sign in to comment.