internal/ui: bug fix: need nil check at dipFromGLFWMonitorPixel

Updates #1878
This commit is contained in:
Hajime Hoshi 2023-09-25 18:42:15 +09:00
parent 4da4b49ce6
commit a65a45586f
2 changed files with 24 additions and 15 deletions

View File

@ -28,12 +28,12 @@ type Monitor struct {
m *glfw.Monitor m *glfw.Monitor
videoMode *glfw.VidMode videoMode *glfw.VidMode
id int id int
name string name string
x int x int
y int y int
contentScale float64 contentScale float64
videoModeScale float64 videoModeScale_ float64
} }
// Name returns the monitor's name. // Name returns the monitor's name.
@ -50,6 +50,15 @@ func (m *Monitor) deviceScaleFactor() float64 {
return m.contentScale return m.contentScale
} }
func (m *Monitor) videoModeScale() 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.videoModeScale_
}
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
@ -134,14 +143,14 @@ func (m *monitors) update() {
} }
newMonitors = append(newMonitors, &Monitor{ newMonitors = append(newMonitors, &Monitor{
m: m, m: m,
videoMode: m.GetVideoMode(), videoMode: m.GetVideoMode(),
id: i, id: i,
name: m.GetName(), name: m.GetName(),
x: x, x: x,
y: y, y: y,
contentScale: contentScale, contentScale: contentScale,
videoModeScale: videoModeScale(m), videoModeScale_: videoModeScale(m),
}) })
} }

View File

@ -111,7 +111,7 @@ func videoModeScale(m *glfw.Monitor) float64 {
} }
func dipFromGLFWMonitorPixel(x float64, monitor *Monitor) float64 { func dipFromGLFWMonitorPixel(x float64, monitor *Monitor) float64 {
return x / (monitor.videoModeScale * monitor.deviceScaleFactor()) return x / (monitor.videoModeScale() * monitor.deviceScaleFactor())
} }
func dipFromGLFWPixel(x float64, monitor *Monitor) float64 { func dipFromGLFWPixel(x float64, monitor *Monitor) float64 {