Waybar/src/modules/sway/mode.cpp

55 lines
1.2 KiB
C++
Raw Normal View History

2018-10-30 12:39:30 +00:00
#include "modules/sway/mode.hpp"
2019-05-18 23:44:45 +00:00
#include <spdlog/spdlog.h>
2018-10-30 12:39:30 +00:00
2019-04-19 09:09:06 +00:00
namespace waybar::modules::sway {
2019-05-07 11:43:48 +00:00
Mode::Mode(const std::string& id, const Json::Value& config) : ALabel(config, "{}") {
2018-12-18 16:30:54 +00:00
label_.set_name("mode");
if (!id.empty()) {
label_.get_style_context()->add_class(id);
}
2019-04-24 10:37:24 +00:00
ipc_.subscribe(R"(["mode"])");
2019-04-19 09:09:06 +00:00
ipc_.signal_event.connect(sigc::mem_fun(*this, &Mode::onEvent));
2018-10-30 12:39:30 +00:00
// Launch worker
worker();
2018-12-18 16:30:54 +00:00
dp.emit();
2018-10-30 12:39:30 +00:00
}
2019-05-07 11:43:48 +00:00
void Mode::onEvent(const struct Ipc::ipc_response& res) {
2019-05-09 08:30:54 +00:00
try {
auto payload = parser_.parse(res.payload);
if (payload["change"] != "default") {
mode_ = payload["change"].asString();
} else {
mode_.clear();
}
dp.emit();
} catch (const std::exception& e) {
2019-05-18 23:44:45 +00:00
spdlog::error("Mode: {}", e.what());
2019-04-19 09:09:06 +00:00
}
}
void Mode::worker() {
2018-10-30 12:39:30 +00:00
thread_ = [this] {
try {
2019-04-19 09:09:06 +00:00
ipc_.handleEvent();
2018-10-30 12:39:30 +00:00
} catch (const std::exception& e) {
2019-05-18 23:44:45 +00:00
spdlog::error("Mode: {}", e.what());
2018-10-30 12:39:30 +00:00
}
};
}
2019-04-19 09:09:06 +00:00
auto Mode::update() -> void {
2018-10-30 12:39:30 +00:00
if (mode_.empty()) {
2018-12-18 16:30:54 +00:00
event_box_.hide();
2018-10-30 12:39:30 +00:00
} else {
2018-11-21 19:49:09 +00:00
label_.set_markup(fmt::format(format_, mode_));
2019-02-22 10:35:26 +00:00
if (tooltipEnabled()) {
label_.set_tooltip_text(mode_);
}
2018-12-18 16:30:54 +00:00
event_box_.show();
2018-10-30 12:39:30 +00:00
}
2019-04-19 09:09:06 +00:00
}
2019-05-18 23:44:45 +00:00
} // namespace waybar::modules::sway