Skip to content

A simple mechanism to notify events between various UI elements

License

Notifications You must be signed in to change notification settings

BobBrownConsulting/picoevents

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

picoevents

A simple mechanism to notify events between various UI elements Not thread-safe (but can be easily made so with mutexes protecting the add / replace / remove / notify methods

A typical use case is, you have a button with a on/off state that can be changed in different parts of the application, and you also need other objects to react when the state of the button changes

so first you define an event:

picoevents::Event<const int> buttonStateChangedEvent;

you'll have somewhere, something that hold the current state with get/set methods

 bool getCurrentButtonState() const
 {
	return _buttonState;
 }
 void setCurrentButtonState(bool value) const
 {
	buttonStateChangedEvent.notify(value);
 }

Note how setCurrentButtonState doesn't change the value, but instead notifies the event. So you need to handle the event:

buttonStateChangedEvent.add([&](bool v) { this->_buttonState=v;});

Now any object in the UI can change the state of the button by calling:

buttonStateChangedEvent.notify(value);

and any object in the UI can be notifed of button change by adding:

buttonStateChangedEvent.add([&](bool v) { dosomething.... });

All of this, without these objects having any knowledge of each other !

You can also prepare a notification, then trigger it at a later time:

auto notifier = buttonStateChangedEvent.makeNotifier(false);
notifier.trigger();  // call buttonStateChangedEvent.notify(false);

That way you can prepare a notification on a thread, and trigger it on a different one.

About

A simple mechanism to notify events between various UI elements

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 100.0%