Fix scroll-anchored selection (shadowing) + IME insets keyboard handling

Two user-reported bugs in the post-wrap build:

1. Selection 'jumped' when scrolling: frameOf's chunked branch shadowed the
   outer start/end with 'start, end, winLine := cb.VisibleByteRange(...)',
   leaving IMEWindowStartByte at 0 for chunked files while scrolled, so the
   window-relative selection highlight (and IME commits) mis-mapped near the
   file top. Fix: declare winLine outside, assign. Mutation-verified
   regression tests: real_file_scroll_selection_test.go.

2. Bottom bar hidden behind the keyboard: GioView extends SurfaceView, which
   unconditionally marks the window FORMAT_TRANSLUCENT; translucent windows
   are never IME-resized, so adjustResize is dead. Fix (build scripts):
   smali-patch a PadInsetsListener OnApplyWindowInsetsListener onto the
   GioView (shrinks it by the IME inset bottom) + setDecorFitsSystemWindows
   (false) on API 30+ so insets are dispatched. targetSdk kept at 34.

3. (Found while fixing 2) Keyboard could not be dismissed: TextField.Draw
   issued SoftKeyboardCmd{Show:true} every frame; with insets-driven
   resizes, the hide animation triggered redraws that re-showed the keyboard
   mid-animation. Fix: ShowIMESeq pulse from the logic layer (open/tap/
   double-tap); renderer shows only on pulse change, re-arming on focus
   loss — matching widget.Editor.

On-device (emulator): BACK dismisses and stays down; tap re-shows; typing
works; bottom bar above keyboard; word selection anchored across flick
scroll. go test -race ./... green. Phone APK rebuilt with the same patch.
This commit is contained in:
gmp
2026-08-17 21:52:53 -04:00
parent 83f7affee9
commit 941285e7cc
7 changed files with 610 additions and 8 deletions
+21 -2
View File
@@ -199,6 +199,13 @@ type TextField struct {
VisibleLines []Line
WordWrap bool
WrapWidth Dp
// ShowIMESeq is a monotonically increasing pulse from the logic layer
// (incremented on focus gain, file open, and editor taps). The renderer
// issues SoftKeyboardCmd{Show:true} only when it changes, never every
// frame: a per-frame show re-shows the keyboard while its hide animation
// is running (the IME insets dispatches redraw the app), so the keyboard
// could not be dismissed. See TextField.Draw.
ShowIMESeq uint64
}
func (tf TextField) Type() string { return "textfield" }
@@ -228,7 +235,17 @@ func (tf TextField) Draw(gtx layout.Context, r *Renderer) {
r.lastIMEField = tf.id
r.lastWasFocused = true
gtx.Execute(key.FocusCmd{Tag: tf.id})
gtx.Execute(key.SoftKeyboardCmd{Show: true})
// Raise the soft keyboard only when the logic layer pulses it (focus
// gain, file open, editor tap), not every frame. A per-frame show was
// harmless while the app only redrew on user events, but the IME
// insets now redraw the app during the keyboard's hide animation; a
// per-frame show re-shows the keyboard mid-animation, so BACK/chevron
// could never dismiss it (matches widget.Editor, which shows on focus
// gain and click only).
if tf.ShowIMESeq != r.lastIMEShowSeq {
r.lastIMEShowSeq = tf.ShowIMESeq
gtx.Execute(key.SoftKeyboardCmd{Show: true})
}
// IME wiring. The visible window (tf.Value) is pushed as the snippet
// with Range {0, len}, so the IME treats the window as the document and
// reports EditEvent.Range window-relative. This lets swipe/autocorrect
@@ -286,12 +303,14 @@ func (tf TextField) Draw(gtx layout.Context, r *Renderer) {
}
} else if r.lastIMEField == tf.id {
// This (previously-focused) field lost focus: forget it so the next focus
// pushes a fresh snippet/selection.
// pushes a fresh snippet/selection. Resetting lastIMEShowSeq makes the
// next focus re-raise the keyboard even without a fresh pulse.
r.lastIMEField = ""
r.lastWasFocused = false
r.lastSnippet = key.Snippet{}
r.lastSelStart = -1
r.lastSelCaret = -1
r.lastIMEShowSeq = 0
}
r.drawWrappedText(gtx, tf.Value, tf.region, tf.WrapWidth, tf.ScrollOffset, tf.CursorPosition, tf.SelectionStart, tf.SelectionEnd, tf.CaretDrag)
}
+3 -2
View File
@@ -88,8 +88,9 @@ type Renderer struct {
lastIMEField string
lastWasFocused bool
lastSnippet key.Snippet
lastSelStart int // window-relative rune index of last-pushed selection start; -1 = no selection
lastSelCaret int // window-relative rune index of last-pushed selection end/caret
lastSelStart int // window-relative rune index of last-pushed selection start; -1 = no selection
lastSelCaret int // window-relative rune index of last-pushed selection end/caret
lastIMEShowSeq uint64 // last ShowIMESeq value that issued SoftKeyboardCmd{Show:true}
// Long-press detection. pressProbe is a plain event tag observing raw
// pointer events inside the editor text region: gesture.Click reports