diff --git a/internal/ui/monitor_glfw.go b/internal/ui/monitor_glfw.go index 9c80a95e0..b014bee6b 100644 --- a/internal/ui/monitor_glfw.go +++ b/internal/ui/monitor_glfw.go @@ -63,7 +63,7 @@ var theMonitors monitors func (m *monitors) append(ms []*Monitor) []*Monitor { if atomic.LoadInt32(&m.updateCalled) == 0 { - m.update() + panic("ui: (*monitors).update must be called before (*monitors).append is called") } m.m.Lock() @@ -91,6 +91,7 @@ func (m *monitors) monitorFromID(id int) *Monitor { return m.monitors[id] } +// update must be called from the main thread. func (m *monitors) update() { glfwMonitors := glfw.GetMonitors() newMonitors := make([]*Monitor, 0, len(glfwMonitors)) diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index c5516a84b..7edacfe51 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -155,13 +155,13 @@ func init() { func init() { hideConsoleWindowOnWindows() + if err := initialize(); err != nil { panic(err) } glfw.SetMonitorCallback(glfw.ToMonitorCallback(func(monitor *glfw.Monitor, event glfw.PeripheralEvent) { theMonitors.update() })) - theMonitors.update() } var glfwSystemCursors = map[CursorShape]*glfw.Cursor{} @@ -174,6 +174,9 @@ func initialize() error { glfw.WindowHint(glfw.Visible, glfw.False) glfw.WindowHint(glfw.ClientAPI, glfw.NoAPI) + // Update the monitor first. The monitor state is depended on various functions like initialMonitorByOS. + theMonitors.update() + m, err := initialMonitorByOS() if err != nil { return err