waybar/src/modules/sway/mode.cpp

43 lines
995 B
C++
Raw Normal View History

2018-10-30 13:39:30 +01:00
#include "modules/sway/mode.hpp"
2018-12-01 00:10:41 +01:00
waybar::modules::sway::Mode::Mode(const Bar& bar, const Json::Value& config)
2018-10-30 13:39:30 +01:00
: ALabel(config, "{}"), bar_(bar)
{
ipc_.connect();
ipc_.subscribe("[ \"mode\" ]");
// Launch worker
worker();
}
void waybar::modules::sway::Mode::worker()
{
thread_ = [this] {
try {
auto res = ipc_.handleEvent();
auto parsed = parser_.parse(res.payload);
if ((parsed["change"]) != "default" ) {
mode_ = parsed["change"].asString();
dp.emit();
}
else if ((parsed["change"]) == "default" ) {
mode_.clear();
dp.emit();
}
} catch (const std::exception& e) {
std::cerr << "Mode: " << e.what() << std::endl;
2018-10-30 13:39:30 +01:00
}
};
}
auto waybar::modules::sway::Mode::update() -> void
{
if (mode_.empty()) {
label_.set_name("");
label_.hide();
} else {
label_.set_name("mode");
2018-11-21 20:49:09 +01:00
label_.set_markup(fmt::format(format_, mode_));
2018-10-30 13:39:30 +01:00
label_.set_tooltip_text(mode_);
label_.show();
}
}