internal/ui: reland: 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 9d73e45677
commit 6ab509f221
4 changed files with 57 additions and 16 deletions

View File

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

View File

@ -194,6 +194,18 @@ func (w *Window) SetCloseCallback(cbfun CloseCallback) (previous CloseCallback)
return f 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)
}
}
func (w *Window) SetDropCallback(cbfun DropCallback) (previous DropCallback) { func (w *Window) SetDropCallback(cbfun DropCallback) (previous DropCallback) {
f, err := (*goglfw.Window)(w).SetDropCallback(cbfun) f, err := (*goglfw.Window)(w).SetDropCallback(cbfun)
if err != nil { if err != nil {
@ -202,12 +214,6 @@ func (w *Window) SetDropCallback(cbfun DropCallback) (previous DropCallback) {
return f 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) { func (w *Window) SetFramebufferSizeCallback(cbfun FramebufferSizeCallback) (previous FramebufferSizeCallback) {
f, err := (*goglfw.Window)(w).SetFramebufferSizeCallback(cbfun) f, err := (*goglfw.Window)(w).SetFramebufferSizeCallback(cbfun)
if err != nil { if err != nil {

View File

@ -66,13 +66,27 @@ func (u *userInterfaceImpl) updateInputStateImpl() error {
for gb, ub := range glfwMouseButtonToMouseButton { for gb, ub := range glfwMouseButtonToMouseButton {
u.inputState.MouseButtonPressed[ub] = u.window.GetMouseButton(gb) == glfw.Press 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() m := u.currentMonitor()
s := u.deviceScaleFactor(m) s := u.deviceScaleFactor(m)
cx = u.dipFromGLFWPixel(cx, m)
cy = u.dipFromGLFWPixel(cy, m) cx, cy := u.savedCursorX, u.savedCursorY
cx, cy = u.context.clientPositionToLogicalPosition(cx, cy, s) 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. // AdjustPosition can return NaN at the initialization.
if !math.IsNaN(cx) && !math.IsNaN(cy) { if !math.IsNaN(cx) && !math.IsNaN(cy) {
@ -108,3 +122,11 @@ func (u *userInterfaceImpl) keyName(key Key) string {
}) })
return name 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" "errors"
"fmt" "fmt"
"image" "image"
"math"
"os" "os"
"runtime" "runtime"
"sync" "sync"
@ -104,8 +105,10 @@ type userInterfaceImpl struct {
fpsModeInited bool fpsModeInited bool
inputState InputState inputState InputState
iwindow glfwWindow iwindow glfwWindow
savedCursorX float64
savedCursorY float64
sizeCallback glfw.SizeCallback sizeCallback glfw.SizeCallback
closeCallback glfw.CloseCallback closeCallback glfw.CloseCallback
@ -145,6 +148,8 @@ func init() {
fpsMode: FPSModeVsyncOn, fpsMode: FPSModeVsyncOn,
origWindowPosX: invalidPos, origWindowPosX: invalidPos,
origWindowPosY: invalidPos, origWindowPosY: invalidPos,
savedCursorX: math.NaN(),
savedCursorY: math.NaN(),
} }
theUI.iwindow.ui = &theUI.userInterfaceImpl theUI.iwindow.ui = &theUI.userInterfaceImpl
} }
@ -1403,6 +1408,10 @@ func (u *userInterfaceImpl) setFullscreen(fullscreen bool) {
return return
} }
if u.window.GetInputMode(glfw.CursorMode) == glfw.CursorDisabled {
u.saveCursorPosition()
}
// Enter the fullscreen. // Enter the fullscreen.
if fullscreen { if fullscreen {
u.disableWindowSizeLimits() u.disableWindowSizeLimits()