textinput

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0 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 defines the neutral text-input mirror seam shared by every platform's native text-editing / IME integration (pre-rebrand tracker #768, epic pre-rebrand tracker #490).

A Session binds the focused editor — any object implementing core.AccessibleTexter, optionally event.Composable — to a native document Backend, and moves state in both directions:

  • push (editor → native): a (text, selection) Snapshot mirrored into the native document whenever it changes; and
  • edit ops (native → editor): insert / delete / replace / setSelection / compose / commit, applied back through the editor's own gated, undo-recorded edit paths.

The seam is deliberately widget- and driver-agnostic: nothing here imports a concrete widget, a driver, or a platform API, and no [bellui.CanvasObject] type appears. Drivers own the Backend (the Android shadow EditText, an iOS UITextField document, a desktop accessibility text protocol) and drive the session — calling Session.Bind / Session.Unbind on focus change and Session.Sync after input events. This is Phase 0 of pre-rebrand tracker #768: the seam is defined and unit-tested, but no driver wires to it yet, so existing behavior is unchanged.

A Session is not safe for concurrent use. Native callbacks that arrive on a platform UI thread must be marshalled onto the main goroutine (via bellui.Do) before touching the session, exactly as the drivers already marshal input — the seam holds no locks, matching the existing per-driver state fields it generalizes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InsertText

func InsertText(ed core.AccessibleTexter, text string)

InsertText replaces the editor's current selection with text — or inserts at the caret when nothing is selected — as one gated, undo-recorded edit.

func ReadSelection

func ReadSelection(ed core.AccessibleTexter) (start, end int)

ReadSelection reads only the editor's half-open [start, end) selection, skipping the document text. It is the read to use for a query that needs the selection but not the content — the desktop text protocols that keep rune offsets (the UIA Text pattern) — so a screen reader tracking the caret over a large document does not re-materialize the whole text per query (pre-rebrand tracker #789). Use Read where the text itself (or a rune↔code-unit conversion needing it) is required.

func ReplaceAll

func ReplaceAll(ed core.AccessibleTexter, text string)

ReplaceAll replaces the editor's entire content with text as one gated, undo-recorded edit — the whole-document replace behind the platform text protocols' "set value" operation.

func ReplaceRange

func ReplaceRange(ed core.AccessibleTexter, start, end int, text string)

ReplaceRange replaces the half-open [start, end) rune range with text as one gated, undo-recorded edit. Offsets are clamped by the editor.

func Select

func Select(ed core.AccessibleTexter, start, end int)

Select moves the editor's selection to the half-open [start, end) rune range (start == end places a bare caret), clamped by the editor.

Types

type Backend

type Backend interface {
	// SetState mirrors the editor's current text and selection into the native
	// document. It is called on bind and whenever the pushed [Snapshot] changes
	// (see [Session.Sync]); redundant pushes are already filtered out.
	SetState(Snapshot)
}

Backend is the native document mirror a driver implements. The session pushes editor state to it via Backend.SetState and calls the session's edit-op methods from the native side when the user edits through the platform IME.

type Session

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

Session binds one focused editor to a Backend at a time and mirrors state between them. Create it once per backend with NewSession; rebind it to the focused editor on every focus change.

func NewSession

func NewSession(backend Backend) *Session

NewSession returns a session that pushes editor state to backend. The backend must be non-nil.

func (*Session) Bind

func (s *Session) Bind(focused any) (editorChanged bool)

Bind attaches the session to the newly-focused object, taking effect only if it implements core.AccessibleTexter; any other object (or nil) unbinds the session. event.Composable is detected additionally to enable the IME ops.

Bind forces the next Session.Sync to push even when the snapshot matches the last one, so focusing a different field with identical text (e.g. two empty entries) still repopulates the backend.

Re-binding the editor that is already bound is a no-op, and in particular does NOT force that push: it is not "a different field", and drivers re-Bind the unchanged focus freely — the Android driver does so on every overlay change (pre-rebrand tracker #1016) — where a forced push would be pure churn against the native IME.

The return value reports whether the bound editor's identity changed (a different field bound, or a bound session unbound) — false for the no-op re-bind. Drivers use it to tell the native input surface the editor changed, so it can tear down any IME session state that belongs to the previous field (Android: composing state, echo filters, and an unconditional restartInput on the seed push — pre-rebrand tracker #966; one Gboard editing session must never span two fields).

func (*Session) Bound

func (s *Session) Bound() bool

Bound reports whether an editor is currently bound.

func (*Session) CancelCompose

func (s *Session) CancelCompose()

CancelCompose discards any in-progress composition without committing, then pushes the (unchanged committed) state. No-op when the bound editor does not support composition (or nothing is bound).

func (*Session) Commit

func (s *Session) Commit(text string)

Commit finalizes IME composition, inserting text as if typed (updating the value, undo history and change callbacks), then pushes the new state. No-op when the bound editor does not support composition (or nothing is bound).

func (*Session) Compose

func (s *Session) Compose(preedit string, cursor int)

Compose shows in-progress IME preedit text inline at the caret with cursor at the given rune offset within preedit. It does not alter the committed value, fire change callbacks or touch undo. No-op when the bound editor does not support composition (or nothing is bound).

func (*Session) Delete

func (s *Session) Delete(start, end int)

Delete removes the half-open [start, end) rune range as one edit, then pushes the new state. Offsets are clamped by the editor. No-op when nothing is bound.

func (*Session) EditorComposing

func (s *Session) EditorComposing() bool

EditorComposing reports whether the bound editor currently has an active IME composition. Editors that do not expose composition state — or a session with no composition-capable editor bound — report true, the conservative default: callers gate destructive decisions (dropping a commit) on a definitive "not composing", and an unknown state must never trigger them.

func (*Session) Insert

func (s *Session) Insert(text string)

Insert replaces the current selection with text — or inserts at the caret when nothing is selected — as one undo-recorded edit, then pushes the new state. No-op when nothing is bound.

func (*Session) Invalidate

func (s *Session) Invalidate()

Invalidate forces the next Session.Sync to push even if the snapshot is unchanged. Drivers call it when the editor's content may have changed outside the input path (e.g. a programmatic SetText) so the native mirror does not go stale.

func (*Session) Replace

func (s *Session) Replace(start, end int, text string)

Replace replaces the half-open [start, end) rune range with text as one edit, then pushes the new state. Offsets are clamped by the editor. No-op when nothing is bound.

func (*Session) SetSelection

func (s *Session) SetSelection(start, end int)

SetSelection moves the selection to the half-open [start, end) rune range (start == end places a bare caret), then pushes the new state. Offsets are clamped by the editor. No-op when nothing is bound.

func (*Session) Sync

func (s *Session) Sync()

Sync reads the bound editor's current text and selection and, when it differs from the last pushed snapshot (or the session was just (re)bound or invalidated), pushes it to the backend. It is a no-op when nothing is bound.

The pushed snapshot carries committed text only: an in-progress IME preedit the editor reflects in its accessible text is subtracted first (see [preeditIncluder], pre-rebrand tracker #1057), because the native mirror holds the composition itself and judges pushes against the committed document.

This is the "push" direction; the redundant-push filter is the debounce. Drivers call Sync after each input event (key, composition, selection change), exactly where the mobile driver calls pushKeyboardState today.

func (*Session) Unbind

func (s *Session) Unbind()

Unbind detaches the session from the current editor. Edit ops and Sync become no-ops until the next Session.Bind.

func (*Session) View

func (s *Session) View() (View, bool)

View captures the bound editor's live document view (see Read); ok is false when nothing is bound. Drivers use it to inspect the document around an incoming native edit — e.g. the Android IC-echo dedup diffs a reported replacement range against the pre-edit text (pre-rebrand tracker #1057).

type Snapshot

type Snapshot struct {
	Text     string
	SelStart int
	SelEnd   int
}

Snapshot is the mirrored state of the focused editor: its full text and the half-open [SelStart, SelEnd) selection as rune offsets into Text. SelStart == SelEnd is a bare caret at that offset. Offsets are rune indices, matching core.AccessibleTexter; a backend converts to its platform's code-unit space.

type View

type View struct {
	Text     string
	SelStart int
	SelEnd   int
	Caret    int
}

View is the pull-side document view of an editor: its full text plus the caret and the half-open [SelStart, SelEnd) selection as rune offsets, captured live via Read. It is the read counterpart to the push Snapshot: backends that serve platform text-protocol queries rather than mirror pushes into a native document — the desktop accessibility text protocols — read the editor through Read instead of calling the core.AccessibleTexter methods ad-hoc, so every backend and the Session see one document view (pre-rebrand tracker #772).

func Read

func Read(ed core.AccessibleTexter) View

Read captures the editor's current document view — text, selection and caret — in one place. It is the sole reader of a core.AccessibleTexter's document state: Session.Sync takes its Snapshot from it, and the desktop accessibility text protocols read every text query through it.

func (View) Snapshot

func (v View) Snapshot() Snapshot

Snapshot returns the push-side subset of the view — its text and selection.

Jump to

Keyboard shortcuts

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