diff --git a/source/plugin.cpp b/source/plugin.cpp index 2aadf314..e1e93cbe 100644 --- a/source/plugin.cpp +++ b/source/plugin.cpp @@ -212,11 +212,6 @@ MODULE_EXPORT bool obs_module_load(void) #endif } -// Frontend -#ifdef ENABLE_FRONTEND - streamfx::ui::handler::initialize(); -#endif - DLOG_INFO("Loaded Version %s", STREAMFX_VERSION_STRING); return true; } catch (std::exception const& ex) { @@ -233,11 +228,6 @@ MODULE_EXPORT void obs_module_unload(void) try { DLOG_INFO("Unloading Version %s", STREAMFX_VERSION_STRING); - // Frontend -#ifdef ENABLE_FRONTEND - streamfx::ui::handler::finalize(); -#endif - // Transitions { #ifdef ENABLE_TRANSITION_SHADER diff --git a/source/ui/ui.cpp b/source/ui/ui.cpp index 8b23878b..2443c7b4 100644 --- a/source/ui/ui.cpp +++ b/source/ui/ui.cpp @@ -257,21 +257,18 @@ void streamfx::ui::handler::on_action_about(bool checked) _about_dialog->show(); } -static std::shared_ptr _handler_singleton; - -void streamfx::ui::handler::initialize() +std::shared_ptr streamfx::ui::handler::instance() { - _handler_singleton = std::make_shared(); -} + static std::weak_ptr winst; + static std::mutex mtx; -void streamfx::ui::handler::finalize() -{ - _handler_singleton.reset(); -} - -std::shared_ptr streamfx::ui::handler::get() -{ - return _handler_singleton; + std::unique_lock lock(mtx); + auto instance = winst.lock(); + if (!instance) { + instance = std::shared_ptr(new streamfx::ui::handler()); + winst = instance; + } + return instance; } streamfx::ui::translator::translator(QObject* parent) {} @@ -294,3 +291,14 @@ QString streamfx::ui::translator::translate(const char* context, const char* sou } return QString(); } + +static std::shared_ptr loader_instance; + +static auto loader = streamfx::loader( + []() { // Initalizer + loader_instance = streamfx::ui::handler::instance(); + }, + []() { // Finalizer + loader_instance.reset(); + }, + streamfx::loader_priority::LOWEST); // Must be loaded after all other functionality. diff --git a/source/ui/ui.hpp b/source/ui/ui.hpp index 380195fb..b1da7695 100644 --- a/source/ui/ui.hpp +++ b/source/ui/ui.hpp @@ -36,8 +36,10 @@ namespace streamfx::ui { std::shared_ptr _updater; #endif - public: + private: handler(); + + public: ~handler(); bool have_shown_about_streamfx(bool shown = false); @@ -63,11 +65,7 @@ namespace streamfx::ui { void on_action_about(bool); public /* Singleton */: - static void initialize(); - - static void finalize(); - - static std::shared_ptr get(); + static std::shared_ptr instance(); }; class translator : public QTranslator {