From 53956d9d1808061f817963c14de89287aa76bcc5 Mon Sep 17 00:00:00 2001 From: Alexis Date: Mon, 27 Aug 2018 01:36:25 +0200 Subject: [PATCH] feat(ALabel): Toggleable labels --- include/ALabel.hpp | 9 +++++++- include/modules/battery.hpp | 1 - include/modules/pulseaudio.hpp | 2 -- resources/config | 3 +++ src/ALabel.cpp | 40 ++++++++++++++++++++++++++++++---- src/modules/battery.cpp | 16 ++------------ src/modules/clock.cpp | 5 ++--- src/modules/cpu.cpp | 5 ++--- src/modules/custom.cpp | 5 ++--- src/modules/memory.cpp | 5 ++--- src/modules/network.cpp | 4 ++-- src/modules/pulseaudio.cpp | 39 ++++++++++++--------------------- src/modules/sway/window.cpp | 4 ++-- 13 files changed, 75 insertions(+), 63 deletions(-) diff --git a/include/ALabel.hpp b/include/ALabel.hpp index ef94b92d..839c5175 100644 --- a/include/ALabel.hpp +++ b/include/ALabel.hpp @@ -7,13 +7,20 @@ namespace waybar { class ALabel : public IModule { public: - ALabel(const Json::Value&); + ALabel(const Json::Value&, const std::string format); virtual ~ALabel() = default; virtual auto update() -> void; + virtual std::string getIcon(uint16_t percentage); virtual operator Gtk::Widget &(); protected: + Gtk::EventBox event_box_; Gtk::Label label_; const Json::Value& config_; + std::string format_; + private: + bool handleToggle(GdkEventButton* const& ev); + bool alt = false; + const std::string default_format_; }; } diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index da9e7587..7b612c4b 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -22,7 +22,6 @@ class Battery : public ALabel { static inline const fs::path data_dir_ = "/sys/class/power_supply/"; void worker(); - std::string getIcon(uint16_t percentage); util::SleeperThread thread_; std::vector batteries_; diff --git a/include/modules/pulseaudio.hpp b/include/modules/pulseaudio.hpp index 3e35371b..7ade2216 100644 --- a/include/modules/pulseaudio.hpp +++ b/include/modules/pulseaudio.hpp @@ -19,8 +19,6 @@ class Pulseaudio : public ALabel { static void sinkInfoCb(pa_context*, const pa_sink_info*, int, void*); static void serverInfoCb(pa_context*, const pa_server_info*, void*); - std::string getIcon(uint16_t); - pa_threaded_mainloop* mainloop_; pa_mainloop_api* mainloop_api_; pa_context* context_; diff --git a/resources/config b/resources/config index b7a08265..d5a7cf37 100644 --- a/resources/config +++ b/resources/config @@ -22,6 +22,9 @@ "sway/window": { "max-length": 50 }, + "clock": { + "format-alt": "{:%Y-%m-%d}" + }, "cpu": { "format": "{}% " }, diff --git a/src/ALabel.cpp b/src/ALabel.cpp index cd66b7b1..d64b5724 100644 --- a/src/ALabel.cpp +++ b/src/ALabel.cpp @@ -1,12 +1,22 @@ #include "ALabel.hpp" -waybar::ALabel::ALabel(const Json::Value& config) - : config_(config) +#include + +waybar::ALabel::ALabel(const Json::Value& config, const std::string format) + : config_(config), + format_(config_["format"] ? config_["format"].asString() : format), + default_format_(format_) { + event_box_.add(label_); if (config_["max-length"]) { label_.set_max_width_chars(config_["max-length"].asUInt()); label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END); } + if (config_["format-alt"]) { + event_box_.add_events(Gdk::BUTTON_PRESS_MASK); + event_box_.signal_button_press_event() + .connect(sigc::mem_fun(*this, &ALabel::handleToggle)); + } } auto waybar::ALabel::update() -> void @@ -14,6 +24,28 @@ auto waybar::ALabel::update() -> void // Nothing here } -waybar::ALabel::operator Gtk::Widget &() { - return label_; +bool waybar::ALabel::handleToggle(GdkEventButton* const& ev) +{ + alt = !alt; + if (alt) { + format_ = config_["format-alt"].asString(); + } else { + format_ = default_format_; + } + dp.emit(); + return true; +} + +std::string waybar::ALabel::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::ALabel::operator Gtk::Widget &() { + return event_box_; } diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index e0d4895b..78470e77 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -1,7 +1,7 @@ #include "modules/battery.hpp" waybar::modules::Battery::Battery(const Json::Value& config) - : ALabel(config) + : ALabel(config, "{capacity}%") { try { for (auto &node : fs::directory_iterator(data_dir_)) { @@ -62,9 +62,7 @@ auto waybar::modules::Battery::update() -> void total += capacity; } uint16_t capacity = total / batteries_.size(); - auto format = config_["format"] - ? config_["format"].asString() : "{capacity}%"; - label_.set_text(fmt::format(format, fmt::arg("capacity", capacity), + label_.set_text(fmt::format(format_, fmt::arg("capacity", capacity), fmt::arg("icon", getIcon(capacity)))); label_.set_tooltip_text(status); bool charging = status == "Charging"; @@ -83,13 +81,3 @@ auto waybar::modules::Battery::update() -> void std::cerr << e.what() << std::endl; } } - -std::string waybar::modules::Battery::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(); -} diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 0fa79f44..3fa6feba 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,7 +1,7 @@ #include "modules/clock.hpp" waybar::modules::Clock::Clock(const Json::Value& config) - : ALabel(config) + : ALabel(config, "{:%H:%M}") { label_.set_name("clock"); uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 60; @@ -17,6 +17,5 @@ waybar::modules::Clock::Clock(const Json::Value& config) auto waybar::modules::Clock::update() -> void { auto localtime = fmt::localtime(std::time(nullptr)); - auto format = config_["format"] ? config_["format"].asString() : "{:%H:%M}"; - label_.set_text(fmt::format(format, localtime)); + label_.set_text(fmt::format(format_, localtime)); } diff --git a/src/modules/cpu.cpp b/src/modules/cpu.cpp index 20c6e158..50f77768 100644 --- a/src/modules/cpu.cpp +++ b/src/modules/cpu.cpp @@ -1,7 +1,7 @@ #include "modules/cpu.hpp" waybar::modules::Cpu::Cpu(const Json::Value& config) - : ALabel(config) + : ALabel(config, "{}%") { label_.set_name("cpu"); uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 10; @@ -17,7 +17,6 @@ auto waybar::modules::Cpu::update() -> void if (sysinfo(&info) == 0) { float f_load = 1.f / (1u << SI_LOAD_SHIFT); uint16_t load = info.loads[0] * f_load * 100 / get_nprocs(); - auto format = config_["format"] ? config_["format"].asString() : "{}%"; - label_.set_text(fmt::format(format, load)); + label_.set_text(fmt::format(format_, load)); } } diff --git a/src/modules/custom.cpp b/src/modules/custom.cpp index d05abc0c..9b1a10cc 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -2,7 +2,7 @@ waybar::modules::Custom::Custom(const std::string name, const Json::Value& config) - : ALabel(config), name_(name) + : ALabel(config, "{}"), name_(name) { if (!config_["exec"]) { throw std::runtime_error(name_ + " has no exec path."); @@ -38,8 +38,7 @@ auto waybar::modules::Custom::update() -> void if (res.out.empty() || res.exit_code != 0) { label_.hide(); } else { - auto format = config_["format"] ? config_["format"].asString() : "{}"; - auto str = fmt::format(format, res.out); + auto str = fmt::format(format_, res.out); label_.set_text(str); label_.set_tooltip_text(str); label_.show(); diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp index 135457b3..d3d9877f 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -1,7 +1,7 @@ #include "modules/memory.hpp" waybar::modules::Memory::Memory(const Json::Value& config) - : ALabel(config) + : ALabel(config, "{}%") { label_.set_name("memory"); uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 30; @@ -18,8 +18,7 @@ auto waybar::modules::Memory::update() -> void auto total = info.totalram * info.mem_unit; auto freeram = info.freeram * info.mem_unit; int used_ram_percentage = 100 * (total - freeram) / total; - auto format = config_["format"] ? config_["format"].asString() : "{}%"; - label_.set_text(fmt::format(format, used_ram_percentage)); + label_.set_text(fmt::format(format_, used_ram_percentage)); auto used_ram_gigabytes = (total - freeram) / std::pow(1024, 3); label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1)); } diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 8062dc61..44f2916c 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -1,7 +1,7 @@ #include "modules/network.hpp" waybar::modules::Network::Network(const Json::Value& config) - : ALabel(config), family_(AF_INET), + : ALabel(config, "{ifname}"), family_(AF_INET), signal_strength_dbm_(0), signal_strength_(0) { sock_fd_ = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); @@ -82,7 +82,7 @@ waybar::modules::Network::~Network() auto waybar::modules::Network::update() -> void { - auto format = config_["format"] ? config_["format"].asString() : "{ifname}"; + auto format = format_; if (ifid_ <= 0) { format = config_["format-disconnected"] ? config_["format-disconnected"].asString() : format; diff --git a/src/modules/pulseaudio.cpp b/src/modules/pulseaudio.cpp index fb3925ab..5d360be7 100644 --- a/src/modules/pulseaudio.cpp +++ b/src/modules/pulseaudio.cpp @@ -1,7 +1,7 @@ #include "modules/pulseaudio.hpp" waybar::modules::Pulseaudio::Pulseaudio(const Json::Value& config) - : ALabel(config), mainloop_(nullptr), mainloop_api_(nullptr), + : ALabel(config, "{volume}%"), mainloop_(nullptr), mainloop_api_(nullptr), context_(nullptr), sink_idx_(0), volume_(0), muted_(false) { label_.set_name("pulseaudio"); @@ -100,33 +100,22 @@ void waybar::modules::Pulseaudio::sinkInfoCb(pa_context* /*context*/, void waybar::modules::Pulseaudio::serverInfoCb(pa_context *context, const pa_server_info *i, void *data) { - pa_context_get_sink_info_by_name(context, i->default_sink_name, - sinkInfoCb, data); + pa_context_get_sink_info_by_name(context, i->default_sink_name, + sinkInfoCb, data); } auto waybar::modules::Pulseaudio::update() -> void { - auto format = - config_["format"] ? config_["format"].asString() : "{volume}%"; - if (muted_) { - format = - config_["format-muted"] ? config_["format-muted"].asString() : format; - label_.get_style_context()->add_class("muted"); - } else { - label_.get_style_context()->remove_class("muted"); - } - label_.set_label(fmt::format(format, - fmt::arg("volume", volume_), - fmt::arg("icon", getIcon(volume_)))); - label_.set_tooltip_text(desc_); -} - -std::string waybar::modules::Pulseaudio::getIcon(uint16_t percentage) -{ - if (!config_["format-icons"] || !config_["format-icons"].isArray()) { - return ""; + auto format = format_; + if (muted_) { + format = + config_["format-muted"] ? config_["format-muted"].asString() : format; + label_.get_style_context()->add_class("muted"); + } else { + label_.get_style_context()->remove_class("muted"); } - auto size = config_["format-icons"].size(); - auto idx = std::clamp(percentage / (100 / size), 0U, size - 1); - return config_["format-icons"][idx].asString(); + label_.set_label(fmt::format(format, + fmt::arg("volume", volume_), + fmt::arg("icon", getIcon(volume_)))); + label_.set_tooltip_text(desc_); } diff --git a/src/modules/sway/window.cpp b/src/modules/sway/window.cpp index 306103c4..aac2b43c 100644 --- a/src/modules/sway/window.cpp +++ b/src/modules/sway/window.cpp @@ -1,7 +1,7 @@ #include "modules/sway/window.hpp" waybar::modules::sway::Window::Window(Bar &bar, const Json::Value& config) - : ALabel(config), bar_(bar) + : ALabel(config, "{}"), bar_(bar) { label_.set_name("window"); ipc_.connect(); @@ -30,7 +30,7 @@ void waybar::modules::sway::Window::worker() auto waybar::modules::sway::Window::update() -> void { - label_.set_text(window_); + label_.set_text(fmt::format(format_, window_)); label_.set_tooltip_text(window_); }