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
- func OffsetFromUTF16(s string, u int) int
- func OffsetToUTF16(s string, i int) int
- type CancelledEvent
- type ChangedEvent
- type KeyInterceptor
- type Model
- func (m *Model) Anchor() int
- func (m *Model) CanRedo() bool
- func (m *Model) CanUndo() bool
- func (m *Model) Checkpoint()
- func (m *Model) ClearHistory()
- func (m *Model) Compose(start, end int, text string, selStart, selEnd int, hasSel bool)
- func (m *Model) DeleteBackward(word bool) bool
- func (m *Model) DeleteForward(word bool) bool
- func (m *Model) DeleteToLineStart() bool
- func (m *Model) DeleteToStart() bool
- func (m *Model) FromUTF16(u int) int
- func (m *Model) HasSelection() bool
- func (m *Model) Head() int
- func (m *Model) Insert(text string) bool
- func (m *Model) LenUTF16() int
- func (m *Model) LineCol(offset int) (line, col int)
- func (m *Model) LineEnd(offset int) int
- func (m *Model) LineStart(offset int) int
- func (m *Model) Marked() (start, end int, ok bool)
- func (m *Model) MarkedOrSelection() (start, end int)
- func (m *Model) MaxRunes() int
- func (m *Model) MoveEnd(extend bool)
- func (m *Model) MoveHome(extend bool)
- func (m *Model) MoveLeft(word, extend bool)
- func (m *Model) MoveLine(delta int, extend bool)
- func (m *Model) MoveLineEnd(extend bool)
- func (m *Model) MoveLineStart(extend bool)
- func (m *Model) MoveRight(word, extend bool)
- func (m *Model) MoveTo(i int, extend bool)
- func (m *Model) Multiline() bool
- func (m *Model) NextGrapheme(i int) int
- func (m *Model) NextWordEnd(i int) int
- func (m *Model) OffsetAtLineCol(line, col int) int
- func (m *Model) Paste(text string) bool
- func (m *Model) PrevGrapheme(i int) int
- func (m *Model) PrevWordStart(i int) int
- func (m *Model) Redo() bool
- func (m *Model) Replace(start, end int, text string) bool
- func (m *Model) Reversed() bool
- func (m *Model) SelectAll()
- func (m *Model) SelectLineAt(i int)
- func (m *Model) SelectWordAt(i int)
- func (m *Model) SelectedText() string
- func (m *Model) Selection() (start, end int)
- func (m *Model) SetMaxRunes(n int)
- func (m *Model) SetMultiline(multiline bool)
- func (m *Model) SetSelection(anchor, head int)
- func (m *Model) SetText(text string) bool
- func (m *Model) SnapGrapheme(i int) int
- func (m *Model) Text() string
- func (m *Model) ToUTF16(i int) int
- func (m *Model) Undo() bool
- func (m *Model) Unmark()
- type Options
- type State
- type Style
- type SubmittedEvent
- type TextInput
- func (t *TextInput) AcceptsTextInput(_ *gpui.Window, _ *gpui.Context[TextInput]) bool
- func (t *TextInput) BoundsForRange(rangeUTF16 gpui.Range, bounds gpui.Bounds[gpui.Pixels], _ *gpui.Window, ...) (gpui.Bounds[gpui.Pixels], bool)
- func (t *TextInput) CanRedo() bool
- func (t *TextInput) CanUndo() bool
- func (t *TextInput) CharacterIndexForPoint(point gpui.Point[gpui.Pixels], _ *gpui.Window, _ *gpui.Context[TextInput]) (int, bool)
- func (t *TextInput) Disabled() bool
- func (t *TextInput) Drop(_ *gpui.App)
- func (t *TextInput) Focus(window *gpui.Window, cx *gpui.App)
- func (t *TextInput) FocusHandle(_ *gpui.App) gpui.FocusHandle
- func (t *TextInput) IsFocused(window *gpui.Window) bool
- func (t *TextInput) IsMasked() bool
- func (t *TextInput) MarkedTextRange(_ *gpui.Window, _ *gpui.Context[TextInput]) (gpui.Range, bool)
- func (t *TextInput) Model() Model
- func (t *TextInput) ReadOnly() bool
- func (t *TextInput) Redo(cx *gpui.Context[TextInput]) bool
- func (t *TextInput) Render(window *gpui.Window, cx *gpui.Context[TextInput]) gpui.IntoElement
- func (t *TextInput) ReplaceAndMarkTextInRange(rangeUTF16 gpui.Option[gpui.Range], text string, ...)
- func (t *TextInput) ReplaceTextInRange(rangeUTF16 gpui.Option[gpui.Range], text string, window *gpui.Window, ...)
- func (t *TextInput) SelectAll(window *gpui.Window, cx *gpui.Context[TextInput])
- func (t *TextInput) SelectedTextRange(_ bool, _ *gpui.Window, _ *gpui.Context[TextInput]) (gpui.UTF16Selection, bool)
- func (t *TextInput) SetAriaLabel(label string, cx *gpui.Context[TextInput])
- func (t *TextInput) SetDisabled(disabled bool, cx *gpui.Context[TextInput])
- func (t *TextInput) SetKeyInterceptor(f KeyInterceptor)
- func (t *TextInput) SetMask(mask rune, cx *gpui.Context[TextInput])
- func (t *TextInput) SetMaxRunes(n int, cx *gpui.Context[TextInput])
- func (t *TextInput) SetPassword(password bool, cx *gpui.Context[TextInput])
- func (t *TextInput) SetPlaceholder(text string, cx *gpui.Context[TextInput])
- func (t *TextInput) SetReadOnly(readOnly bool, cx *gpui.Context[TextInput])
- func (t *TextInput) SetSelectedTextRange(rangeUTF16 gpui.Range, window *gpui.Window, cx *gpui.Context[TextInput])
- func (t *TextInput) SetSelection(anchor, head int, window *gpui.Window, cx *gpui.Context[TextInput])
- func (t *TextInput) SetStyle(style Style, cx *gpui.Context[TextInput])
- func (t *TextInput) SetText(text string, cx *gpui.Context[TextInput])
- func (t *TextInput) Text() string
- func (t *TextInput) TextForRange(rangeUTF16 gpui.Range, adjusted *gpui.Option[gpui.Range], _ *gpui.Window, ...) (string, bool)
- func (t *TextInput) TextLengthUTF16(_ *gpui.Window, _ *gpui.Context[TextInput]) (int, bool)
- func (t *TextInput) Undo(cx *gpui.Context[TextInput]) bool
- func (t *TextInput) UnmarkText(window *gpui.Window, cx *gpui.Context[TextInput])
Constants ¶
const BlinkInterval = 500 * time.Millisecond
BlinkInterval is how long the caret stays on or off while blinking.
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 ¶
OffsetFromUTF16 converts a UTF-16 offset in s to a byte offset, clamped to len(s).
func OffsetToUTF16 ¶
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 ¶
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 ¶
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 ¶
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) 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 ¶
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 ¶
DeleteBackward deletes the selection, or else the grapheme (or word) before the caret.
func (*Model) DeleteForward ¶
DeleteForward deletes the selection, or else the grapheme (or word) after the caret.
func (*Model) DeleteToLineStart ¶
DeleteToLineStart deletes from the start of the current logical line to the caret, or the selection if any.
func (*Model) DeleteToStart ¶
DeleteToStart deletes from the start of the text to the caret, or the selection if any.
func (*Model) HasSelection ¶
HasSelection reports whether the selection is non-empty.
func (*Model) Insert ¶
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) LineCol ¶
LineCol returns the 0-based logical line index and grapheme column for byte offset.
func (*Model) LineEnd ¶
LineEnd returns the end byte offset (before any newline) of the logical line containing offset.
func (*Model) LineStart ¶
LineStart returns the start byte offset of the logical line containing offset.
func (*Model) MarkedOrSelection ¶
MarkedOrSelection returns the marked range if there is one, else the selection.
func (*Model) MoveLeft ¶
MoveLeft moves the caret one grapheme (or one word) left. Without extend, a selection collapses to its start instead.
func (*Model) MoveLine ¶
MoveLine moves the caret vertically by delta logical lines, preserving a sticky column across consecutive vertical moves.
func (*Model) MoveLineEnd ¶
MoveLineEnd moves the caret to the end of the current logical line.
func (*Model) MoveLineStart ¶
MoveLineStart moves the caret to the start of the current logical line.
func (*Model) MoveRight ¶
MoveRight moves the caret one grapheme (or one word) right. Without extend, a selection collapses to its end instead.
func (*Model) NextGrapheme ¶
NextGrapheme returns the end of the grapheme cluster that starts at or contains i.
func (*Model) NextWordEnd ¶
NextWordEnd returns the end of the nearest word (UAX #29) that ends after i, or len(Text).
func (*Model) OffsetAtLineCol ¶
OffsetAtLineCol returns the byte offset for the 0-based logical line and grapheme column.
func (*Model) Paste ¶
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 ¶
PrevGrapheme returns the start of the grapheme cluster before i.
func (*Model) PrevWordStart ¶
PrevWordStart returns the start of the nearest word (UAX #29) that starts before i, or 0.
func (*Model) Redo ¶
Redo reapplies the last undone edit group and reports whether the state changed.
func (*Model) SelectAll ¶
func (m *Model) SelectAll()
SelectAll selects the whole text with the caret at the end.
func (*Model) SelectLineAt ¶
SelectLineAt selects the logical line containing byte offset i.
func (*Model) SelectWordAt ¶
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 ¶
SelectedText returns the selected text.
func (*Model) Selection ¶
Selection returns the selected range as start <= end; start == end means no selection.
func (*Model) SetMaxRunes ¶
SetMaxRunes sets the rune cap (0 for none) and truncates the text to it.
func (*Model) SetMultiline ¶
SetMultiline enables or disables multi-line mode. Disabling multi-line mode removes existing line breaks.
func (*Model) SetSelection ¶
SetSelection sets the anchor and head, snapped to rune boundaries.
func (*Model) SetText ¶
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 ¶
SnapGrapheme returns the grapheme cluster boundary at or before i.
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 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 ¶
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 ¶
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) CharacterIndexForPoint ¶
func (t *TextInput) CharacterIndexForPoint(point gpui.Point[gpui.Pixels], _ *gpui.Window, _ *gpui.Context[TextInput]) (int, bool)
CharacterIndexForPoint implements gpui.EntityInputHandler.
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) MarkedTextRange ¶
MarkedTextRange implements gpui.EntityInputHandler.
func (*TextInput) Model ¶
Model returns the editing model for reading; change it through TextInput methods.
func (*TextInput) Redo ¶
Redo reapplies the last undone edit group when editable and reports whether the state changed.
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) SelectedTextRange ¶
func (t *TextInput) SelectedTextRange(_ bool, _ *gpui.Window, _ *gpui.Context[TextInput]) (gpui.UTF16Selection, bool)
SelectedTextRange implements gpui.EntityInputHandler.
func (*TextInput) SetAriaLabel ¶
SetAriaLabel sets the accessible name.
func (*TextInput) SetDisabled ¶
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) SetMaxRunes ¶
SetMaxRunes sets the rune cap (0 for none), truncating the text if needed.
func (*TextInput) SetPassword ¶
SetPassword enables or disables password masking.
func (*TextInput) SetPlaceholder ¶
SetPlaceholder sets the text shown while the input is empty.
func (*TextInput) SetReadOnly ¶
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) SetText ¶
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) 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 ¶
TextLengthUTF16 implements gpui.EntityInputHandler.