editor+ui: complete Android IME wiring (SelectionCmd, SnippetCmd, InputHintOp)
Finish Phase 1 items 1, 2, 4, on top of the range-handling done in 9b78219:
- TextField.Draw now emits, when focused:
* key.InputHintOp{HintText} (item 4: enable text keyboard/autocorrect)
* key.SnippetCmd (the visible window as the snippet, Range {0,len})
(item 2: swipe/autocorrect source)
* key.SelectionCmd (caret, window-relative rune index)
(item 1: IME selection sync)
The snippet is the visible window (not the whole file), so the IME treats
the window as the document and reports EditEvent.Range window-relative.
- HandleReplaceRange now resolves the window-relative range against
IMEWindowText and offsets by IMEWindowStartByte to address the buffer
(string and chunked paths). Falls back to the whole buffer when layout
has not set the window (tests).
- EditorState gains IMEWindowStartByte / IMEWindowText, set during layout.
- Add runeCount helper (utf8 leading-byte scan) in the ui package.
- Fix a data race in three editor e2e/integration tests: they called
OpenFile from the test goroutine while the logic goroutine ran layout;
now wrapped in withState so the state write happens on the owner.
Tests: ime_range_test.go gains windowed-path coverage (string+chunked).
go build, go test, go vet, and go test -race are all green.
This commit is contained in:
@@ -212,10 +212,44 @@ func (tf TextField) Draw(gtx layout.Context, r *Renderer) {
|
||||
fmt.Printf("Focused: tag = %s\n", tf.id)
|
||||
gtx.Execute(key.FocusCmd{Tag: tf.id})
|
||||
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
|
||||
// operate on the visible text without shipping the whole file to the IME.
|
||||
//
|
||||
// Item 4: tell the IME this is a text field (enables the text keyboard,
|
||||
// autocorrect, and suggestions).
|
||||
key.InputHintOp{Tag: tf.id, Hint: key.HintText}.Add(gtx.Ops)
|
||||
// Item 2: push the snippet (the visible window) for swipe/autocorrect.
|
||||
gtx.Execute(key.SnippetCmd{Tag: tf.id, Snippet: key.Snippet{
|
||||
Range: key.Range{Start: 0, End: runeCount(tf.Value, len(tf.Value))},
|
||||
Text: tf.Value,
|
||||
}})
|
||||
// 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).
|
||||
caret := runeCount(tf.Value, tf.CursorPosition)
|
||||
gtx.Execute(key.SelectionCmd{Tag: tf.id, Range: key.Range{Start: caret, End: caret}, Caret: key.Caret{}})
|
||||
}
|
||||
r.drawWrappedText(gtx, tf.Value, tf.region, tf.WrapWidth, tf.ScrollOffset, tf.CursorPosition)
|
||||
}
|
||||
|
||||
// runeCount returns the number of UTF-8 runes in s[:bytePos] (bytePos is a
|
||||
// byte offset, clamped to len(s)). A rune starts at an ASCII byte (<0x80) or a
|
||||
// multi-byte lead byte (>=0xC0); 0x80-0xBF are continuation bytes.
|
||||
func runeCount(s string, bytePos int) int {
|
||||
if bytePos > len(s) {
|
||||
bytePos = len(s)
|
||||
}
|
||||
n := 0
|
||||
for i := 0; i < bytePos; i++ {
|
||||
b := s[i]
|
||||
if b < 0x80 || b >= 0xC0 {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// NewTextField creates a visible multiline TextField.
|
||||
func NewTextField(id string, value string, region Region, wrapWidth Dp, scrollOffset Dp, cursorPos int, interactions []Interaction) TextField {
|
||||
return TextField{
|
||||
|
||||
Reference in New Issue
Block a user