From 6aee51479df32c9e907e92d3d9fafb6f63653209 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 15 Apr 2019 10:18:27 +0200 Subject: [PATCH] feat: ability to add multiple classes --- include/modules/custom.hpp | 3 +-- src/modules/custom.cpp | 28 ++++++++++++++++------------ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/include/modules/custom.hpp b/include/modules/custom.hpp index 70be81d9..5eab56bd 100644 --- a/include/modules/custom.hpp +++ b/include/modules/custom.hpp @@ -26,8 +26,7 @@ class Custom : public ALabel { std::string text_; std::string alt_; std::string tooltip_; - std::string class_; - std::string prevclass_; + std::vector class_; int percentage_; waybar::util::SleeperThread thread_; waybar::util::command::res output_; diff --git a/src/modules/custom.cpp b/src/modules/custom.cpp index 706c1de4..fb2dc9d7 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -108,15 +108,12 @@ auto waybar::modules::Custom::update() -> void label_.set_tooltip_text(tooltip_); } } - if (class_ != "") { - if (prevclass_ != "") { - label_.get_style_context()->remove_class(prevclass_); - } - label_.get_style_context()->add_class(class_); - prevclass_ = class_; - } else { - label_.get_style_context()->remove_class(prevclass_); - prevclass_ = ""; + auto classes = label_.get_style_context()->list_classes(); + for (auto const &c : classes) { + label_.get_style_context()->remove_class(c); + } + for (auto const &c : class_) { + label_.get_style_context()->add_class(c); } event_box_.show(); @@ -136,11 +133,11 @@ void waybar::modules::Custom::parseOutputRaw() text_ = line; } tooltip_ = line; - class_ = ""; + class_.clear(); } else if (i == 1) { tooltip_ = line; } else if (i == 2) { - class_ = line; + class_.push_back(line); } else { break; } @@ -152,6 +149,7 @@ void waybar::modules::Custom::parseOutputJson() { std::istringstream output(output_.out); std::string line; + class_.clear(); while (getline(output, line)) { auto parsed = parser_.parse(line); if (config_["escape"].isBool() && config_["escape"].asBool()) { @@ -165,7 +163,13 @@ void waybar::modules::Custom::parseOutputJson() alt_ = parsed["alt"].asString(); } tooltip_ = parsed["tooltip"].asString(); - class_ = parsed["class"].asString(); + if (parsed["class"].isString()) { + class_.push_back(parsed["class"].asString()); + } else if (parsed["class"].isArray()) { + for (auto const &c : parsed["class"]) { + class_.push_back(c.asString()); + } + } if (!parsed["percentage"].asString().empty() && parsed["percentage"].isUInt()) { percentage_ = parsed["percentage"].asUInt(); } else {