Module: ui.widgets.textfield

Source: ./ui/widgets/textfield.reef


Overview

ui/widgets/textfield.reef - Single-line text input Part of reef-stdlib UI module

The TextField widget provides single-line text editing capabilities. It supports text entry, cursor positioning, and basic editing operations.

TextField supports:

  • Text input and display
  • Cursor positioning and movement
  • Text selection (planned)
  • Placeholder text
  • Password masking
  • Focus handling

Objects

TextField

Extends: widget.Widget

Fields:

Name Type
text string
text_length int
placeholder string
cursor int
focused bool
password bool
max_length int
scroll_offset int
padding_h float
padding_v float

proc init(id: widget.WidgetId)

Methods:

override shared fn kind(): string

exclusive proc set_text(text: string)

exclusive proc set_placeholder(placeholder: string)

exclusive proc set_cursor(position: int)

exclusive proc set_enabled(enabled: bool)

exclusive proc set_password(password: bool)

exclusive proc set_max_length(max: int)

exclusive proc set_focused(focused: bool)

shared fn clone(): TextField

D2 (docs/design/2026-09-08-ui-modernization.md): a SHALLOW copy. Setters mutate the receiver (reef-lang#62), so "give me a variant and leave the original alone" has to be asked for explicitly. Referenced objects are shared, not deep-copied.

shared fn preferred_size(th: theme.Theme): core.Size

exclusive proc layout(constraints: layout.Constraints, th: theme.Theme)

override exclusive fn handle_event(e: event.Event): widget.EventResult

override exclusive proc paint(surf: surface.Surface, th: theme.Theme)


Functions

fn textfield(): TextField

fn textfield_with_text(text: string): TextField

fn textfield_with_placeholder(placeholder: string): TextField

fn handle_mouse_event(t: TextField, me: event.MouseEvent, bounds: core.Rect): widget.EventResult

fn handle_key_event(t: TextField, ke: event.KeyEvent): widget.EventResult

fn handle_key_down(t: TextField, ke: event.KeyEvent): widget.EventResult

fn handle_focus_event(t: TextField, fe: event.FocusEvent): widget.EventResult

fn insert_char(t: TextField, c: char): bool

The three editing primitives. Each returns whether it actually changed the text, so handle_event can report Changed only when something did -- before reef-lang#64 all three were stubs that returned the receiver unmodified and every keypress reported Changed regardless.

fn delete_char_before(t: TextField): bool

fn delete_char_at(t: TextField): bool

fn get_background_color(t: TextField, colors: theme.ThemeColors): core.Color

fn get_border_color(t: TextField, colors: theme.ThemeColors): core.Color

fn get_text_color(t: TextField, colors: theme.ThemeColors): core.Color

fn calculate_cursor_from_x(t: TextField, mouse_x: float, bounds: core.Rect): int

fn point_in_rect(px: float, py: float, r: core.Rect): bool


Procedures

proc write_text(t: TextField, s: string)

The single place text and its cached length are written.

reef-lang#64: text_length is redundant -- str.length has been O(1) since BUG-147 -- but it is public API, and a cache that every mutation site maintains by hand is a cache that drifts. It already had: set_text stored the full string while recording a truncated length, and delete_char_before moved the cursor without shortening the text. Routing every write through here is what keeps the three fields from disagreeing.

proc draw_text_content(surf: surface.Surface, t: TextField, x: float, y: float, font_size: float, color: core.Color)

proc draw_placeholder(surf: surface.Surface, t: TextField, x: float, y: float, font_size: float, color: core.Color)

proc draw_cursor(surf: surface.Surface, t: TextField, x: float, y: float, font_size: float, color: core.Color)

proc draw_rect_border(surf: surface.Surface, x: float, y: float, w: float, h: float, color: core.Color, width: float)


Generated by reefc doc