ui: Recalc device scale repeatedly for multiple displays (#644)

This commit is contained in:
Hajime Hoshi 2018-08-05 21:19:22 +09:00
parent 26731f1ee2
commit bf850e12a4
2 changed files with 19 additions and 9 deletions

View File

@ -19,15 +19,12 @@ import (
)
var (
scale = 0.0
m sync.Mutex
m sync.Mutex
)
func DeviceScale() float64 {
m.Lock()
defer m.Unlock()
if scale == 0.0 {
scale = impl()
}
return scale
// TODO: Cache this value (again) for mobile platforms.
return impl()
}

View File

@ -51,6 +51,10 @@ type userInterface struct {
runnableInBackground bool
vsync bool
deviceScale float64
deviceScaleUpdated int64
lastActualScale float64
initFullscreen bool
initCursorVisible bool
initWindowDecorated bool
@ -540,7 +544,13 @@ func (u *userInterface) getScale() float64 {
// actualScreenScale must be called from the main thread.
func (u *userInterface) actualScreenScale() float64 {
return u.getScale() * devicescale.DeviceScale()
n := time.Now().UnixNano()
// As devicescale.DeviceScale accesses OS API, not call this too often.
if u.deviceScale == 0 || n-u.deviceScaleUpdated < int64(time.Second/2) {
u.deviceScale = devicescale.DeviceScale()
u.deviceScaleUpdated = n
}
return u.getScale() * u.deviceScale
}
// pollEvents must be called from the main thread.
@ -554,11 +564,14 @@ func (u *userInterface) updateGraphicsContext(g GraphicsContext) {
sizeChanged := false
// TODO: Is it possible to reduce 'runOnMainThread' calls?
_ = u.runOnMainThread(func() error {
if !u.toChangeSize {
actualScale = u.actualScreenScale()
if !u.toChangeSize && u.lastActualScale == actualScale {
return nil
}
u.lastActualScale = actualScale
u.toChangeSize = false
actualScale = u.actualScreenScale()
sizeChanged = true
return nil
})