Skip to Content
Onevra

Slider

Picks a value, or a range, by dragging a thumb along a track.

Loading the live example…

slider.go
import (
	"fmt"

	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
)

type mixer struct {
	volume, quality, low, high float64
}

func (m *mixer) View(c *ui.Context) {
	t := c.Theme()
	row := func(label, value string, slider func()) {
		ui.Column(c).Key(label).FillWidth().Gap(t.Space(1)).Children(func() {
			ui.Row(c).FillWidth().Children(func() {
				components.Text(c, label).Grow(1)
				components.Text(c, value).TextColor(t.TextMuted)
			})
			slider()
		})
	}
	ui.Column(c).Width(320).MaxWidthPercent(100).Gap(16).Children(func() {
		row("Volume", fmt.Sprintf("%.0f", m.volume), func() {
			components.Slider(c, &m.volume, 0, 100).Label("Volume").FillWidth()
		})
		row("Quality", fmt.Sprintf("%.0f", m.quality), func() {
			components.Slider(c, &m.quality, 0, 100, components.SliderOptions{Step: 20}).
				Label("Quality").FillWidth()
		})
		row("Price", fmt.Sprintf("$%.0f – $%.0f", m.low, m.high), func() {
			components.Slider(c, &m.low, 0, 100, components.SliderOptions{Range: &m.high}).
				Label("Price").FillWidth()
		})
	})
}
Source

A slider is continuous by default. Step snaps it and shows ticks; Range adds a second thumb, with the slider's value as the minimum. Tab moves between the thumbs and the arrow keys adjust the focused one.

API

Slider

func Slider(c *ui.Context, value *float64, lo, hi float64, options ...SliderOptions) *SliderControl

Slider creates a continuous, stepped or two-thumb slider. Existing calls can omit options. Range and Step may be combined. Applications own both values; range values are clamped and ordered on construction. Bounds must be finite with hi >= lo; values and Step must be finite. Nonpositive Step is continuous.

s := Slider(c, &low, 0, 100, SliderOptions{Range: &high, Step: 5})
s.Label("Price").FillWidth()
if s.Changed() { /* use low and high */ }

Pointer input uses MyGo's drag capture. Tab moves between range thumbs; arrows, PageUp/PageDown, Home and End adjust the focused thumb without crossing.

pkg.go.dev

SliderOptions

SliderOptions selects value behavior, not a visual variant.

Prop

Type

pkg.go.dev

SliderControl

SliderControl exposes the underlying layout element. Keep this handle to query Changed after applying Element layout methods, which return ui.Element.

Prop

Type

Methods
func (s *SliderControl) AutoFocus() *SliderControl

AutoFocus focuses the single thumb or the minimum thumb of a range.

func (s *SliderControl) Changed() bool

Changed reports an input-driven change to either value, not initial clamping.

func (s *SliderControl) Label(label string) *SliderControl

Label names the slider and, in range mode, both independently focusable thumbs.

pkg.go.dev
Edit this page on GitHub

On this page