internal/ui: update mouse and touch cursors after the layout is determined on browsers

Updates #2763
This commit is contained in:
Hajime Hoshi 2023-09-17 16:17:01 +09:00
parent 6ab509f221
commit a5aa721bda
2 changed files with 40 additions and 19 deletions

View File

@ -31,6 +31,12 @@ var (
stringTouchmove = js.ValueOf("touchmove") stringTouchmove = js.ValueOf("touchmove")
) )
type touchInClient struct {
id TouchID
x float64
y float64
}
func jsKeyToID(key js.Value) Key { func jsKeyToID(key js.Value) Key {
// js.Value cannot be used as a map key. // js.Value cannot be used as a map key.
// As the number of keys is around 100, just a dumb loop should work. // As the number of keys is around 100, just a dumb loop should work.
@ -112,35 +118,34 @@ func (u *userInterfaceImpl) setMouseCursorFromEvent(e js.Value) {
return return
} }
s := u.DeviceScaleFactor() u.origCursorXInClient = e.Get("clientX").Float()
x, y := u.context.clientPositionToLogicalPosition(e.Get("clientX").Float(), e.Get("clientY").Float(), s) u.origCursorYInClient = e.Get("clientY").Float()
u.origCursorX, u.origCursorY = x, y
if u.cursorMode == CursorModeCaptured { if u.cursorMode == CursorModeCaptured {
dx, dy := e.Get("movementX").Float()/s, e.Get("movementY").Float()/s u.cursorXInClient += e.Get("movementX").Float()
u.inputState.CursorX += dx u.cursorYInClient += e.Get("movementY").Float()
u.inputState.CursorY += dy
return return
} }
u.inputState.CursorX, u.inputState.CursorY = x, y u.cursorXInClient = u.origCursorXInClient
u.cursorYInClient = u.origCursorYInClient
} }
func (u *userInterfaceImpl) recoverCursorPosition() { func (u *userInterfaceImpl) recoverCursorPosition() {
u.inputState.CursorX, u.inputState.CursorY = u.origCursorX, u.origCursorY u.cursorXInClient = u.origCursorXInClient
u.cursorYInClient = u.origCursorYInClient
} }
func (u *userInterfaceImpl) updateTouchesFromEvent(e js.Value) { func (u *userInterfaceImpl) updateTouchesFromEvent(e js.Value) {
u.inputState.Touches = u.inputState.Touches[:0] u.touchesInClient = u.touchesInClient[:0]
touches := e.Get("targetTouches") touches := e.Get("targetTouches")
for i := 0; i < touches.Length(); i++ { for i := 0; i < touches.Length(); i++ {
t := touches.Call("item", i) t := touches.Call("item", i)
x, y := u.context.clientPositionToLogicalPosition(t.Get("clientX").Float(), t.Get("clientY").Float(), u.DeviceScaleFactor()) u.touchesInClient = append(u.touchesInClient, touchInClient{
u.inputState.Touches = append(u.inputState.Touches, Touch{ id: TouchID(t.Get("identifier").Int()),
ID: TouchID(t.Get("identifier").Int()), x: t.Get("clientX").Float(),
X: int(x), y: t.Get("clientY").Float(),
Y: int(y),
}) })
} }
} }
@ -226,6 +231,19 @@ func UpdateInputFromEvent(e js.Value) {
} }
func (u *userInterfaceImpl) updateInputState() error { func (u *userInterfaceImpl) updateInputState() error {
// TODO: Adjust cursor and touch positions based on the latest layout (#2763). s := u.DeviceScaleFactor()
cx, cy := u.context.clientPositionToLogicalPosition(u.cursorXInClient, u.cursorYInClient, s)
u.inputState.CursorX, u.inputState.CursorY = cx, cy
u.inputState.Touches = u.inputState.Touches[:0]
for _, t := range u.touchesInClient {
x, y := u.context.clientPositionToLogicalPosition(t.x, t.y, s)
u.inputState.Touches = append(u.inputState.Touches, Touch{
ID: t.id,
X: int(x),
Y: int(y),
})
}
return nil return nil
} }

View File

@ -98,10 +98,13 @@ type userInterfaceImpl struct {
err error err error
context *context context *context
inputState InputState inputState InputState
origCursorX float64 cursorXInClient float64
origCursorY float64 cursorYInClient float64
origCursorXInClient float64
origCursorYInClient float64
touchesInClient []touchInClient
keyboardLayoutMap js.Value keyboardLayoutMap js.Value