Touch selection (v1): long-press/double-tap word selection, drag handles, floating copy/cut/paste menu

Implement the Android-native touch selection model, verified on-device:
- long-press selects the word under the finger (blank -> caret + paste-only
  menu); double-tap selects the word; drag handles resize the selection,
  drag the highlighted body to move it; floating menu offers copy/cut/paste
  (selection) or paste (bare caret), closing on any item tap.
- Renderer reports finger positions (app-local Dp) as tap/double-tap/
  long-press/selection-drag events; the logic goroutine owns all geometry
  (EditorRegion, menu rect, hit-testing, handles) and the renderer only
  draws the frame snapshot.
- Long press: 400 ms still-press on the editor, cancelled by movement
  (non-grabbing raw pointer probe) or by a scroll/handle grab. The main
  loop keeps invalidating while a press is pending (Gio renders on demand;
  a stationary finger produces no frames).
- Clipboard crosses the goroutine boundary via buffered channels
  (clipboardSetChan/pasteReqChan logic->main, pasteChan main->logic);
  main executes the Gio ops and, on Android, invalidates after ReadCmd
  because a queued transfer.DataEvent schedules no frame of its own.

Renderer fixes found while validating on-device:
- clickReg was stored by value in a map; range yielded copies so per-frame
  press bookkeeping (long-press state) was silently discarded. Now pointers.
- On Android a tap's press+release arrive in the same frame and
  gesture.Click/Drag return one event per Update call; without draining
  each gesture's queue every frame the release was lost on an idle window
  and every menu tap was swallowed (needed a second tap to 'rescue' it).
  Click and drag loops now drain to exhaustion (scroll already does).
- pointer.Filter queries must name Kinds: a zero-kinds filter matches
  nothing (the press-probe query was dead).
- Menu.Draw offsets items by the menu origin; the clippable drawElement
  branch registers SelDrag (handles now draw for the TextField).

Tests: internal/editor/touch_selection_test.go (word range, long-press,
double-tap, tap/menu guards, handle drags, menu actions, selection edits)
and internal/test/e2e/touch_selection_e2e_test.go; full suite green under
-race. Docs: spec.md §2.2 + §7, architecture.md §6.3a, development_plan.md
Phases 8-9.
This commit is contained in:
gmp
2026-08-17 08:57:55 -04:00
parent ec11abf8f1
commit 2a16c0017c
10 changed files with 1720 additions and 56 deletions
+57
View File
@@ -270,6 +270,63 @@ Only the visible byte range is shaped and drawn each frame:
before the glyphs; the IME `SelectionCmd` push dedups on the
(selectionStart, caret) pair.
### 6.3a Touch selection (v1)
- **Division of labor: the renderer reports finger positions, the logic owns
all geometry.** Touch input is delivered to the logic goroutine as
`ui.Point` (tap), `ui.DoubleTapPoint`, `ui.LongPressPoint`, and selection
drag events (which handle + app-local Dp position). The logic converts them
to text coordinates using the `EditorRegion` stored on `State` (set each
layout frame) plus the scroll offset, hit-tests the floating-menu items
itself, and owns the menu rect, highlight range, and handle positions.
The renderer only draws what the `Frame` snapshot says (menu, handles,
highlight) and registers the input regions.
- **Menu taps are logic-decided.** The menu panel is one `Tap` interaction
over its whole rect; the editor's tap handler ignores any tap inside a
visible menu (`pointInMenu` guard) so both handlers can coexist regardless
of dispatch order. Item identity comes from the tap's X within the panel.
- **Long press needs frames to elapse.** Gio renders on demand; a stationary
finger produces no pointer events and therefore no frames, so the 400 ms
threshold could never be checked. The main loop polls
`Renderer.PendingLongPress()` and keeps invalidating the window while a
press is held still on the editor. A non-grabbing raw pointer probe
(plain `event.Op` tag on the editor region) observes the press's motion and
cancels the pending long press on movement; a scroll or handle drag grabs
the pointer (`pointer.GrabCmd`), which cancels it via `pointer.Cancel`.
- **A pointer filter with zero `Kinds` matches nothing.**
`pointer.Filter.Matches` tests `e.Kind & f.Kinds == e.Kind`, so a query
without `Kinds` silently receives no pointer events — probe queries must
name the kinds they want.
- **One event per Update is a trap on Android.** A tap's down+up routinely
arrive in a *single* frame, and `gesture.Click`/`gesture.Drag` return at
most one event per `Update` call. If the renderer processed only one event
per gesture per frame, the release would sit in the queue until the next
redraw — which on an idle window may never come — and the tap is swallowed
(this is why menu taps initially required a second tap to "rescue" the
first). The renderer therefore drains each click/drag gesture's queue to
exhaustion every frame; `gesture.Scroll` already drains internally.
- **Per-frame gesture bookkeeping must survive the frame.** Click-registry
state (press time/position, long-press-fired flag) is kept in structs held
**by pointer** in a map; `range` over a value-type map yields copies and
silently discards the mutations.
- **Clipboard crosses the goroutine boundary via channels; the ops run on
the main/Gio frame path.** Logic→main: `clipboardSetChan` (string to write)
and `pasteReqChan` (token). Main→logic: `pasteChan` (string). All three are
buffered (16) so a harness with no main loop can never block the logic
goroutine. Main executes `clipboard.WriteCmd` / `clipboard.ReadCmd` during
a frame and forwards read results back on `pasteChan` from the
`transfer.DataEvent`.
- **Android clipboard reads need an explicit invalidation.** Gio v0.10 on
Android answers `ReadCmd` synchronously during the op flush by queueing a
`transfer.DataEvent`; a queued DataEvent schedules **no** frame wakeup of
its own. Main therefore invalidates the window after each read so a
follow-up frame exists in which the DataEvent is consumed.
- **The menu closes on any item tap.** Copy keeps the selection (only the
menu disappears); cut removes it (via `ClearSelection`, which also hides
the menu and cancels drag bookkeeping); paste closes the menu immediately
even though the actual insert happens on a later frame when the clipboard
content arrives.
### 6.4 IME (Android soft keyboard)
- The editor exposes to the IME a **windowed snippet**: `IMEWindowText` is the
+73 -2
View File
@@ -1,7 +1,8 @@
# Development Plan: reach a lean, usable Android text editor
Status: v7, 2026-08-16 (Phases 0–3 + doc reorg + Phase 6 scroll-perf/clamping
verification + tap-to-position-cursor fix + selection + real-file e2e). Written
Status: v8, 2026-08-17 (Phases 0–3 + doc reorg + Phase 6 scroll-perf/clamping
verification + tap-to-position-cursor fix + selection + real-file e2e +
Android arrow/shift workaround + touch selection). 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`,
@@ -44,6 +45,18 @@ Verified end-to-end on the emulator: plain arrows move the caret,
shift+arrow shows a highlight, typing replaces the selection. Remaining:
real-device swipe/autocorrect sign-off (the emulator's AOSP/Gboard keyboard
is a proxy).
Phase 9 (2026-08-17) added **touch selection** — the Android-native selection
model: long-press selects the word under the finger (blank → caret + paste-only
menu), double-tap selects the word, drag handles resize the selection, drag the
highlighted body moves it, and a floating copy/cut/paste menu is hit-tested in
logic and drawn by the renderer. All flows verified on-device, including the
full copy→paste and cut cycles through the real Android clipboard. Debugging
this surfaced two renderer bugs worth knowing about: (1) a map of click-registry
**values** (not pointers) silently discarded per-frame mutations, so the
long-press never fired; (2) on Android a tap's press and release arrive in the
**same frame**, and `gesture.Click`/`gesture.Drag` return one event per Update
call, so without draining each gesture's queue every frame the release was lost
on an idle window and every menu tap was swallowed.
## 1. Decision summary (updated)
@@ -339,6 +352,64 @@ real bug:
- Also removed the per-tap `log.Printf` debug lines in `SetCursorFromPoint`
(per-tap, per-glyph logcat noise).
### Phase 8 — selection on-device + Gio Android key limitation — DONE (2026-08-17)
Verified shift+arrow selection on the emulator, then root-caused why plain
hardware arrows never reached the editor on Android (see the Phase 8 note in the
status header): Gio v0.10's Android JNI drops modifier state, and plain arrow
*presses* are wrapped in `input.SystemEvent` for focus navigation. Fix in
main.go: explicit named `key.Filter`s for the four arrows (delivers the press
and suppresses the focus jump) plus app-side shift tracking (`NameShift`
press/release, reset on focus loss); forwarded events carry
`Shift: Modifiers.Contain(ModShift) || shiftDown`. Verified on-device: `keyevent
22` moves the caret, `keycombination 59 22` extends the selection, typing
replaces it. Documented as a Gio-version-sensitive workaround (architecture.md
§2.1).
### Phase 9 — touch selection — DONE (2026-08-17)
Implemented the Android-native touch selection model (v1 scope: word selection
+ resize + move + floating menu; no handle-tap-to-caret, no marquee, no
auto-scroll-to-cursor). See spec.md §2.2 and architecture.md §6.3 for the
contracts; the highlights:
- **Renderer reports finger positions, logic owns all geometry.** Touch events
are delivered as `ui.Point` (tap), `ui.DoubleTapPoint`, `ui.LongPressPoint`
(app-local Dp); the logic goroutine converts them to text coordinates with
the stored `EditorRegion` + scroll offset, hit-tests menu items itself, and
owns the menu rect, highlight range, and handle positions. The renderer only
draws what the frame snapshot says.
- **Long press needs invalidation to elapse.** Gio renders on demand; a
stationary finger produces no events and no frames, so the 400 ms threshold
could never be checked. The main loop polls `Renderer.PendingLongPress()`
and keeps `w.Invalidate()`-ing while a press is held still on the editor.
A non-grabbing raw pointer probe (`event.Op` tag) watches the press for
movement so a scroll cancels the pending long press; scroll/drag gestures
grab the pointer (`pointer.GrabCmd`) and cancel it the same way.
- **One event per Update is a trap on Android.** A tap's down+up routinely
land in one frame; `gesture.Click`/`gesture.Drag` return at most one event
per `Update` call, so the release sat in the queue until the *next* redraw —
which on an idle window never comes. The renderer now drains each gesture's
queue to exhaustion every frame (scroll already drains internally).
- **Clipboard crosses the goroutine boundary via channels; the ops run on the
main/Gio frame path** (`clipboardSetChan`/`pasteReqChan` logic→main,
`pasteChan` main→logic, all buffered so the harness never blocks logic).
On Android, `clipboard.ReadCmd` is answered synchronously during op flush
with a queued `transfer.DataEvent` that schedules **no** frame of its own,
so main invalidates the window after each read to guarantee a follow-up
frame in which the DataEvent is consumed and forwarded to logic.
- **Bugs found while validating on-device:** `clickReg` stored by value in a
map (per-frame press bookkeeping lost → long press never fired); `Menu.Draw`
didn't offset items by the menu origin; the clippable `drawElement` branch
skipped `SelDrag` registration (handles never drew); the press-probe query
omitted `Kinds` (a `pointer.Filter` with zero kinds matches nothing).
- **Verification:** unit tests (`touch_selection_test.go`, ~18) + e2e
(`touch_selection_e2e_test.go`) green under `-race`; on-device: long-press
word/blank, double-tap, handle-drag resize, single-tap copy/cut/paste
(including copy→paste and cut cycles through the real Android clipboard),
menu close-on-tap, plain-tap caret placement, and scroll-drag not firing a
long press.
## 6. File-size decision (re-framed)
v1 framed this as "accept a limit vs build a windowed editor." The live repo
+15 -6
View File
@@ -56,11 +56,21 @@ elsewhere.
hardware keyboard, arrow keys, Home/End, and Page Up/Down move the cursor
(verified on the Android emulator; Gio's mobile focus-navigation default
for arrow keys is overridden — see architecture.md §2.1).
- **Text selection:** shift+arrow (hardware keyboard) extends a selection;
insert, backspace, and delete replace the selected text; the IME replaces
a selection when the user types over it. The selection is highlighted in
the editor and pushed to the IME. (Shift state is tracked by the app,
since Gio's Android bridge drops modifier keys — architecture.md §2.1.)
- **Text selection:** two input paths. **Touch** (the Android-native model):
long-press selects the word under the finger (on a blank spot it places the
caret and offers a paste-only menu); double-tap selects the word; the
selection has drag handles at both ends (resize) and the highlighted body
can be dragged to move it (length preserved); a floating menu offers
copy/cut/paste when a selection is active and paste alone for a bare caret.
Any menu tap closes the menu; copy keeps the selection, cut removes it, and
paste inserts at the caret or replaces the selection. A plain tap still
places the caret and clears any selection; taps inside the visible menu are
ignored by the editor. **Hardware keyboard:** shift+arrow extends a
selection. In both cases insert, backspace, and delete replace the selected
text, and the IME replaces a selection when the user types over it. The
selection is highlighted in the editor and pushed to the IME. (Shift state
is tracked by the app, since Gio's Android bridge drops modifier keys —
architecture.md §2.1.)
- **Autosave:** every edit restarts a 1 s debounce; on expiry the full content
is written to disk by a worker. Failed writes are retried. This is the only
persistence mechanism.
@@ -154,7 +164,6 @@ recorded here so future rounds don't mistake doc text for behavior:
| File-system watcher | **not implemented** | Browser does not live-refresh; it re-scans on navigation. |
| Alphabetical index sidebar | **not implemented** | `AlphaIndex` element exists but is unused. |
| In-file search, tabs, split view | **not implemented** | — |
| Touch selection (long-press, drag handles) | **not implemented** | Selection is reachable only via hardware-keyboard shift+arrow; touch users can position the caret by tap. |
| Files > 50 MB | **not supported** | `TooLarge` state instead. |
| Desktop / other platforms | **not supported** | Android-first. |