+
+int Theme::set_theme_menu(lua_State *L)
+{
+ for (const Theme::MenuEntry &entry : theme_menu) {
+ luaL_unref(L, LUA_REGISTRYINDEX, entry.lua_ref);
+ }
+ theme_menu.clear();
+
+ int num_elements = lua_gettop(L);
+ for (int i = 1; i <= num_elements; ++i) {
+ lua_rawgeti(L, i, 1);
+ const string text = checkstdstring(L, -1);
+ lua_pop(L, 1);
+
+ lua_rawgeti(L, i, 2);
+ luaL_checktype(L, -1, LUA_TFUNCTION);
+ int ref = luaL_ref(L, LUA_REGISTRYINDEX);
+
+ theme_menu.push_back(MenuEntry{ text, ref });
+ }
+ lua_pop(L, num_elements);
+ assert(lua_gettop(L) == 0);
+
+ if (theme_menu_callback != nullptr) {
+ theme_menu_callback();
+ }
+
+ return 0;
+}
+
+void Theme::theme_menu_entry_clicked(int lua_ref)
+{
+ lua_rawgeti(L, LUA_REGISTRYINDEX, lua_ref);
+ if (lua_pcall(L, 0, 0, 0) != 0) {
+ fprintf(stderr, "error running menu callback: %s\n", lua_tostring(L, -1));
+ exit(1);
+ }
+}