textinput

package
v0.0.0-...-6a4b757 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package textinput provides TextInput, a single-line or multi-line text input view for gpui-go, and Model, the editing state behind it.

Upstream GPUI has no text input (Zed's editor lives in a separate crate), so this package is a gpui-go addition built on gpui's EntityInputHandler, clipboard and focus APIs. It handles caret movement by grapheme and by UAX #29 word, Shift and mouse selection, double-click word-drag and triple-click line-drag selection, clipboard shortcuts, bounded undo/redo with edit coalescing, word deletion, an optional rune cap, password masking, read-only and disabled states, multi-line editing with soft wrapping and scrolling, a placeholder, caret blinking (solid while App.ReduceMotion is set), IME preedit through the platform input handler, and accessibility nodes with RoleTextRun children, caret/selection reporting and text actions.

Create the view inside cx.New, render it as a child and subscribe to its events:

input := cx.New(func(cx *gpui.Context[textinput.TextInput]) *textinput.TextInput {
	return textinput.New(cx, textinput.Options{Placeholder: "Name", MaxRunes: 24})
})
sub := cx.Subscribe(input, func(this *View, _ gpui.Entity[textinput.TextInput], ev *textinput.SubmittedEvent, cx *gpui.Context[View]) {
	this.save(ev.Text)
})

Keys are handled in the input's own key-down listener, so no key bindings are needed. Tab and keys the input does not use keep propagating. A host sees keys first through SetKeyInterceptor, or through CaptureKeyDown on an ancestor element.

Index

Constants

View Source
const BlinkInterval = 500 * time.Millisecond

BlinkInterval is how long the caret stays on or off while blinking.

View Source
const KeyContext = "TextInput"

KeyContext is the key context the input's element pushes, for host key bindings.

Variables

This section is empty.

Functions

func OffsetFromUTF16

func OffsetFromUTF16(s string, u int) int

OffsetFromUTF16 converts a UTF-16 offset in s to a byte offset, clamped to len(s).

func OffsetToUTF16

func OffsetToUTF16(s string, i int) int

OffsetToUTF16 converts a byte offset in s to a UTF-16 offset.

Types

type CancelledEvent

type CancelledEvent struct{}

CancelledEvent is emitted when Escape is pressed outside an IME composition. Escape keeps propagating to ancestors after the event.

type ChangedEvent

type ChangedEvent struct{ Text string }

ChangedEvent is emitted when the text changes through user input, paste, cut, undo/redo, IME or an accessibility mutation request. SetText does not emit it.

type KeyInterceptor

type KeyInterceptor func(event *gpui.KeyDownEvent, window *gpui.Window, cx *gpui.App) bool

KeyInterceptor sees every key press before the input handles it; returning true consumes the key (propagation stops and the input ignores it). It runs outside the input's update, so it may update the input entity.

type Model

type Model struct {
	// contains filtered or unexported fields
}

Model is the editing state of a single-line or multi-line text field: the text, a selection made of an anchor and a head (the caret), an optional marked (IME preedit) range, an optional rune cap and bounded undo/redo history. All offsets are UTF-8 byte offsets into Text. Model has no gpui dependency; TextInput wraps it.

func NewModel

func NewModel(text string, maxRunes int) Model

NewModel returns a single-line model holding text (sanitized and capped) with the caret at the end. A maxRunes of 0 means no cap.

func NewMultilineModel

func NewMultilineModel(text string, maxRunes int) Model

NewMultilineModel returns a multi-line model holding text (newlines normalized to \n and capped) with the caret at the end. A maxRunes of 0 means no cap.

func (*Model) Anchor

func (m *Model) Anchor() int

Anchor returns the fixed end of the selection.

func (*Model) CanRedo

func (m *Model) CanRedo() bool

CanRedo reports whether there is a redo entry available.

func (*Model) CanUndo

func (m *Model) CanUndo() bool

CanUndo reports whether there is an undo entry available.

func (*Model) Checkpoint

func (m *Model) Checkpoint()

Checkpoint ends the current edit coalescing group so the next edit starts a new undo entry.

func (*Model) ClearHistory

func (m *Model) ClearHistory()

ClearHistory discards all undo and redo entries.

func (*Model) Compose

func (m *Model) Compose(start, end int, text string, selStart, selEnd int, hasSel bool)

Compose replaces [start, end) with preedit text and marks it. The rune cap is not applied to preedit text; it applies when the composition is committed through Insert. When hasSel is set, selStart and selEnd are UTF-16 offsets relative to the inserted text and become the selection; otherwise the caret goes to the end of the preedit. An empty text removes the marked range.

func (*Model) DeleteBackward

func (m *Model) DeleteBackward(word bool) bool

DeleteBackward deletes the selection, or else the grapheme (or word) before the caret.

func (*Model) DeleteForward

func (m *Model) DeleteForward(word bool) bool

DeleteForward deletes the selection, or else the grapheme (or word) after the caret.

func (*Model) DeleteToLineStart

func (m *Model) DeleteToLineStart() bool

DeleteToLineStart deletes from the start of the current logical line to the caret, or the selection if any.

func (*Model) DeleteToStart

func (m *Model) DeleteToStart() bool

DeleteToStart deletes from the start of the text to the caret, or the selection if any.

func (*Model) FromUTF16

func (m *Model) FromUTF16(u int) int

FromUTF16 converts a UTF-16 offset to a byte offset in Text.

func (*Model) HasSelection

func (m *Model) HasSelection() bool

HasSelection reports whether the selection is non-empty.

func (*Model) Head

func (m *Model) Head() int

Head returns the caret, the moving end of the selection.

func (*Model) Insert

func (m *Model) Insert(text string) bool

Insert replaces the marked range, or else the selection, with typed or committed text. Control characters (and line breaks in single-line mode) are dropped and the result is cut to the rune cap. It reports whether the text changed.

func (*Model) LenUTF16

func (m *Model) LenUTF16() int

LenUTF16 returns the length of Text in UTF-16 code units.

func (*Model) LineCol

func (m *Model) LineCol(offset int) (line, col int)

LineCol returns the 0-based logical line index and grapheme column for byte offset.

func (*Model) LineEnd

func (m *Model) LineEnd(offset int) int

LineEnd returns the end byte offset (before any newline) of the logical line containing offset.

func (*Model) LineStart

func (m *Model) LineStart(offset int) int

LineStart returns the start byte offset of the logical line containing offset.

func (*Model) Marked

func (m *Model) Marked() (start, end int, ok bool)

Marked returns the marked (IME preedit) range, if any.

func (*Model) MarkedOrSelection

func (m *Model) MarkedOrSelection() (start, end int)

MarkedOrSelection returns the marked range if there is one, else the selection.

func (*Model) MaxRunes

func (m *Model) MaxRunes() int

MaxRunes returns the rune cap, or 0 when there is none.

func (*Model) MoveEnd

func (m *Model) MoveEnd(extend bool)

MoveEnd moves the caret to the end of the text.

func (*Model) MoveHome

func (m *Model) MoveHome(extend bool)

MoveHome moves the caret to the start of the text.

func (*Model) MoveLeft

func (m *Model) MoveLeft(word, extend bool)

MoveLeft moves the caret one grapheme (or one word) left. Without extend, a selection collapses to its start instead.

func (*Model) MoveLine

func (m *Model) MoveLine(delta int, extend bool)

MoveLine moves the caret vertically by delta logical lines, preserving a sticky column across consecutive vertical moves.

func (*Model) MoveLineEnd

func (m *Model) MoveLineEnd(extend bool)

MoveLineEnd moves the caret to the end of the current logical line.

func (*Model) MoveLineStart

func (m *Model) MoveLineStart(extend bool)

MoveLineStart moves the caret to the start of the current logical line.

func (*Model) MoveRight

func (m *Model) MoveRight(word, extend bool)

MoveRight moves the caret one grapheme (or one word) right. Without extend, a selection collapses to its end instead.

func (*Model) MoveTo

func (m *Model) MoveTo(i int, extend bool)

MoveTo moves the caret to i; extend keeps the anchor so the selection grows.

func (*Model) Multiline

func (m *Model) Multiline() bool

Multiline reports whether the model preserves line breaks.

func (*Model) NextGrapheme

func (m *Model) NextGrapheme(i int) int

NextGrapheme returns the end of the grapheme cluster that starts at or contains i.

func (*Model) NextWordEnd

func (m *Model) NextWordEnd(i int) int

NextWordEnd returns the end of the nearest word (UAX #29) that ends after i, or len(Text).

func (*Model) OffsetAtLineCol

func (m *Model) OffsetAtLineCol(line, col int) int

OffsetAtLineCol returns the byte offset for the 0-based logical line and grapheme column.

func (*Model) Paste

func (m *Model) Paste(text string) bool

Paste is Insert for clipboard text: tabs become spaces, and line breaks are removed in single-line mode or normalized to \n in multi-line mode. Paste never coalesces into the preceding undo entry.

func (*Model) PrevGrapheme

func (m *Model) PrevGrapheme(i int) int

PrevGrapheme returns the start of the grapheme cluster before i.

func (*Model) PrevWordStart

func (m *Model) PrevWordStart(i int) int

PrevWordStart returns the start of the nearest word (UAX #29) that starts before i, or 0.

func (*Model) Redo

func (m *Model) Redo() bool

Redo reapplies the last undone edit group and reports whether the state changed.

func (*Model) Replace

func (m *Model) Replace(start, end int, text string) bool

Replace replaces the byte range [start, end) with text as Insert does.

func (*Model) Reversed

func (m *Model) Reversed() bool

Reversed reports whether the caret is before the anchor.

func (*Model) SelectAll

func (m *Model) SelectAll()

SelectAll selects the whole text with the caret at the end.

func (*Model) SelectLineAt

func (m *Model) SelectLineAt(i int)

SelectLineAt selects the logical line containing byte offset i.

func (*Model) SelectWordAt

func (m *Model) SelectWordAt(i int)

SelectWordAt selects the word around byte offset i. Off a word it selects the run of non-word characters (spaces or punctuation) around i instead.

func (*Model) SelectedText

func (m *Model) SelectedText() string

SelectedText returns the selected text.

func (*Model) Selection

func (m *Model) Selection() (start, end int)

Selection returns the selected range as start <= end; start == end means no selection.

func (*Model) SetMaxRunes

func (m *Model) SetMaxRunes(n int)

SetMaxRunes sets the rune cap (0 for none) and truncates the text to it.

func (*Model) SetMultiline

func (m *Model) SetMultiline(multiline bool)

SetMultiline enables or disables multi-line mode. Disabling multi-line mode removes existing line breaks.

func (*Model) SetSelection

func (m *Model) SetSelection(anchor, head int)

SetSelection sets the anchor and head, snapped to rune boundaries.

func (*Model) SetText

func (m *Model) SetText(text string) bool

SetText replaces the whole text, as pasted text is sanitized, truncates it to the cap, clears the marked range, breaks edit coalescing and puts the caret at the end. It reports whether the text changed.

func (*Model) SnapGrapheme

func (m *Model) SnapGrapheme(i int) int

SnapGrapheme returns the grapheme cluster boundary at or before i.

func (*Model) Text

func (m *Model) Text() string

Text returns the current text, including any marked preedit text.

func (*Model) ToUTF16

func (m *Model) ToUTF16(i int) int

ToUTF16 converts a byte offset in Text to a UTF-16 offset.

func (*Model) Undo

func (m *Model) Undo() bool

Undo reverts the last edit group and reports whether the state changed.

func (*Model) Unmark

func (m *Model) Unmark()

Unmark clears the marked range, keeping its text.

type Options

type Options struct {
	Text        string
	Placeholder string
	// MaxRunes caps the text length in runes; 0 means no cap.
	MaxRunes int
	// Multiline enables multi-line editing (Enter inserts \n, Cmd/Ctrl+Enter submits).
	Multiline bool
	// NoSoftWrap disables soft wrapping in multiline mode so long lines scroll horizontally.
	NoSoftWrap bool
	// Password masks rendered text with Mask (defaulting to '•') and blocks copy/cut.
	Password bool
	// Mask overrides the mask rune; setting a non-zero rune enables masking.
	Mask rune
	// ReadOnly allows focus, selection and copy while blocking edits, IME and undo/redo.
	ReadOnly bool
	// Disabled removes the input from tab stops, ignores interaction and hides the caret.
	Disabled bool
	Style    Style
	// ID is the element id, which also keys the accessibility node; it defaults to one derived
	// from the focus handle.
	ID gpui.Option[gpui.ElementID]
	// AriaLabel is the accessible name.
	AriaLabel string
	// NoTabStop leaves the input out of Tab navigation.
	NoTabStop bool
	// TabIndex orders the input among tab stops.
	TabIndex int
}

Options configure a new TextInput.

type State

type State struct {
	Focused  bool
	Empty    bool
	ReadOnly bool
	Disabled bool
	Masked   bool
}

State is the state passed to Style hooks.

type Style

type Style struct {
	TextColor        gpui.Option[gpui.Hsla]
	PlaceholderColor gpui.Option[gpui.Hsla]
	SelectionColor   gpui.Option[gpui.Hsla]
	CaretColor       gpui.Option[gpui.Hsla]
	// CaretWidth defaults to 1.5px.
	CaretWidth gpui.Pixels
	// Container styles the outer element (padding, border, background, focus ring, text style).
	// It runs before Prefix, the text and Suffix are added as children.
	Container func(el *gpui.Stateful[*gpui.Div], state State) *gpui.Stateful[*gpui.Div]
	// Prefix and Suffix, when set, add an element before or after the text (an icon, a clear
	// button). Returning nil adds nothing.
	Prefix func(state State, window *gpui.Window, cx *gpui.App) gpui.IntoElement
	Suffix func(state State, window *gpui.Window, cx *gpui.App) gpui.IntoElement
}

Style controls how the input looks. The font, size and default text colour come from the inherited text style, so set them on an ancestor or in Container. Unset colours derive from the text colour.

type SubmittedEvent

type SubmittedEvent struct{ Text string }

SubmittedEvent is emitted when Enter is pressed (or Cmd/Ctrl+Enter in multiline mode) outside an IME composition.

type TextInput

type TextInput struct {
	gpui.EntityInputHandlerDefaults[TextInput]
	// contains filtered or unexported fields
}

TextInput is a single-line or multi-line text input view. Create it with New inside cx.New, render it with Child(input.Clone()) and subscribe to ChangedEvent, SubmittedEvent and CancelledEvent.

func New

func New(cx *gpui.Context[TextInput], opts Options) *TextInput

New builds a TextInput; call it from cx.New. Release the entity as usual; Drop releases the focus handle and stops the caret blink.

func (*TextInput) AcceptsTextInput

func (t *TextInput) AcceptsTextInput(_ *gpui.Window, _ *gpui.Context[TextInput]) bool

AcceptsTextInput implements gpui.EntityInputHandler.

func (*TextInput) BoundsForRange

func (t *TextInput) BoundsForRange(rangeUTF16 gpui.Range, bounds gpui.Bounds[gpui.Pixels], _ *gpui.Window, _ *gpui.Context[TextInput]) (gpui.Bounds[gpui.Pixels], bool)

BoundsForRange implements gpui.EntityInputHandler.

func (*TextInput) CanRedo

func (t *TextInput) CanRedo() bool

CanRedo reports whether an undone edit can be redone.

func (*TextInput) CanUndo

func (t *TextInput) CanUndo() bool

CanUndo reports whether an edit can be undone.

func (*TextInput) CharacterIndexForPoint

func (t *TextInput) CharacterIndexForPoint(point gpui.Point[gpui.Pixels], _ *gpui.Window, _ *gpui.Context[TextInput]) (int, bool)

CharacterIndexForPoint implements gpui.EntityInputHandler.

func (*TextInput) Disabled

func (t *TextInput) Disabled() bool

Disabled reports whether the input is disabled.

func (*TextInput) Drop

func (t *TextInput) Drop(_ *gpui.App)

Drop releases the focus handle and cancels the caret blink.

func (*TextInput) Focus

func (t *TextInput) Focus(window *gpui.Window, cx *gpui.App)

Focus focuses the input unless it is disabled.

func (*TextInput) FocusHandle

func (t *TextInput) FocusHandle(_ *gpui.App) gpui.FocusHandle

FocusHandle returns a clone of the input's focus handle; the caller releases it.

func (*TextInput) IsFocused

func (t *TextInput) IsFocused(window *gpui.Window) bool

IsFocused reports whether the input has focus.

func (*TextInput) IsMasked

func (t *TextInput) IsMasked() bool

IsMasked reports whether rendered text is masked.

func (*TextInput) MarkedTextRange

func (t *TextInput) MarkedTextRange(_ *gpui.Window, _ *gpui.Context[TextInput]) (gpui.Range, bool)

MarkedTextRange implements gpui.EntityInputHandler.

func (*TextInput) Model

func (t *TextInput) Model() Model

Model returns the editing model for reading; change it through TextInput methods.

func (*TextInput) ReadOnly

func (t *TextInput) ReadOnly() bool

ReadOnly reports whether the input is read-only.

func (*TextInput) Redo

func (t *TextInput) Redo(cx *gpui.Context[TextInput]) bool

Redo reapplies the last undone edit group when editable and reports whether the state changed.

func (*TextInput) Render

func (t *TextInput) Render(window *gpui.Window, cx *gpui.Context[TextInput]) gpui.IntoElement

Render renders the input.

func (*TextInput) ReplaceAndMarkTextInRange

func (t *TextInput) ReplaceAndMarkTextInRange(rangeUTF16 gpui.Option[gpui.Range], text string, selected gpui.Option[gpui.Range], window *gpui.Window, cx *gpui.Context[TextInput])

ReplaceAndMarkTextInRange implements gpui.EntityInputHandler.

func (*TextInput) ReplaceTextInRange

func (t *TextInput) ReplaceTextInRange(rangeUTF16 gpui.Option[gpui.Range], text string, window *gpui.Window, cx *gpui.Context[TextInput])

ReplaceTextInRange implements gpui.EntityInputHandler.

func (*TextInput) SelectAll

func (t *TextInput) SelectAll(window *gpui.Window, cx *gpui.Context[TextInput])

SelectAll selects the whole text.

func (*TextInput) SelectedTextRange

func (t *TextInput) SelectedTextRange(_ bool, _ *gpui.Window, _ *gpui.Context[TextInput]) (gpui.UTF16Selection, bool)

SelectedTextRange implements gpui.EntityInputHandler.

func (*TextInput) SetAriaLabel

func (t *TextInput) SetAriaLabel(label string, cx *gpui.Context[TextInput])

SetAriaLabel sets the accessible name.

func (*TextInput) SetDisabled

func (t *TextInput) SetDisabled(disabled bool, cx *gpui.Context[TextInput])

SetDisabled toggles disabled mode.

func (*TextInput) SetKeyInterceptor

func (t *TextInput) SetKeyInterceptor(f KeyInterceptor)

SetKeyInterceptor installs f to see key presses first; nil removes it.

func (*TextInput) SetMask

func (t *TextInput) SetMask(mask rune, cx *gpui.Context[TextInput])

SetMask sets the masking rune; 0 disables masking.

func (*TextInput) SetMaxRunes

func (t *TextInput) SetMaxRunes(n int, cx *gpui.Context[TextInput])

SetMaxRunes sets the rune cap (0 for none), truncating the text if needed.

func (*TextInput) SetPassword

func (t *TextInput) SetPassword(password bool, cx *gpui.Context[TextInput])

SetPassword enables or disables password masking.

func (*TextInput) SetPlaceholder

func (t *TextInput) SetPlaceholder(text string, cx *gpui.Context[TextInput])

SetPlaceholder sets the text shown while the input is empty.

func (*TextInput) SetReadOnly

func (t *TextInput) SetReadOnly(readOnly bool, cx *gpui.Context[TextInput])

SetReadOnly toggles read-only mode.

func (*TextInput) SetSelectedTextRange

func (t *TextInput) SetSelectedTextRange(rangeUTF16 gpui.Range, window *gpui.Window, cx *gpui.Context[TextInput])

SetSelectedTextRange implements gpui.EntityInputHandler.

func (*TextInput) SetSelection

func (t *TextInput) SetSelection(anchor, head int, window *gpui.Window, cx *gpui.Context[TextInput])

SetSelection sets the selection in byte offsets.

func (*TextInput) SetStyle

func (t *TextInput) SetStyle(style Style, cx *gpui.Context[TextInput])

SetStyle replaces the style.

func (*TextInput) SetText

func (t *TextInput) SetText(text string, cx *gpui.Context[TextInput])

SetText replaces the text and puts the caret at the end. It does not emit ChangedEvent; the platform IME is told about the change on the next render.

func (*TextInput) Text

func (t *TextInput) Text() string

Text returns the raw text, including any IME preedit text.

func (*TextInput) TextForRange

func (t *TextInput) TextForRange(rangeUTF16 gpui.Range, adjusted *gpui.Option[gpui.Range], _ *gpui.Window, _ *gpui.Context[TextInput]) (string, bool)

TextForRange implements gpui.EntityInputHandler.

func (*TextInput) TextLengthUTF16

func (t *TextInput) TextLengthUTF16(_ *gpui.Window, _ *gpui.Context[TextInput]) (int, bool)

TextLengthUTF16 implements gpui.EntityInputHandler.

func (*TextInput) Undo

func (t *TextInput) Undo(cx *gpui.Context[TextInput]) bool

Undo reverts the last edit group when editable and reports whether the state changed.

func (*TextInput) UnmarkText

func (t *TextInput) UnmarkText(window *gpui.Window, cx *gpui.Context[TextInput])

UnmarkText implements gpui.EntityInputHandler.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL