From 014c95a9fe8a961e4c5418bfb533b12abbc6521f Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 4 Jul 2026 00:04:47 +0200 Subject: [PATCH] refactor(modules): migrate audio/network/power modules to ALabel tooltip helpers Migrate network, pulseaudio, wireplumber, jack and power_profiles_daemon to the generic ALabel tooltip helpers: - jack and power-profiles-daemon use updateLabelAndTooltip() since their label/tooltip share a single arg set and a format-string tooltip default. - network, pulseaudio and wireplumber keep their rich in-module format selection but build a single fmt arg store, render the label/tooltip through the dedup-aware setLabelMarkup()/setTooltipMarkup() setters and resolveTooltipFormat(), preserving their custom fallbacks (label text, sink/node description) and visibility handling exactly. --- src/modules/jack.cpp | 17 ++--- src/modules/network.cpp | 95 ++++++++++++--------------- src/modules/power_profiles_daemon.cpp | 20 +++--- src/modules/pulseaudio.cpp | 29 ++++---- src/modules/wireplumber.cpp | 41 +++++------- 5 files changed, 85 insertions(+), 117 deletions(-) diff --git a/src/modules/jack.cpp b/src/modules/jack.cpp index 578fb4e0..c7bd5d92 100644 --- a/src/modules/jack.cpp +++ b/src/modules/jack.cpp @@ -72,19 +72,10 @@ auto JACK::update() -> void { } else format = "{load}%"; - label_.set_markup(fmt::format(fmt::runtime(format), fmt::arg("load", std::round(load_)), - fmt::arg("bufsize", bufsize_), fmt::arg("samplerate", samplerate_), - fmt::arg("latency", fmt::format("{:.2f}", latency)), - fmt::arg("xruns", xruns_))); - - if (tooltipEnabled()) { - std::string tooltip_format = "{bufsize}/{samplerate} {latency}ms"; - if (config_["tooltip-format"].isString()) tooltip_format = config_["tooltip-format"].asString(); - label_.set_tooltip_markup(fmt::format( - fmt::runtime(tooltip_format), fmt::arg("load", std::round(load_)), - fmt::arg("bufsize", bufsize_), fmt::arg("samplerate", samplerate_), - fmt::arg("latency", fmt::format("{:.2f}", latency)), fmt::arg("xruns", xruns_))); - } + updateLabelAndTooltip( + format, "{bufsize}/{samplerate} {latency}ms", fmt::arg("load", std::round(load_)), + fmt::arg("bufsize", bufsize_), fmt::arg("samplerate", samplerate_), + fmt::arg("latency", fmt::format("{:.2f}", latency)), fmt::arg("xruns", xruns_)); // Call parent update ALabel::update(); diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 8bb54fce..04a1642d 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -350,32 +350,45 @@ auto waybar::modules::Network::update() -> void { final_ipaddr_ += ipaddr6_; } - auto text = fmt::format( - fmt::runtime(format_), fmt::arg("essid", essid_), fmt::arg("bssid", bssid_), - fmt::arg("signaldBm", signal_strength_dbm_), fmt::arg("signalStrength", signal_strength_), - fmt::arg("signalStrengthApp", signal_strength_app_), fmt::arg("ifname", ifname_), - fmt::arg("netmask", netmask_), fmt::arg("netmask6", netmask6_), - fmt::arg("ipaddr", final_ipaddr_), fmt::arg("gwaddr", gwaddr_), fmt::arg("cidr", cidr_), - fmt::arg("cidr6", cidr6_), fmt::arg("frequency", fmt::format("{:.1f}", frequency_)), - fmt::arg("icon", getIcon(signal_strength_, state_)), - fmt::arg("bandwidthDownBits", pow_format(bandwidth_down * 8ull / elapsed_seconds, "b/s")), - fmt::arg("bandwidthUpBits", pow_format(bandwidth_up * 8ull / elapsed_seconds, "b/s")), + fmt::dynamic_format_arg_store store; + store.push_back(fmt::arg("essid", essid_)); + store.push_back(fmt::arg("bssid", bssid_)); + store.push_back(fmt::arg("signaldBm", signal_strength_dbm_)); + store.push_back(fmt::arg("signalStrength", signal_strength_)); + store.push_back(fmt::arg("signalStrengthApp", signal_strength_app_)); + store.push_back(fmt::arg("ifname", ifname_)); + store.push_back(fmt::arg("netmask", netmask_)); + store.push_back(fmt::arg("netmask6", netmask6_)); + store.push_back(fmt::arg("ipaddr", final_ipaddr_)); + store.push_back(fmt::arg("gwaddr", gwaddr_)); + store.push_back(fmt::arg("cidr", cidr_)); + store.push_back(fmt::arg("cidr6", cidr6_)); + store.push_back(fmt::arg("frequency", fmt::format("{:.1f}", frequency_))); + store.push_back(fmt::arg("icon", getIcon(signal_strength_, state_))); + store.push_back( + fmt::arg("bandwidthDownBits", pow_format(bandwidth_down * 8ull / elapsed_seconds, "b/s"))); + store.push_back( + fmt::arg("bandwidthUpBits", pow_format(bandwidth_up * 8ull / elapsed_seconds, "b/s"))); + store.push_back( fmt::arg("bandwidthTotalBits", - pow_format((bandwidth_up + bandwidth_down) * 8ull / elapsed_seconds, "b/s")), - fmt::arg("bandwidthDownOctets", pow_format(bandwidth_down / elapsed_seconds, "o/s")), - fmt::arg("bandwidthUpOctets", pow_format(bandwidth_up / elapsed_seconds, "o/s")), - fmt::arg("bandwidthTotalOctets", - pow_format((bandwidth_up + bandwidth_down) / elapsed_seconds, "o/s")), - fmt::arg("bandwidthDownBytes", pow_format(bandwidth_down / elapsed_seconds, "B/s")), - fmt::arg("bandwidthUpBytes", pow_format(bandwidth_up / elapsed_seconds, "B/s")), - fmt::arg("bandwidthDownBytesCompact", - pow_format(bandwidth_down / elapsed_seconds, "B", false, 2)), - fmt::arg("bandwidthUpBytesCompact", - pow_format(bandwidth_up / elapsed_seconds, "B", false, 2)), - fmt::arg("bandwidthTotalBytes", - pow_format((bandwidth_up + bandwidth_down) / elapsed_seconds, "B/s"))); - if (text.compare(label_.get_label()) != 0) { - label_.set_markup(text); + pow_format((bandwidth_up + bandwidth_down) * 8ull / elapsed_seconds, "b/s"))); + store.push_back( + fmt::arg("bandwidthDownOctets", pow_format(bandwidth_down / elapsed_seconds, "o/s"))); + store.push_back(fmt::arg("bandwidthUpOctets", pow_format(bandwidth_up / elapsed_seconds, "o/s"))); + store.push_back(fmt::arg("bandwidthTotalOctets", + pow_format((bandwidth_up + bandwidth_down) / elapsed_seconds, "o/s"))); + store.push_back( + fmt::arg("bandwidthDownBytes", pow_format(bandwidth_down / elapsed_seconds, "B/s"))); + store.push_back(fmt::arg("bandwidthUpBytes", pow_format(bandwidth_up / elapsed_seconds, "B/s"))); + store.push_back(fmt::arg("bandwidthDownBytesCompact", + pow_format(bandwidth_down / elapsed_seconds, "B", false, 2))); + store.push_back(fmt::arg("bandwidthUpBytesCompact", + pow_format(bandwidth_up / elapsed_seconds, "B", false, 2))); + store.push_back(fmt::arg("bandwidthTotalBytes", + pow_format((bandwidth_up + bandwidth_down) / elapsed_seconds, "B/s"))); + + auto text = fmt::vformat(format_, store); + if (setLabelMarkup(text)) { if (text.empty()) { event_box_.hide(); } else { @@ -387,35 +400,9 @@ auto waybar::modules::Network::update() -> void { tooltip_format = config_["tooltip-format"].asString(); } if (!tooltip_format.empty()) { - auto tooltip_text = fmt::format( - fmt::runtime(tooltip_format), fmt::arg("essid", essid_), fmt::arg("bssid", bssid_), - fmt::arg("signaldBm", signal_strength_dbm_), fmt::arg("signalStrength", signal_strength_), - fmt::arg("signalStrengthApp", signal_strength_app_), fmt::arg("ifname", ifname_), - fmt::arg("netmask", netmask_), fmt::arg("netmask6", netmask6_), - fmt::arg("ipaddr", final_ipaddr_), fmt::arg("gwaddr", gwaddr_), fmt::arg("cidr", cidr_), - fmt::arg("cidr6", cidr6_), fmt::arg("frequency", fmt::format("{:.1f}", frequency_)), - fmt::arg("icon", getIcon(signal_strength_, state_)), - fmt::arg("bandwidthDownBits", pow_format(bandwidth_down * 8ull / elapsed_seconds, "b/s")), - fmt::arg("bandwidthUpBits", pow_format(bandwidth_up * 8ull / elapsed_seconds, "b/s")), - fmt::arg("bandwidthTotalBits", - pow_format((bandwidth_up + bandwidth_down) * 8ull / elapsed_seconds, "b/s")), - fmt::arg("bandwidthDownOctets", pow_format(bandwidth_down / elapsed_seconds, "o/s")), - fmt::arg("bandwidthUpOctets", pow_format(bandwidth_up / elapsed_seconds, "o/s")), - fmt::arg("bandwidthTotalOctets", - pow_format((bandwidth_up + bandwidth_down) / elapsed_seconds, "o/s")), - fmt::arg("bandwidthDownBytes", pow_format(bandwidth_down / elapsed_seconds, "B/s")), - fmt::arg("bandwidthUpBytes", pow_format(bandwidth_up / elapsed_seconds, "B/s")), - fmt::arg("bandwidthDownBytesCompact", - pow_format(bandwidth_down / elapsed_seconds, "B", false, 2)), - fmt::arg("bandwidthUpBytesCompact", - pow_format(bandwidth_up / elapsed_seconds, "B", false, 2)), - fmt::arg("bandwidthTotalBytes", - pow_format((bandwidth_up + bandwidth_down) / elapsed_seconds, "B/s"))); - if (label_.get_tooltip_text() != tooltip_text) { - label_.set_tooltip_markup(tooltip_text); - } - } else if (label_.get_tooltip_text() != text) { - label_.set_tooltip_markup(text); + setTooltipMarkup(fmt::vformat(tooltip_format, store)); + } else { + setTooltipMarkup(text); } } diff --git a/src/modules/power_profiles_daemon.cpp b/src/modules/power_profiles_daemon.cpp index d7eb5bcd..b897cd1c 100644 --- a/src/modules/power_profiles_daemon.cpp +++ b/src/modules/power_profiles_daemon.cpp @@ -187,18 +187,14 @@ void PowerProfilesDaemon::switchToProfile(std::string const& str) { auto PowerProfilesDaemon::update() -> void { if (connected_ && activeProfile_ != availableProfiles_.end()) { auto profile = (*activeProfile_); - // Set label - fmt::dynamic_format_arg_store store; - store.push_back(fmt::arg("profile", profile.name)); - // Legacy placeholder, kept for backward compatibility with existing configs. - store.push_back(fmt::arg("driver", profile.driver)); - store.push_back(fmt::arg("cpu_driver", profile.cpuDriver)); - store.push_back(fmt::arg("platform_driver", profile.platformDriver)); - store.push_back(fmt::arg("icon", getIcon(0, profile.name))); - label_.set_markup(fmt::vformat(format_, store)); - if (tooltipEnabled()) { - label_.set_tooltip_markup(fmt::vformat(tooltipFormat_, store)); - } + // Set label and tooltip + updateLabelAndTooltip(format_, tooltipFormat_, fmt::arg("profile", profile.name), + // Legacy placeholder, kept for backward compatibility with existing + // configs. + fmt::arg("driver", profile.driver), + fmt::arg("cpu_driver", profile.cpuDriver), + fmt::arg("platform_driver", profile.platformDriver), + fmt::arg("icon", getIcon(0, profile.name))); // Set CSS class if (!currentStyle_.empty()) { diff --git a/src/modules/pulseaudio.cpp b/src/modules/pulseaudio.cpp index 10fb74f9..874f5d97 100644 --- a/src/modules/pulseaudio.cpp +++ b/src/modules/pulseaudio.cpp @@ -72,7 +72,6 @@ const std::vector waybar::modules::Pulseaudio::getPulseIcon() const auto waybar::modules::Pulseaudio::update() -> void { auto format = format_; - std::string tooltip_format; auto sink_volume = backend->getSinkVolume(); if (!alt_) { std::string format_name = "format"; @@ -120,29 +119,29 @@ auto waybar::modules::Pulseaudio::update() -> void { auto source_desc = backend->getSourceDesc(); format_source = fmt::format(fmt::runtime(format_source), fmt::arg("volume", source_volume)); - auto text = fmt::format( - fmt::runtime(format), fmt::arg("desc", sink_desc), fmt::arg("volume", sink_volume), - fmt::arg("format_source", format_source), fmt::arg("source_volume", source_volume), - fmt::arg("source_desc", source_desc), fmt::arg("icon", getIcon(sink_volume, getPulseIcon()))); + + fmt::dynamic_format_arg_store store; + store.push_back(fmt::arg("desc", sink_desc)); + store.push_back(fmt::arg("volume", sink_volume)); + store.push_back(fmt::arg("format_source", format_source)); + store.push_back(fmt::arg("source_volume", source_volume)); + store.push_back(fmt::arg("source_desc", source_desc)); + store.push_back(fmt::arg("icon", getIcon(sink_volume, getPulseIcon()))); + + auto text = fmt::vformat(format, store); if (text.empty()) { label_.hide(); } else { - label_.set_markup(text); + setLabelMarkup(text); label_.show(); } if (tooltipEnabled()) { - if (tooltip_format.empty() && config_["tooltip-format"].isString()) { - tooltip_format = config_["tooltip-format"].asString(); - } + auto tooltip_format = resolveTooltipFormat(""); if (!tooltip_format.empty()) { - label_.set_tooltip_markup(fmt::format( - fmt::runtime(tooltip_format), fmt::arg("desc", sink_desc), - fmt::arg("volume", sink_volume), fmt::arg("format_source", format_source), - fmt::arg("source_volume", source_volume), fmt::arg("source_desc", source_desc), - fmt::arg("icon", getIcon(sink_volume, getPulseIcon())))); + setTooltipMarkup(fmt::vformat(tooltip_format, store)); } else { - label_.set_tooltip_markup(sink_desc); + setTooltipMarkup(sink_desc); } } diff --git a/src/modules/wireplumber.cpp b/src/modules/wireplumber.cpp index 751e885c..e6a3c64b 100644 --- a/src/modules/wireplumber.cpp +++ b/src/modules/wireplumber.cpp @@ -432,7 +432,6 @@ void waybar::modules::Wireplumber::asyncLoadRequiredApiModules() { auto waybar::modules::Wireplumber::update() -> void { auto format = format_; - std::string tooltipFormat; // Handle sink mute state if (muted_) { @@ -486,32 +485,28 @@ auto waybar::modules::Wireplumber::update() -> void { std::string formatted_source = fmt::format(fmt::runtime(format_source), fmt::arg("volume", source_vol)); - std::string markup = - fmt::format(fmt::runtime(format), fmt::arg("node_name", node_name_), fmt::arg("volume", vol), - fmt::arg("icon", getIcon(vol)), fmt::arg("format_source", formatted_source), - fmt::arg("source_volume", source_vol), fmt::arg("source_desc", source_name_), - fmt::arg("volume_linear", volume_), fmt::arg("volume_cubic", vol_cube), - fmt::arg("volume_db", vol_db), fmt::arg("source_volume_linear", source_volume_), - fmt::arg("source_volume_cubic", source_vol_cube), - fmt::arg("source_volume_db", source_vol_db)); - label_.set_markup(markup); + fmt::dynamic_format_arg_store store; + store.push_back(fmt::arg("node_name", node_name_)); + store.push_back(fmt::arg("volume", vol)); + store.push_back(fmt::arg("icon", getIcon(vol))); + store.push_back(fmt::arg("format_source", formatted_source)); + store.push_back(fmt::arg("source_volume", source_vol)); + store.push_back(fmt::arg("source_desc", source_name_)); + store.push_back(fmt::arg("volume_linear", volume_)); + store.push_back(fmt::arg("volume_cubic", vol_cube)); + store.push_back(fmt::arg("volume_db", vol_db)); + store.push_back(fmt::arg("source_volume_linear", source_volume_)); + store.push_back(fmt::arg("source_volume_cubic", source_vol_cube)); + store.push_back(fmt::arg("source_volume_db", source_vol_db)); + + setLabelMarkup(fmt::vformat(format, store)); if (tooltipEnabled()) { - if (tooltipFormat.empty() && config_["tooltip-format"].isString()) { - tooltipFormat = config_["tooltip-format"].asString(); - } - + auto tooltipFormat = resolveTooltipFormat(""); if (!tooltipFormat.empty()) { - label_.set_tooltip_markup(fmt::format( - fmt::runtime(tooltipFormat), fmt::arg("node_name", node_name_), fmt::arg("volume", vol), - fmt::arg("icon", getIcon(vol)), fmt::arg("format_source", formatted_source), - fmt::arg("source_volume", source_vol), fmt::arg("source_desc", source_name_), - fmt::arg("volume_linear", volume_), fmt::arg("volume_cubic", vol_cube), - fmt::arg("volume_db", vol_db), fmt::arg("source_volume_linear", source_volume_), - fmt::arg("source_volume_cubic", source_vol_cube), - fmt::arg("source_volume_db", source_vol_db))); + setTooltipMarkup(fmt::vformat(tooltipFormat, store)); } else { - label_.set_tooltip_markup(node_name_); + setTooltipMarkup(node_name_); } }