Add text selection, real-file e2e tests, and Android arrow-key support
Selection: shift+arrow extends a selection (absolute byte offsets,
anchor/caret model); insert/backspace/delete replace the selection; the
IME unions its reported range with the active selection; the highlight
is drawn in the TextField and the selection is pushed to the IME.
Android key input (the blocker found during on-device validation):
Gio v0.10 on Android (a) drops modifier state in the JNI bridge and
(b) wraps plain arrow-key presses in input.SystemEvent for focus
navigation, so arrow keys never reached the editor. main.go now
registers explicit named key.Filters for the four arrows (delivers the
press and suppresses the focus jump) and tracks the shift key itself.
Verified on the emulator: plain arrows move the caret, shift+arrow
shows a highlight, typing replaces the selection.
Real-file e2e tests (real on-disk files via the real FileSystem,
multi-chunk 256KB files, chunk-boundary and multi-byte edits) found
and fixed two real bugs:
1. Line index: UpdateLineIndexAfterEdit only shifted offsets; edits
involving newlines left it permanently inconsistent. Replaced with
newline-aware UpdateLineIndexAfterInsert/UpdateLineIndexAfterDelete.
2. Rune granularity: HandleBackspace/HandleDelete deleted one byte,
corrupting multi-byte UTF-8 characters (e.g. a 2-byte char
straddling a chunk boundary). Now rune-granular.
Also: airtight e2e harness load-wait (StatFile/ReadFile/BuildLineIndex
interleaving could satisfy the old condition early).
Full suite green under -race; on-device verified.
This commit is contained in:
+54
-10
@@ -187,6 +187,11 @@ type TextField struct {
|
||||
Focused bool
|
||||
Multiline bool
|
||||
CursorPosition int
|
||||
// SelectionStart/SelectionEnd are byte offsets into Value (the visible
|
||||
// window); -1 means no selection. Used for the IME selection push and the
|
||||
// in-app highlight.
|
||||
SelectionStart int
|
||||
SelectionEnd int
|
||||
ScrollOffset Dp
|
||||
VisibleLines []Line
|
||||
WordWrap bool
|
||||
@@ -214,6 +219,7 @@ func (tf TextField) Draw(gtx layout.Context, r *Renderer) {
|
||||
// the IME starts from a known state.
|
||||
if r.lastIMEField != tf.id || !r.lastWasFocused {
|
||||
r.lastSnippet = key.Snippet{}
|
||||
r.lastSelStart = -1
|
||||
r.lastSelCaret = -1
|
||||
}
|
||||
r.lastIMEField = tf.id
|
||||
@@ -240,14 +246,40 @@ func (tf TextField) Draw(gtx layout.Context, r *Renderer) {
|
||||
r.lastSnippet = snippet
|
||||
gtx.Execute(key.SnippetCmd{Tag: tf.id, Snippet: snippet})
|
||||
}
|
||||
// Item 1: sync the caret so the IME's selection matches. Window-relative
|
||||
// rune index of the caret (tf.CursorPosition is a byte offset in
|
||||
// tf.Value). Push only when it moves, so a static caret does not reset
|
||||
// the IME every frame.
|
||||
caret := runeCount(tf.Value, tf.CursorPosition)
|
||||
if caret != r.lastSelCaret {
|
||||
r.lastSelCaret = caret
|
||||
gtx.Execute(key.SelectionCmd{Tag: tf.id, Range: key.Range{Start: caret, End: caret}, Caret: key.Caret{}})
|
||||
// Item 1: sync the caret/selection so the IME's selection matches.
|
||||
// Window-relative rune indices (tf.CursorPosition and the selection
|
||||
// bounds are byte offsets into tf.Value). With a selection, push the
|
||||
// full range so the IME highlights it and a commit replaces it (the
|
||||
// logic side unions the commit range with the selection, so the
|
||||
// replacement is deterministic regardless of what the IME reports).
|
||||
// Push only when the (start, end) pair changes, so a static selection
|
||||
// does not reset the IME every frame.
|
||||
var selStart, selEnd int
|
||||
if tf.SelectionStart >= 0 && tf.SelectionEnd > tf.SelectionStart {
|
||||
// Clamp to the window (the element may be built from a window that
|
||||
// does not fully contain the selection).
|
||||
s := tf.SelectionStart
|
||||
if s < 0 {
|
||||
s = 0
|
||||
}
|
||||
e := tf.SelectionEnd
|
||||
if e > len(tf.Value) {
|
||||
e = len(tf.Value)
|
||||
}
|
||||
selStart = runeCount(tf.Value, s)
|
||||
selEnd = runeCount(tf.Value, e)
|
||||
} else {
|
||||
selStart = -1
|
||||
selEnd = runeCount(tf.Value, tf.CursorPosition)
|
||||
}
|
||||
if selStart != r.lastSelStart || selEnd != r.lastSelCaret {
|
||||
r.lastSelStart = selStart
|
||||
r.lastSelCaret = selEnd
|
||||
rng := key.Range{Start: selStart, End: selEnd}
|
||||
if selStart < 0 {
|
||||
rng = key.Range{Start: selEnd, End: selEnd}
|
||||
}
|
||||
gtx.Execute(key.SelectionCmd{Tag: tf.id, Range: rng, Caret: key.Caret{}})
|
||||
}
|
||||
} else if r.lastIMEField == tf.id {
|
||||
// This (previously-focused) field lost focus: forget it so the next focus
|
||||
@@ -255,9 +287,10 @@ func (tf TextField) Draw(gtx layout.Context, r *Renderer) {
|
||||
r.lastIMEField = ""
|
||||
r.lastWasFocused = false
|
||||
r.lastSnippet = key.Snippet{}
|
||||
r.lastSelStart = -1
|
||||
r.lastSelCaret = -1
|
||||
}
|
||||
r.drawWrappedText(gtx, tf.Value, tf.region, tf.WrapWidth, tf.ScrollOffset, tf.CursorPosition)
|
||||
r.drawWrappedText(gtx, tf.Value, tf.region, tf.WrapWidth, tf.ScrollOffset, tf.CursorPosition, tf.SelectionStart, tf.SelectionEnd)
|
||||
}
|
||||
|
||||
// runeCount returns the number of UTF-8 runes in s[:bytePos] (bytePos is a
|
||||
@@ -278,7 +311,7 @@ func runeCount(s string, bytePos int) int {
|
||||
}
|
||||
|
||||
// NewTextField creates a visible multiline TextField.
|
||||
func NewTextField(id string, value string, region Region, wrapWidth Dp, scrollOffset Dp, cursorPos int, interactions []Interaction) TextField {
|
||||
func NewTextField(id string, value string, region Region, wrapWidth Dp, scrollOffset Dp, cursorPos int, selStart, selEnd int, interactions []Interaction) TextField {
|
||||
return TextField{
|
||||
id: id,
|
||||
region: region,
|
||||
@@ -290,6 +323,8 @@ func NewTextField(id string, value string, region Region, wrapWidth Dp, scrollOf
|
||||
WrapWidth: wrapWidth,
|
||||
ScrollOffset: scrollOffset,
|
||||
CursorPosition: cursorPos,
|
||||
SelectionStart: selStart,
|
||||
SelectionEnd: selEnd,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -829,6 +864,15 @@ type Interactive interface {
|
||||
}
|
||||
|
||||
// Point defines a 2D coordinate in device-independent pixels (Dp).
|
||||
// KeyEvent is a key press with its modifier state, delivered as the Data of
|
||||
// an InputEvent to a ui.KeyDown handler. It replaces passing a bare key.Name
|
||||
// (which discarded modifier state) so handlers can distinguish e.g.
|
||||
// shift+arrow (extend selection) from plain arrow (move cursor).
|
||||
type KeyEvent struct {
|
||||
Name key.Name
|
||||
Shift bool
|
||||
}
|
||||
|
||||
type Point struct {
|
||||
X, Y Dp
|
||||
}
|
||||
|
||||
+45
-6
@@ -75,7 +75,8 @@ type Renderer struct {
|
||||
lastIMEField string
|
||||
lastWasFocused bool
|
||||
lastSnippet key.Snippet
|
||||
lastSelCaret int // window-relative rune index of last-pushed caret; -1 = none
|
||||
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
|
||||
}
|
||||
|
||||
// New creates a new Renderer.
|
||||
@@ -481,7 +482,7 @@ func (r *Renderer) drawLine(gtx layout.Context, line []text.Glyph, x, y Dp, col
|
||||
// detection via WrapHeuristically. Long words overflow the wrap width.
|
||||
// Line spacing is fixed: LineHeight = fontSize × LineHeightScale, independent
|
||||
// of glyph metrics. The shaper's first.Y accounts for line spacing.
|
||||
func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, wrapWidth Dp, scrollOffset Dp, cursorPos int) {
|
||||
func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, wrapWidth Dp, scrollOffset Dp, cursorPos, selStart, selEnd int) {
|
||||
if str == "" {
|
||||
return
|
||||
}
|
||||
@@ -512,7 +513,10 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
|
||||
y := reg.Y - scrollOffset
|
||||
col := Color{R: 0, G: 0, B: 0, A: 255} // black text
|
||||
|
||||
m := op.Record(gtx.Ops)
|
||||
// Pass 1: collect glyph lines and per-glyph layout data (no drawing yet),
|
||||
// so the selection highlight can be emitted before the text ops and render
|
||||
// underneath it.
|
||||
var lines [][]text.Glyph
|
||||
var glyphs [32]text.Glyph
|
||||
line := glyphs[:0]
|
||||
lineCount := 0
|
||||
@@ -522,6 +526,10 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
|
||||
layout.LineHeight = Dp(float32(lineHeightSp))
|
||||
byteOffset := 0
|
||||
layout.VisualLineStarts = append(layout.VisualLineStarts, byteOffset)
|
||||
flushLine := func() {
|
||||
lines = append(lines, append([]text.Glyph(nil), line...))
|
||||
line = line[:0]
|
||||
}
|
||||
for g, ok := r.shp.NextGlyph(); ok; g, ok = r.shp.NextGlyph() {
|
||||
// Record layout data for this glyph.
|
||||
// g.X is in fixed.Int26_6 — shift >> 6 for device pixels, divide by scale for Dp.
|
||||
@@ -539,8 +547,7 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
|
||||
|
||||
line = append(line, g)
|
||||
if g.Flags&text.FlagLineBreak != 0 || cap(line)-len(line) == 0 {
|
||||
r.drawLine(gtx, line, reg.X, y, col)
|
||||
line = line[:0]
|
||||
flushLine()
|
||||
if g.Flags&text.FlagLineBreak != 0 {
|
||||
lineCount++
|
||||
layout.VisualLineStarts = append(layout.VisualLineStarts, byteOffset)
|
||||
@@ -548,9 +555,41 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
|
||||
}
|
||||
}
|
||||
if len(line) > 0 {
|
||||
r.drawLine(gtx, line, reg.X, y, col)
|
||||
flushLine()
|
||||
lineCount++
|
||||
}
|
||||
|
||||
// Pass 2: selection highlight (translucent blue), one rect per covered
|
||||
// glyph, emitted before the text so glyphs draw on top of it.
|
||||
if selStart >= 0 && selEnd > selStart {
|
||||
for i := range layout.ByteOffsets {
|
||||
b0 := layout.ByteOffsets[i]
|
||||
b1 := len(str)
|
||||
if i+1 < len(layout.ByteOffsets) {
|
||||
b1 = layout.ByteOffsets[i+1]
|
||||
}
|
||||
if b0 >= selEnd || b1 <= selStart {
|
||||
continue
|
||||
}
|
||||
hx := reg.X + layout.X[i]
|
||||
hy := reg.Y - scrollOffset + layout.Y[i] - Dp(r.theme.FontSize)
|
||||
hw := layout.Advance[i]
|
||||
hh := Dp(r.theme.FontSize) * 1.2
|
||||
rect := clip.Rect{
|
||||
Min: image.Point{X: int(r.toPx(hx)), Y: int(r.toPx(hy))},
|
||||
Max: image.Point{X: int(r.toPx(hx + hw)), Y: int(r.toPx(hy + hh))},
|
||||
}.Op().Push(gtx.Ops)
|
||||
paint.ColorOp{Color: color.NRGBA{R: 0x33, G: 0x99, B: 0xFF, A: 0x59}}.Add(gtx.Ops)
|
||||
paint.PaintOp{}.Add(gtx.Ops)
|
||||
rect.Pop()
|
||||
}
|
||||
}
|
||||
|
||||
// Pass 3: the text itself.
|
||||
m := op.Record(gtx.Ops)
|
||||
for _, ln := range lines {
|
||||
r.drawLine(gtx, ln, reg.X, y, col)
|
||||
}
|
||||
call := m.Stop()
|
||||
call.Add(gtx.Ops)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user