uidriver/glfw: Avoid using the window position before initializing

Especially in the initial phase before calling Run/RunGame, the
window position is not reliable and then getting the device scale
factor does not make sense based on the window position. Avoid
using the window position, and instead use the glfw.Monitor in
this situation.
This commit is contained in:
Hajime Hoshi 2020-03-29 00:09:08 +09:00
parent 14200eb42c
commit b7ab3d2df4
3 changed files with 12 additions and 6 deletions

View File

@ -536,11 +536,13 @@ func (u *UserInterface) DeviceScaleFactor() float64 {
// deviceScaleFactor must be called from the main thread. // deviceScaleFactor must be called from the main thread.
func (u *UserInterface) deviceScaleFactor() float64 { func (u *UserInterface) deviceScaleFactor() float64 {
// Before calling SetWindowPosition, the window's positin is not reliable.
if u.iwindow.setPositionCalled {
// Avoid calling monitor.GetPos if we have the monitor position cached already. // Avoid calling monitor.GetPos if we have the monitor position cached already.
if cm, ok := getCachedMonitor(u.window.GetPos()); ok { if cm, ok := getCachedMonitor(u.window.GetPos()); ok {
return devicescale.GetAt(cm.x, cm.y) return devicescale.GetAt(cm.x, cm.y)
} }
// TODO: When is this reached? }
return devicescale.GetAt(u.currentMonitor().GetPos()) return devicescale.GetAt(u.currentMonitor().GetPos())
} }

View File

@ -27,6 +27,7 @@ import (
type window struct { type window struct {
ui *UserInterface ui *UserInterface
setPositionCalled bool
} }
func (w *window) IsDecorated() bool { func (w *window) IsDecorated() bool {
@ -205,6 +206,9 @@ func (w *window) SetPosition(x, y int) {
return return
} }
_ = w.ui.t.Call(func() error { _ = w.ui.t.Call(func() error {
defer func() {
w.setPositionCalled = true
}()
mx, my := w.ui.currentMonitor().GetPos() mx, my := w.ui.currentMonitor().GetPos()
xf := w.ui.toDeviceDependentPixel(float64(x)) xf := w.ui.toDeviceDependentPixel(float64(x))
yf := w.ui.toDeviceDependentPixel(float64(y)) yf := w.ui.toDeviceDependentPixel(float64(y))