Skip to content

Commit

Permalink
#5231: Query the UI module using a custom message to check whether th…
Browse files Browse the repository at this point in the history
…e application is active or not.
  • Loading branch information
codereader committed Aug 13, 2020
1 parent be0fc77 commit 2804d0b
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/imessagebus.h
Expand Up @@ -48,6 +48,7 @@ class IMessage
FileSelectionRequest,
Notification,
TextureChanged,
ApplicationIsActiveQuery,

UserDefinedMessagesGoHigherThanThis = 999,
};
Expand Down
44 changes: 44 additions & 0 deletions libs/messages/ApplicationIsActiveRequest.h
@@ -0,0 +1,44 @@
#pragma once

#include "imessagebus.h"

namespace radiant
{

/**
* Message sent to query whether DarkRadiant is active.
* The UI module should respond to this request and set the flag
* in the message instance accordingly, using setApplicationIsActive().
*
* If no code responds to this query, the application is considered
* active by default.
*/
class ApplicationIsActiveRequest :
public radiant::IMessage
{
private:
bool _isActive;

public:
ApplicationIsActiveRequest() :
_isActive(true)
{}

std::size_t getId() const override
{
return Type::ApplicationIsActiveQuery;
}

void setApplicationIsActive(bool isActive)
{
_isActive = isActive;
}

// TRUE if the application is currently active
bool getApplicationIsActive() const
{
return _isActive;
}
};

}
17 changes: 17 additions & 0 deletions radiant/ui/statusbar/EditingStopwatchStatus.h
Expand Up @@ -2,10 +2,12 @@

#include "i18n.h"
#include "iuimanager.h"
#include "imainframe.h"
#include "ieditstopwatch.h"
#include <sigc++/connection.h>
#include <sigc++/functors/mem_fun.h>
#include <fmt/format.h>
#include "messages/ApplicationIsActiveRequest.h"

namespace ui
{
Expand All @@ -17,13 +19,20 @@ class EditingStopwatchStatus

const char* const STATUS_BAR_ELEMENT = "EditTime";

std::size_t _msgSubscription;

public:
EditingStopwatchStatus()
{
_conn = GlobalMapEditStopwatch().sig_TimerChanged().connect(
sigc::mem_fun(this, &EditingStopwatchStatus::onTimerChanged)
);

_msgSubscription = GlobalRadiantCore().getMessageBus().addListener(
radiant::IMessage::Type::ApplicationIsActiveQuery,
radiant::TypeListener<radiant::ApplicationIsActiveRequest>(
sigc::mem_fun(this, &EditingStopwatchStatus::handleRequest)));

// Add the status bar element
GlobalUIManager().getStatusBarManager().addTextElement(STATUS_BAR_ELEMENT, "stopwatch.png",
IStatusBarManager::POS_MAP_EDIT_TIME, _("Time spent on this map"));
Expand All @@ -33,10 +42,18 @@ class EditingStopwatchStatus

~EditingStopwatchStatus()
{
GlobalRadiantCore().getMessageBus().removeListener(_msgSubscription);
_conn.disconnect();
}

private:
void handleRequest(radiant::ApplicationIsActiveRequest& msg)
{
msg.setApplicationIsActive(
GlobalMainFrame().isActiveApp() && GlobalMainFrame().screenUpdatesEnabled()
);
}

void onTimerChanged()
{
auto secondsEdited = GlobalMapEditStopwatch().getTotalSecondsEdited();
Expand Down
13 changes: 10 additions & 3 deletions radiantcore/map/EditingStopwatch.cpp
Expand Up @@ -5,7 +5,6 @@
#include "i18n.h"
#include "iradiant.h"
#include "itextstream.h"
#include "imainframe.h"
#include "imap.h"
#include "imapresource.h"
#include "imapinfofile.h"
Expand All @@ -14,6 +13,7 @@
#include "string/convert.h"
#include "module/StaticModule.h"
#include "EditingStopwatchInfoFileModule.h"
#include "messages/ApplicationIsActiveRequest.h"

namespace map
{
Expand Down Expand Up @@ -86,13 +86,20 @@ void EditingStopwatch::onRadiantStartup()

void EditingStopwatch::onIntervalReached(wxTimerEvent& ev)
{
// TODO: Send signal over messagebus
if (GlobalMainFrame().isActiveApp() && GlobalMainFrame().screenUpdatesEnabled())
if (applicationIsActive())
{
setTotalSecondsEdited(getTotalSecondsEdited() + TIMER_INTERVAL_SECS);
}
}

bool EditingStopwatch::applicationIsActive()
{
radiant::ApplicationIsActiveRequest msg;
GlobalRadiantCore().getMessageBus().sendMessage(msg);

return msg.getApplicationIsActive();
}

void EditingStopwatch::onMapEvent(IMap::MapEvent ev)
{
switch (ev)
Expand Down
2 changes: 2 additions & 0 deletions radiantcore/map/EditingStopwatch.h
Expand Up @@ -48,6 +48,8 @@ class EditingStopwatch :
sigc::signal<void>& sig_TimerChanged() override;

private:
bool applicationIsActive();

void onMapEvent(IMap::MapEvent ev);
void onRadiantStartup();
void onIntervalReached(wxTimerEvent& ev);
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/libs.vcxproj
Expand Up @@ -158,6 +158,7 @@
<ClInclude Include="..\..\libs\generic\callback.h" />
<ClInclude Include="..\..\libs\KeyValueStore.h" />
<ClInclude Include="..\..\libs\maplib.h" />
<ClInclude Include="..\..\libs\messages\ApplicationIsActiveRequest.h" />
<ClInclude Include="..\..\libs\messages\ApplicationShutdownRequest.h" />
<ClInclude Include="..\..\libs\messages\AutomaticMapSaveRequest.h" />
<ClInclude Include="..\..\libs\messages\CommandExecutionFailed.h" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/libs.vcxproj.filters
Expand Up @@ -246,6 +246,9 @@
<ClInclude Include="..\..\libs\time\Timer.h">
<Filter>time</Filter>
</ClInclude>
<ClInclude Include="..\..\libs\messages\ApplicationIsActiveRequest.h">
<Filter>messages</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="util">
Expand Down

0 comments on commit 2804d0b

Please sign in to comment.