Fix scroll-anchored selection (shadowing) + IME insets keyboard handling
Two user-reported bugs in the post-wrap build:
1. Selection 'jumped' when scrolling: frameOf's chunked branch shadowed the
outer start/end with 'start, end, winLine := cb.VisibleByteRange(...)',
leaving IMEWindowStartByte at 0 for chunked files while scrolled, so the
window-relative selection highlight (and IME commits) mis-mapped near the
file top. Fix: declare winLine outside, assign. Mutation-verified
regression tests: real_file_scroll_selection_test.go.
2. Bottom bar hidden behind the keyboard: GioView extends SurfaceView, which
unconditionally marks the window FORMAT_TRANSLUCENT; translucent windows
are never IME-resized, so adjustResize is dead. Fix (build scripts):
smali-patch a PadInsetsListener OnApplyWindowInsetsListener onto the
GioView (shrinks it by the IME inset bottom) + setDecorFitsSystemWindows
(false) on API 30+ so insets are dispatched. targetSdk kept at 34.
3. (Found while fixing 2) Keyboard could not be dismissed: TextField.Draw
issued SoftKeyboardCmd{Show:true} every frame; with insets-driven
resizes, the hide animation triggered redraws that re-showed the keyboard
mid-animation. Fix: ShowIMESeq pulse from the logic layer (open/tap/
double-tap); renderer shows only on pulse change, re-arming on focus
loss — matching widget.Editor.
On-device (emulator): BACK dismisses and stays down; tap re-shows; typing
works; bottom bar above keyboard; word selection anchored across flick
scroll. go test -race ./... green. Phone APK rebuilt with the same patch.
This commit is contained in:
@@ -85,6 +85,11 @@ type EditorState struct {
|
||||
// when its EditSeq matches, so a layout shaped before an edit can never
|
||||
// stamp stale wrap counts onto shifted lines.
|
||||
EditSeq uint64
|
||||
// ShowIMESeq pulses whenever the logic layer wants the soft keyboard up
|
||||
// (file open, tap/double-tap on the editor). The renderer issues
|
||||
// SoftKeyboardCmd{Show:true} only on a change (see TextField.ShowIMESeq),
|
||||
// so a user-dismissed keyboard stays down until the next pulse.
|
||||
ShowIMESeq uint64
|
||||
// --- Touch selection (v1) ---
|
||||
// CaretDrag: after a long press on blank space a single draggable caret
|
||||
// handle is shown (no selection). MenuVisible/MenuRect/MenuItems: the
|
||||
@@ -381,6 +386,7 @@ func OpenFile(data any) {
|
||||
TheState.ScrollOffset = 0 // Reset editor scroll to top when opening a new file
|
||||
TheState.page = EditorPage
|
||||
TheState.FocusedElementID = "editor_text" // Set focus to editor
|
||||
TheState.Editor.ShowIMESeq++ // raise the keyboard for the newly opened file
|
||||
// The tap that opened this file is delivered to the browser row, but its
|
||||
// gesture can leak into the now-visible editor and move the cursor/scroll.
|
||||
// Record the open time so the editor can swallow taps in a short window
|
||||
@@ -882,6 +888,7 @@ func HandleTapAt(x, y ui.Dp) {
|
||||
localX := float64(x - TheState.EditorRegion.X)
|
||||
localY := tapLocalY(y, TheState.EditorRegion.Y)
|
||||
SetCursorFromPoint(localX, localY)
|
||||
TheState.Editor.ShowIMESeq++ // tap = intent to type: re-raise a dismissed keyboard
|
||||
}
|
||||
|
||||
// HandleLongPressAt implements Android long-press: on a word it selects the
|
||||
@@ -941,6 +948,7 @@ func HandleDoubleTapAt(x, y ui.Dp) {
|
||||
} else {
|
||||
SetCursorFromPoint(localX, localY)
|
||||
}
|
||||
TheState.Editor.ShowIMESeq++ // tap = intent to type: re-raise a dismissed keyboard
|
||||
}
|
||||
|
||||
// HandleSelDragEvt is the registered SelDrag interaction handler. The
|
||||
@@ -1699,7 +1707,14 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
|
||||
if lh := TheState.Editor.GlyphLayout.LineHeight; lh > 0 {
|
||||
lineHeight = lh
|
||||
}
|
||||
start, end, winLine := cb.VisibleByteRange(TheState.ScrollOffset, TheState.ByteOffset, viewportHeight, lineHeight, TheState.WordWrap, TheState.Editor.GlyphLayout, nil)
|
||||
// Assign (not :=) into the outer start/end: they are read below the
|
||||
// block to derive IMEWindowStartByte and the window-relative
|
||||
// selection. A `:=` here would shadow them (winLine is new) and the
|
||||
// outer pair would stay 0 — every scrolled frame would then map the
|
||||
// selection against byte 0 (the selection "jumps" with the scroll) and
|
||||
// IME commits would land at the wrong buffer position.
|
||||
var winLine int
|
||||
start, end, winLine = cb.VisibleByteRange(TheState.ScrollOffset, TheState.ByteOffset, viewportHeight, lineHeight, TheState.WordWrap, TheState.Editor.GlyphLayout, nil)
|
||||
// Ship with the frame: the shaped layout's wrap counts belong to THIS
|
||||
// window's lines (a scroll may move the window before the layout
|
||||
// arrives, but an edit invalidates it — see EditorState.EditSeq).
|
||||
@@ -1824,7 +1839,9 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
|
||||
},
|
||||
)
|
||||
// Set Focused so TextField.Draw() issues key.FocusCmd, which is required
|
||||
// for Gio to deliver key events to this element.
|
||||
// for Gio to deliver key events to this element. ShowIMESeq carries the
|
||||
// keyboard-raise pulse (see TextField.ShowIMESeq).
|
||||
editorElem.ShowIMESeq = TheState.Editor.ShowIMESeq
|
||||
editorElem.Focused = TheState.FocusedElementID == "editor_text"
|
||||
// Caret handle visibility (long press on blank space).
|
||||
editorElem.CaretDrag = TheState.Editor.CaretDrag
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
package e2e_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gioui.org/io/key"
|
||||
"pad/internal/editor"
|
||||
"pad/internal/test/e2e"
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
// uniformContent returns a file of n lines, each exactly 21 bytes:
|
||||
// "Lxxx" + 16 filler chars (derived from the line number, so every line is
|
||||
// unique) + "\n". Line i therefore starts at byte i*21.
|
||||
func uniformContent(n int) string {
|
||||
var b strings.Builder
|
||||
for i := 0; i < n; i++ {
|
||||
b.WriteString(fmt.Sprintf("L%03d", i))
|
||||
b.WriteString(strings.Repeat(string(rune('a'+i%26)), 16))
|
||||
b.WriteString("\n")
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// The chunked-buffer window bookkeeping must stay in lockstep with the
|
||||
// visible window text:
|
||||
// - Editor.IMEWindowStartByte is the absolute byte where the window starts
|
||||
// (it offsets the IME's window-relative EditEvent.Range into the buffer);
|
||||
// - the TextField's SelectionStart/End are window-relative.
|
||||
//
|
||||
// A classic shadowing regression broke both (a `:=` inside the
|
||||
// `cb != nil` block of frameOf shadowed the outer start/end, so both were
|
||||
// always 0 for chunked files): at non-zero scroll the selection highlight
|
||||
// was mapped against byte 0 (it "jumped" to different text as the user
|
||||
// scrolled) and IME commits landed near the top of the file instead of at
|
||||
// the caret — silent data corruption. These tests scroll to a non-zero
|
||||
// window start and pin both mappings.
|
||||
|
||||
// lastEditorTextField finds the editor_text TextField in the latest captured
|
||||
// frame and reports whether it was found.
|
||||
func lastEditorTextField(t *testing.T, h *e2e.Harness) (ui.TextField, bool) {
|
||||
t.Helper()
|
||||
frames := h.GetFrames()
|
||||
if len(frames) == 0 {
|
||||
t.Fatal("no frames captured")
|
||||
}
|
||||
for _, elem := range frames[len(frames)-1] {
|
||||
if tf, ok := elem.(ui.TextField); ok && tf.ID() == "editor_text" {
|
||||
return tf, true
|
||||
}
|
||||
}
|
||||
return ui.TextField{}, false
|
||||
}
|
||||
|
||||
func TestRealFile_WindowSelectionAtScroll(t *testing.T) {
|
||||
const (
|
||||
lines = 60
|
||||
lineLen = 21
|
||||
scrollLn = 10 // window starts at line 10 (byte 210)
|
||||
)
|
||||
// 60 lines * 16.8 dp = 1008 dp of content; a 400 dp window gives a
|
||||
// ~304 dp editor region (~18 lines), so the file is scrollable and the
|
||||
// window at line 10 is fully inside the file.
|
||||
h, _ := realFileHarness(t, "win.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)
|
||||
}
|
||||
|
||||
// Select 6 filler chars on line 12 (well inside the window that starts
|
||||
// at line 10). Filler starts at in-line offset 4 (after "L012").
|
||||
selStart := 12*lineLen + 9 // byte 261
|
||||
selEnd := 12*lineLen + 15 // byte 267
|
||||
// Scroll 5 dp INTO line 10: the app's line pitch is a float32 (16.8000007…)
|
||||
// that differs in the last bits from this file's float64 16.8, so scrolling
|
||||
// to the exact line boundary is fragile; an interior offset is robust.
|
||||
if err := h.WithState(func(st *editor.State) {
|
||||
st.ScrollOffset = ui.Dp(16.8*scrollLn + 5)
|
||||
st.Editor.SelectionStart = selStart
|
||||
st.Editor.SelectionEnd = selEnd
|
||||
st.Editor.CursorPosition = selEnd
|
||||
}); err != nil {
|
||||
t.Fatalf("WithState: %v", err)
|
||||
}
|
||||
|
||||
// Config re-triggers a frame (state changes alone do not emit frames).
|
||||
prev := h.FrameCount()
|
||||
h.SendConfig(780, 400)
|
||||
if _, err := h.WaitForFrameCount(prev+1, 5*time.Second); err != nil {
|
||||
t.Fatalf("wait for scrolled frame: %v", err)
|
||||
}
|
||||
|
||||
// 1) The IME window start must be the window's absolute byte start.
|
||||
v, err := h.Inspect(func(st *editor.State) any {
|
||||
return st.Editor.IMEWindowStartByte
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
if got, want := v.(int), scrollLn*lineLen; got != want {
|
||||
t.Fatalf("IMEWindowStartByte = %d, want %d (window starts at line %d)", got, want, scrollLn)
|
||||
}
|
||||
|
||||
// 2) The rendered window-relative selection must be offset by that same
|
||||
// start (the pre-fix code mapped it against byte 0).
|
||||
tf, ok := lastEditorTextField(t, h)
|
||||
if !ok {
|
||||
t.Fatal("no editor_text TextField in latest frame")
|
||||
}
|
||||
wantS, wantE := selStart-scrollLn*lineLen, selEnd-scrollLn*lineLen
|
||||
if tf.SelectionStart != wantS || tf.SelectionEnd != wantE {
|
||||
t.Fatalf("window selection = [%d,%d), want [%d,%d)", tf.SelectionStart, tf.SelectionEnd, wantS, wantE)
|
||||
}
|
||||
// The window text must actually begin at the window start.
|
||||
if want, got := "L010", tf.Value[:4]; got != want {
|
||||
t.Fatalf("window text starts with %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRealFile_IMECommitAtScroll(t *testing.T) {
|
||||
const (
|
||||
lines = 60
|
||||
lineLen = 21
|
||||
scrollLn = 10
|
||||
editLn = 12
|
||||
)
|
||||
h, path := realFileHarness(t, "win2.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)
|
||||
}
|
||||
|
||||
// The selection (absolute) and the matching window-relative EditEvent
|
||||
// range: the IME reports offsets relative to the pushed snippet, which
|
||||
// is the visible window.
|
||||
selStart := editLn*lineLen + 5
|
||||
selEnd := editLn*lineLen + 11
|
||||
// Scroll 5 dp into line 10 (see TestRealFile_WindowSelectionAtScroll for
|
||||
// why the offset avoids the exact line boundary).
|
||||
if err := h.WithState(func(st *editor.State) {
|
||||
st.ScrollOffset = ui.Dp(16.8*scrollLn + 5)
|
||||
st.Editor.SelectionStart = selStart
|
||||
st.Editor.SelectionEnd = selEnd
|
||||
st.Editor.CursorPosition = selEnd
|
||||
}); 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: %v", err)
|
||||
}
|
||||
|
||||
// Typing replaces the selection: "ZZ" over the 6 selected filler chars
|
||||
// (in-line offsets [5,11)) on line 12. The pre-fix code (windowStart=0)
|
||||
// applied this at byte 47 — line 2 — corrupting the file.
|
||||
h.SendInput([]ui.InputEvent{{
|
||||
Handler: editor.HandleKeyDown,
|
||||
Data: key.EditEvent{
|
||||
Range: key.Range{Start: selStart - scrollLn*lineLen, End: selEnd - scrollLn*lineLen},
|
||||
Text: "ZZ",
|
||||
},
|
||||
}})
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
got, err := h.FullContent()
|
||||
if err != nil {
|
||||
t.Fatalf("FullContent: %v", err)
|
||||
}
|
||||
linesOut := strings.Split(got, "\n")
|
||||
// Expectation derived from the original line: in-line [5,11) replaced.
|
||||
filler := strings.Repeat(string(rune('a'+editLn%26)), 16)
|
||||
origLn := fmt.Sprintf("L%03d", editLn) + filler
|
||||
wantLn12 := origLn[:5] + "ZZ" + origLn[11:]
|
||||
if linesOut[editLn] != wantLn12 {
|
||||
t.Fatalf("line %d = %q, want %q (IME commit must land in the scrolled window, not at byte 0)", editLn, linesOut[editLn], wantLn12)
|
||||
}
|
||||
// Line 2 must be untouched (the pre-fix corruption target).
|
||||
if want := fmt.Sprintf("L%03d", 2) + strings.Repeat("c", 16); linesOut[2] != want {
|
||||
t.Fatalf("line 2 = %q, want %q (must not be clobbered by a scrolled commit)", linesOut[2], want)
|
||||
}
|
||||
|
||||
// Persistence check: the on-disk file matches.
|
||||
if err := h.Flush(); err != nil {
|
||||
t.Fatalf("flush: %v", err)
|
||||
}
|
||||
if disk := readDisk(t, path); disk != got {
|
||||
t.Fatalf("disk = %q, want in-memory content", disk)
|
||||
}
|
||||
}
|
||||
+21
-2
@@ -199,6 +199,13 @@ type TextField struct {
|
||||
VisibleLines []Line
|
||||
WordWrap bool
|
||||
WrapWidth Dp
|
||||
// ShowIMESeq is a monotonically increasing pulse from the logic layer
|
||||
// (incremented on focus gain, file open, and editor taps). The renderer
|
||||
// issues SoftKeyboardCmd{Show:true} only when it changes, never every
|
||||
// frame: a per-frame show re-shows the keyboard while its hide animation
|
||||
// is running (the IME insets dispatches redraw the app), so the keyboard
|
||||
// could not be dismissed. See TextField.Draw.
|
||||
ShowIMESeq uint64
|
||||
}
|
||||
|
||||
func (tf TextField) Type() string { return "textfield" }
|
||||
@@ -228,7 +235,17 @@ func (tf TextField) Draw(gtx layout.Context, r *Renderer) {
|
||||
r.lastIMEField = tf.id
|
||||
r.lastWasFocused = true
|
||||
gtx.Execute(key.FocusCmd{Tag: tf.id})
|
||||
gtx.Execute(key.SoftKeyboardCmd{Show: true})
|
||||
// Raise the soft keyboard only when the logic layer pulses it (focus
|
||||
// gain, file open, editor tap), not every frame. A per-frame show was
|
||||
// harmless while the app only redrew on user events, but the IME
|
||||
// insets now redraw the app during the keyboard's hide animation; a
|
||||
// per-frame show re-shows the keyboard mid-animation, so BACK/chevron
|
||||
// could never dismiss it (matches widget.Editor, which shows on focus
|
||||
// gain and click only).
|
||||
if tf.ShowIMESeq != r.lastIMEShowSeq {
|
||||
r.lastIMEShowSeq = tf.ShowIMESeq
|
||||
gtx.Execute(key.SoftKeyboardCmd{Show: true})
|
||||
}
|
||||
// IME wiring. The visible window (tf.Value) is pushed as the snippet
|
||||
// with Range {0, len}, so the IME treats the window as the document and
|
||||
// reports EditEvent.Range window-relative. This lets swipe/autocorrect
|
||||
@@ -286,12 +303,14 @@ func (tf TextField) Draw(gtx layout.Context, r *Renderer) {
|
||||
}
|
||||
} else if r.lastIMEField == tf.id {
|
||||
// This (previously-focused) field lost focus: forget it so the next focus
|
||||
// pushes a fresh snippet/selection.
|
||||
// pushes a fresh snippet/selection. Resetting lastIMEShowSeq makes the
|
||||
// next focus re-raise the keyboard even without a fresh pulse.
|
||||
r.lastIMEField = ""
|
||||
r.lastWasFocused = false
|
||||
r.lastSnippet = key.Snippet{}
|
||||
r.lastSelStart = -1
|
||||
r.lastSelCaret = -1
|
||||
r.lastIMEShowSeq = 0
|
||||
}
|
||||
r.drawWrappedText(gtx, tf.Value, tf.region, tf.WrapWidth, tf.ScrollOffset, tf.CursorPosition, tf.SelectionStart, tf.SelectionEnd, tf.CaretDrag)
|
||||
}
|
||||
|
||||
@@ -88,8 +88,9 @@ type Renderer struct {
|
||||
lastIMEField string
|
||||
lastWasFocused bool
|
||||
lastSnippet key.Snippet
|
||||
lastSelStart int // window-relative rune index of last-pushed selection start; -1 = no selection
|
||||
lastSelCaret int // window-relative rune index of last-pushed selection end/caret
|
||||
lastSelStart int // window-relative rune index of last-pushed selection start; -1 = no selection
|
||||
lastSelCaret int // window-relative rune index of last-pushed selection end/caret
|
||||
lastIMEShowSeq uint64 // last ShowIMESeq value that issued SoftKeyboardCmd{Show:true}
|
||||
|
||||
// Long-press detection. pressProbe is a plain event tag observing raw
|
||||
// pointer events inside the editor text region: gesture.Click reports
|
||||
|
||||
Reference in New Issue
Block a user