Handle drags: 1:1 finger tracking with cross-flip; fix caret/taps on empty lines

Selection handles now track the finger 1:1 (anchor grab point + displacement)
instead of snapping by whole lines, and crossing the opposite handle flips
the selection (native behaviour) instead of clearing it.

Caret and tap/handle line resolution use VisualLineStarts instead of the
min-Y baseline: the window's first visual line may be an empty line with no
recorded glyphs, which used to draw boundary carets one line too low per
leading empty line and land taps/dragged handles one line below the finger.
New exported ui.CaretPoint centralises byte->insertion-point mapping.

The off-screen caret no longer clamps to the window edge: EditorLayout ships
the true (possibly negative / past-end) window-relative cursor and the
renderer skips the caret when the cursor is outside the shaped window, so
scrolling past the caret no longer makes it jump onto the top/bottom line.

IME/router replay fixes: key.FocusCmd is issued only on a focus transition
(a per-frame no-op still takes the immediate-command path and re-queues all
pointer events), and the key.SelectionCmd IME sync is deferred while a
handle drag is in progress (each push re-injected the drag into every
gesture). Handle drags forward only Grabbed events; a tap inside a handle
grab box is a no-op.

Also: key.FocusEvent no longer logs as unexpected in main; dead code removed
(worker taskWrapper, browser applyXxxResult stubs, scrollIndex, mock_setup
sortModeKey/lineSpan helpers); mock FileSystem.ListPaths prefix match uses
strings.HasPrefix; build scripts run the new scripts/check.sh static gate
(go vet + staticcheck). Tests: caret_point_test, touch_selection updates
(flip/empty-line cases), off-window caret e2e, selection drag e2e grab step.
This commit is contained in:
gmp
2026-08-19 22:34:12 -04:00
parent 110c92e3fa
commit d8b5bc704b
21 changed files with 955 additions and 377 deletions
+93
View File
@@ -106,3 +106,96 @@ func TestEditorClickToMoveCursorWithScroll(t *testing.T) {
t.Errorf("expected cursor to move to 7 (Line 2), but got %d", pos)
}
}
// TestRealFile_CaretCursorOutsideWindow pins the off-screen caret contract:
// when the user scrolls the caret's line out of the viewport, the shaped
// window no longer contains the cursor's byte, and the frame must carry the
// TRUE window-relative cursor position (negative when the cursor is above
// the window, past len(Value) when below) so the renderer can skip the caret
// entirely.
//
// Pre-fix, EditorLayout clamped a negative window-relative cursor to 0 and
// drawWrappedText drew the caret unconditionally: scrolling the caret's line
// just past the top of the viewport made the caret "jump" onto the first
// visible line, then snap back to its real line when the user scrolled back
// (the reported cursor-jumps-to-adjacent-line bug). Native Android simply
// does not draw a caret that is off-screen.
func TestRealFile_CaretCursorOutsideWindow(t *testing.T) {
const (
lines = 60
lineLen = 21
scrollLn = 10
)
h, _ := realFileHarness(t, "caret_window.txt", uniformContent(lines))
defer h.Cleanup()
h.SendConfig(780, 400)
if _, err := h.WaitForFrame(5 * time.Second); err != nil {
t.Fatalf("wait for frame: %v", err)
}
// Scroll so the window starts at line 10 (byte 210); put the caret on
// line 5 (byte 105), ABOVE the window.
caretAbove := 5 * lineLen
if err := h.WithState(func(st *editor.State) {
st.ScrollOffset = ui.Dp(16.8*scrollLn + 5)
st.Editor.CursorPosition = caretAbove
}); err != nil {
t.Fatalf("WithState: %v", err)
}
prev := h.FrameCount()
h.SendConfig(780, 400) // state changes alone do not emit frames
if _, err := h.WaitForFrameCount(prev+1, 5*time.Second); err != nil {
t.Fatalf("wait for scrolled frame: %v", err)
}
tf, ok := lastEditorTextField(t, h)
if !ok {
t.Fatal("no editor_text TextField in latest frame")
}
if want := caretAbove - scrollLn*lineLen; tf.CursorPosition != want {
t.Fatalf("window cursor above = %d, want %d (negative: cursor above window, caret must be hidden)", tf.CursorPosition, want)
}
// Mirror case: caret on line 50 (byte 1050), BELOW the window (the
// ~18-line window at line 10 ends far short of it).
caretBelow := 50 * lineLen
if err := h.WithState(func(st *editor.State) {
st.Editor.CursorPosition = caretBelow
}); err != nil {
t.Fatalf("WithState: %v", err)
}
prev = h.FrameCount()
h.SendConfig(780, 400)
if _, err := h.WaitForFrameCount(prev+1, 5*time.Second); err != nil {
t.Fatalf("wait for scrolled frame (below): %v", err)
}
tf, ok = lastEditorTextField(t, h)
if !ok {
t.Fatal("no editor_text TextField in latest frame")
}
if want := caretBelow - scrollLn*lineLen; tf.CursorPosition != want {
t.Fatalf("window cursor below = %d, want %d (past len(Value): cursor below window, caret must be hidden)", tf.CursorPosition, want)
}
if tf.CursorPosition <= len(tf.Value) {
t.Fatalf("window cursor %d should be past window end %d", tf.CursorPosition, len(tf.Value))
}
// And the in-window case must still carry the plain relative offset.
if err := h.WithState(func(st *editor.State) {
st.Editor.CursorPosition = 12*lineLen + 9
}); err != nil {
t.Fatalf("WithState: %v", err)
}
prev = h.FrameCount()
h.SendConfig(780, 400)
if _, err := h.WaitForFrameCount(prev+1, 5*time.Second); err != nil {
t.Fatalf("wait for scrolled frame (inside): %v", err)
}
tf, ok = lastEditorTextField(t, h)
if !ok {
t.Fatal("no editor_text TextField in latest frame")
}
if want := 12*lineLen + 9 - scrollLn*lineLen; tf.CursorPosition != want {
t.Fatalf("window cursor inside = %d, want %d", tf.CursorPosition, want)
}
}
@@ -127,10 +127,12 @@ func TestRealFile_TouchSelectionDragEndHandle(t *testing.T) {
t.Fatalf("wait frame: %v", err)
}
// Drag the end handle (which=1) past the last glyph: selects the line.
// Drag the end handle (which=1): grab near the anchor (byte 5 at local
// x=50), then move past the last glyph — the anchor follows the finger
// 1:1 and selects the line.
before = h.FrameCount()
h.SendInput([]ui.InputEvent{
{Handler: editor.HandleSelDragEvt, Data: ui.SelectionDragEvent{Which: 1, X: reg.X + 115, Y: reg.Y + 8}},
{Handler: editor.HandleSelDragEvt, Data: ui.SelectionDragEvent{Which: 1, X: reg.X + 52, Y: reg.Y + 8}},
{Handler: editor.HandleSelDragEvt, Data: ui.SelectionDragEvent{Which: 1, X: reg.X + 115, Y: reg.Y + 8}},
{Handler: editor.HandleSelDragEvt, Data: ui.SelectionDragEnd{}},
})