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 ¶
- func InsertText(ed core.AccessibleTexter, text string)
- func ReadSelection(ed core.AccessibleTexter) (start, end int)
- func ReplaceAll(ed core.AccessibleTexter, text string)
- func ReplaceRange(ed core.AccessibleTexter, start, end int, text string)
- func Select(ed core.AccessibleTexter, start, end int)
- type Backend
- type Session
- func (s *Session) Bind(focused any) (editorChanged bool)
- func (s *Session) Bound() bool
- func (s *Session) CancelCompose()
- func (s *Session) Commit(text string)
- func (s *Session) Compose(preedit string, cursor int)
- func (s *Session) Delete(start, end int)
- func (s *Session) EditorComposing() bool
- func (s *Session) Insert(text string)
- func (s *Session) Invalidate()
- func (s *Session) Replace(start, end int, text string)
- func (s *Session) SetSelection(start, end int)
- func (s *Session) Sync()
- func (s *Session) Unbind()
- func (s *Session) View() (View, bool)
- type Snapshot
- type View
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 ¶
NewSession returns a session that pushes editor state to backend. The backend must be non-nil.
func (*Session) Bind ¶
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) 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.