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
+24
View File
@@ -63,6 +63,30 @@ func RunInJVM(f func(env *C.JNIEnv)) {
f(env)
}
// SetGestureExclusions forwards the selection-handle grab boxes (view-local
// px) to View.setSystemGestureExclusionRects on Android so drags starting on
// an edge handle are not stolen by the system back gesture (see
// jni_android.c). It is called from the frame loop, which runs on the Android
// UI thread. No-op (C-side) before API 29.
func SetGestureExclusions(rects [][4]int) {
if theJVM == nil {
return
}
RunInJVM(func(env *C.JNIEnv) {
if len(rects) == 0 {
C.SetGestureExclusions(env, 0, nil)
return
}
buf := make([]C.jint, len(rects)*4)
for i, rc := range rects {
for j, v := range rc {
buf[i*4+j] = C.jint(v)
}
}
C.SetGestureExclusions(env, C.jint(len(rects)), &buf[0])
})
}
func OpenFile(path string) {
var env *C.JNIEnv
var detach bool
+4
View File
@@ -13,3 +13,7 @@ var (
func handleEvent(e event.Event) { }
func OpenFile(path string) { }
// SetGestureExclusions is a no-op off Android (system gesture exclusion
// rects are an Android API 29+ feature).
func SetGestureExclusions(rects [][4]int) {}
+72
View File
@@ -9,8 +9,20 @@
// Global context
static jobject ctx = NULL;
// g_view: global ref to the GioView, set in registerFragment. Used by
// SetGestureExclusions to reach View.setSystemGestureExclusionRects.
static jobject g_view = NULL;
void
registerFragment(JNIEnv *env, jobject view) {
if (view != NULL) {
if (g_view == NULL) {
g_view = (*env)->NewGlobalRef(env, view);
} else if (g_view != view) {
(*env)->DeleteGlobalRef(env, g_view);
g_view = (*env)->NewGlobalRef(env, view);
}
}
jclass cls = (*env)->GetObjectClass(env, view);
jmethodID mid = (*env)->GetMethodID(env, cls, "getContext", "()Landroid/content/Context;");
jobject l_ctx = (*env)->CallObjectMethod(env, view, mid);
@@ -119,3 +131,63 @@ void open_file_in_termux(JNIEnv *env, const char *path) {
(*env)->CallVoidMethod(env, ctx, startActivity, intent);
}
// Set the exclusion rects ON THE UI THREAD. This function is called from the
// Go frame loop, which runs on its own OS thread (NOT the Android UI thread
// — the JNI event pump merely enqueues events that the app goroutine drains).
// setSystemGestureExclusionRects stores the rects on the view and triggers a
// relayout; it is the RELAYOUT that reports them to the window manager, which
// owns the edge back-gesture zone. A direct cross-thread call leaves the
// window manager unaware, so the rects are applied through
// view.post(PadExcl) — a Runnable (org.gioui.PadExcl, added by the build
// scripts' smali patch) that runs on the UI thread.
void SetGestureExclusions(JNIEnv *env, jint nrects, const jint *xyxy) {
if (g_view == NULL) return;
jclass verCls = (*env)->FindClass(env, "android/os/Build$VERSION");
if (verCls == NULL) return;
jfieldID sdkF = (*env)->GetStaticFieldID(env, verCls, "SDK_INT", "I");
if (sdkF == NULL) return;
if ((*env)->GetStaticIntField(env, verCls, sdkF) < 29) return;
jclass rectCls = (*env)->FindClass(env, "android/graphics/Rect");
jmethodID rectInit = (*env)->GetMethodID(env, rectCls, "<init>", "(IIII)V");
jclass listCls = (*env)->FindClass(env, "java/util/ArrayList");
jmethodID listInit = (*env)->GetMethodID(env, listCls, "<init>", "()V");
jobject list = (*env)->NewObject(env, listCls, listInit);
if (list == NULL) return;
jmethodID addM = (*env)->GetMethodID(env, listCls, "add", "(Ljava/lang/Object;)Z");
for (int i = 0; i < nrects; i++) {
jobject rect = (*env)->NewObject(env, rectCls, rectInit,
xyxy[i * 4], xyxy[i * 4 + 1], xyxy[i * 4 + 2], xyxy[i * 4 + 3]);
if (rect == NULL) continue;
(*env)->CallBooleanMethod(env, list, addM, rect);
(*env)->DeleteLocalRef(env, rect);
}
// Find the app class through the context's class loader. A bare
// FindClass from this (non-Java) thread resolves against the BOOT
// class loader only and would never find org.gioui.PadExcl.
jclass ctxCls = (*env)->GetObjectClass(env, ctx);
jmethodID gclM = (*env)->GetMethodID(env, ctxCls, "getClassLoader", "()Ljava/lang/ClassLoader;");
jobject loader = (*env)->CallObjectMethod(env, ctx, gclM);
jclass loaderCls = (*env)->GetObjectClass(env, loader);
jmethodID fcM = (*env)->GetMethodID(env, loaderCls, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;");
jstring clsName = (*env)->NewStringUTF(env, "org.gioui.PadExcl");
jclass padExclCls = (jclass)(*env)->CallObjectMethod(env, loader, fcM, clsName);
if (padExclCls == NULL) {
(*env)->ExceptionClear(env);
(*env)->DeleteLocalRef(env, list);
return;
}
jmethodID padExclInit = (*env)->GetMethodID(env, padExclCls, "<init>", "(Landroid/view/View;Ljava/util/List;)V");
jobject runnable = (*env)->NewObject(env, padExclCls, padExclInit, g_view, list);
if (runnable == NULL) {
(*env)->ExceptionClear(env);
(*env)->DeleteLocalRef(env, list);
return;
}
jmethodID postM = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, g_view),
"post", "(Ljava/lang/Runnable;)Z");
(void)(*env)->CallBooleanMethod(env, g_view, postM, runnable);
(*env)->ExceptionClear(env);
(*env)->DeleteLocalRef(env, runnable);
(*env)->DeleteLocalRef(env, list);
}
+1
View File
@@ -12,3 +12,4 @@ jint AttachCurrentThread(JavaVM *vm, JNIEnv **p_env, void *thr_args);
jint DetachCurrentThread(JavaVM *vm);
jobject NewGlobalRef(JNIEnv *env, jobject o);
void open_file_in_termux(JNIEnv *env, const char *path);
void SetGestureExclusions(JNIEnv *env, jint nrects, const jint *xyxy);
+27
View File
@@ -117,6 +117,13 @@ func run(w *app.Window) error {
go frameReceiver(w, &mu, &frame, logic.FrameChan())
go logic.Run()
// lastExcl tracks the selection-handle grab boxes last sent to
// SetGestureExclusions (Android: keeps the system back gesture from
// stealing drags that start on an edge handle). Only the JNI call when
// the set changes — the rects move every frame while a selection is
// visible/scrolling, but identical repeats are skipped.
var lastExcl [][4]int
for {
switch e := w.Event().(type) {
case app.DestroyEvent:
@@ -209,6 +216,13 @@ func run(w *app.Window) error {
if renderer.PendingLongPress() {
w.Invalidate()
}
// Selection-handle system-gesture exclusions (Android): forward the
// frame's handle grab boxes when they changed. Runs on the UI
// thread (the frame pump is), as View methods require.
if excl := renderer.GestureExclusions(); !exclEqual(excl, lastExcl) {
lastExcl = excl
SetGestureExclusions(excl)
}
// Gather key events
focusedID := frame.FocusedElementID
@@ -300,6 +314,19 @@ func run(w *app.Window) error {
}
}
// exclEqual reports whether two exclusion-rect sets are identical.
func exclEqual(a, b [][4]int) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func frameReceiver(w *app.Window, mu *sync.Mutex, frame *editor.Frame, frameChan <-chan editor.Frame) {
for {
f := <-frameChan