IME: map commits against the whole buffer; drop the renderer-side model

The renderer kept a mirror of the pushed IME snippet (the 'IME model')
to translate commit positions, but it transiently desynced from the
buffer on fling/tap sequences (observed as a few-byte mapping drift on
both the x86_64 emulator and the ARM phone), corrupting text. The model
string also sat on the main goroutine next to the JNI render path,
where the app observed states that were impossible for Go memory
(string contents changing between reads microseconds apart), pointing
at corruption in the native bridge layer.

Restructure along the lines of the Android InputConnection contract
and Gio's own reference editor (widget/editor.go):

- Commits carry absolute file runes (the pushed snippet's coordinate
  space) straight to the logic goroutine, which maps them to bytes
  against the WHOLE buffer (runeToByteWhole, an 8 KiB-step scan).
  Scrolling moves the window, not the buffer, so the mapping is exact
  mid-fling by construction — no mirror to desync.
- Drift guard in HandleIMECommit: a small commit (range <= 2 runes)
  is always anchored at the caret the IME was last told about; if the
  IME reports it ending elsewhere, its snippet text is stale (a
  dropped restartInput, as Gboard does during flings) and its
  position is in the stale text's coordinates — snap the commit to
  the cursor, the only position it cannot drift from.
- FlushIME simplifies to: push the snippet when the frame's
  (context+window) text differs from the last push (gioui dedupes
  against its own cache), force the selection re-push in the same
  frame. After a commit the frame text equals what the IME already
  holds locally, so the restart is naturally suppressed; a fling
  re-anchors the IME once per text change.
- Remove the renderer model (adoptFrame/ModelTranslate/
  ApplyIMEEdit/ApplyIMEKey/IMECaret), the IME freeze/settle
  machinery (IMEFrozen, markIMEScrollActive, imeSettleChan), and the
  window-relative imeRuneToByte.

Also fixed along the way (both found while chasing the corruption):

- real.ReadFileAt: loop over short reads. A single ReadAt on Android
  FUSE can return a short read, silently truncating a chunk and
  shifting every byte offset after it.
- logic: a late lazy-chunk result no longer clobbers a buffer that
  SetContent has already fully loaded.
- e2e: large-file IME test (1.6 MB file, fling + commit).
- app icon (scripts/make_icon.py + cmd/pad/appicon.png) so gogio
  builds the mipmap/adaptive icon set.

Verified: go vet + staticcheck, go test -race (all packages), and the
emulator scenario loop (open moby excerpt, fling to mid-file, tap,
type 'a', byte-compare the saved file) 75/75 clean.
This commit is contained in:
gmp
2026-09-13 11:57:38 -04:00
parent 4c8a13b779
commit f54ca2f81a
15 changed files with 1096 additions and 188 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 123 KiB

+33 -2
View File
@@ -371,10 +371,27 @@ func run(w *app.Window) error {
})
}
case key.EditEvent:
if focusedID == "editor_text" {
// The commit range is in absolute file runes (the
// coordinate space of the pushed snippet, whose
// Range.Start is the context start). The logic maps
// it to bytes against the whole buffer — exact
// even mid-fling — and applies the drift guard
// (see editor.HandleIMECommit).
events = append(events, ui.InputEvent{
Handler: reg.Handler,
Data: k,
Handler: editor.HandleIMECommit,
Data: editor.IMECommit{
StartRune: k.Range.Start,
EndRune: k.Range.End,
Text: k.Text,
},
})
break
}
events = append(events, ui.InputEvent{
Handler: reg.Handler,
Data: k,
})
case key.SnippetEvent:
// Handle snippet event if necessary, or ignore
case key.FocusEvent:
@@ -387,8 +404,22 @@ func run(w *app.Window) error {
}
}
}
// Flush the IME snippet/selection for the focused editor field
// (see Renderer.FlushIME): after the key events above were drained
// and mirrored into the IME model, and before e.Frame below flushes
// the ops to the OS (which is when gioui compares the pushed state
// against the IME's own state.
if focusedID == "editor_text" {
for _, el := range frame.Elems {
if tf, ok := el.(ui.TextField); ok && tf.ID() == "editor_text" {
renderer.FlushIME(gtx, tf)
break
}
}
}
e.Frame(&ops)
mu.Unlock()
if newScale != curScale || newFontScale != curFontScale {
logic.ConfigChan() <- editor.ScaleEvent{Scale: newScale, FontScale: newFontScale}
}