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:
@@ -2,15 +2,19 @@ package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gioui.org/app"
|
||||
"gioui.org/font/gofont"
|
||||
"gioui.org/io/clipboard"
|
||||
"gioui.org/io/key"
|
||||
"gioui.org/io/transfer"
|
||||
"gioui.org/op"
|
||||
"gioui.org/text"
|
||||
"gioui.org/unit"
|
||||
@@ -97,6 +101,14 @@ func run(w *app.Window) error {
|
||||
var searchEditor widget.Editor
|
||||
renderer.RegisterGioEditor("search_bar", &searchEditor)
|
||||
|
||||
// Clipboard plumbing (architecture.md §6.3): the logic goroutine only
|
||||
// REQUESTS clipboard operations through channels (copy/cut write, paste
|
||||
// read); the main goroutine executes the Gio ops during a frame and
|
||||
// forwards the read result back. clipTag tags the read so its
|
||||
// transfer.DataEvent can be matched. A nil clipboard (e.g. a headless
|
||||
// test) simply never completes a read.
|
||||
var clipTag = struct{}{}
|
||||
|
||||
// frame is the frame-receiver-stored handoff (architecture.md §2.2/§9):
|
||||
// the ONLY data the main goroutine reads from the logic side.
|
||||
var mu sync.Mutex
|
||||
@@ -134,6 +146,40 @@ func run(w *app.Window) error {
|
||||
}
|
||||
gtx := app.NewContext(&ops, e)
|
||||
newScale := gtx.Metric.PxPerDp
|
||||
// Clipboard: forward a read result from an earlier frame to the
|
||||
// logic goroutine (one DataEvent per ReadCmd).
|
||||
for {
|
||||
evt, ok := gtx.Event(transfer.TargetFilter{Target: clipTag, Type: "application/text"})
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if de, ok := evt.(transfer.DataEvent); ok {
|
||||
body := de.Open()
|
||||
data, rerr := io.ReadAll(body)
|
||||
body.Close()
|
||||
if rerr == nil {
|
||||
logic.PasteChan() <- string(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Clipboard: a copy/cut write requested by the logic goroutine.
|
||||
select {
|
||||
case t := <-logic.ClipboardSetChan():
|
||||
gtx.Execute(clipboard.WriteCmd{Type: "application/text", Data: io.NopCloser(strings.NewReader(t))})
|
||||
default:
|
||||
}
|
||||
// Clipboard: a paste request (one ReadCmd per request; the result
|
||||
// arrives as a DataEvent on a later frame). On Android the read is
|
||||
// synchronous and the resulting DataEvent is queued during this
|
||||
// frame's op flush — but a queued DataEvent schedules no wakeup of
|
||||
// its own, so invalidate to guarantee a follow-up frame in which
|
||||
// the loop above can consume it.
|
||||
select {
|
||||
case <-logic.PasteReqChan():
|
||||
gtx.Execute(clipboard.ReadCmd{Tag: clipTag})
|
||||
w.Invalidate()
|
||||
default:
|
||||
}
|
||||
// Read ONLY the frame-receiver-stored snapshot; the main goroutine
|
||||
// never touches logic State (architecture.md §1).
|
||||
mu.Lock()
|
||||
@@ -148,6 +194,12 @@ func run(w *app.Window) error {
|
||||
newQuery := searchEditor.Text()
|
||||
sendQuery := newQuery != frame.Query
|
||||
events := renderer.CheckGestures(e.Source, gtx.Metric)
|
||||
// Keep frames flowing while a long press is pending: a stationary
|
||||
// finger generates no pointer events, so without this the window
|
||||
// would sleep and the long-press threshold would never be reached.
|
||||
if renderer.PendingLongPress() {
|
||||
w.Invalidate()
|
||||
}
|
||||
|
||||
// Gather key events
|
||||
focusedID := frame.FocusedElementID
|
||||
|
||||
Reference in New Issue
Block a user