feat(pulseaudio): volume icons

This commit is contained in:
Alexis 2018-08-13 22:33:07 +02:00
parent ea9a08d473
commit 1555cb71e1
6 changed files with 26 additions and 10 deletions

View File

@ -6,6 +6,7 @@
#include <iostream> #include <iostream>
#include <fmt/format.h> #include <fmt/format.h>
#include <sys/inotify.h> #include <sys/inotify.h>
#include <algorithm>
#include "util/chrono.hpp" #include "util/chrono.hpp"
#include "IModule.hpp" #include "IModule.hpp"

View File

@ -3,6 +3,7 @@
#include <pulse/pulseaudio.h> #include <pulse/pulseaudio.h>
#include <json/json.h> #include <json/json.h>
#include <fmt/format.h> #include <fmt/format.h>
#include <algorithm>
#include "IModule.hpp" #include "IModule.hpp"
namespace waybar::modules { namespace waybar::modules {
@ -13,6 +14,7 @@ namespace waybar::modules {
auto update() -> void; auto update() -> void;
operator Gtk::Widget &(); operator Gtk::Widget &();
private: private:
std::string _getIcon(uint16_t percentage);
static void _subscribeCb(pa_context *context, static void _subscribeCb(pa_context *context,
pa_subscription_event_type_t type, uint32_t idx, void *data); pa_subscription_event_type_t type, uint32_t idx, void *data);
static void _contextStateCb(pa_context *c, void *data); static void _contextStateCb(pa_context *c, void *data);

View File

@ -14,7 +14,7 @@
"format": "{}% " "format": "{}% "
}, },
"battery": { "battery": {
"format": "{value}% {icon}", "format": "{capacity}% {icon}",
"format-icons": ["", "", "", "", ""] "format-icons": ["", "", "", "", ""]
}, },
"network": { "network": {
@ -22,8 +22,9 @@
"format": "{essid} ({signalStrength}%) " "format": "{essid} ({signalStrength}%) "
}, },
"pulseaudio": { "pulseaudio": {
"format": "{}% ", "format": "{volume}% {icon}",
"format-muted": "" "format-muted": "",
"format-icons": ["", ""]
}, },
"custom/spotify": { "custom/spotify": {
"format": " {}", "format": " {}",

View File

@ -48,8 +48,9 @@ auto waybar::modules::Battery::update() -> void
total += capacity; total += capacity;
} }
uint16_t capacity = total / _batteries.size(); uint16_t capacity = total / _batteries.size();
auto format = _config["format"] ? _config["format"].asString() : "{}%"; auto format = _config["format"]
_label.set_text(fmt::format(format, fmt::arg("value", capacity), ? _config["format"].asString() : "{capacity}%";
_label.set_text(fmt::format(format, fmt::arg("capacity", capacity),
fmt::arg("icon", _getIcon(capacity)))); fmt::arg("icon", _getIcon(capacity))));
_label.set_tooltip_text(status); _label.set_tooltip_text(status);
if (charging) if (charging)
@ -68,8 +69,9 @@ auto waybar::modules::Battery::update() -> void
std::string waybar::modules::Battery::_getIcon(uint16_t percentage) std::string waybar::modules::Battery::_getIcon(uint16_t percentage)
{ {
if (!_config["format-icons"] || !_config["format-icons"].isArray()) return ""; if (!_config["format-icons"] || !_config["format-icons"].isArray()) return "";
auto step = 100 / _config["format-icons"].size(); auto size = _config["format-icons"].size();
return _config["format-icons"][percentage / step].asString(); auto idx = std::clamp(percentage / (100 / size), 0U, size - 1);
return _config["format-icons"][idx].asString();
} }
waybar::modules::Battery::operator Gtk::Widget &() waybar::modules::Battery::operator Gtk::Widget &()

View File

@ -16,7 +16,7 @@ waybar::modules::Network::Network(Json::Value config)
auto waybar::modules::Network::update() -> void auto waybar::modules::Network::update() -> void
{ {
_getInfo(); _getInfo();
auto format = _config["format"] ? _config["format"].asString() : "{}"; auto format = _config["format"] ? _config["format"].asString() : "{essid}";
_label.set_text(fmt::format(format, _label.set_text(fmt::format(format,
fmt::arg("essid", _essid), fmt::arg("essid", _essid),
fmt::arg("signaldBm", _signalStrengthdBm), fmt::arg("signaldBm", _signalStrengthdBm),

View File

@ -97,17 +97,27 @@ void waybar::modules::Pulseaudio::_serverInfoCb(pa_context *context,
auto waybar::modules::Pulseaudio::update() -> void auto waybar::modules::Pulseaudio::update() -> void
{ {
auto format = _config["format"] ? _config["format"].asString() : "{}%"; auto format = _config["format"] ? _config["format"].asString() : "{volume}%";
if (_muted) { if (_muted) {
format = format =
_config["format-muted"] ? _config["format-muted"].asString() : format; _config["format-muted"] ? _config["format-muted"].asString() : format;
_label.get_style_context()->add_class("muted"); _label.get_style_context()->add_class("muted");
} else } else
_label.get_style_context()->remove_class("muted"); _label.get_style_context()->remove_class("muted");
_label.set_label(fmt::format(format, _volume)); _label.set_label(fmt::format(format,
fmt::arg("volume", _volume),
fmt::arg("icon", _getIcon(_volume))));
_label.set_tooltip_text(_desc); _label.set_tooltip_text(_desc);
} }
std::string waybar::modules::Pulseaudio::_getIcon(uint16_t percentage)
{
if (!_config["format-icons"] || !_config["format-icons"].isArray()) return "";
auto size = _config["format-icons"].size();
auto idx = std::clamp(percentage / (100 / size), 0U, size - 1);
return _config["format-icons"][idx].asString();
}
waybar::modules::Pulseaudio::operator Gtk::Widget &() { waybar::modules::Pulseaudio::operator Gtk::Widget &() {
return _label; return _label;
} }