internal/ui: consider the case when currentMonitor() returns nil

Closes #1887
This commit is contained in:
Hajime Hoshi 2022-04-01 03:51:24 +09:00
parent 03bf60cd92
commit 09a548e055
2 changed files with 23 additions and 5 deletions

View File

@ -18,6 +18,7 @@
package ui package ui
import ( import (
"errors"
"fmt" "fmt"
"image" "image"
"os" "os"
@ -175,6 +176,11 @@ func initialize() error {
m = glfw.GetPrimaryMonitor() m = glfw.GetPrimaryMonitor()
} }
// GetPrimaryMonitor might return nil in theory (#1887).
if m == nil {
return errors.New("ui: no monitor was found at initialize")
}
theUI.initMonitor = m theUI.initMonitor = m
theUI.initDeviceScaleFactor = theUI.deviceScaleFactor(m) theUI.initDeviceScaleFactor = theUI.deviceScaleFactor(m)
// GetVideoMode must be called from the main thread, then call this here and record // GetVideoMode must be called from the main thread, then call this here and record
@ -469,6 +475,9 @@ func (u *userInterfaceImpl) ScreenSizeInFullscreen() (int, int) {
var w, h int var w, h int
u.t.Call(func() { u.t.Call(func() {
m := u.currentMonitor() m := u.currentMonitor()
if m == nil {
return
}
v := m.GetVideoMode() v := m.GetVideoMode()
w = int(u.dipFromGLFWMonitorPixel(float64(v.Width), m)) w = int(u.dipFromGLFWMonitorPixel(float64(v.Width), m))
h = int(u.dipFromGLFWMonitorPixel(float64(v.Height), m)) h = int(u.dipFromGLFWMonitorPixel(float64(v.Height), m))
@ -920,11 +929,12 @@ func (u *userInterfaceImpl) updateSize() (float64, float64) {
// fullscreened. Use the monitor size. // fullscreened. Use the monitor size.
// On macOS's native fullscreen, the window's size returns a more precise size // On macOS's native fullscreen, the window's size returns a more precise size
// reflecting the adjustment of the view size (#1745). // reflecting the adjustment of the view size (#1745).
m := u.currentMonitor() if m := u.currentMonitor(); m != nil {
v := m.GetVideoMode() v := m.GetVideoMode()
ww, wh := v.Width, v.Height ww, wh := v.Width, v.Height
w = u.dipFromGLFWMonitorPixel(float64(ww), m) w = u.dipFromGLFWMonitorPixel(float64(ww), m)
h = u.dipFromGLFWMonitorPixel(float64(wh), m) h = u.dipFromGLFWMonitorPixel(float64(wh), m)
}
} else { } else {
// Instead of u.windowWidthInDIP and u.windowHeightInDIP, use the actual window size // Instead of u.windowWidthInDIP and u.windowHeightInDIP, use the actual window size
// here. On Windows, the specified size at SetSize and the actual window size might // here. On Windows, the specified size at SetSize and the actual window size might
@ -1230,6 +1240,10 @@ func (u *userInterfaceImpl) setWindowSizeInDIPImpl(width, height int, fullscreen
u.setNativeFullscreen(fullscreen) u.setNativeFullscreen(fullscreen)
} else { } else {
m := u.currentMonitor() m := u.currentMonitor()
if m == nil {
return
}
v := m.GetVideoMode() v := m.GetVideoMode()
u.window.SetMonitor(m, 0, 0, v.Width, v.Height, v.RefreshRate) u.window.SetMonitor(m, 0, 0, v.Width, v.Height, v.RefreshRate)

View File

@ -67,6 +67,10 @@ func clearVideoModeScaleCache() {
// videoModeScale must be called from the main thread. // videoModeScale must be called from the main thread.
func videoModeScale(m *glfw.Monitor) float64 { func videoModeScale(m *glfw.Monitor) float64 {
if m == nil {
return 1
}
// Caching wrapper for videoModeScaleUncached as // Caching wrapper for videoModeScaleUncached as
// videoModeScaleUncached may be expensive (uses blocking calls on X connection) // videoModeScaleUncached may be expensive (uses blocking calls on X connection)
// and public ScreenSizeInFullscreen API needs the videoModeScale. // and public ScreenSizeInFullscreen API needs the videoModeScale.