core/textfield/widget.go

Functions Structs

Functions

func Children

Children returns nil because a text field is a leaf widget.

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

func CursorPosition

CursorPosition returns the current cursor position as a rune index.

func (w *Widget) CursorPosition() int {
	return w.sel.cursor
}

func Draw

Draw renders the text field to the canvas.

func (w *Widget) Draw(_ widget.Context, canvas widget.Canvas) {
	// Sync from signal if bound.
	if w.cfg.signal != nil {
		current := w.cfg.signal.Get()
		if current != w.cfg.value {
			w.cfg.value = current
			runes := []rune(current)
			w.sel.SetCursor(clampPos(w.sel.cursor, len(runes)))
		}
	}

	w.painter.PaintTextField(canvas, PaintState{
		Text:		w.resolvedText(),
		Placeholder:	w.cfg.placeholder,
		Focused:	w.IsFocused(),
		Hovered:	w.hovered,
		Disabled:	w.cfg.ResolvedDisabled(),
		HasError:	w.errorMsg != "",
		ErrorMsg:	w.errorMsg,
		CursorPos:	w.sel.cursor,
		SelectStart:	w.sel.anchor,
		SelectEnd:	w.sel.cursor,
		InputType:	w.cfg.inputType,
		Bounds:		w.Bounds(),
	})
}

func ErrorMessage

ErrorMessage returns the current validation error message, or empty string if valid.

func (w *Widget) ErrorMessage() string {
	return w.errorMsg
}

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 HasError

HasError returns true if the field has a validation error.

func (w *Widget) HasError() bool {
	return w.errorMsg != ""
}

func IsFocusable

IsFocusable reports whether the text field can currently receive focus.

A text field 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 text field's preferred size within the given constraints.

func (w *Widget) Layout(_ widget.Context, constraints geometry.Constraints) geometry.Size {
	width := constraints.MaxWidth
	if width <= 0 || width == geometry.Infinity {
		width = minFieldWidth
	}
	return constraints.Constrain(geometry.Sz(width, defaultFieldHeight))
}

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.signal != nil {
		b := state.BindToScheduler(w.cfg.signal, w, sched)
		w.AddBinding(b)
	}
}

func New

New creates a new text field Widget with the given options.

 

The returned widget is visible, enabled, and focusable by default.

Use options to configure placeholder, change handler, input type, etc.

func New(opts ...Option) *Widget {
	w := &Widget{
		padding:	defaultPaddingValue,
		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
	}

	// Initialize text from signal if bound.
	if w.cfg.signal != nil {
		w.cfg.value = w.cfg.signal.Get()
	}

	// Place cursor at end of initial value.
	runes := []rune(w.cfg.value)
	w.sel.SetCursor(len(runes))

	// Run initial validation.
	if len(w.cfg.validation) > 0 {
		w.errorMsg = runValidation(w.cfg.validation, w.cfg.value)
	}

	return w
}

func Padding

Padding sets the padding around the text field content.

Returns the widget for method chaining.

func (w *Widget) Padding(v float32) *Widget {
	w.padding = v
	return w
}

func Selection

Selection returns the current selection range as (start, end) rune indices.

If start == end, there is no selection.

func (w *Widget) Selection() (int, int) {
	return w.sel.OrderedRange()
}

func SetText

SetText sets the text value programmatically and revalidates.

func (w *Widget) SetText(text string) {
	w.setText(text)
	runes := []rune(text)
	w.sel.SetCursor(clampPos(w.sel.cursor, len(runes)))
	w.validate()
}

func Text

Text returns the current text value.

func (w *Widget) Text() string {
	return w.resolvedText()
}

func Unmount

Unmount is called when the text field 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 full-featured text input field with validation,

selection, and accessibility support.

 

A text field is created with [New] using functional options:

 

field := textfield.New(

textfield.Placeholder("Enter email"),

textfield.OnChange(handleChange),

textfield.InputType(textfield.TypeEmail),

textfield.MaxLength(255),

)

 

Fluent styling methods may be chained after construction:

 

field.Padding(12)

type Widget struct {
	widget.WidgetBase
	cfg	config
	sel	selection
	painter	Painter

	// Interaction state.
	hovered		bool
	dragging	bool

	// Validation state.
	errorMsg	string

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