internal/ui: bug fix: preserve cursor positions for toggling fullscreening for desktops

Updates #2475
This commit is contained in:
Hajime Hoshi 2023-09-17 04:37:22 +09:00
parent 7becaa19e6
commit 7ed4db90be
4 changed files with 58 additions and 16 deletions

View File

@ -178,6 +178,11 @@ func (w *Window) SetCharModsCallback(cbfun CharModsCallback) (previous CharModsC
return ToCharModsCallback(nil) // TODO
}
func (w *Window) SetCloseCallback(cbfun CloseCallback) (previous CloseCallback) {
w.w.SetCloseCallback(cbfun)
return ToCloseCallback(nil) // TODO
}
func (w *Window) SetCursor(cursor *Cursor) {
var c *cglfw.Cursor
if cursor != nil {
@ -186,9 +191,8 @@ func (w *Window) SetCursor(cursor *Cursor) {
w.w.SetCursor(c)
}
func (w *Window) SetCloseCallback(cbfun CloseCallback) (previous CloseCallback) {
w.w.SetCloseCallback(cbfun)
return ToCloseCallback(nil) // TODO
func (w *Window) SetCursorPos(xpos, ypos float64) {
w.w.SetCursorPos(xpos, ypos)
}
func (w *Window) SetDropCallback(cbfun DropCallback) (previous DropCallback) {

View File

@ -194,6 +194,19 @@ func (w *Window) SetCloseCallback(cbfun CloseCallback) (previous CloseCallback)
return f
}
func (w *Window) SetCursor(cursor *Cursor) {
if err := (*goglfw.Window)(w).SetCursor((*goglfw.Cursor)(cursor)); err != nil {
panic(err)
}
}
func (w *Window) SetCursorPos(xpos, ypos float64) {
if err := (*goglfw.Window)(w).SetCursorPos(xpos, ypos); err != nil {
panic(err)
}
return x, y
}
func (w *Window) SetDropCallback(cbfun DropCallback) (previous DropCallback) {
f, err := (*goglfw.Window)(w).SetDropCallback(cbfun)
if err != nil {
@ -202,12 +215,6 @@ func (w *Window) SetDropCallback(cbfun DropCallback) (previous DropCallback) {
return f
}
func (w *Window) SetCursor(cursor *Cursor) {
if err := (*goglfw.Window)(w).SetCursor((*goglfw.Cursor)(cursor)); err != nil {
panic(err)
}
}
func (w *Window) SetFramebufferSizeCallback(cbfun FramebufferSizeCallback) (previous FramebufferSizeCallback) {
f, err := (*goglfw.Window)(w).SetFramebufferSizeCallback(cbfun)
if err != nil {

View File

@ -66,13 +66,27 @@ func (u *userInterfaceImpl) updateInputStateImpl() error {
for gb, ub := range glfwMouseButtonToMouseButton {
u.inputState.MouseButtonPressed[ub] = u.window.GetMouseButton(gb) == glfw.Press
}
cx, cy := u.window.GetCursorPos()
// TODO: This is tricky. Rename the function?
m := u.currentMonitor()
s := u.deviceScaleFactor(m)
cx = u.dipFromGLFWPixel(cx, m)
cy = u.dipFromGLFWPixel(cy, m)
cx, cy = u.context.clientPositionToLogicalPosition(cx, cy, s)
cx, cy := u.savedCursorX, u.savedCursorY
defer func() {
u.savedCursorX = math.NaN()
u.savedCursorY = math.NaN()
}()
if !math.IsNaN(cx) && !math.IsNaN(cy) {
cx2, cy2 := u.context.logicalPositionToClientPosition(cx, cy, s)
cx2 = u.dipToGLFWPixel(cx2, m)
cy2 = u.dipToGLFWPixel(cy2, m)
u.window.SetCursorPos(cx2, cy2)
} else {
cx2, cy2 := u.window.GetCursorPos()
cx2 = u.dipFromGLFWPixel(cx2, m)
cy2 = u.dipFromGLFWPixel(cy2, m)
cx, cy = u.context.clientPositionToLogicalPosition(cx2, cy2, s)
}
// AdjustPosition can return NaN at the initialization.
if !math.IsNaN(cx) && !math.IsNaN(cy) {
@ -108,3 +122,11 @@ func (u *userInterfaceImpl) keyName(key Key) string {
})
return name
}
func (u *userInterfaceImpl) saveCursorPosition() {
u.m.Lock()
defer u.m.Unlock()
u.savedCursorX = u.inputState.CursorX
u.savedCursorY = u.inputState.CursorY
}

View File

@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"image"
"math"
"os"
"runtime"
"sync"
@ -104,8 +105,10 @@ type userInterfaceImpl struct {
fpsModeInited bool
inputState InputState
iwindow glfwWindow
inputState InputState
iwindow glfwWindow
savedCursorX float64
savedCursorY float64
sizeCallback glfw.SizeCallback
closeCallback glfw.CloseCallback
@ -145,6 +148,8 @@ func init() {
fpsMode: FPSModeVsyncOn,
origWindowPosX: invalidPos,
origWindowPosY: invalidPos,
savedCursorX: math.NaN(),
savedCursorY: math.NaN(),
}
theUI.iwindow.ui = &theUI.userInterfaceImpl
}
@ -1403,6 +1408,10 @@ func (u *userInterfaceImpl) setFullscreen(fullscreen bool) {
return
}
if u.window.GetInputMode(glfw.CursorMode) == glfw.CursorDisabled {
u.saveCursorPosition()
}
// Enter the fullscreen.
if fullscreen {
u.disableWindowSizeLimits()