Compare commits

...

2 Commits

Author SHA1 Message Date
Hajime Hoshi
ec9b0882ab .github/workflow: disable wasm tests on Windows temporarily
Updates #2982
2024-06-12 01:58:28 +09:00
Hajime Hoshi
b466ba65b7 exp/textinput: bug fix: issues on browsers
This change addresses on these issues on browsers:

* Pressing cursor keys didn't work well.
  This is fixed by calling trySend with true on desktops (not mobiles).
* Inputting failed just after moving a cursor.
  This is fixed by resetting a session correctly.
* Sometimes pressing the enter key inserted a new line unexpectedly.
  This is fixed by calling `preventDefault`.

Closes #3014
2024-06-12 01:31:52 +09:00
3 changed files with 42 additions and 6 deletions

View File

@ -167,9 +167,10 @@ jobs:
env GOARCH=386 EBITENGINE_DIRECTX=version=12 go test -shuffle=on -v ./... env GOARCH=386 EBITENGINE_DIRECTX=version=12 go test -shuffle=on -v ./...
- name: go test (Wasm) - name: go test (Wasm)
if: runner.os != 'macOS' if: ${{ runner.os != 'macOS' && runner.os != 'Windows' }}
run: | run: |
# Wasm tests don't work on macOS with the headless mode, and the headless mode doesn't work in GitHub Actions (#2972). # Wasm tests don't work on macOS with the headless mode, and the headless mode doesn't work in GitHub Actions (#2972).
# Wasm tests don't work on Windows well due to mysterious timeouts (#2982).
env GOOS=js GOARCH=wasm cleanenv -remove-prefix GITHUB_ -remove-prefix JAVA_ -remove-prefix PSModulePath -remove-prefix STATS_ -remove-prefix RUNNER_ -- go test -shuffle=on -v ./... env GOOS=js GOARCH=wasm cleanenv -remove-prefix GITHUB_ -remove-prefix JAVA_ -remove-prefix PSModulePath -remove-prefix STATS_ -remove-prefix RUNNER_ -- go test -shuffle=on -v ./...
- name: Install ebitenmobile - name: Install ebitenmobile

View File

@ -106,7 +106,7 @@ func (f *Field) HandleInput(x, y int) (handled bool, err error) {
f.ch, f.end = Start(x, y) f.ch, f.end = Start(x, y)
// Start returns nil for non-supported envrionments. // Start returns nil for non-supported envrionments.
if f.ch == nil { if f.ch == nil {
return true, nil return false, nil
} }
} }

View File

@ -16,6 +16,7 @@ package textinput
import ( import (
"fmt" "fmt"
"strings"
"syscall/js" "syscall/js"
"github.com/hajimehoshi/ebiten/v2/internal/ui" "github.com/hajimehoshi/ebiten/v2/internal/ui"
@ -79,7 +80,10 @@ func (t *textInput) init() {
e.Call("preventDefault") e.Call("preventDefault")
} }
if e.Get("code").String() == "Enter" || e.Get("key").String() == "Enter" { if e.Get("code").String() == "Enter" || e.Get("key").String() == "Enter" {
// Ignore Enter key to avoid ebiten.IsKeyPressed(ebiten.KeyEnter) unexpectedly becomes true, especially for iOS Safari. // Ignore Enter key to avoid ebiten.IsKeyPressed(ebiten.KeyEnter) unexpectedly becomes true.
e.Call("preventDefault")
ui.Get().UpdateInputFromEvent(e)
t.trySend(true)
return nil return nil
} }
if !e.Get("isComposing").Bool() { if !e.Get("isComposing").Bool() {
@ -96,6 +100,11 @@ func (t *textInput) init() {
})) }))
t.textareaElement.Call("addEventListener", "input", js.FuncOf(func(this js.Value, args []js.Value) any { t.textareaElement.Call("addEventListener", "input", js.FuncOf(func(this js.Value, args []js.Value) any {
e := args[0] e := args[0]
// On iOS Safari, `isComposing` can be undefined.
if e.Get("isComposing").IsUndefined() {
t.trySend(false)
return nil
}
if e.Get("isComposing").Bool() { if e.Get("isComposing").Bool() {
t.trySend(false) t.trySend(false)
return nil return nil
@ -109,9 +118,13 @@ func (t *textInput) init() {
t.trySend(true) t.trySend(true)
return nil return nil
} }
// Though `isComposing` is false, send the text as being not committed for text completion on mobile browsers. // Though `isComposing` is false, send the text as being not committed for text completion with a virtual keyboard.
if isVirtualKeyboard() {
t.trySend(false) t.trySend(false)
return nil return nil
}
t.trySend(true)
return nil
})) }))
t.textareaElement.Call("addEventListener", "change", js.FuncOf(func(this js.Value, args []js.Value) any { t.textareaElement.Call("addEventListener", "change", js.FuncOf(func(this js.Value, args []js.Value) any {
t.trySend(true) t.trySend(true)
@ -172,7 +185,13 @@ func (t *textInput) Start(x, y int) (chan State, func()) {
s := newSession() s := newSession()
t.session = s t.session = s
} }
return t.session.ch, t.session.end return t.session.ch, func() {
if t.session != nil {
t.session.end()
// Reset the session explictly, or a new session cannot be created above.
t.session = nil
}
}
} }
if t.session != nil { if t.session != nil {
@ -193,6 +212,10 @@ func (t *textInput) trySend(committed bool) {
} }
textareaValue := t.textareaElement.Get("value").String() textareaValue := t.textareaElement.Get("value").String()
if textareaValue == "" {
return
}
start := t.textareaElement.Get("selectionStart").Int() start := t.textareaElement.Get("selectionStart").Int()
end := t.textareaElement.Get("selectionEnd").Int() end := t.textareaElement.Get("selectionEnd").Int()
startInBytes := convertUTF16CountToByteCount(textareaValue, start) startInBytes := convertUTF16CountToByteCount(textareaValue, start)
@ -213,3 +236,15 @@ func (t *textInput) trySend(committed bool) {
t.textareaElement.Set("value", "") t.textareaElement.Set("value", "")
} }
} }
func isVirtualKeyboard() bool {
// Detect a virtual keyboard by the user agent.
// Note that this is not a correct way to detect a virtual keyboard.
// In the future, we should use the `navigator.virtualKeyboard` API.
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/virtualKeyboard
ua := js.Global().Get("navigator").Get("userAgent").String()
if strings.Contains(ua, "Android") || strings.Contains(ua, "iPhone") || strings.Contains(ua, "iPad") || strings.Contains(ua, "iPod") {
return true
}
return false
}