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 ( var (
scale = 0.0 m sync.Mutex
m sync.Mutex
) )
func DeviceScale() float64 { func DeviceScale() float64 {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
if scale == 0.0 { // TODO: Cache this value (again) for mobile platforms.
scale = impl() return impl()
}
return scale
} }

View File

@ -51,6 +51,10 @@ type userInterface struct {
runnableInBackground bool runnableInBackground bool
vsync bool vsync bool
deviceScale float64
deviceScaleUpdated int64
lastActualScale float64
initFullscreen bool initFullscreen bool
initCursorVisible bool initCursorVisible bool
initWindowDecorated bool initWindowDecorated bool
@ -540,7 +544,13 @@ func (u *userInterface) getScale() float64 {
// actualScreenScale must be called from the main thread. // actualScreenScale must be called from the main thread.
func (u *userInterface) actualScreenScale() float64 { 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. // pollEvents must be called from the main thread.
@ -554,11 +564,14 @@ func (u *userInterface) updateGraphicsContext(g GraphicsContext) {
sizeChanged := false sizeChanged := false
// TODO: Is it possible to reduce 'runOnMainThread' calls? // TODO: Is it possible to reduce 'runOnMainThread' calls?
_ = u.runOnMainThread(func() error { _ = u.runOnMainThread(func() error {
if !u.toChangeSize { actualScale = u.actualScreenScale()
if !u.toChangeSize && u.lastActualScale == actualScale {
return nil return nil
} }
u.lastActualScale = actualScale
u.toChangeSize = false u.toChangeSize = false
actualScale = u.actualScreenScale()
sizeChanged = true sizeChanged = true
return nil return nil
}) })