Skip to content

Commit

Permalink
Adds ability to run code after the app initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
crsib committed Jan 24, 2024
1 parent 1581652 commit d35e55f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
59 changes: 59 additions & 0 deletions libraries/lib-utility/AppEvents.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* SPDX-License-Identifier: GPL-2.0-or-later
* SPDX-FileName: AppEvents.cpp
* SPDX-FileContributor: Dmitry Vedenko
*/

#include "AppEvents.h"

#include <cassert>
#include <vector>

namespace AppEvents
{
namespace
{

struct EventHandlers final
{
std::vector<std::function<void()>> appInitialized;
bool AppInitializedCalled {};
};

EventHandlers& GetEventHandlers()
{
static EventHandlers handlers;
return handlers;
}

} // namespace

void OnAppInitialized(std::function<void()> 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<std::function<void()>> callbacks;
std::swap(callbacks, handlers.appInitialized);

for (auto& callback : handlers.appInitialized)
callback();
}

} // namespace AppEvents
28 changes: 28 additions & 0 deletions libraries/lib-utility/AppEvents.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* SPDX-License-Identifier: GPL-2.0-or-later
* SPDX-FileName: AppEvents.h
* SPDX-FileContributor: Dmitry Vedenko
*/

#pragma once

#include <functional>

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<void()> callback);

class UTILITY_API AppEventsProvider /* not final */
{
protected:
virtual ~AppEventsProvider() = default;

void HandleAppInitialized();
};

} // namespace AppEvents
2 changes: 2 additions & 0 deletions libraries/lib-utility/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ correspond to things in the standard.
]]#

set( SOURCES
AppEvents.cpp
AppEvents.h
BufferedStreamReader.cpp
BufferedStreamReader.h
CFResources.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/AudacityApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,8 @@ bool AudacityApp::InitPart2()
}
#endif

HandleAppInitialized();

return TRUE;
}

Expand Down
6 changes: 5 additions & 1 deletion src/AudacityApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "Identifier.h"
#include "Observer.h"
#include "Theme.h"
#include "AppEvents.h"

#include <wx/app.h> // to inherit
#include <wx/timer.h> // member variable
Expand All @@ -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();
Expand Down

0 comments on commit d35e55f

Please sign in to comment.