Skip to content

Commit

Permalink
make slider disableable
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Dec 23, 2023
1 parent 62c45ee commit 3600f77
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
5 changes: 4 additions & 1 deletion cmd/fyne_demo/tutorials/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ func makeInputTab(_ fyne.Window) fyne.CanvasObject {
disabledRadio := widget.NewRadioGroup([]string{"Disabled radio"}, func(string) {})
disabledRadio.Disable()

disabledSlider := widget.NewSlider(0, 1000)
disabledSlider.Disable()
return container.NewVBox(
widget.NewSelect([]string{"Option 1", "Option 2", "Option 3"}, func(s string) { fmt.Println("selected", s) }),
selectEntry,
Expand All @@ -359,7 +361,8 @@ func makeInputTab(_ fyne.Window) fyne.CanvasObject {
checkGroup,
radio,
disabledRadio,
widget.NewSlider(0, 1000),
container.NewBorder(nil, nil, widget.NewLabel("Slider"), nil, widget.NewSlider(0, 1000)),
container.NewBorder(nil, nil, widget.NewLabel("Disabled slider"), nil, disabledSlider),
)
}

Expand Down
64 changes: 59 additions & 5 deletions widget/slider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var _ fyne.Draggable = (*Slider)(nil)
var _ fyne.Focusable = (*Slider)(nil)
var _ desktop.Hoverable = (*Slider)(nil)
var _ fyne.Tappable = (*Slider)(nil)
var _ fyne.Disableable = (*Slider)(nil)

// Slider is a widget that can slide between two fixed values.
type Slider struct {
Expand All @@ -45,6 +46,7 @@ type Slider struct {
binder basicBinder
hovered bool
focused bool
disabled bool // don't use DisableableWidget so we can put Since comments on funcs
pendingChange bool // true if value changed since last OnChangeEnded
}

Expand Down Expand Up @@ -87,11 +89,16 @@ func (s *Slider) Bind(data binding.Float) {

// DragEnd is called when the drag ends.
func (s *Slider) DragEnd() {
s.fireChangeEnded()
if !s.disabled {
s.fireChangeEnded()
}
}

// DragEnd is called when a drag event occurs.
func (s *Slider) Dragged(e *fyne.DragEvent) {
if s.disabled {
return
}
ratio := s.getRatio(&e.PointEvent)
lastValue := s.Value

Expand Down Expand Up @@ -147,6 +154,9 @@ func (s *Slider) fireChangeEnded() {
//
// Since: 2.4
func (s *Slider) FocusGained() {
if s.disabled {
return
}
s.focused = true
s.Refresh()
}
Expand All @@ -155,6 +165,9 @@ func (s *Slider) FocusGained() {
//
// Since: 2.4
func (s *Slider) FocusLost() {
if s.disabled {
return
}
s.focused = false
s.Refresh()
}
Expand All @@ -163,6 +176,9 @@ func (s *Slider) FocusLost() {
//
// Since: 2.4
func (s *Slider) MouseIn(_ *desktop.MouseEvent) {
if s.disabled {
return
}
s.hovered = true
s.Refresh()
}
Expand All @@ -177,6 +193,9 @@ func (s *Slider) MouseMoved(_ *desktop.MouseEvent) {
//
// Since: 2.4
func (s *Slider) MouseOut() {
if s.disabled {
return
}
s.hovered = false
s.Refresh()
}
Expand All @@ -185,6 +204,9 @@ func (s *Slider) MouseOut() {
//
// Since: 2.4
func (s *Slider) TypedKey(key *fyne.KeyEvent) {
if s.disabled {
return
}
if s.Orientation == Vertical {
switch key.Name {
case fyne.KeyUp:
Expand Down Expand Up @@ -293,6 +315,33 @@ func (s *Slider) MinSize() fyne.Size {
return s.BaseWidget.MinSize()
}

// Disable disables the slider
//
// Since: 2.5
func (s *Slider) Disable() {
if !s.disabled {
defer s.Refresh()
}
s.disabled = true
}

// Enable enables the slider
//
// Since: 2.5
func (s *Slider) Enable() {
if s.disabled {
defer s.Refresh()
}
s.disabled = false
}

// Disabled returns true if the slider is currently disabled
//
// Since: 2.5
func (s *Slider) Disabled() bool {
return s.disabled
}

// CreateRenderer links this widget to its renderer.
func (s *Slider) CreateRenderer() fyne.WidgetRenderer {
s.ExtendBaseWidget(s)
Expand Down Expand Up @@ -373,12 +422,17 @@ type sliderRenderer struct {
// Refresh updates the widget state for drawing.
func (s *sliderRenderer) Refresh() {
s.track.FillColor = theme.InputBackgroundColor()
s.thumb.FillColor = theme.ForegroundColor()
s.active.FillColor = theme.ForegroundColor()
if s.slider.disabled {
s.thumb.FillColor = theme.DisabledColor()
s.active.FillColor = theme.DisabledColor()
} else {
s.thumb.FillColor = theme.ForegroundColor()
s.active.FillColor = theme.ForegroundColor()
}

if s.slider.focused {
if s.slider.focused && !s.slider.disabled {
s.focusIndicator.FillColor = theme.FocusColor()
} else if s.slider.hovered {
} else if s.slider.hovered && !s.slider.disabled {
s.focusIndicator.FillColor = theme.HoverColor()
} else {
s.focusIndicator.FillColor = color.Transparent
Expand Down
23 changes: 23 additions & 0 deletions widget/slider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,26 @@ func TestSlider_Focus(t *testing.T) {
slider.TypedKey(down)
assert.Equal(t, slider.Min, slider.Value)
}

func TestSlider_Disabled(t *testing.T) {
slider := NewSlider(0, 5)
slider.Disable()

changes := 0
slider.OnChanged = func(_ float64) {
changes++
}

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

slider.TypedKey(&fyne.KeyEvent{Name: fyne.KeyRight})
assert.Equal(t, 0, changes)

slider.Enable()

slider.Dragged(drag)
assert.Equal(t, 1, changes)
}

0 comments on commit 3600f77

Please sign in to comment.