Skip to content

Commit

Permalink
#5180: Allow disconnection of registry keys observed by registry::obs…
Browse files Browse the repository at this point in the history
…erveBooleanKey
  • Loading branch information
codereader committed May 12, 2020
1 parent 7e0dc32 commit 8ddc9e6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
5 changes: 3 additions & 2 deletions libs/registry/adaptors.h
Expand Up @@ -3,6 +3,7 @@
#include "registry.h"

#include <sigc++/bind.h>
#include <sigc++/connection.h>

namespace registry
{
Expand All @@ -26,11 +27,11 @@ namespace detail
* slot invoked when the value changes to true and the other invoked when the
* value changes to false.
*/
inline void observeBooleanKey(const std::string& key,
inline sigc::connection observeBooleanKey(const std::string& key,
sigc::slot<void> trueCallback,
sigc::slot<void> falseCallback)
{
GlobalRegistry().signalForKey(key).connect(
return GlobalRegistry().signalForKey(key).connect(
sigc::bind(sigc::ptr_fun(&detail::invokeFromBoolean),
key,
trueCallback,
Expand Down
19 changes: 13 additions & 6 deletions radiant/eventmanager/RegistryToggle.h
Expand Up @@ -5,6 +5,7 @@

#include "registry/adaptors.h"
#include <functional>
#include <sigc++/connection.h>

namespace ui
{
Expand All @@ -23,32 +24,38 @@ class RegistryToggle :
// The attached registrykey
const std::string _registryKey;

sigc::connection _registryConn;

void setState(bool state)
{
_toggled = state;
updateWidgets();
}

// Dummy callback for the Toggle base class, we don't need any callbacks...
static void doNothing(bool) {}

public:

RegistryToggle(const std::string& registryKey) :
Toggle(std::bind(&RegistryToggle::doNothing, this, std::placeholders::_1)),
Toggle(doNothing),
_registryKey(registryKey)
{
// Initialise the current state
_toggled = registry::getValue<bool>(_registryKey);

// Register to get notified on key changes
registry::observeBooleanKey(
_registryConn = registry::observeBooleanKey(
_registryKey,
sigc::bind(sigc::mem_fun(this, &RegistryToggle::setState), true),
sigc::bind(sigc::mem_fun(this, &RegistryToggle::setState), false)
);

);
}

// Dummy callback for the Toggle base class, we don't need any callbacks...
void doNothing(bool) {}
~RegistryToggle()
{
_registryConn.disconnect();
}

virtual bool setToggled(const bool toggled)
{
Expand Down

0 comments on commit 8ddc9e6

Please sign in to comment.