Fix Android window geometry, selection menu tracking

- SurfaceView windows are always translucent, so adjustResize cannot
  resize them: the keyboard overlapped the bottom of the window and the
  top bar sat under the status bar. Consume the insets in app instead:
  a smali-injected PadInsetsListener (identical block in build_emu.sh
  and build_phone.sh) shrinks the GioView to statusBarBottom..keyboardTop
  (or ..navBarTop) on every API 30+ insets dispatch, with
  setDecorFitsSystemWindows(false) so IME insets are delivered at
  targetSdk 34.
- Fix a change-detection bug in the listener: the height write skipped
  the topMargin/bottomMargin checks on every IME state transition, so
  the margins were never applied while the keyboard animated. All three
  params are now checked with a single requestLayout on any change.
- Selection menu now re-anchors to the selected word every frame while
  it is visible and hides when the word scrolls out of the viewport
  (positionSelectionMenu + regression test).
- Doc: development_plan.md section 14 (invariants, geometry contract,
  build-script identity invariant).

Verified on device: top bar below status bar and clickable, bottom bar
flush with keyboard top (view 128..1517 at 1080x2400), BACK dismiss +
re-tap re-raise without a show loop, typing saves, menu tracks scrolls.
This commit is contained in:
gmp
2026-08-17 23:58:19 -04:00
parent 941285e7cc
commit dcb96d04d8
5 changed files with 491 additions and 84 deletions
+63
View File
@@ -843,3 +843,66 @@ Residual (accepted) invariants:
goroutine (input-handler closures run on the owner via `SendInput`).
- `Logic.FlushAll` is lock-free by design; it must not run concurrently with
`Run` (guaranteed by the `Shutdown` order).
## 14. Android window geometry + selection menu tracking (2026-08-17)
Three user-visible bugs, all on the Android target: (1) the word-selection
menu did not follow the selected text when scrolling; (2) a large empty gap
between the keyboard top and the app's bottom bar when the IME was shown;
(3) the top bar rendered under the status bar and could not be clicked.
**Root cause of (2) and (3): the SurfaceView window cannot be resized by
Android.** Gio's `GioView` is a `SurfaceView`; the moment it attaches, the
window becomes translucent (the surface has a transparent region), and
`windowSoftInputMode=adjustResize` no longer resizes it — the keyboard
simply overlaps the window's bottom. No theme, background, or flag change
can make the window opaque again. The fix is to consume the insets *in
app*: a smali-injected `PadInsetsListener` (see `scripts/build_emu.sh`,
identical block in `scripts/build_phone.sh`) sets
`setOnApplyWindowInsetsListener` on the GioView in `GioActivity.onCreate`
(API 21+) and `setDecorFitsSystemWindows(false)` (API 30+, which delivers
IME insets at targetSdk 34). On every insets dispatch the listener shrinks
the view:
- `displayH = displayMetrics.heightPixels + statusTop + navBottom` — the
metrics report the *content* height, while IME insets are measured from
the absolute display bottom; the two frames must be reconciled before
subtracting.
- `bottomLimit = displayH − imeInset` (IME shown) or `displayH − navBottom`
(IME hidden); `height = bottomLimit − statusTop`, clamped ≥ 0.
- `topMargin = statusTop`, `bottomMargin = navBottom`, so the view spans
exactly `statusBarBottom..keyboardTop` (or `..navBarTop`).
On API < 30 there are no per-type insets; the listener degrades to no-op
and the keyboard overlaps (accepted limitation — both build scripts target
minSdk 16).
**Layout-params invariant (the subtle bug found while verifying):** the
height, topMargin, and bottomMargin change-detection checks must *all* run
on every dispatch, with a single `requestLayout` if any of the three
changed. The first version `goto`-skipped the margin checks whenever the
height changed — which is exactly the transition (keyboard show/hide), so
the margins were silently never applied during IME state changes and only
accidentally picked up on a later duplicate dispatch. The listener now
tracks a changed flag across all three checks.
**Selection menu tracking (1):** the menu anchor is recomputed every frame
in the logic goroutine (`positionSelectionMenu`): while a selection is
visible, its screen position is derived from the current layout (byte
offset → glyph → window-relative Dp) and the menu re-anchors to it; if the
selected word scrolls fully out of the viewport the menu hides instead of
stranding. Regression: `internal/editor/selection_menu_track_test.go`.
**Build-script invariant:** the Python patch heredoc in `build_emu.sh` and
`build_phone.sh` must stay byte-identical (both inject the same
`PadInsetsListener` class and the same `GioActivity` wiring); a drift would
produce an emulator APK that behaves differently from the phone APK. The
listener uses only int arithmetic and registers v0–v5 (the smali 22c/11x
formats accept only 4-bit register operands; apktool 3.0.3's smali also
lacks `cmpg-f`).
On-device verified (Pixel 6 profile, API 35, 1080×2400): top bar below the
status bar and clickable; bottom bar flush with the keyboard top (view
spans 128..1517 with the IME shown); BACK dismisses the keyboard and
re-tap re-raises it without a re-show loop; typing saves; the selection
menu tracks small scrolls and hides when the word leaves the viewport.