2018-10-30 13:39:30 +01:00
|
|
|
#include "modules/sway/mode.hpp"
|
|
|
|
|
2019-04-19 11:09:06 +02:00
|
|
|
namespace waybar::modules::sway {
|
|
|
|
|
|
|
|
Mode::Mode(const std::string& id, const Bar& bar, const Json::Value& config)
|
2019-04-18 17:52:00 +02: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\" ]");
|
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
|
|
|
|
worker();
|
2018-12-18 17:30:54 +01:00
|
|
|
dp.emit();
|
2018-10-30 13:39:30 +01:00
|
|
|
}
|
|
|
|
|
2019-04-19 11:09:06 +02:00
|
|
|
void Mode::onEvent(const struct Ipc::ipc_response res) {
|
2019-04-23 11:41:49 +02:00
|
|
|
if (res.payload["change"] != "default") {
|
|
|
|
mode_ = res.payload["change"].asString();
|
2019-04-19 11:09:06 +02:00
|
|
|
} else {
|
|
|
|
mode_.clear();
|
|
|
|
}
|
|
|
|
dp.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mode::worker() {
|
2018-10-30 13:39:30 +01:00
|
|
|
thread_ = [this] {
|
|
|
|
try {
|
2019-04-19 11:09:06 +02:00
|
|
|
ipc_.handleEvent();
|
2018-10-30 13:39:30 +01:00
|
|
|
} catch (const std::exception& e) {
|
2018-12-09 10:49:28 +01:00
|
|
|
std::cerr << "Mode: " << e.what() << std::endl;
|
2018-10-30 13:39:30 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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 {
|
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
|
|
|
}
|
2019-04-19 11:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace waybar::modules::sway
|