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.
This commit is contained in:
Hajime Hoshi 2023-09-24 01:31:32 +09:00
parent e40268c3ca
commit 92424319a2
2 changed files with 6 additions and 2 deletions

View File

@ -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))

View File

@ -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