From 5c7bfd3ed74a492f73be2a3a0e243c8ff1eaaaa5 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 3 Feb 2024 22:16:16 +0900 Subject: [PATCH] exp/textinput, examples/textinput: bug fix: handle input states correctly on Android Chrome Updates #2898 --- examples/textinput/main.go | 15 +++++++++++++++ exp/textinput/textinput_js.go | 14 +++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/examples/textinput/main.go b/examples/textinput/main.go index de7056aa9..5e43048b7 100644 --- a/examples/textinput/main.go +++ b/examples/textinput/main.go @@ -138,6 +138,21 @@ func (t *TextField) Blur() { func (t *TextField) Update() { if !t.focused { + // If the text field still has a session, read the last state and process it just in case. + if t.ch != nil { + select { + case state, ok := <-t.ch: + if ok && state.Committed { + t.text = t.text[:t.selectionStart] + state.Text + t.text[t.selectionEnd:] + t.selectionStart += len(state.Text) + t.selectionEnd = t.selectionStart + t.state = textinput.State{} + } + t.state = state + default: + break + } + } if t.end != nil { t.end() t.ch = nil diff --git a/exp/textinput/textinput_js.go b/exp/textinput/textinput_js.go index a116985ea..e1141f2d5 100644 --- a/exp/textinput/textinput_js.go +++ b/exp/textinput/textinput_js.go @@ -91,7 +91,19 @@ func (t *textInput) init() { })) t.textareaElement.Call("addEventListener", "input", js.FuncOf(func(this js.Value, args []js.Value) any { e := args[0] - t.trySend(!e.Get("isComposing").Bool()) + if e.Get("isComposing").Bool() { + t.trySend(false) + return nil + } + if e.Get("inputType").String() == "insertLineBreak" { + t.trySend(true) + return nil + } + t.trySend(false) + return nil + })) + t.textareaElement.Call("addEventListener", "change", js.FuncOf(func(this js.Value, args []js.Value) any { + t.trySend(true) return nil })) // TODO: What about other events like wheel?