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:
+40
-2
@@ -66,6 +66,13 @@ func run(w *app.Window) error {
|
||||
var perfOn bool
|
||||
var presentN int
|
||||
var presentStart time.Time
|
||||
// shiftDown tracks the hardware shift key on the MAIN goroutine.
|
||||
// On Android Gio's JNI bridge drops modifier state (GioView.onKeyEvent
|
||||
// never reads event.getMetaState), so key.Event.Modifiers is always 0 and
|
||||
// shift+arrow is indistinguishable from a plain arrow. Gio does deliver
|
||||
// the NameShift press/release as plain key.Events, so we track them here
|
||||
// and attach the state to the ui.KeyEvent we forward.
|
||||
var shiftDown bool
|
||||
if _, err := os.Stat(filepath.Join(perfDir, "enable")); err == nil {
|
||||
prof = perf.New(true, perfDir, "logic_frames.csv")
|
||||
editor.PerfRecord = func(rec editor.ProbeRecord) {
|
||||
@@ -144,24 +151,55 @@ func run(w *app.Window) error {
|
||||
|
||||
// Gather key events
|
||||
focusedID := frame.FocusedElementID
|
||||
if focusedID == "" {
|
||||
// No focused input: drop any stale shift state (a release event
|
||||
// for a key held across focus loss may never arrive).
|
||||
shiftDown = false
|
||||
}
|
||||
if focusedID != "" {
|
||||
if reg, ok := renderer.Keys[focusedID]; ok {
|
||||
// Use key.Filter to only receive events destined for the focused element.
|
||||
// We need both Key events (for arrow keys) and Edit events (for text input).
|
||||
// In Gio, key.Filter covers key presses, while key.FocusFilter covers
|
||||
// focus and text edit events.
|
||||
//
|
||||
// On mobile the window layer wraps plain arrow-key PRESSES in
|
||||
// input.SystemEvent (it wants to use them for focus navigation) and
|
||||
// such events match only filters that name the key explicitly. Querying
|
||||
// the four arrow names below therefore (a) makes the presses deliverable
|
||||
// and (b) suppresses the focus-move side effect: a matched event makes
|
||||
// WakeupTime report handled, which skips the window's moveFocus call.
|
||||
for {
|
||||
// Filter for key events and focus/edit events targeted at focusedID
|
||||
evt, ok := gtx.Event(key.Filter{Focus: focusedID}, key.FocusFilter{Target: focusedID})
|
||||
evt, ok := gtx.Event(
|
||||
key.Filter{Focus: focusedID},
|
||||
key.Filter{Focus: focusedID, Name: key.NameLeftArrow},
|
||||
key.Filter{Focus: focusedID, Name: key.NameRightArrow},
|
||||
key.Filter{Focus: focusedID, Name: key.NameUpArrow},
|
||||
key.Filter{Focus: focusedID, Name: key.NameDownArrow},
|
||||
key.FocusFilter{Target: focusedID},
|
||||
)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
switch k := evt.(type) {
|
||||
case key.Event:
|
||||
if k.Name == key.NameShift {
|
||||
// Track shift; never forward it as a content key.
|
||||
shiftDown = k.State == key.Press
|
||||
continue
|
||||
}
|
||||
if k.State == key.Press {
|
||||
// ui.KeyEvent carries modifier state so handlers
|
||||
// can distinguish shift+arrow (extend selection)
|
||||
// from plain arrow (move cursor). On Android the
|
||||
// Modifiers field is always empty, hence the shiftDown OR.
|
||||
events = append(events, ui.InputEvent{
|
||||
Handler: reg.Handler,
|
||||
Data: k.Name,
|
||||
Data: ui.KeyEvent{
|
||||
Name: k.Name,
|
||||
Shift: k.Modifiers.Contain(key.ModShift) || shiftDown,
|
||||
},
|
||||
})
|
||||
}
|
||||
case key.EditEvent:
|
||||
|
||||
Reference in New Issue
Block a user