internal/ui: refactoring: add (*Monitor).deviceScaleFactor

This commit is contained in:
Hajime Hoshi 2023-09-24 19:40:43 +09:00
parent 0e23045b90
commit b32575b7b2
5 changed files with 21 additions and 22 deletions

View File

@ -68,7 +68,7 @@ func (u *userInterfaceImpl) updateInputStateImpl() error {
} }
m := u.currentMonitor() m := u.currentMonitor()
s := u.deviceScaleFactor(m) s := m.deviceScaleFactor()
cx, cy := u.savedCursorX, u.savedCursorY cx, cy := u.savedCursorX, u.savedCursorY
defer func() { defer func() {

View File

@ -41,6 +41,15 @@ func (m *Monitor) Name() string {
return m.name return m.name
} }
func (m *Monitor) deviceScaleFactor() float64 {
// It is rare, but monitor can be nil when glfw.GetPrimaryMonitor returns nil.
// In this case, return 1 as a tentative scale (#1878).
if m == nil {
return 1
}
return m.contentScale
}
type monitors struct { type monitors struct {
// monitors is the monitor list cache for desktop glfw compile targets. // monitors is the monitor list cache for desktop glfw compile targets.
// populated by 'updateMonitors' which is called on init and every // populated by 'updateMonitors' which is called on init and every

View File

@ -724,7 +724,7 @@ func (u *userInterfaceImpl) DeviceScaleFactor() float64 {
return 0 return 0
} }
if !u.isRunning() { if !u.isRunning() {
return u.deviceScaleFactor(u.getInitMonitor()) return u.getInitMonitor().deviceScaleFactor()
} }
var f float64 var f float64
@ -732,21 +732,11 @@ func (u *userInterfaceImpl) DeviceScaleFactor() float64 {
if u.isTerminated() { if u.isTerminated() {
return return
} }
f = u.deviceScaleFactor(u.currentMonitor()) f = u.currentMonitor().deviceScaleFactor()
}) })
return f return f
} }
func (u *userInterfaceImpl) deviceScaleFactor(monitor *Monitor) float64 {
// It is rare, but monitor can be nil when glfw.GetPrimaryMonitor returns nil.
// In this case, return 1 as a tentative scale (#1878).
if monitor == nil {
return 1
}
return monitor.contentScale
}
func init() { func init() {
// Lock the main thread. // Lock the main thread.
runtime.LockOSThread() runtime.LockOSThread()
@ -853,7 +843,7 @@ func (u *userInterfaceImpl) registerWindowFramebufferSizeCallback() {
// The framebuffer size is always scaled by the device scale factor (#1975). // The framebuffer size is always scaled by the device scale factor (#1975).
// See also the implementation in uiContext.updateOffscreen. // See also the implementation in uiContext.updateOffscreen.
s := u.deviceScaleFactor(u.currentMonitor()) s := u.currentMonitor().deviceScaleFactor()
ww := int(float64(w) / s) ww := int(float64(w) / s)
wh := int(float64(h) / s) wh := int(float64(h) / s)
u.setWindowSizeInDIP(ww, wh, false) u.setWindowSizeInDIP(ww, wh, false)
@ -1172,7 +1162,7 @@ func (u *userInterfaceImpl) updateGame() error {
var err error var err error
if u.mainThread.Call(func() { if u.mainThread.Call(func() {
outsideWidth, outsideHeight, err = u.update() outsideWidth, outsideHeight, err = u.update()
deviceScaleFactor = u.deviceScaleFactor(u.currentMonitor()) deviceScaleFactor = u.currentMonitor().deviceScaleFactor()
}); err != nil { }); err != nil {
return err return err
} }
@ -1332,7 +1322,7 @@ func (u *userInterfaceImpl) setWindowSizeInDIP(width, height int, callSetSize bo
height = 1 height = 1
} }
scale := u.deviceScaleFactor(u.currentMonitor()) scale := u.currentMonitor().deviceScaleFactor()
if u.origWindowWidthInDIP == width && u.origWindowHeightInDIP == height && u.lastDeviceScaleFactor == scale { if u.origWindowWidthInDIP == width && u.origWindowHeightInDIP == height && u.lastDeviceScaleFactor == scale {
return return
} }

View File

@ -111,15 +111,15 @@ func videoModeScale(m *glfw.Monitor) float64 {
} }
func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *Monitor) float64 { func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *Monitor) float64 {
return x / (monitor.videoModeScale * u.deviceScaleFactor(monitor)) return x / (monitor.videoModeScale * monitor.deviceScaleFactor())
} }
func (u *userInterfaceImpl) dipFromGLFWPixel(x float64, monitor *Monitor) float64 { func (u *userInterfaceImpl) dipFromGLFWPixel(x float64, monitor *Monitor) float64 {
return x / u.deviceScaleFactor(monitor) return x / monitor.deviceScaleFactor()
} }
func (u *userInterfaceImpl) dipToGLFWPixel(x float64, monitor *Monitor) float64 { func (u *userInterfaceImpl) dipToGLFWPixel(x float64, monitor *Monitor) float64 {
return x * u.deviceScaleFactor(monitor) return x * monitor.deviceScaleFactor()
} }
func (u *userInterfaceImpl) adjustWindowPosition(x, y int, monitor *Monitor) (int, int) { func (u *userInterfaceImpl) adjustWindowPosition(x, y int, monitor *Monitor) (int, int) {

View File

@ -92,15 +92,15 @@ func videoModeScale(monitor *glfw.Monitor) float64 {
} }
func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *Monitor) float64 { func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *Monitor) float64 {
return x / u.deviceScaleFactor(monitor) return x / monitor.deviceScaleFactor()
} }
func (u *userInterfaceImpl) dipFromGLFWPixel(x float64, monitor *Monitor) float64 { func (u *userInterfaceImpl) dipFromGLFWPixel(x float64, monitor *Monitor) float64 {
return x / u.deviceScaleFactor(monitor) return x / monitor.deviceScaleFactor()
} }
func (u *userInterfaceImpl) dipToGLFWPixel(x float64, monitor *Monitor) float64 { func (u *userInterfaceImpl) dipToGLFWPixel(x float64, monitor *Monitor) float64 {
return x * u.deviceScaleFactor(monitor) return x * monitor.deviceScaleFactor()
} }
func (u *userInterfaceImpl) adjustWindowPosition(x, y int, monitor *Monitor) (int, int) { func (u *userInterfaceImpl) adjustWindowPosition(x, y int, monitor *Monitor) (int, int) {