From d35e55f1f0943b6cb3df049a84e6f715982a255e Mon Sep 17 00:00:00 2001 From: Dmitry Vedenko Date: Thu, 7 Dec 2023 15:37:22 +0300 Subject: [PATCH] Adds ability to run code after the app initialization --- libraries/lib-utility/AppEvents.cpp | 59 ++++++++++++++++++++++++++++ libraries/lib-utility/AppEvents.h | 28 +++++++++++++ libraries/lib-utility/CMakeLists.txt | 2 + src/AudacityApp.cpp | 2 + src/AudacityApp.h | 6 ++- 5 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 libraries/lib-utility/AppEvents.cpp create mode 100644 libraries/lib-utility/AppEvents.h diff --git a/libraries/lib-utility/AppEvents.cpp b/libraries/lib-utility/AppEvents.cpp new file mode 100644 index 000000000000..d41e14540ab1 --- /dev/null +++ b/libraries/lib-utility/AppEvents.cpp @@ -0,0 +1,59 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * SPDX-FileName: AppEvents.cpp + * SPDX-FileContributor: Dmitry Vedenko + */ + +#include "AppEvents.h" + +#include +#include + +namespace AppEvents +{ +namespace +{ + +struct EventHandlers final +{ + std::vector> appInitialized; + bool AppInitializedCalled {}; +}; + +EventHandlers& GetEventHandlers() +{ + static EventHandlers handlers; + return handlers; +} + +} // namespace + +void OnAppInitialized(std::function callback) +{ + assert(callback); + + if (!callback) + return; + + auto& handlers = GetEventHandlers(); + + if (handlers.AppInitializedCalled) + callback(); + else + handlers.appInitialized.push_back(std::move(callback)); +} + +void AppEventsProvider::HandleAppInitialized() +{ + auto& handlers = GetEventHandlers(); + + handlers.AppInitializedCalled = true; + + std::vector> callbacks; + std::swap(callbacks, handlers.appInitialized); + + for (auto& callback : handlers.appInitialized) + callback(); +} + +} // namespace AppEvents diff --git a/libraries/lib-utility/AppEvents.h b/libraries/lib-utility/AppEvents.h new file mode 100644 index 000000000000..2677f58d0bc3 --- /dev/null +++ b/libraries/lib-utility/AppEvents.h @@ -0,0 +1,28 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * SPDX-FileName: AppEvents.h + * SPDX-FileContributor: Dmitry Vedenko + */ + +#pragma once + +#include + +namespace AppEvents +{ +/*! Register callback to be called when application is initialized. + * If application is already initialized, callback will be called immediately. + * @param callback Callback to be called when application is initialized. + * @pre `!!calback` + */ +UTILITY_API void OnAppInitialized(std::function callback); + +class UTILITY_API AppEventsProvider /* not final */ +{ +protected: + virtual ~AppEventsProvider() = default; + + void HandleAppInitialized(); +}; + +} // namespace AppEvents diff --git a/libraries/lib-utility/CMakeLists.txt b/libraries/lib-utility/CMakeLists.txt index 8332a693141e..5610c4f290c3 100644 --- a/libraries/lib-utility/CMakeLists.txt +++ b/libraries/lib-utility/CMakeLists.txt @@ -16,6 +16,8 @@ correspond to things in the standard. ]]# set( SOURCES + AppEvents.cpp + AppEvents.h BufferedStreamReader.cpp BufferedStreamReader.h CFResources.cpp diff --git a/src/AudacityApp.cpp b/src/AudacityApp.cpp index fb84d6ce2196..0ce6e6fd2147 100644 --- a/src/AudacityApp.cpp +++ b/src/AudacityApp.cpp @@ -1731,6 +1731,8 @@ bool AudacityApp::InitPart2() } #endif + HandleAppInitialized(); + return TRUE; } diff --git a/src/AudacityApp.h b/src/AudacityApp.h index 2764cae2fe69..9ecea52a7c63 100644 --- a/src/AudacityApp.h +++ b/src/AudacityApp.h @@ -18,6 +18,7 @@ #include "Identifier.h" #include "Observer.h" #include "Theme.h" +#include "AppEvents.h" #include // to inherit #include // member variable @@ -34,7 +35,10 @@ class CommandHandler; class AppCommandEvent; class AudacityProject; -class AudacityApp final : public wxApp { +class AudacityApp final + : public wxApp + , private AppEvents::AppEventsProvider +{ public: AudacityApp(); ~AudacityApp();