refactor(modules): migrate state modules to ALabel tooltip helper
Migrate cpu, cpu_usage, cpu_frequency and memory to the generic updateLabelAndTooltip/ForState helper so label and tooltip rendering go through the dedup-aware setters and shared tooltip-format resolution. Add store-accepting overloads of the helper to ALabel for modules that build a dynamic fmt argument store (per-core cpu stats). cpu keeps its tooltip-format-<state> selection via the state overload; the others keep their existing tooltip-format-only behavior.
This commit is contained in:
+1
-15
@@ -62,21 +62,7 @@ auto waybar::modules::Cpu::update() -> void {
|
||||
store.push_back(fmt::arg(arg_names.back().c_str(), core_icon));
|
||||
}
|
||||
store.push_back(fmt::arg("icons", all_icons));
|
||||
label_.set_markup(fmt::vformat(format, store));
|
||||
|
||||
if (tooltipEnabled()) {
|
||||
std::string tooltip_format;
|
||||
if (!state.empty() && config_["tooltip-format-" + state].isString()) {
|
||||
tooltip_format = config_["tooltip-format-" + state].asString();
|
||||
} else if (config_["tooltip-format"].isString()) {
|
||||
tooltip_format = config_["tooltip-format"].asString();
|
||||
}
|
||||
if (!tooltip_format.empty()) {
|
||||
label_.set_tooltip_markup(fmt::vformat(tooltip_format, store));
|
||||
} else {
|
||||
label_.set_tooltip_markup(tooltip);
|
||||
}
|
||||
}
|
||||
updateLabelAndTooltipForState(state, format, tooltip, store);
|
||||
}
|
||||
|
||||
// Call parent update
|
||||
|
||||
@@ -32,24 +32,12 @@ auto waybar::modules::CpuFrequency::update() -> void {
|
||||
} else {
|
||||
event_box_.show();
|
||||
auto icons = std::vector<std::string>{state};
|
||||
fmt::dynamic_format_arg_store<fmt::format_context> store;
|
||||
store.push_back(fmt::arg("icon", getIcon(avg_frequency, icons)));
|
||||
store.push_back(fmt::arg("max_frequency", max_frequency));
|
||||
store.push_back(fmt::arg("min_frequency", min_frequency));
|
||||
store.push_back(fmt::arg("avg_frequency", avg_frequency));
|
||||
label_.set_markup(fmt::vformat(format, store));
|
||||
|
||||
if (tooltipEnabled()) {
|
||||
std::string tooltip;
|
||||
if (config_["tooltip-format"].isString()) {
|
||||
tooltip = config_["tooltip-format"].asString();
|
||||
label_.set_tooltip_markup(fmt::vformat(tooltip, store));
|
||||
} else {
|
||||
tooltip = "Minimum frequency: {}\nAverage frequency: {}\nMaximum frequency: {}\n";
|
||||
label_.set_tooltip_markup(
|
||||
fmt::format(fmt::runtime(tooltip), min_frequency, avg_frequency, max_frequency));
|
||||
}
|
||||
}
|
||||
updateLabelAndTooltip(
|
||||
format,
|
||||
"Minimum frequency: {min_frequency}\nAverage frequency: {avg_frequency}\nMaximum "
|
||||
"frequency: {max_frequency}\n",
|
||||
fmt::arg("icon", getIcon(avg_frequency, icons)), fmt::arg("max_frequency", max_frequency),
|
||||
fmt::arg("min_frequency", min_frequency), fmt::arg("avg_frequency", avg_frequency));
|
||||
}
|
||||
|
||||
// Call parent update
|
||||
|
||||
@@ -49,16 +49,7 @@ auto waybar::modules::CpuUsage::update() -> void {
|
||||
store.push_back(fmt::arg(arg_names.back().c_str(), core_icon));
|
||||
}
|
||||
store.push_back(fmt::arg("icons", all_icons));
|
||||
label_.set_markup(fmt::vformat(format, store));
|
||||
|
||||
if (tooltipEnabled()) {
|
||||
if (config_["tooltip-format"].isString()) {
|
||||
tooltip = config_["tooltip-format"].asString();
|
||||
label_.set_tooltip_markup(fmt::vformat(tooltip, store));
|
||||
} else {
|
||||
label_.set_tooltip_markup(tooltip);
|
||||
}
|
||||
}
|
||||
updateLabelAndTooltip(format, tooltip, store);
|
||||
}
|
||||
|
||||
// Call parent update
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
#include "modules/memory.hpp"
|
||||
|
||||
namespace {
|
||||
const std::unordered_map<std::string, float> kUnits = {
|
||||
{"kB", 1.000},
|
||||
{"kiB", 1.024},
|
||||
{"MB", 1.000 * 1000.0},
|
||||
{"MiB", 1.024 * 1024.0},
|
||||
{"GB", 1.000 * 1000.0 * 1000.0},
|
||||
{"GiB", 1.024 * 1024.0 * 1024.0},
|
||||
{"TB", 1.000 * 1000.0 * 1000.0 * 1000.0},
|
||||
{"TiB", 1.024 * 1024.0 * 1024.0 * 1024.0}
|
||||
};
|
||||
const std::unordered_map<std::string, float> kUnits = {{"kB", 1.000},
|
||||
{"kiB", 1.024},
|
||||
{"MB", 1.000 * 1000.0},
|
||||
{"MiB", 1.024 * 1024.0},
|
||||
{"GB", 1.000 * 1000.0 * 1000.0},
|
||||
{"GiB", 1.024 * 1024.0 * 1024.0},
|
||||
{"TB", 1.000 * 1000.0 * 1000.0 * 1000.0},
|
||||
{"TiB", 1.024 * 1024.0 * 1024.0 * 1024.0}};
|
||||
}
|
||||
|
||||
waybar::modules::Memory::Memory(const std::string& id, const Json::Value& config)
|
||||
@@ -54,7 +52,7 @@ auto waybar::modules::Memory::update() -> void {
|
||||
if (memtotal > 0 && memfree >= 0) {
|
||||
int used_ram_percentage = 100 * (memtotal - memfree) / memtotal;
|
||||
int used_swap_percentage = 0;
|
||||
if ((bool) swaptotal) {
|
||||
if ((bool)swaptotal) {
|
||||
used_swap_percentage = 100 * (swaptotal - swapfree) / swaptotal;
|
||||
}
|
||||
|
||||
@@ -77,31 +75,14 @@ auto waybar::modules::Memory::update() -> void {
|
||||
} else {
|
||||
event_box_.show();
|
||||
auto icons = std::vector<std::string>{state};
|
||||
label_.set_markup(fmt::format(
|
||||
fmt::runtime(format), used_ram_percentage,
|
||||
fmt::arg("icon", getIcon(used_ram_percentage, icons)),
|
||||
fmt::arg("total", total_ram), fmt::arg("swapTotal", total_swap),
|
||||
fmt::arg("percentage", used_ram_percentage),
|
||||
updateLabelAndTooltip(
|
||||
format, fmt::format("{:.{}f}{} used", used_ram, 1, unit_), used_ram_percentage,
|
||||
fmt::arg("icon", getIcon(used_ram_percentage, icons)), fmt::arg("total", total_ram),
|
||||
fmt::arg("swapTotal", total_swap), fmt::arg("percentage", used_ram_percentage),
|
||||
fmt::arg("swapState", swaptotal == 0 ? "Off" : "On"),
|
||||
fmt::arg("swapPercentage", used_swap_percentage), fmt::arg("used", used_ram),
|
||||
fmt::arg("swapUsed", used_swap), fmt::arg("avail", available_ram),
|
||||
fmt::arg("swapAvail", available_swap)));
|
||||
}
|
||||
|
||||
if (tooltipEnabled()) {
|
||||
if (config_["tooltip-format"].isString()) {
|
||||
auto tooltip_format = config_["tooltip-format"].asString();
|
||||
label_.set_tooltip_markup(fmt::format(
|
||||
fmt::runtime(tooltip_format), used_ram_percentage,
|
||||
fmt::arg("total", total_ram), fmt::arg("swapTotal", total_swap),
|
||||
fmt::arg("percentage", used_ram_percentage),
|
||||
fmt::arg("swapState", swaptotal == 0 ? "Off" : "On"),
|
||||
fmt::arg("swapPercentage", used_swap_percentage), fmt::arg("used", used_ram),
|
||||
fmt::arg("swapUsed", used_swap), fmt::arg("avail", available_ram),
|
||||
fmt::arg("swapAvail", available_swap)));
|
||||
} else {
|
||||
label_.set_tooltip_markup(fmt::format("{:.{}f}{} used", used_ram, 1, unit_));
|
||||
}
|
||||
fmt::arg("swapAvail", available_swap));
|
||||
}
|
||||
} else {
|
||||
event_box_.hide();
|
||||
|
||||
Reference in New Issue
Block a user