Revert "internal/ui: refactoring: remove unnecessary hack for unfocusing"

This reverts commit 3f753b7086.

Reason: this was actually necessary.

Updates #982
Updates #987
Closes #2521
This commit is contained in:
Hajime Hoshi 2023-01-03 02:05:40 +09:00
parent 70bcec189d
commit da97657afb

View File

@ -1015,6 +1015,21 @@ func (u *userInterfaceImpl) loopGame() error {
}
func (u *userInterfaceImpl) updateGame() error {
var unfocused bool
// On Windows, the focusing state might be always false (#987).
// On Windows, even if a window is in another workspace, vsync seems to work.
// Then let's assume the window is always 'focused' as a workaround.
if runtime.GOOS != "windows" {
unfocused = u.window.GetAttrib(glfw.Focused) == glfw.False
}
var t1, t2 time.Time
if unfocused {
t1 = time.Now()
}
var outsideWidth, outsideHeight float64
var deviceScaleFactor float64
var err error
@ -1046,6 +1061,20 @@ func (u *userInterfaceImpl) updateGame() error {
u.swapBuffersOnRenderThread()
})
if unfocused {
t2 = time.Now()
}
// When a window is not focused, SwapBuffers might return immediately and CPU might be busy.
// Mitigate this by sleeping (#982).
if unfocused {
d := t2.Sub(t1)
const wait = time.Second / 60
if d < wait {
time.Sleep(wait - d)
}
}
return nil
}