IME: fix random mid-word capitalization (stable snippet window + commit in-flight handling)

Gboard capitalizes the first character of a fresh input session and
derives its word context from the text it holds locally; it never
queries the app for context while typing. The editor was resetting that
session on essentially every event, so Gboard re-anchored to a fresh
session mid-word and caps went random:

- The pushed snippet WAS the render window. Any viewport change (keyboard
  show/hide animation, tap re-centering, scroll) changed the snippet, and
  gioui turns every snippet change into imm.restartInput — a full IME
  session reset. The snippet is now a hysteresis window around the caret
  (32 KB, re-anchor only when the caret is within 4 KB of an edge),
  decoupled from the render window: typing, taps within range, keyboard
  animation and flings never re-push it.
- gioui's EditEvent callback applies the commit to its own window state
  directly, so the op queue lags it by one commit; any frame event in
  the gap regressed the state and sent a restartInput with pre-commit
  text per keystroke. The drained commit is now applied to the pushed
  model immediately (Renderer.ApplyIMECommitToModel), and a short hold
  keeps stale pre-commit frames out of FlushIME until the logic's
  post-commit frame arrives.
- The layout feedback loop re-emitted on exact float equality of the
  derived last-line Y, spinning a re-emit -> shape -> re-emit loop
  (float-sum noise) that re-drew the editor and re-pushed IME state;
  gate it with a 0.5 dp epsilon.
- The render window bottom mapped through the WrapIndex whose
  in-viewport counts land while this very window is being shaped: the
  bottom oscillated frame to frame, resizing the window (and the old
  snippet) every frame. Use a fixed line span from the stable top
  instead (each logical line yields >= 1 visual line, so the viewport is
  always covered).

Result: zero snippet re-pushes during typing or flings (one re-anchor at
a far tap/file switch); emulator typing tests show all-lowercase
mid-word commits ('thaaaaaaaae', 'vapoaaaaaaaar') and the stress suite
(7 scenarios) passes clean with contiguous insertions only.
This commit is contained in:
gmp
2026-09-13 17:00:49 -04:00
parent 36b6a86873
commit dd9493bdbd
7 changed files with 309 additions and 149 deletions
+15 -22
View File
@@ -81,41 +81,34 @@ func TestRealFile_LargeFile_IMECommits(t *testing.T) {
// 1) IME window invariants for a window deep in a large file.
var win struct {
Start int
Text string
CtxStart int
Context string
OffsetRune int
Start int
Text string
Snip string
}
v, err := h.Inspect(func(st *editor.State) any {
e := &st.Editor
return struct {
Start int
Text string
CtxStart int
Context string
OffsetRune int
}{e.IMEWindowStartByte, e.IMEWindowText, e.IMEContextStartByte, e.IMEContext, e.IMEOffsetRune}
Start int
Text string
Snip string
}{e.IMEWindowStartByte, e.IMEWindowText, e.IMESnippetText}
})
if err != nil {
t.Fatalf("Inspect: %v", err)
}
win = v.(struct {
Start int
Text string
CtxStart int
Context string
OffsetRune int
Start int
Text string
Snip string
})
if want := model[win.Start : win.Start+len(win.Text)]; win.Text != want {
t.Fatalf("IME window text desync at start=%d", win.Start)
}
if win.CtxStart+len(win.Context) != win.Start ||
win.Context != model[win.CtxStart:win.Start] {
t.Fatalf("IME context desync: ctxStart=%d ctx=%q", win.CtxStart, win.Context)
}
if want := utf8.RuneCountInString(model[:win.CtxStart]); win.OffsetRune != want {
t.Fatalf("IME offset rune = %d, want %d", win.OffsetRune, want)
// The IME snippet is a hysteresis window around the caret (see
// State.computeIMESnippetWindow); it must always be a fresh slice of
// the model.
if i := strings.Index(model, win.Snip); i < 0 {
t.Fatalf("IME snippet not a slice of the model: %q", win.Snip[:min(40, len(win.Snip))])
}
// 2) Replacement commit: absolute file runes (the coordinate space the