#include "window_manager.h" #include #include "audio/plugin_host/plugin_host.h" window_manager::window_manager() { main_window_ = nullptr; } void window_manager::init(singleton_initliazer& initliazer) { singleton_t::init(initliazer); start_idle_thread(); } void window_manager::release() { singleton_t::release(); if (main_window_) glfwDestroyWindow(main_window_); stop_idle_thread(); } void window_manager::tick() { glfwPollEvents(); if (should_close()) { destroy_all_plugin_host_window(); return; } update_host_window(); } void window_manager::destroy_all_plugin_host_window() { for (const auto& window: host_window_map_ | std::views::values) { glfwDestroyWindow(window); } host_window_map_.clear(); } GLFWwindow* window_manager::create_main_window(const char* title, int width, int height) { if (!main_window_) main_window_ = glfwCreateWindow(width, height, title, nullptr, nullptr); return main_window_; } GLFWwindow* window_manager::create_plugin_host_window(plugin_host* host) { if (!host->has_editor()) return nullptr; if (host_window_map_.contains(host)) return host_window_map_[host]; auto editor_size = host->get_editor_size(); auto new_window = glfwCreateWindow(editor_size.x, editor_size.y, host->name.c_str(), nullptr, nullptr); host_window_map_[host] = new_window; return new_window; } void window_manager::destroy_plugin_host_window(plugin_host* host) { if (!host_window_map_.contains(host)) return; glfwDestroyWindow(host_window_map_[host]); host_window_map_.erase(host); } void window_manager::resize_plugin_host_window(plugin_host* host, int width, int height) { if (!host_window_map_.contains(host)) return; glfwSetWindowSize(host_window_map_[host], width, height); } void window_manager::idle_plugin_host_window() const { for (const auto& window_map: host_window_map_) { window_map.first->idle_editor(); } } void window_manager::start_idle_thread() { idle_thread_ = std::thread(&window_manager::idle_thread_func, this); idle_thread_.detach(); } void window_manager::stop_idle_thread() { idle_thread_running_ = false; if (idle_thread_.joinable()) idle_thread_.join(); } void window_manager::idle_thread_func() { idle_thread_running_ = true; while (idle_thread_running_) { idle_plugin_host_window(); std::this_thread::yield(); } } void window_manager::update_host_window() { std::vector host_editor_to_close; for (const auto& [host, window]: host_window_map_) { if (glfwWindowShouldClose(window)) { host->close_editor(); glfwDestroyWindow(window); host_editor_to_close.push_back(host); } } for (auto host: host_editor_to_close) { host_window_map_.erase(host); } }