Fix selection-handle drags, menu anchoring, and left-edge back-gesture theft

Three user-reported selection bugs, one root cause each:

1. Start handle ungrabbable at line start. Two interacting causes:
   a) The 48dp grab box straddles two visual lines; a finger in the
      lower half mapped (by y-to-line) to the neighbouring line, whose
      byte past the other handle clamped to a zero-length selection ->
      cleared on the first drag event. The cleared selection
      un-registered the drag op, so the router silently stopped
      delivering drag events (the observed 'stream cutoff'). Fix:
      handle drags now project the finger's x onto the anchor's own
      visual line (visualLineOfByte + textPosOnLineAtX); the anchor
      never crosses lines during a handle drag.
   b) A horizontal flick from the line-start handle (screen x~26px)
      started the system back gesture, which cancelled the touch
      stream. Fix: report the handle grab rects as system gesture
      exclusion rects (setSystemGestureExclusionRects, API 29+),
      marshalled to the UI thread via a PadExcl smali Runnable
      (generated identically by build_emu.sh/build_phone.sh).

2. End-handle drag downward made the menu chase the finger and cover
   the selection. Fix: the menu anchors to the STABLE end of the
   selection (the end not being dragged), so it stays parked by the
   selection start, clear of the finger and the highlighted text.

3. Menu above the selection vanished permanently when the selection
   was extended onto the top line. Fix: off-window anchors no longer
   hide the menu while any part of the selection is visible (keep-last
   rect, clamped); hiding happens only for fully off-window selections.

Also: registerDrag simplified (single shared drag path, body before
handles in z-order), debug logging removed, regression tests
(mutation-verified) for line projection and menu anchoring, docs
section 17. Verified on device: start-handle drag shrinks the word
without clearing or triggering back navigation; end-handle vertical
drag leaves menu/highlight/handles undisturbed; menu stays visible
with the selection at the top line.
This commit is contained in:
gmp
2026-08-18 13:09:29 -04:00
parent 2b7c9cd3eb
commit 275a78efaa
12 changed files with 748 additions and 37 deletions
+47 -4
View File
@@ -124,8 +124,18 @@ type Renderer struct {
// (-1 = none), set by CheckGestures and read by drawWrappedText to
// enlarge the grabbed handle, as the framework does while dragging.
selDraggingWhich int
// gestureExclusions holds the selection-handle grab boxes (view-local
// px, [x0, y0, x1, y1]) collected by drawWrappedText for the current
// frame. On Android the main loop forwards them to
// View.setSystemGestureExclusionRects (API 29+) so drags starting on an
// edge handle are not stolen by the system back gesture.
gestureExclusions [][4]int
}
// GestureExclusions returns the handle grab boxes collected for the last
// frame (see gestureExclusions).
func (r *Renderer) GestureExclusions() [][4]int { return r.gestureExclusions }
// New creates a new Renderer.
func New(th Theme, shp *text.Shaper) *Renderer {
r := &Renderer{
@@ -664,6 +674,7 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
if str == "" {
return
}
r.gestureExclusions = nil // rebuilt from this frame's handle boxes
// Fixed line height based on font size, not glyph metrics.
lineHeightSp := unit.Sp(float32(r.theme.FontSize) * LineHeightScale)
@@ -840,6 +851,30 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
const grab = Dp(24) // 48dp box
minx, miny := int(r.toPx(hx-grab)), int(r.toPx(cy-grab))
maxx, maxy := int(r.toPx(hx+grab)), int(r.toPx(cy+grab))
// System-gesture exclusion (Android API 29+): a drag that STARTS
// inside ~20dp of the screen edge can be taken over by the system
// back gesture (predictive back) — it cancels the handle drag and
// navigates the app away. Excluding the grab boxes (clipped to the
// editor region, which is what is actually grabbable) keeps edge
// handles, e.g. the left handle of a line-start selection, usable.
// The main loop forwards these rects to
// View.setSystemGestureExclusionRects (see SetGestureExclusions).
ex0, ey0, ex1, ey1 := minx, miny, maxx, maxy
if ex0 < int(r.toPx(reg.X)) {
ex0 = int(r.toPx(reg.X))
}
if ey0 < int(r.toPx(reg.Y)) {
ey0 = int(r.toPx(reg.Y))
}
if ex1 > int(r.toPx(reg.X+reg.W)) {
ex1 = int(r.toPx(reg.X + reg.W))
}
if ey1 > int(r.toPx(reg.Y+reg.H)) {
ey1 = int(r.toPx(reg.Y + reg.H))
}
if ex1 > ex0 && ey1 > ey0 {
r.gestureExclusions = append(r.gestureExclusions, [4]int{ex0, ey0, ex1, ey1})
}
hc := clip.Rect{
Min: image.Point{X: minx, Y: miny},
Max: image.Point{X: maxx, Y: maxy},
@@ -850,10 +885,14 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
if selStart >= 0 && selEnd > selStart {
sx, sy := handleAt(selStart)
ex, ey := handleAt(selEnd)
registerDrag(&r.selDragStart, sx, sy)
r.selDragsOn[0] = true
registerDrag(&r.selDragEnd, ex, ey)
r.selDragsOn[1] = true
// Body FIRST, handles after: Gio routes a touch to the TOPMOST op
// whose clip contains the point, and a drag only grabs after it
// received the PRESS. The handle boxes reach up into the text line
// (their centres hang below the line) and the body box spans the
// line, so wherever they overlap the later-registered op wins. The
// handles are the more specific target and must win the overlap;
// registering the body last made line-start handles effectively
// ungrabbable (the body ate the presses).
// Body: bounding box of the selected glyphs (only for 2+ glyphs; a
// single-glyph selection is already covered by its two handles).
var bx0, by0, bx1, by1 Dp
@@ -898,6 +937,10 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
bc.Pop()
r.selDragsOn[2] = true
}
registerDrag(&r.selDragStart, sx, sy)
r.selDragsOn[0] = true
registerDrag(&r.selDragEnd, ex, ey)
r.selDragsOn[1] = true
r.drawHandle(gtx, sx, sy, lineH, r.selDraggingWhich == 0)
r.drawHandle(gtx, ex, ey, lineH, r.selDraggingWhich == 1)
} else {