From 09a548e0559f5bbeb783859d257f1cb75be9c5fb Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 1 Apr 2022 03:51:24 +0900 Subject: [PATCH] internal/ui: consider the case when currentMonitor() returns nil Closes #1887 --- internal/ui/ui_glfw.go | 24 +++++++++++++++++++----- internal/ui/ui_glfw_unix.go | 4 ++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index c7bc86f09..c7ecb53a9 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -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) diff --git a/internal/ui/ui_glfw_unix.go b/internal/ui/ui_glfw_unix.go index e272e47ae..37590a8fd 100644 --- a/internal/ui/ui_glfw_unix.go +++ b/internal/ui/ui_glfw_unix.go @@ -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.