waybar/src/modules/sway/mode.cpp

46 lines
1.1 KiB
C++
Raw Normal View History

2018-10-30 13:39:30 +01:00
#include "modules/sway/mode.hpp"
2018-12-18 17:30:54 +01:00
waybar::modules::sway::Mode::Mode(const std::string& id, const Bar& bar, const Json::Value& config)
2018-10-30 13:39:30 +01:00
: ALabel(config, "{}"), bar_(bar)
{
2018-12-18 17:30:54 +01:00
label_.set_name("mode");
if (!id.empty()) {
label_.get_style_context()->add_class(id);
}
2018-10-30 13:39:30 +01:00
ipc_.connect();
ipc_.subscribe("[ \"mode\" ]");
// Launch worker
worker();
2018-12-18 17:30:54 +01:00
dp.emit();
2018-10-30 13:39:30 +01:00
}
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()) {
2018-12-18 17:30:54 +01:00
event_box_.hide();
2018-10-30 13:39:30 +01:00
} else {
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_);
2018-12-18 17:30:54 +01:00
event_box_.show();
2018-10-30 13:39:30 +01:00
}
}