polybar-dwm/src/modules/systray.cpp
Patrick Ziegler 26be83f893
module: Implement action router (#2336)
* module: Implement proof of concept action router

Action implementation inside module becomes much cleaner because each
module just registers action names together with a callback (pointer to
member function) and the action router does the rest.

* Make input function final

This forces all modules to use the action router

* modules: Catch exceptions in action handlers

* Use action router for all modules

* Use action_ prefix for function names

The mpd module's 'stop' action overwrote the base module's stop function
which caused difficult to debug behavior.

To prevent this in the future we now prefix each function that is
responsible for an action with 'action_'

* Cleanup

* actions: Throw exception when re-registering action

Action names are unique inside modules. Unfortunately there is no way to
ensure this statically, the next best thing is to crash the module and
let the user know that this is a bug.

* Formatting

* actions: Ignore data for actions without data

This is the same behavior as before.

* action_router: Write tests
2021-01-04 10:25:52 +01:00

66 lines
1.5 KiB
C++

#if DEBUG
#include "modules/systray.hpp"
#include "drawtypes/label.hpp"
#include "modules/meta/base.inl"
#include "x11/connection.hpp"
#include "x11/tray_manager.hpp"
POLYBAR_NS
namespace modules {
template class module<systray_module>;
/**
* Construct module
*/
systray_module::systray_module(const bar_settings& bar, string name_)
: static_module<systray_module>(bar, move(name_)), m_connection(connection::make()) {
m_router->register_action(EVENT_TOGGLE, &systray_module::action_toggle);
// Add formats and elements
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL_TOGGLE, {TAG_LABEL_TOGGLE, TAG_TRAY_CLIENTS});
if (m_formatter->has(TAG_LABEL_TOGGLE)) {
m_label = load_label(m_conf, name(), TAG_LABEL_TOGGLE);
}
}
/**
* Update
*/
void systray_module::update() {
if (m_label) {
m_label->reset_tokens();
m_label->replace_token("%title%", "");
}
broadcast();
}
/**
* Build output
*/
bool systray_module::build(builder* builder, const string& tag) const {
if (tag == TAG_LABEL_TOGGLE) {
builder->action(mousebtn::LEFT, *this, EVENT_TOGGLE, "", m_label);
} else if (tag == TAG_TRAY_CLIENTS && !m_hidden) {
builder->append(TRAY_PLACEHOLDER);
} else {
return false;
}
return true;
}
/**
* Handle input event
*/
void systray_module::action_toggle() {
m_hidden = !m_hidden;
broadcast();
}
} // namespace modules
POLYBAR_NS_END
#endif