radio

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package radio provides a mutually-exclusive radio group widget.

A radio group contains a set of radio items, exactly one of which may be selected at a time. Construction uses functional options for immutable configuration:

rg := radio.NewGroup(
    radio.Items(
        radio.ItemDef{Value: "s", Label: "Small"},
        radio.ItemDef{Value: "m", Label: "Medium"},
        radio.ItemDef{Value: "l", Label: "Large"},
    ),
    radio.Selected("m"),
    radio.OnChange(handleChange),
)

Visual Style

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

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

Direction

Items can be laid out vertically (default) or horizontally:

Keyboard Navigation

Arrow keys navigate between items within a group:

Space or Enter on a focused item selects it.

Signal Binding

Radio group supports reactive signal bindings for the selected value and disabled state:

sel := state.New("m")
rg := radio.NewGroup(
    radio.Items(
        radio.ItemDef{Value: "s", Label: "Small"},
        radio.ItemDef{Value: "m", Label: "Medium"},
        radio.ItemDef{Value: "l", Label: "Large"},
    ),
    radio.SelectedSignal(sel),
)

SelectedSignal provides TWO-WAY binding: reading uses the signal value, and user selection writes back to the signal. GroupDisabledSignal is one-way (read-only from signal).

Priority for selected: Signal > Static. Priority for disabled: Signal > Fn > Static.

Interaction

Clicking a radio item selects it and deselects the previously selected item. Disabled groups ignore all interaction and are drawn with a dimmed appearance.

Focus

Individual radio items implement widget.Focusable and participate in tab navigation. The group manages focus transfer between items via arrow keys.

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 radio button -- useful for testing and as a base reference.

func (DefaultPainter) PaintRadio

func (p DefaultPainter) PaintRadio(canvas widget.Canvas, state PaintState)

PaintRadio renders a minimal radio item with gray colors. If state.ColorScheme is non-zero, its colors are used instead of built-in defaults.

type Direction

type Direction int

Direction controls the layout orientation of radio items within a group.

const (
	// Vertical lays out items from top to bottom.
	Vertical Direction = iota

	// Horizontal lays out items from left to right.
	Horizontal
)

Direction constants.

func (Direction) String

func (d Direction) String() string

String returns a human-readable name for the direction.

type Group

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

Group is a container widget that holds a set of mutually-exclusive radio items.

Exactly one item may be selected at a time. Create a group with NewGroup:

rg := radio.NewGroup(
    radio.Items(
        radio.ItemDef{Value: "s", Label: "Small"},
        radio.ItemDef{Value: "m", Label: "Medium"},
    ),
    radio.Selected("m"),
    radio.OnChange(func(v string) { fmt.Println("selected:", v) }),
)

func NewGroup

func NewGroup(opts ...GroupOption) *Group

NewGroup creates a new radio Group with the given options.

The returned group is visible, enabled, and lays out items vertically by default. Use options to configure items, selected value, direction, and change handler.

func (*Group) Children

func (g *Group) Children() []widget.Widget

Children returns the items as a slice of widget.Widget.

func (*Group) Draw

func (g *Group) Draw(ctx widget.Context, canvas widget.Canvas)

Draw renders all items to the canvas.

func (*Group) Event

func (g *Group) Event(ctx widget.Context, e event.Event) bool

Event handles an input event by delegating to the appropriate item.

func (*Group) IsFocusable

func (g *Group) IsFocusable() bool

IsFocusable reports whether the group itself can receive focus. The group delegates focus to its items, so it always returns false.

func (*Group) ItemAt

func (g *Group) ItemAt(index int) *Item

ItemAt returns the item at the given index. Panics if index is out of range.

func (*Group) ItemCount

func (g *Group) ItemCount() int

ItemCount returns the number of items in the group.

func (*Group) Layout

func (g *Group) Layout(ctx widget.Context, constraints geometry.Constraints) geometry.Size

Layout calculates the group's preferred size by laying out all items.

func (*Group) Mount

func (g *Group) Mount(ctx widget.Context)

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

func (*Group) Select

func (g *Group) Select(value string)

Select programmatically selects the item with the given value. If no item matches the value, the selection is cleared.

func (*Group) Selected

func (g *Group) Selected() string

Selected returns the value of the currently selected item. When a SelectedSignal is bound, the signal value is returned directly. Returns an empty string if no item is selected.

func (*Group) Unmount

func (g *Group) Unmount()

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

type GroupOption

type GroupOption func(*groupConfig)

GroupOption configures a radio group during construction.

func DirectionOpt

func DirectionOpt(d Direction) GroupOption

DirectionOpt sets the layout direction for the group's items.

func GroupA11yLabel

func GroupA11yLabel(s string) GroupOption

GroupA11yLabel sets the accessibility label for the radio group.

func GroupDisabled

func GroupDisabled(d bool) GroupOption

GroupDisabled sets the group's disabled state. A disabled group does not respond to user input and all items are drawn with a dimmed appearance.

func GroupDisabledFn

func GroupDisabledFn(fn func() bool) GroupOption

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

func GroupDisabledReadonlySignal

func GroupDisabledReadonlySignal(sig state.ReadonlySignal[bool]) GroupOption

GroupDisabledReadonlySignal binds the group'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 GroupDisabledSignal

func GroupDisabledSignal(sig state.Signal[bool]) GroupOption

GroupDisabledSignal binds the group's disabled state to a reactive signal. When set, the signal value takes precedence over both GroupDisabledFn and GroupDisabled but not over GroupDisabledReadonlySignal.

func GroupPainter

func GroupPainter(p Painter) GroupOption

GroupPainter sets the painter used to render each radio item. Each design system provides its own painter. If not set, DefaultPainter is used.

func Items

func Items(defs ...ItemDef) GroupOption

Items sets the item definitions for the group. Each ItemDef describes a single radio item's value and label.

func OnChange

func OnChange(fn func(value string)) GroupOption

OnChange sets the callback invoked when the selected item changes. The callback receives the value of the newly selected item.

func Selected

func Selected(value string) GroupOption

Selected sets the initially selected item by value. If no item matches the value, no item is selected.

func SelectedSignal

func SelectedSignal(sig state.Signal[string]) GroupOption

SelectedSignal binds the group's selected value to a reactive signal. This is a TWO-WAY binding: the widget reads the selected value from the signal, and when the user selects an item, the new value is written back to the signal. When set, the signal value takes precedence over Selected.

type Item

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

Item represents a single radio button within a Group.

Items are created automatically by NewGroup from the ItemDef list provided via the Items option. Each item holds a reference to its parent group for mutual-exclusion selection.

func (*Item) Children

func (it *Item) Children() []widget.Widget

Children returns nil because a radio item is a leaf widget.

func (*Item) Draw

func (it *Item) Draw(ctx widget.Context, canvas widget.Canvas)

Draw renders the radio item to the canvas.

func (*Item) Event

func (it *Item) Event(ctx widget.Context, e event.Event) bool

Event handles an input event and returns true if consumed.

func (*Item) IsFocusable

func (it *Item) IsFocusable() bool

IsFocusable reports whether the item can currently receive focus. An item is focusable when it is visible, enabled, and its group is not disabled.

func (*Item) Label

func (it *Item) Label() string

Label returns the display label of this radio item.

func (*Item) Layout

func (it *Item) Layout(_ widget.Context, constraints geometry.Constraints) geometry.Size

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

func (*Item) Value

func (it *Item) Value() string

Value returns the programmatic value of this radio item.

type ItemDef

type ItemDef struct {
	// Value is the programmatic identifier returned by [Group.Selected].
	Value string

	// Label is the human-readable text displayed next to the radio circle.
	Label string
}

ItemDef describes a radio item's value and display label.

type PaintState

type PaintState struct {
	Label    string
	Selected bool
	Hovered  bool
	Pressed  bool
	Focused  bool
	Disabled bool
	Bounds   geometry.Rect

	// ColorScheme provides theme-derived colors (zero value means use defaults).
	ColorScheme RadioColorScheme
}

PaintState provides the current radio item state to the painter.

type Painter

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

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

If no Painter is set, the radio group uses DefaultPainter.

type RadioColorScheme

type RadioColorScheme struct {
	SelectedBg       widget.Color // Filled circle when selected
	SelectedFg       widget.Color // Inner dot color
	UnselectedBorder widget.Color // Circle border when unselected
	LabelColor       widget.Color
	DisabledBg       widget.Color
	DisabledFg       widget.Color
	FocusRing        widget.Color
}

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

Jump to

Keyboard shortcuts

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