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
import (
"errors"
"fmt"
"image"
"os"
@ -175,6 +176,11 @@ func initialize() error {
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.initDeviceScaleFactor = theUI.deviceScaleFactor(m)
// 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
u.t.Call(func() {
m := u.currentMonitor()
if m == nil {
return
}
v := m.GetVideoMode()
w = int(u.dipFromGLFWMonitorPixel(float64(v.Width), m))
h = int(u.dipFromGLFWMonitorPixel(float64(v.Height), m))
@ -920,11 +929,12 @@ func (u *userInterfaceImpl) updateSize() (float64, float64) {
// fullscreened. Use the monitor size.
// On macOS's native fullscreen, the window's size returns a more precise size
// reflecting the adjustment of the view size (#1745).
m := u.currentMonitor()
v := m.GetVideoMode()
ww, wh := v.Width, v.Height
w = u.dipFromGLFWMonitorPixel(float64(ww), m)
h = u.dipFromGLFWMonitorPixel(float64(wh), m)
if m := u.currentMonitor(); m != nil {
v := m.GetVideoMode()
ww, wh := v.Width, v.Height
w = u.dipFromGLFWMonitorPixel(float64(ww), m)
h = u.dipFromGLFWMonitorPixel(float64(wh), m)
}
} else {
// 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
@ -1230,6 +1240,10 @@ func (u *userInterfaceImpl) setWindowSizeInDIPImpl(width, height int, fullscreen
u.setNativeFullscreen(fullscreen)
} else {
m := u.currentMonitor()
if m == nil {
return
}
v := m.GetVideoMode()
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.
func videoModeScale(m *glfw.Monitor) float64 {
if m == nil {
return 1
}
// Caching wrapper for videoModeScaleUncached as
// videoModeScaleUncached may be expensive (uses blocking calls on X connection)
// and public ScreenSizeInFullscreen API needs the videoModeScale.