mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2026-04-23 00:07:15 +08:00
db24fb7c6b
The session's state queue buffers input events that arrive between a commit-end and the next session start, which is needed when multiple keys are pressed simultaneously (#3382). However, when a text field was unfocused, keystroke events continued to be queued by the platform layer and were replayed when a field was next focused, causing unexpected text to appear. Clear the queued states in Field.cleanUp(), which is called when a field is blurred. This preserves the queue for simultaneous keypresses within a focused field while discarding stale input from unfocused periods. Also refactor textInput to move the session field and theTextInput variable to the cross-platform textinput.go, with each platform defining only a textInputImpl with platform-specific fields. Updates #3382 Closes #3429 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
// Copyright 2024 The Ebitengine Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
//go:build (!darwin && !js && !windows) || ios
|
|
|
|
package textinput
|
|
|
|
import (
|
|
"image"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
)
|
|
|
|
type textInputImpl struct {
|
|
rs []rune
|
|
lastTick int64
|
|
}
|
|
|
|
func (t *textInput) Start(bounds image.Rectangle) (<-chan textInputState, func()) {
|
|
// AppendInputChars is updated only when the tick is updated.
|
|
// If the tick is not updated, return nil immediately.
|
|
tick := ebiten.Tick()
|
|
if t.lastTick == tick {
|
|
return nil, nil
|
|
}
|
|
defer func() {
|
|
t.lastTick = tick
|
|
}()
|
|
|
|
ch, _ := t.session.start()
|
|
|
|
// This is a pseudo implementation with AppendInputChars without IME.
|
|
// This is tentative and should be replaced with IME in the future.
|
|
t.rs = ebiten.AppendInputChars(t.rs[:0])
|
|
if len(t.rs) == 0 {
|
|
return nil, nil
|
|
}
|
|
t.session.send(textInputState{
|
|
Text: string(t.rs),
|
|
Committed: true,
|
|
})
|
|
t.session.end()
|
|
return ch, func() {}
|
|
}
|