@@ -107,10 +107,29 @@ static void PlaygroundKeyCallback(GLFWwindow* window,
107
107
ImGui::StyleColorsDark ();
108
108
ImGui::GetIO ().IniFilename = nullptr ;
109
109
110
- if (::glfwInit () != GLFW_TRUE) {
111
- return false ;
110
+ // This guard is a hack to work around a problem where glfwCreateWindow
111
+ // hangs when opening a second window after GLFW has been reinitialized (for
112
+ // example, when flipping through multiple playground tests).
113
+ //
114
+ // Explanation:
115
+ // * glfwCreateWindow calls [NSApp run], which begins running the event loop
116
+ // on the current thread.
117
+ // * GLFW then immediately stops the loop when applicationDidFinishLaunching
118
+ // is fired.
119
+ // * applicationDidFinishLaunching is only ever fired once during the
120
+ // application's lifetime, so subsequent calls to [NSApp run] will always
121
+ // hang with this setup.
122
+ // * glfwInit resets the flag that guards against [NSApp run] being
123
+ // called a second time, which causes the subsequent `glfwCreateWindow` to
124
+ // hang indefinitely in the event loop, because
125
+ // applicationDidFinishLaunching is never fired.
126
+ static bool first_run = true ;
127
+ if (first_run) {
128
+ first_run = false ;
129
+ if (::glfwInit () != GLFW_TRUE) {
130
+ return false ;
131
+ }
112
132
}
113
- fml::ScopedCleanupClosure terminate ([]() { ::glfwTerminate (); });
114
133
115
134
::glfwWindowHint (GLFW_CLIENT_API, GLFW_NO_API);
116
135
0 commit comments