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.
func (u *UserInterface) deviceScaleFactor() float64 {
// Avoid calling monitor.GetPos if we have the monitor position cached already.
if cm, ok := getCachedMonitor(u.window.GetPos()); ok {
return devicescale.GetAt(cm.x, cm.y)
// 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.
if cm, ok := getCachedMonitor(u.window.GetPos()); ok {
return devicescale.GetAt(cm.x, cm.y)
}
}
// TODO: When is this reached?
return devicescale.GetAt(u.currentMonitor().GetPos())
}

View File

@ -114,7 +114,7 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) {
if err != nil {
panic(err)
}
if y < my + t {
if y < my+t {
y = my + t
}
return x, y

View File

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