mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
feat(states): add generic 'states' to all labels
This commit is contained in:
@ -129,6 +129,30 @@ std::string waybar::ALabel::getIcon(uint16_t percentage, const std::string& alt)
|
||||
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() {
|
||||
return config_["tooltip"].isBool() ? config_["tooltip"].asBool() : true;
|
||||
}
|
||||
|
@ -109,29 +109,7 @@ const std::string waybar::modules::Battery::getAdapterStatus(uint8_t capacity) c
|
||||
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 [capacity, status] = getInfos();
|
||||
|
Reference in New Issue
Block a user