checkbox

package
v0.1.7 Latest Latest
Warning

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

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

Documentation

Overview

Package checkbox provides a toggleable checkbox widget.

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

cb := checkbox.New(
    checkbox.Label("Accept terms"),
    checkbox.OnToggle(handleToggle),
    checkbox.Checked(true),
).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 checkboxes in the appropriate visual style.

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

States

A checkbox has three visual check states:

  • Unchecked (default) -- empty box with a border
  • Checked -- filled box with a checkmark
  • Indeterminate -- filled box with a horizontal dash

The indeterminate state is used for "select all" checkboxes when only some items are selected.

Signal Binding

Checkbox properties can be bound to reactive signals from the state package. Signals take highest priority in value resolution (Signal > Fn > Static).

  • LabelSignal — one-way binding for the display label
  • CheckedSignal — TWO-WAY binding: reads from signal AND writes back on toggle
  • DisabledSignal — one-way binding for the disabled state

Example:

checked := state.NewSignal(false)
cb := checkbox.New(
    checkbox.Label("Accept"),
    checkbox.CheckedSignal(checked),
)
// User toggles checkbox → checked.Get() returns new value
// checked.Set(true) → checkbox reflects new state on next draw

Interaction

Checkboxes respond to mouse click (left button) and keyboard activation (Space when focused). Each activation toggles the checked state and invokes the OnToggle callback. Disabled checkboxes ignore all interaction and are drawn with a dimmed appearance.

Focus

Checkboxes implement widget.Focusable and participate in tab navigation. A focus ring is drawn when the checkbox has keyboard focus.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CheckboxColorScheme

type CheckboxColorScheme struct {
	CheckedBg       widget.Color // Filled box background when checked
	CheckedFg       widget.Color // Checkmark color
	UncheckedBorder widget.Color // Border color when unchecked
	LabelColor      widget.Color // Label text color
	DisabledBg      widget.Color
	DisabledFg      widget.Color
	FocusRing       widget.Color
}

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

type DefaultPainter

type DefaultPainter struct{}

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

func (DefaultPainter) PaintCheckbox

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

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

type Option

type Option func(*config)

Option configures a checkbox during construction.

func A11yHint

func A11yHint(hint string) Option

A11yHint sets the accessibility hint text for the checkbox.

func Background

func Background(color widget.Color) Option

Background sets a custom background color override via option.

This is a convenience alias for BackgroundOpt.

func BackgroundOpt

func BackgroundOpt(color widget.Color) Option

BackgroundOpt sets a custom background color override.

func Checked

func Checked(b bool) Option

Checked sets the checkbox's initial checked state.

func CheckedFn

func CheckedFn(fn func() bool) Option

CheckedFn sets a dynamic function that is evaluated to determine whether the checkbox is checked. When set, this takes precedence over the static value but not over a signal set via CheckedSignal.

func CheckedSignal

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

CheckedSignal binds the checkbox's checked state to a reactive signal. This is a TWO-WAY binding: the widget reads the checked state from the signal, and when the user toggles the checkbox, the new state is written back to the signal. When set, the signal value takes precedence over both CheckedFn and Checked.

func Disabled

func Disabled(d bool) Option

Disabled sets the checkbox's disabled state. A disabled checkbox 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 checkbox 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 checkbox'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 checkbox's disabled state to a reactive signal. When set, the signal value takes precedence over both DisabledFn and Disabled but not over DisabledReadonlySignal.

func Indeterminate

func Indeterminate(b bool) Option

Indeterminate sets the checkbox to the indeterminate (mixed) state. An indeterminate checkbox displays a horizontal dash instead of a checkmark.

func Label

func Label(s string) Option

Label sets the checkbox's static display label.

This is a convenience alias for LabelOpt.

func LabelFn

func LabelFn(fn func() string) Option

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

func LabelOpt

func LabelOpt(s string) Option

LabelOpt sets the checkbox's static display label.

func LabelReadonlySignal

func LabelReadonlySignal(sig state.ReadonlySignal[string]) Option

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

func LabelSignal

func LabelSignal(sig state.Signal[string]) Option

LabelSignal binds the checkbox's display label to a reactive signal. When set, the signal value takes precedence over both LabelFn and LabelOpt but not over LabelReadonlySignal.

func OnToggle

func OnToggle(fn func(checked bool)) Option

OnToggle sets the callback invoked when the checkbox is toggled. The callback receives the new checked state.

func PainterOpt

func PainterOpt(p Painter) Option

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

type PaintState

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

	// Styling overrides (zero value means use design system defaults).
	Background  *widget.Color
	ColorScheme CheckboxColorScheme
}

PaintState provides the current checkbox state to the painter.

type Painter

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

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

If no Painter is set, the checkbox uses DefaultPainter.

type Widget

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

Widget implements a toggleable checkbox with configurable appearance and behavior.

A checkbox is created with New using functional options:

cb := checkbox.New(
    checkbox.Label("Accept terms"),
    checkbox.OnToggle(handleToggle),
    checkbox.Checked(true),
)

Fluent styling methods may be chained after construction:

cb.Padding(8).SetBackground(theme.Primary)

func New

func New(opts ...Option) *Widget

New creates a new checkbox Widget with the given options.

The returned widget is visible, enabled, and focusable by default. Use options to configure label, toggle handler, and checked state.

func (*Widget) Children

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

Children returns nil because a checkbox is a leaf widget.

func (*Widget) Draw

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

Draw renders the checkbox 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 checkbox can currently receive focus. A checkbox 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 checkbox'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 checkbox box. Returns the widget for method chaining.

func (*Widget) SetBackground

func (w *Widget) SetBackground(c widget.Color) *Widget

SetBackground sets a custom background color, overriding the default. Returns the widget for method chaining.

func (*Widget) Unmount

func (w *Widget) Unmount()

Unmount is called when the checkbox 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