core/checkbox/widget.go

Functions Structs

Functions

func Children

Children returns nil because a checkbox is a leaf widget.

func (w *Widget) Children() []widget.Widget {
	return nil
}

func Draw

Draw renders the checkbox to the canvas.

func (w *Widget) Draw(ctx widget.Context, canvas widget.Canvas) {
	w.painter.PaintCheckbox(canvas, PaintState{
		Label:		w.cfg.ResolvedLabel(),
		Checked:	w.cfg.ResolvedChecked(),
		Indeterminate:	w.cfg.indeterminate,
		Hovered:	w.state == stateHover,
		Pressed:	w.state == statePressed,
		Focused:	w.IsFocused(),
		Disabled:	w.cfg.ResolvedDisabled(),
		Bounds:		w.Bounds(),
		Background:	w.cfg.background,
	})
}

func Event

Event handles an input event and returns true if consumed.

func (w *Widget) Event(ctx widget.Context, e event.Event) bool {
	return handleEvent(w, ctx, e)
}

func IsFocusable

IsFocusable reports whether the checkbox can currently receive focus.

A checkbox is focusable when it is visible, enabled, and not disabled.

func (w *Widget) IsFocusable() bool {
	return w.IsVisible() && w.IsEnabled() && !w.cfg.ResolvedDisabled()
}

func Layout

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

func (w *Widget) Layout(_ widget.Context, constraints geometry.Constraints) geometry.Size {
	// Box size + optional label width.
	totalWidth := boxSize + w.padding*2
	totalHeight := boxSize + w.padding*2

	label := w.cfg.ResolvedLabel()
	if label != "" {
		textWidth := float32(len(label)) * defaultFontSize * charWidthRatio
		totalWidth += labelGap + textWidth
	}

	// Ensure minimum height.
	if totalHeight < minHeight {
		totalHeight = minHeight
	}

	preferred := geometry.Sz(totalWidth, totalHeight)
	return constraints.Constrain(preferred)
}

func Mount

Mount creates signal bindings for push-based invalidation.

Implements [widget.Lifecycle].

func (w *Widget) Mount(ctx widget.Context) {
	sched := ctx.Scheduler()
	if sched == nil {
		return
	}
	if w.cfg.checkedSignal != nil {
		b := state.BindToScheduler(w.cfg.checkedSignal, w, sched)
		w.AddBinding(b)
	}
	if w.cfg.readonlyLabelSig != nil {
		b := state.BindToScheduler(w.cfg.readonlyLabelSig, w, sched)
		w.AddBinding(b)
	} else if w.cfg.labelSignal != nil {
		b := state.BindToScheduler(w.cfg.labelSignal, w, sched)
		w.AddBinding(b)
	}
	if w.cfg.readonlyDisabledSig != nil {
		b := state.BindToScheduler(w.cfg.readonlyDisabledSig, w, sched)
		w.AddBinding(b)
	} else if w.cfg.disabledSignal != nil {
		b := state.BindToScheduler(w.cfg.disabledSignal, w, sched)
		w.AddBinding(b)
	}
}

func New

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 New(opts ...Option) *Widget {
	w := &Widget{
		padding:	defaultPadding,
		painter:	DefaultPainter{},
	}
	w.SetVisible(true)
	w.SetEnabled(true)

	for _, opt := range opts {
		opt(&w.cfg)
	}

	// Apply painter from config if set.
	if w.cfg.painter != nil {
		w.painter = w.cfg.painter
	}

	return w
}

func Unmount

Unmount is called when the checkbox is removed from the widget tree.

Implements [widget.Lifecycle].

func (w *Widget) Unmount() {
	// Bindings are cleaned up automatically by WidgetBase.CleanupBindings().
}

Structs

type Widget struct

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)

type Widget struct {
	widget.WidgetBase
	cfg	config
	state	interactionState
	painter	Painter

	// Styling overrides set via fluent methods.
	padding	float32
}