Skip to content

Commit

Permalink
Adds OnIdle handler
Browse files Browse the repository at this point in the history
  • Loading branch information
crsib committed Mar 5, 2024
1 parent dc77967 commit 986c2a1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
21 changes: 20 additions & 1 deletion libraries/lib-utility/AppEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@
#include <cassert>
#include <vector>



namespace AppEvents
{
namespace
{
struct IdleEvent
{
};

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

using Observer::Publisher<IdleEvent>::Subscribe;
using Observer::Publisher<IdleEvent>::Publish;
};

EventHandlers& GetEventHandlers()
Expand All @@ -43,6 +51,12 @@ void OnAppInitialized(std::function<void()> callback)
handlers.appInitialized.push_back(std::move(callback));
}

Observer::Subscription OnAppIdle(std::function<void()> callback)
{
return GetEventHandlers().Subscribe([callback = std::move(callback)](auto&)
{ callback(); });
}

void ProviderBase::HandleAppInitialized()
{
auto& handlers = GetEventHandlers();
Expand All @@ -56,4 +70,9 @@ void ProviderBase::HandleAppInitialized()
callback();
}

void ProviderBase::HandleAppIdle()
{
GetEventHandlers().Publish(IdleEvent{});
}

} // namespace AppEvents
10 changes: 10 additions & 0 deletions libraries/lib-utility/AppEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include <functional>

#include "Observer.h"

namespace AppEvents
{
/*! Register callback to be called when application is initialized.
Expand All @@ -17,6 +19,13 @@ namespace AppEvents
*/
UTILITY_API void OnAppInitialized(std::function<void()> callback);

/*! Register callback to be called when application is idle.
* @param callback Callback to be called when application is idle.
* @pre `!!calback`
*/

UTILITY_API Observer::Subscription OnAppIdle(std::function<void()> callback);

/*! Base class for application events providers.
* This class has no virtual methods and should not be publicly derived from.
* Derived classes should use the provided interface to fire events.
Expand All @@ -29,6 +38,7 @@ class UTILITY_API ProviderBase /* not final */
virtual ~ProviderBase() = default;

void HandleAppInitialized();
void HandleAppIdle();
};

} // namespace AppEvents
2 changes: 2 additions & 0 deletions src/AudacityApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,8 @@ void AudacityApp::OnIdle( wxIdleEvent &evt )
{
evt.Skip();
try {
HandleAppIdle();

if ( Journal::Dispatch() )
evt.RequestMore();
}
Expand Down

0 comments on commit 986c2a1

Please sign in to comment.