waybar/src/modules/sway/mode.cpp

45 lines
1.0 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_.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);
2018-12-26 11:13:36 +01:00
if (parsed["change"] != "default") {
2018-10-30 13:39:30 +01:00
mode_ = parsed["change"].asString();
2018-12-26 11:13:36 +01:00
} else {
2018-10-30 13:39:30 +01:00
mode_.clear();
}
2018-12-26 11:13:36 +01:00
dp.emit();
2018-10-30 13:39:30 +01:00
} 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_));
2019-02-22 11:35:26 +01:00
if (tooltipEnabled()) {
label_.set_tooltip_text(mode_);
}
2018-12-18 17:30:54 +01:00
event_box_.show();
2018-10-30 13:39:30 +01:00
}
}