feat(percent): adds a percent class to numeric modules (#297)

feat(percent): adds a percent class to numeric modules
pull/285/head
Alex 2019-05-03 17:35:21 +02:00 committed by GitHub
commit e5d5735e9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 30 additions and 25 deletions

View File

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

View File

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

View File

@ -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;
}

View File

@ -181,6 +181,7 @@ auto waybar::modules::Backlight::update() -> void {
const auto percent = best->get_max() == 0 ? 100 : best->get_actual() * 100 / best->get_max();
label_.set_markup(fmt::format(
format_, fmt::arg("percent", std::to_string(percent)), fmt::arg("icon", getIcon(percent))));
getState(percent);
} else {
if (!previous_best_.has_value()) {
return;

View File

@ -109,30 +109,6 @@ 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();
if (status == "Unknown") {

View File

@ -20,6 +20,7 @@ auto waybar::modules::Cpu::update() -> void {
label_.set_tooltip_text(tooltip);
}
label_.set_markup(fmt::format(format_, fmt::arg("load", cpu_load), fmt::arg("usage", cpu_usage)));
getState(cpu_usage);
}
uint16_t waybar::modules::Cpu::getCpuLoad() {

View File

@ -16,6 +16,7 @@ auto waybar::modules::Memory::update() -> void {
parseMeminfo();
if (memtotal_ > 0 && memfree_ >= 0) {
int used_ram_percentage = 100 * (memtotal_ - memfree_) / memtotal_;
getState(used_ram_percentage);
label_.set_markup(fmt::format(format_, used_ram_percentage));
auto used_ram_gigabytes = (memtotal_ - memfree_) / std::pow(1024, 2);
if (tooltipEnabled()) {

View File

@ -170,6 +170,7 @@ auto waybar::modules::Network::update() -> void {
if (!alt_) {
format_ = default_format_;
}
getState(signal_strength_);
auto text = fmt::format(format_,
fmt::arg("essid", essid_),
fmt::arg("signaldBm", signal_strength_dbm_),

View File

@ -202,6 +202,7 @@ auto waybar::modules::Pulseaudio::update() -> void {
}
label_.set_markup(fmt::format(
format, fmt::arg("volume", volume_), fmt::arg("icon", getIcon(volume_, getPortIcon()))));
getState(volume_);
if (tooltipEnabled()) {
label_.set_tooltip_text(desc_);
}