What's wrong with this Extended Slider? #6078
Answered
by
dweymouth
lordofscripts
asked this question in
Q&A
-
|
I was trying to bind the mousewheel (mouse scrollwheel) to increase/decrease the slider. It appears to work partly. For one thing, I don't know whether that 25 DY is always the same across platforms. But my main issue with it is that if you move forward/backward, at some point you see that the value isdecreased but the visual ball on the slider actually decreases. import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/widget"
)
type ScrollableSlider struct {
*widget.Slider
OnScrolled func(*fyne.ScrollEvent)
}
func NewScrollableSlider(min, max float64) *ScrollableSlider {
ss := &ScrollableSlider{
Slider: widget.NewSlider(min, max),
}
return ss
}
func NewScrollableSliderWidthData(min, max float64, data binding.Float) *ScrollableSlider {
ss := &ScrollableSlider{
Slider: widget.NewSliderWithData(min, max, data),
}
return ss
}
// implements fyne.Scrollable
func (g *ScrollableSlider) Scrolled(event *fyne.ScrollEvent) {
// @note FWD scroll gives Dx:0 Dy:25 and BACK scroll Dx:0 Dy:-25
// I wonder if that 25 is the same for all platforms or does it vary?
const DIVIDER int = 25
// dx := int(float64(event.Scrolled.DX))
dy := int(float64(event.Scrolled.DY)) / DIVIDER
//fmt.Printf("Slider mousewheel dX:%d dY:%d\n", dx, dy)
newValue := g.Value + float64(dy)
if newValue > g.Max {
newValue = g.Max
} else if newValue < g.Min {
newValue = g.Min
}
fmt.Printf("Slider mousewheel dY:%d new:%f\n", dy, newValue)
g.SetValue(newValue)
g.Refresh()
} |
Beta Was this translation helpful? Give feedback.
Answered by
dweymouth
Jan 16, 2026
Replies: 1 comment 1 reply
-
|
Two things:
So it should look something like: type ScrollableSlider struct {
widget.Slider // removed pointer embed
OnScrolled func(*fyne.ScrollEvent)
}
func NewScrollableSlider(min, max float64) *ScrollableSlider {
ss := &ScrollableSlider{
Slider: widget.Slider{Min: min, Max: max), // init base struct directly
}
ss.ExtendBaseWidget(ss) // need this to set up extension correctly
return ss
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
lordofscripts
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Two things:
ExtendBaseWidgetwidget.Slider, not*widget.SliderSo it should look something like: