waybar/src/modules/sway/mode.cpp

56 lines
1.3 KiB
C++
Raw Normal View History

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