Merge remote-tracking branch 'origin/mig-B' into refactor/generic-tooltip

This commit is contained in:
Alex
2026-07-04 00:06:12 +02:00
5 changed files with 42 additions and 76 deletions
+20
View File
@@ -69,6 +69,26 @@ class ALabel : public AModule {
updateLabelAndTooltipForState("", labelFormat, tooltipDefault, std::forward<Args>(args)...); updateLabelAndTooltipForState("", labelFormat, tooltipDefault, std::forward<Args>(args)...);
} }
// Overloads accepting a pre-built argument store, for modules that must
// assemble a dynamic set of format arguments (e.g. per-core CPU stats) that
// cannot be expressed through a fixed variadic call.
// A non-const reference is used so this overload is preferred over the
// variadic template above (which would otherwise bind the store as a single
// forwarded argument).
void updateLabelAndTooltipForState(const std::string& state, const std::string& labelFormat,
const std::string& tooltipDefault,
fmt::dynamic_format_arg_store<fmt::format_context>& store) {
setLabelMarkup(fmt::vformat(labelFormat, store));
if (tooltipEnabled()) {
setTooltipMarkup(fmt::vformat(resolveTooltipFormat(tooltipDefault, state), store));
}
}
void updateLabelAndTooltip(const std::string& labelFormat, const std::string& tooltipDefault,
fmt::dynamic_format_arg_store<fmt::format_context>& store) {
updateLabelAndTooltipForState("", labelFormat, tooltipDefault, store);
}
bool handleToggle(GdkEventButton* const& e) override; bool handleToggle(GdkEventButton* const& e) override;
void copyToClipboard(const std::string&); void copyToClipboard(const std::string&);
virtual std::string getState(uint8_t value, bool lesser = false); virtual std::string getState(uint8_t value, bool lesser = false);
+1 -15
View File
@@ -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(arg_names.back().c_str(), core_icon));
} }
store.push_back(fmt::arg("icons", all_icons)); store.push_back(fmt::arg("icons", all_icons));
label_.set_markup(fmt::vformat(format, store)); updateLabelAndTooltipForState(state, format, tooltip, 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);
}
}
} }
// Call parent update // Call parent update
+6 -18
View File
@@ -32,24 +32,12 @@ auto waybar::modules::CpuFrequency::update() -> void {
} else { } else {
event_box_.show(); event_box_.show();
auto icons = std::vector<std::string>{state}; auto icons = std::vector<std::string>{state};
fmt::dynamic_format_arg_store<fmt::format_context> store; updateLabelAndTooltip(
store.push_back(fmt::arg("icon", getIcon(avg_frequency, icons))); format,
store.push_back(fmt::arg("max_frequency", max_frequency)); "Minimum frequency: {min_frequency}\nAverage frequency: {avg_frequency}\nMaximum "
store.push_back(fmt::arg("min_frequency", min_frequency)); "frequency: {max_frequency}\n",
store.push_back(fmt::arg("avg_frequency", avg_frequency)); fmt::arg("icon", getIcon(avg_frequency, icons)), fmt::arg("max_frequency", max_frequency),
label_.set_markup(fmt::vformat(format, store)); fmt::arg("min_frequency", min_frequency), fmt::arg("avg_frequency", avg_frequency));
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));
}
}
} }
// Call parent update // Call parent update
+1 -10
View File
@@ -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(arg_names.back().c_str(), core_icon));
} }
store.push_back(fmt::arg("icons", all_icons)); store.push_back(fmt::arg("icons", all_icons));
label_.set_markup(fmt::vformat(format, store)); updateLabelAndTooltip(format, tooltip, 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);
}
}
} }
// Call parent update // Call parent update
+8 -27
View File
@@ -1,16 +1,14 @@
#include "modules/memory.hpp" #include "modules/memory.hpp"
namespace { namespace {
const std::unordered_map<std::string, float> kUnits = { const std::unordered_map<std::string, float> kUnits = {{"kB", 1.000},
{"kB", 1.000},
{"kiB", 1.024}, {"kiB", 1.024},
{"MB", 1.000 * 1000.0}, {"MB", 1.000 * 1000.0},
{"MiB", 1.024 * 1024.0}, {"MiB", 1.024 * 1024.0},
{"GB", 1.000 * 1000.0 * 1000.0}, {"GB", 1.000 * 1000.0 * 1000.0},
{"GiB", 1.024 * 1024.0 * 1024.0}, {"GiB", 1.024 * 1024.0 * 1024.0},
{"TB", 1.000 * 1000.0 * 1000.0 * 1000.0}, {"TB", 1.000 * 1000.0 * 1000.0 * 1000.0},
{"TiB", 1.024 * 1024.0 * 1024.0 * 1024.0} {"TiB", 1.024 * 1024.0 * 1024.0 * 1024.0}};
};
} }
waybar::modules::Memory::Memory(const std::string& id, const Json::Value& config) 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) { if (memtotal > 0 && memfree >= 0) {
int used_ram_percentage = 100 * (memtotal - memfree) / memtotal; int used_ram_percentage = 100 * (memtotal - memfree) / memtotal;
int used_swap_percentage = 0; int used_swap_percentage = 0;
if ((bool) swaptotal) { if ((bool)swaptotal) {
used_swap_percentage = 100 * (swaptotal - swapfree) / swaptotal; used_swap_percentage = 100 * (swaptotal - swapfree) / swaptotal;
} }
@@ -77,31 +75,14 @@ auto waybar::modules::Memory::update() -> void {
} else { } else {
event_box_.show(); event_box_.show();
auto icons = std::vector<std::string>{state}; auto icons = std::vector<std::string>{state};
label_.set_markup(fmt::format( updateLabelAndTooltip(
fmt::runtime(format), used_ram_percentage, format, fmt::format("{:.{}f}{} used", used_ram, 1, unit_), used_ram_percentage,
fmt::arg("icon", getIcon(used_ram_percentage, icons)), fmt::arg("icon", getIcon(used_ram_percentage, icons)), fmt::arg("total", total_ram),
fmt::arg("total", total_ram), fmt::arg("swapTotal", total_swap), fmt::arg("swapTotal", total_swap), fmt::arg("percentage", used_ram_percentage),
fmt::arg("percentage", used_ram_percentage),
fmt::arg("swapState", swaptotal == 0 ? "Off" : "On"), fmt::arg("swapState", swaptotal == 0 ? "Off" : "On"),
fmt::arg("swapPercentage", used_swap_percentage), fmt::arg("used", used_ram), fmt::arg("swapPercentage", used_swap_percentage), fmt::arg("used", used_ram),
fmt::arg("swapUsed", used_swap), fmt::arg("avail", available_ram), fmt::arg("swapUsed", used_swap), fmt::arg("avail", available_ram),
fmt::arg("swapAvail", available_swap))); 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_));
}
} }
} else { } else {
event_box_.hide(); event_box_.hide();