slider

package
v0.1.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package slider provides a draggable slider widget for selecting a value from a continuous or discrete range.

Construction uses functional options for immutable configuration, while fluent methods handle mutable styling:

s := slider.New(
    slider.Min(0),
    slider.Max(100),
    slider.Value(50),
    slider.OnChange(handleChange),
).Padding(8)

Visual Style

The visual rendering is provided by a Painter implementation. Each design system (Material 3, Fluent, Cupertino) supplies its own painter to render sliders in the appropriate visual style.

If no painter is set, DefaultPainter is used, which draws a minimal slider suitable for testing and prototyping.

Orientation

Two orientations are available:

Value Range

The slider operates within [Min, Max] bounds. If Step is set to a value greater than zero, the value snaps to the nearest step. When Step is zero, the slider is continuous.

Marks

Optional Mark values can annotate specific positions on the track (e.g. tick marks or labeled positions). Marks are purely visual and do not constrain the slider value.

Interaction

Sliders respond to:

  • Mouse click on the track to jump to a value
  • Mouse drag on the thumb to continuously adjust the value
  • Keyboard: Left/Down to decrease, Right/Up to increase, Home for minimum, End for maximum, Page Up/Down for larger steps

Disabled sliders ignore all interaction and are drawn with a dimmed appearance.

Signal Binding

Slider properties can be bound to reactive signals from the state package. When a signal value changes, the slider automatically reflects the new state.

  • ValueSignal -- TWO-WAY binding: reads value from signal AND writes back on change
  • DisabledSignal -- one-way binding for the disabled state

Example:

volume := state.NewSignal[float32](50)
s := slider.New(
    slider.Min(0),
    slider.Max(100),
    slider.ValueSignal(volume),
    slider.OnChange(func(v float32) {
        fmt.Printf("volume: %.0f\n", v)
    }),
)

Focus

Sliders implement widget.Focusable and participate in tab navigation. A focus ring is drawn around the thumb when the slider has keyboard focus.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultPainter

type DefaultPainter struct{}

DefaultPainter provides a minimal fallback painter with no design system styling. It draws a simple slider -- useful for testing and as a base reference.

func (DefaultPainter) PaintSlider

func (p DefaultPainter) PaintSlider(canvas widget.Canvas, ps PaintState)

PaintSlider renders a minimal slider with gray track, blue active segment, and a white thumb circle with border. If state.ColorScheme is non-zero, its colors are used instead of built-in defaults.

type Mark

type Mark struct {
	// Value is the position on the slider where the mark appears.
	Value float32

	// Label is the optional text label displayed near the mark.
	// If empty, only a tick mark is drawn.
	Label string
}

Mark represents a labeled position on the slider track. Marks are purely visual annotations; they do not constrain the slider value.

type Option

type Option func(*config)

Option configures a slider during construction.

func A11yHint

func A11yHint(hint string) Option

A11yHint sets the accessibility hint text for the slider.

func Disabled

func Disabled(d bool) Option

Disabled sets the slider's disabled state. A disabled slider does not respond to user input and is drawn with a dimmed appearance.

func DisabledFn

func DisabledFn(fn func() bool) Option

DisabledFn sets a dynamic function that is evaluated to determine whether the slider is disabled. When set, this takes precedence over the static value but not over a signal set via DisabledSignal.

func DisabledReadonlySignal

func DisabledReadonlySignal(sig state.ReadonlySignal[bool]) Option

DisabledReadonlySignal binds the slider's disabled state to a read-only signal. This is useful for computed signals created via state.NewComputed. When set, this takes highest precedence over all other disabled sources.

func DisabledSignal

func DisabledSignal(sig state.Signal[bool]) Option

DisabledSignal binds the slider's disabled state to a reactive signal. When set, the signal value takes precedence over both DisabledFn and Disabled but not over DisabledReadonlySignal.

func Marks

func Marks(m []Mark) Option

Marks sets the slider's tick marks.

func Max

func Max(v float32) Option

Max sets the slider's maximum value. Default is 100.

func Min

func Min(v float32) Option

Min sets the slider's minimum value. Default is 0.

func OnChange

func OnChange(fn func(float32)) Option

OnChange sets the callback invoked when the slider value changes. The callback receives the new value.

func OrientationOpt

func OrientationOpt(o Orientation) Option

OrientationOpt sets the slider's orientation.

func PainterOpt

func PainterOpt(p Painter) Option

PainterOpt sets the painter used to render the slider. Each design system provides its own painter. If not set, DefaultPainter is used.

func Step

func Step(v float32) Option

Step sets the step increment. When step > 0, the value snaps to the nearest multiple of step within [min, max]. When step is 0 (default), the slider is continuous.

func Value

func Value(v float32) Option

Value sets the slider's initial static value.

func ValueFn

func ValueFn(fn func() float32) Option

ValueFn sets a dynamic value function that is evaluated on each draw. When set, this takes precedence over the static value but not over a signal set via ValueSignal.

func ValueReadonlySignal

func ValueReadonlySignal(sig state.ReadonlySignal[float32]) Option

ValueReadonlySignal binds the slider's value to a read-only signal. This is useful for computed signals created via state.NewComputed. When set, this takes highest precedence over all other value sources.

func ValueSignal

func ValueSignal(sig state.Signal[float32]) Option

ValueSignal binds the slider's value to a reactive signal. This is a TWO-WAY binding: the widget reads the value from the signal, and when the user drags the thumb, the new value is written back to the signal. When set, the signal value takes precedence over both ValueFn and Value but not over ValueReadonlySignal.

type Orientation

type Orientation uint8

Orientation controls the direction of the slider track.

const (
	// Horizontal renders the slider with the thumb moving left to right.
	// This is the default orientation.
	Horizontal Orientation = iota

	// Vertical renders the slider with the thumb moving bottom to top.
	Vertical
)

Orientation constants.

func (Orientation) String

func (o Orientation) String() string

String returns a human-readable name for the orientation.

type PaintState

type PaintState struct {
	Value       float32           // current value (within [Min, Max])
	Min         float32           // minimum value
	Max         float32           // maximum value
	Progress    float32           // normalized 0..1 progress
	Hovered     bool              // mouse is over the slider
	Dragging    bool              // thumb is being dragged
	Focused     bool              // slider has keyboard focus
	Disabled    bool              // slider is disabled
	Bounds      geometry.Rect     // total widget bounds
	Orientation Orientation       // horizontal or vertical
	Marks       []Mark            // optional tick marks
	ColorScheme SliderColorScheme // theme-derived colors
}

PaintState provides the current slider state to the painter.

type Painter

type Painter interface {
	PaintSlider(canvas widget.Canvas, state PaintState)
}

Painter draws the visual representation of a slider. Each design system (Material 3, Fluent, Cupertino) provides its own Painter implementation to render the slider in its visual style.

If no Painter is set, the slider uses DefaultPainter.

type SliderColorScheme

type SliderColorScheme struct {
	ActiveTrack   widget.Color // active (filled) portion of the track
	InactiveTrack widget.Color // inactive (empty) portion of the track
	Thumb         widget.Color // thumb circle fill
	ThumbBorder   widget.Color // thumb circle border
	FocusRing     widget.Color // focus indicator color
	DisabledTrack widget.Color // track color when disabled
	DisabledThumb widget.Color // thumb color when disabled
	MarkColor     widget.Color // tick mark color
}

SliderColorScheme provides theme-derived colors for slider painting. Zero value means the painter should use its built-in defaults.

type Widget

type Widget struct {
	widget.WidgetBase
	// contains filtered or unexported fields
}

Widget implements a draggable slider for selecting a value from a range.

A slider is created with New using functional options:

s := slider.New(
    slider.Min(0),
    slider.Max(100),
    slider.Value(50),
    slider.OnChange(handleChange),
)

Fluent styling methods may be chained after construction:

s.Padding(8)

func New

func New(opts ...Option) *Widget

New creates a new slider Widget with the given options.

The returned widget is visible, enabled, and focusable by default. The default range is [0, 100] with Horizontal orientation.

func (*Widget) Children

func (w *Widget) Children() []widget.Widget

Children returns nil because a slider is a leaf widget.

func (*Widget) Draw

func (w *Widget) Draw(ctx widget.Context, canvas widget.Canvas)

Draw renders the slider to the canvas.

func (*Widget) Event

func (w *Widget) Event(ctx widget.Context, e event.Event) bool

Event handles an input event and returns true if consumed.

func (*Widget) IsFocusable

func (w *Widget) IsFocusable() bool

IsFocusable reports whether the slider can currently receive focus. A slider is focusable when it is visible, enabled, and not disabled.

func (*Widget) Layout

func (w *Widget) Layout(_ widget.Context, constraints geometry.Constraints) geometry.Size

Layout calculates the slider's preferred size within the given constraints.

func (*Widget) Mount

func (w *Widget) Mount(ctx widget.Context)

Mount creates signal bindings for push-based invalidation. Implements widget.Lifecycle.

func (*Widget) Padding

func (w *Widget) Padding(v float32) *Widget

Padding sets the padding around the slider. Returns the widget for method chaining.

func (*Widget) Unmount

func (w *Widget) Unmount()

Unmount is called when the slider is removed from the widget tree. Implements widget.Lifecycle.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL