feat(states): add generic 'states' to all labels

This commit is contained in:
Dan 2019-05-02 21:39:34 -04:00
parent 472363a623
commit e158a3e132
4 changed files with 27 additions and 24 deletions

View File

@ -31,6 +31,7 @@ class ALabel : public IModule {
virtual bool handleToggle(GdkEventButton *const &ev); virtual bool handleToggle(GdkEventButton *const &ev);
virtual bool handleScroll(GdkEventScroll *); virtual bool handleScroll(GdkEventScroll *);
virtual std::string getState(uint8_t value);
private: private:
std::vector<int> pid_; std::vector<int> pid_;

View File

@ -34,7 +34,7 @@ class Battery : public ALabel {
void worker(); void worker();
const std::string getAdapterStatus(uint8_t capacity) const; const std::string getAdapterStatus(uint8_t capacity) const;
const std::tuple<uint8_t, std::string> getInfos() const; const std::tuple<uint8_t, std::string> getInfos() const;
const std::string getState(uint8_t) const; // const std::string getState(uint8_t) const;
util::SleeperThread thread_; util::SleeperThread thread_;
util::SleeperThread thread_timer_; util::SleeperThread thread_timer_;

View File

@ -129,6 +129,30 @@ std::string waybar::ALabel::getIcon(uint16_t percentage, const std::string& alt)
return ""; return "";
} }
std::string waybar::ALabel::getState(uint8_t value) {
// Get current state
std::vector<std::pair<std::string, uint8_t>> states;
if (config_["states"].isObject()) {
for (auto it = config_["states"].begin(); it != config_["states"].end(); ++it) {
if (it->isUInt() && it.key().isString()) {
states.emplace_back(it.key().asString(), it->asUInt());
}
}
}
// Sort states
std::sort(states.begin(), states.end(), [](auto& a, auto& b) { return a.second < b.second; });
std::string valid_state;
for (auto const& state : states) {
if (value <= state.second && valid_state.empty()) {
label_.get_style_context()->add_class(state.first);
valid_state = state.first;
} else {
label_.get_style_context()->remove_class(state.first);
}
}
return valid_state;
}
bool waybar::ALabel::tooltipEnabled() { bool waybar::ALabel::tooltipEnabled() {
return config_["tooltip"].isBool() ? config_["tooltip"].asBool() : true; return config_["tooltip"].isBool() ? config_["tooltip"].asBool() : true;
} }

View File

@ -109,29 +109,7 @@ const std::string waybar::modules::Battery::getAdapterStatus(uint8_t capacity) c
return "Unknown"; return "Unknown";
} }
const std::string waybar::modules::Battery::getState(uint8_t capacity) const {
// Get current state
std::vector<std::pair<std::string, uint8_t>> states;
if (config_["states"].isObject()) {
for (auto it = config_["states"].begin(); it != config_["states"].end(); ++it) {
if (it->isUInt() && it.key().isString()) {
states.emplace_back(it.key().asString(), it->asUInt());
}
}
}
// Sort states
std::sort(states.begin(), states.end(), [](auto& a, auto& b) { return a.second < b.second; });
std::string valid_state;
for (auto const& state : states) {
if (capacity <= state.second && valid_state.empty()) {
label_.get_style_context()->add_class(state.first);
valid_state = state.first;
} else {
label_.get_style_context()->remove_class(state.first);
}
}
return valid_state;
}
auto waybar::modules::Battery::update() -> void { auto waybar::modules::Battery::update() -> void {
auto [capacity, status] = getInfos(); auto [capacity, status] = getInfos();