editor: fix tap-to-position-cursor clamping on large files

Tapping in the editor repositions the cursor, but it was never actually
verified (the old unit test only checked in-bounds/no-panic). On-device
verification found that on a large file scrolled deep, the cursor clamped to
the bottom of the viewport regardless of tap position.

Root cause: the tap handler computed the tap's text-local Y in content space
(pt.Y - region.Y + full ScrollOffset) and passed it to SetCursorFromPoint,
whose visualLine = y/lineHeight then produced a huge content-line number
(e.g. 91,640). But the GlyphLayout is window-relative (layout.Y==0 is the top
of the visible window), so the number far exceeded the window's line count and
clamped to the last group (bottom line). The Phase 3 windowing refactor
introduced the windowed layout without updating the tap handler.

Fix: tapLocalY() converts the tap Y to window-relative space by adding only the
sub-line remainder (ScrollOffset mod lineHeight), never the full scroll.
Extracted as a named helper so it is unit-testable. Added two regression tests
that fail on the pre-fix formula (cursor clamps to the bottom line) and pass on
the fix. Verified on-device: taps now map linearly across the viewport.

Also removed the per-tap/per-glyph log.Printf debug lines in SetCursorFromPoint.
This commit is contained in:
gmp
2026-08-16 17:38:24 -04:00
parent be48ad8157
commit 6dedffcba7
3 changed files with 122 additions and 23 deletions
+42 -4
View File
@@ -1,7 +1,8 @@
# Development Plan: reach a lean, usable Android text editor
Status: v5, 2026-08-16 (Phases 0–3 + doc reorg + Phase 6 scroll-perf/clamping
verification complete). Written against the **live** repo `/home/gmp/pad`. v1 (the widget-rebuild plan) is
Status: v6, 2026-08-16 (Phases 0–3 + doc reorg + Phase 6 scroll-perf/clamping
verification + tap-to-position-cursor fix). Written against the **live** repo
`/home/gmp/pad`. v1 (the widget-rebuild plan) is
superseded — see §12 for why. Doc reorganization (2026-08-16): the over-detailed
docs (`*_implementation_plan.md`, `touch.md`, `element_model.md`,
`layout_rendering.md`, `virtual_scroll_render_optimization.md`,
@@ -14,8 +15,13 @@ whole-file shaper memory-leak fix, a measured 50 MB size limit, and the IME
rapid-commit desync fix (snippet/selection dedup). Phase 6 (2026-08-16) added a
default-off in-app performance profiler and verified: scroll does not degrade at
large offsets (10 MB file), scroll clamping is exact across 2→130,955-line
files, and memory plateaus ~250 MB (no leak). Remaining: real-device
swipe/autocorrect sign-off (the emulator's AOSP/Gboard keyboard is a proxy).
files, and memory plateaus ~250 MB (no leak). Phase 7 (2026-08-16) verified
tap-to-position-cursor on-device and found + fixed a real bug: for large files
scrolled deep, `SetCursorFromPoint` clamped the cursor to the bottom of the
viewport (a content-space vs window-relative Y mismatch from the Phase 3 windowing
refactor); it only worked on small files by luck. Fixed (`tapLocalY`), with
regression tests. Remaining: real-device swipe/autocorrect sign-off (the
emulator's AOSP/Gboard keyboard is a proxy).
## 1. Decision summary (updated)
@@ -280,6 +286,38 @@ at ~`(86, 264)` px, not the glyph's apparent 1:1 position. Browser file rows
logcat line `Logic: OpenFileChan /storage/emulated/0/Notes/<file>`, not a loose
`OpenFileChan` match.
### Phase 7 — tap-to-position-cursor verification + fix — DONE (2026-08-16)
Asked "does tapping in the editor reposition the cursor, and has it been
verified?" — it had **not** been verified (the old unit test only checked
in-bounds/no-panic, never the landed offset). On-device verification found a
real bug:
- **Symptom:** on a large file scrolled deep (e.g. `big10mb.txt` at 70% down),
tapping anywhere in the viewport placed the cursor on the **bottom** line of
the viewport, regardless of where you tapped. It worked on small files only
because their visible window happened to be wide enough to keep the line number
in range.
- **Root cause:** the tap handler computed the tap's text-local Y in
**content space** (`pt.Y - region.Y + full ScrollOffset`) and passed it to
`SetCursorFromPoint`, whose `visualLine = y/lineHeight` then produced a huge
content-line number (e.g. 91,640). But the `GlyphLayout` is **window-relative**
(`layout.Y==0` is the top of the *visible window*, not the file), so the line
number far exceeded the window's ~86 lines and clamped to the last group (the
bottom line). The Phase 3 windowing refactor introduced the windowed layout
but the tap handler was never updated to match it.
- **Fix:** `tapLocalY()` converts the tap Y to window-relative space by adding
only the sub-line remainder of the scroll (`ScrollOffset mod lineHeight`),
never the full scroll. Extracted as a named helper so it is unit-testable.
- **Verification:** on-device, taps now map linearly across the viewport (top tap
→ window line 3; y=430/750/1000/1200 → lines 3/11/16/21, cursor 132/464/677/924),
and a screenshot confirms the cursor bar lands on the tapped line, not the
bottom. Two regression tests added (`TestTapLocalY_WindowRelative`,
`TestTapToPosition_LargeFileScrolled`) — both fail on the pre-fix formula
(cursor clamps to byte 78 = bottom line) and pass on the fix.
- Also removed the per-tap `log.Printf` debug lines in `SetCursorFromPoint`
(per-tap, per-glyph logcat noise).
## 6. File-size decision (re-framed)
v1 framed this as "accept a limit vs build a windowed editor." The live repo