Skip to content

Commit

Permalink
Merge pull request #2108 from fpabl0/fix/1748
Browse files Browse the repository at this point in the history
Call OnChanged only when the slider.Value has changed, fixes #1748
  • Loading branch information
andydotxyz committed Mar 23, 2021
2 parents 888304b + 59192e8 commit 194cc44
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 13 additions & 0 deletions widget/slider.go
Expand Up @@ -100,7 +100,14 @@ func (s *Slider) DragEnd() {
func (s *Slider) Dragged(e *fyne.DragEvent) {
ratio := s.getRatio(&(e.PointEvent))

lastValue := s.Value

s.updateValue(ratio)

if lastValue == s.Value {
return
}

s.Refresh()

if s.OnChanged != nil {
Expand Down Expand Up @@ -174,9 +181,15 @@ func (s *Slider) SetValue(value float64) {
return
}

lastValue := s.Value

s.Value = value
s.clampValueToRange()

if lastValue == s.Value {
return
}

if s.OnChanged != nil {
s.OnChanged(s.Value)
}
Expand Down
14 changes: 12 additions & 2 deletions widget/slider_test.go
Expand Up @@ -118,7 +118,8 @@ func TestSlider_VerticalLayout(t *testing.T) {
}

func TestSlider_OnChanged(t *testing.T) {
slider := NewSlider(0, 1)
slider := NewSlider(0, 2)
slider.Resize(slider.MinSize())
assert.Empty(t, slider.OnChanged)

changes := 0
Expand All @@ -130,9 +131,18 @@ func TestSlider_OnChanged(t *testing.T) {
assert.Equal(t, 0, changes)

slider.SetValue(0.5)
assert.Equal(t, 0, changes)

drag := &fyne.DragEvent{}
drag.PointEvent.Position = fyne.NewPos(25, 2)
slider.Dragged(drag)
assert.Equal(t, 1, changes)

drag.PointEvent.Position = fyne.NewPos(25, 2)
slider.Dragged(drag)
assert.Equal(t, 1, changes)

drag := &fyne.DragEvent{Dragged: fyne.NewDelta(10, 2)}
drag.PointEvent.Position = fyne.NewPos(50, 2)
slider.Dragged(drag)
assert.Equal(t, 2, changes)
}
Expand Down

0 comments on commit 194cc44

Please sign in to comment.