forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds ability to run code after the app initialization
- Loading branch information
Showing
5 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1731,6 +1731,8 @@ bool AudacityApp::InitPart2() | |
} | ||
#endif | ||
|
||
HandleAppInitialized(); | ||
|
||
return TRUE; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters