From 69e3a2b974a2f2126550e5e707c5193258145475 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 24 Sep 2023 01:31:32 +0900 Subject: [PATCH] internal/ui: refactoring: (*monitors).update must be called from the main thread In the current implementation, (*monitors).update is not called from other threads, but the current code is fragile. --- internal/ui/monitor_glfw.go | 3 ++- internal/ui/ui_glfw.go | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) 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