diff --git a/include/ALabel.hpp b/include/ALabel.hpp index 4b04c00d..bb68768d 100644 --- a/include/ALabel.hpp +++ b/include/ALabel.hpp @@ -1,10 +1,14 @@ #pragma once +#include +#include #include #include #include #include +#include +#include #include "AModule.hpp" @@ -30,6 +34,41 @@ class ALabel : public AModule { bool setLabelMarkup(const Glib::ustring& markup); bool setTooltipMarkup(const Glib::ustring& markup); + // Resolve the tooltip format string: prefers `tooltip-format-` (when a + // non-empty state is given), then `tooltip-format`, then `defaultFormat`. + std::string resolveTooltipFormat(const std::string& defaultFormat, + const std::string& state = "") const { + if (!state.empty() && config_["tooltip-format-" + state].isString()) { + return config_["tooltip-format-" + state].asString(); + } + if (config_["tooltip-format"].isString()) { + return config_["tooltip-format"].asString(); + } + return defaultFormat; + } + + // Combined label + tooltip helper. Builds a single fmt argument store from + // `args`, renders `labelFormat` into the label and the resolved tooltip format + // into the tooltip, both through the dedup-aware setters. Honors the `tooltip` + // toggle. This replaces the label/tooltip formatting boilerplate that modules + // used to duplicate. `state` selects `tooltip-format-` when non-empty. + template + void updateLabelAndTooltipForState(const std::string& state, const std::string& labelFormat, + const std::string& tooltipDefault, Args&&... args) { + fmt::dynamic_format_arg_store store; + (store.push_back(std::forward(args)), ...); + setLabelMarkup(fmt::vformat(labelFormat, store)); + if (tooltipEnabled()) { + setTooltipMarkup(fmt::vformat(resolveTooltipFormat(tooltipDefault, state), store)); + } + } + + template + void updateLabelAndTooltip(const std::string& labelFormat, const std::string& tooltipDefault, + Args&&... args) { + updateLabelAndTooltipForState("", labelFormat, tooltipDefault, std::forward(args)...); + } + bool handleToggle(GdkEventButton* const& e) override; void copyToClipboard(const std::string&); virtual std::string getState(uint8_t value, bool lesser = false); diff --git a/src/modules/disk.cpp b/src/modules/disk.cpp index 462940b2..5b9d7e41 100644 --- a/src/modules/disk.cpp +++ b/src/modules/disk.cpp @@ -68,25 +68,13 @@ auto waybar::modules::Disk::update() -> void { event_box_.hide(); } else { event_box_.show(); - label_.set_markup(fmt::format( - fmt::runtime(format), stats.f_bavail * 100 / stats.f_blocks, fmt::arg("free", free), + updateLabelAndTooltip( + format, "{used} used out of {total} on {path} ({percentage_used}%)", + stats.f_bavail * 100 / stats.f_blocks, fmt::arg("free", free), fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks), fmt::arg("used", used), fmt::arg("percentage_used", percentage_used), fmt::arg("total", total), fmt::arg("path", path_), fmt::arg("specific_free", specific_free), - fmt::arg("specific_used", specific_used), fmt::arg("specific_total", specific_total))); - } - - if (tooltipEnabled()) { - std::string tooltip_format = "{used} used out of {total} on {path} ({percentage_used}%)"; - if (config_["tooltip-format"].isString()) { - tooltip_format = config_["tooltip-format"].asString(); - } - label_.set_tooltip_markup(fmt::format( - fmt::runtime(tooltip_format), stats.f_bavail * 100 / stats.f_blocks, fmt::arg("free", free), - fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks), fmt::arg("used", used), - fmt::arg("percentage_used", percentage_used), fmt::arg("total", total), - fmt::arg("path", path_), fmt::arg("specific_free", specific_free), - fmt::arg("specific_used", specific_used), fmt::arg("specific_total", specific_total))); + fmt::arg("specific_used", specific_used), fmt::arg("specific_total", specific_total)); } // Call parent update ALabel::update(); diff --git a/src/modules/temperature.cpp b/src/modules/temperature.cpp index 1d5e6522..4b410cd3 100644 --- a/src/modules/temperature.cpp +++ b/src/modules/temperature.cpp @@ -155,19 +155,10 @@ auto waybar::modules::Temperature::update() -> void { event_box_.show(); auto max_temp = config_["critical-threshold"].isInt() ? config_["critical-threshold"].asInt() : 0; - label_.set_markup(fmt::format(fmt::runtime(format), fmt::arg("temperatureC", temperature_c), - fmt::arg("temperatureF", temperature_f), - fmt::arg("temperatureK", temperature_k), - fmt::arg("icon", getIcon(temperature_c, "", max_temp)))); - if (tooltipEnabled()) { - std::string tooltip_format = "{temperatureC}°C"; - if (config_["tooltip-format"].isString()) { - tooltip_format = config_["tooltip-format"].asString(); - } - label_.set_tooltip_markup(fmt::format( - fmt::runtime(tooltip_format), fmt::arg("temperatureC", temperature_c), - fmt::arg("temperatureF", temperature_f), fmt::arg("temperatureK", temperature_k))); - } + updateLabelAndTooltip(format, "{temperatureC}°C", fmt::arg("temperatureC", temperature_c), + fmt::arg("temperatureF", temperature_f), + fmt::arg("temperatureK", temperature_k), + fmt::arg("icon", getIcon(temperature_c, "", max_temp))); // Call parent update ALabel::update(); }