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:
- Vertical -- items stacked top-to-bottom
- Horizontal -- items placed left-to-right
Keyboard Navigation ¶
Arrow keys navigate between items within a group:
- Up/Down for Vertical layout
- Left/Right for Horizontal layout
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 ¶
- type DefaultPainter
- type Direction
- type Group
- func (g *Group) Children() []widget.Widget
- func (g *Group) Draw(ctx widget.Context, canvas widget.Canvas)
- func (g *Group) Event(ctx widget.Context, e event.Event) bool
- func (g *Group) IsFocusable() bool
- func (g *Group) ItemAt(index int) *Item
- func (g *Group) ItemCount() int
- func (g *Group) Layout(ctx widget.Context, constraints geometry.Constraints) geometry.Size
- func (g *Group) Mount(ctx widget.Context)
- func (g *Group) Select(value string)
- func (g *Group) Selected() string
- func (g *Group) Unmount()
- type GroupOption
- func DirectionOpt(d Direction) GroupOption
- func GroupA11yLabel(s string) GroupOption
- func GroupDisabled(d bool) GroupOption
- func GroupDisabledFn(fn func() bool) GroupOption
- func GroupDisabledReadonlySignal(sig state.ReadonlySignal[bool]) GroupOption
- func GroupDisabledSignal(sig state.Signal[bool]) GroupOption
- func GroupPainter(p Painter) GroupOption
- func Items(defs ...ItemDef) GroupOption
- func OnChange(fn func(value string)) GroupOption
- func Selected(value string) GroupOption
- func SelectedSignal(sig state.Signal[string]) GroupOption
- type Item
- func (it *Item) Children() []widget.Widget
- func (it *Item) Draw(ctx widget.Context, canvas widget.Canvas)
- func (it *Item) Event(ctx widget.Context, e event.Event) bool
- func (it *Item) IsFocusable() bool
- func (it *Item) Label() string
- func (it *Item) Layout(_ widget.Context, constraints geometry.Constraints) geometry.Size
- func (it *Item) Value() string
- type ItemDef
- type PaintState
- type Painter
- type RadioColorScheme
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.
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) IsFocusable ¶
IsFocusable reports whether the group itself can receive focus. The group delegates focus to its items, so it always returns false.
func (*Group) Mount ¶
Mount creates signal bindings for push-based invalidation. Implements widget.Lifecycle.
func (*Group) Select ¶
Select programmatically selects the item with the given value. If no item matches the value, the selection is cleared.
func (*Group) Selected ¶
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) IsFocusable ¶
IsFocusable reports whether the item can currently receive focus. An item is focusable when it is visible, enabled, and its group is not disabled.
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.