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
+19 -2
View File
@@ -85,6 +85,11 @@ type EditorState struct {
// when its EditSeq matches, so a layout shaped before an edit can never
// stamp stale wrap counts onto shifted lines.
EditSeq uint64
// ShowIMESeq pulses whenever the logic layer wants the soft keyboard up
// (file open, tap/double-tap on the editor). The renderer issues
// SoftKeyboardCmd{Show:true} only on a change (see TextField.ShowIMESeq),
// so a user-dismissed keyboard stays down until the next pulse.
ShowIMESeq uint64
// --- Touch selection (v1) ---
// CaretDrag: after a long press on blank space a single draggable caret
// handle is shown (no selection). MenuVisible/MenuRect/MenuItems: the
@@ -381,6 +386,7 @@ func OpenFile(data any) {
TheState.ScrollOffset = 0 // Reset editor scroll to top when opening a new file
TheState.page = EditorPage
TheState.FocusedElementID = "editor_text" // Set focus to editor
TheState.Editor.ShowIMESeq++ // raise the keyboard for the newly opened file
// The tap that opened this file is delivered to the browser row, but its
// gesture can leak into the now-visible editor and move the cursor/scroll.
// Record the open time so the editor can swallow taps in a short window
@@ -882,6 +888,7 @@ func HandleTapAt(x, y ui.Dp) {
localX := float64(x - TheState.EditorRegion.X)
localY := tapLocalY(y, TheState.EditorRegion.Y)
SetCursorFromPoint(localX, localY)
TheState.Editor.ShowIMESeq++ // tap = intent to type: re-raise a dismissed keyboard
}
// HandleLongPressAt implements Android long-press: on a word it selects the
@@ -941,6 +948,7 @@ func HandleDoubleTapAt(x, y ui.Dp) {
} else {
SetCursorFromPoint(localX, localY)
}
TheState.Editor.ShowIMESeq++ // tap = intent to type: re-raise a dismissed keyboard
}
// HandleSelDragEvt is the registered SelDrag interaction handler. The
@@ -1699,7 +1707,14 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
if lh := TheState.Editor.GlyphLayout.LineHeight; lh > 0 {
lineHeight = lh
}
start, end, winLine := cb.VisibleByteRange(TheState.ScrollOffset, TheState.ByteOffset, viewportHeight, lineHeight, TheState.WordWrap, TheState.Editor.GlyphLayout, nil)
// Assign (not :=) into the outer start/end: they are read below the
// block to derive IMEWindowStartByte and the window-relative
// selection. A `:=` here would shadow them (winLine is new) and the
// outer pair would stay 0 — every scrolled frame would then map the
// selection against byte 0 (the selection "jumps" with the scroll) and
// IME commits would land at the wrong buffer position.
var winLine int
start, end, winLine = cb.VisibleByteRange(TheState.ScrollOffset, TheState.ByteOffset, viewportHeight, lineHeight, TheState.WordWrap, TheState.Editor.GlyphLayout, nil)
// Ship with the frame: the shaped layout's wrap counts belong to THIS
// window's lines (a scroll may move the window before the layout
// arrives, but an edit invalidates it — see EditorState.EditSeq).
@@ -1824,7 +1839,9 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
},
)
// Set Focused so TextField.Draw() issues key.FocusCmd, which is required
// for Gio to deliver key events to this element.
// for Gio to deliver key events to this element. ShowIMESeq carries the
// keyboard-raise pulse (see TextField.ShowIMESeq).
editorElem.ShowIMESeq = TheState.Editor.ShowIMESeq
editorElem.Focused = TheState.FocusedElementID == "editor_text"
// Caret handle visibility (long press on blank space).
editorElem.CaretDrag = TheState.Editor.CaretDrag