From b1fd4d7b8287bfa400bdc27c0d27734ae53af289 Mon Sep 17 00:00:00 2001 From: Alexis Date: Sat, 18 Aug 2018 11:43:48 +0200 Subject: [PATCH] feat(modules): generic label module to allow max-length on all labels --- include/ALabel.hpp | 19 +++++++++++++++++++ include/modules/battery.hpp | 8 ++------ include/modules/clock.hpp | 8 ++------ include/modules/cpu.hpp | 8 ++------ include/modules/custom.hpp | 8 ++------ include/modules/memory.hpp | 8 ++------ include/modules/network.hpp | 9 ++------- include/modules/pulseaudio.hpp | 8 ++------ include/modules/sway/window.hpp | 7 ++----- resources/config | 3 +++ src/ALabel.cpp | 24 ++++++++++++++++++++++++ src/modules/battery.cpp | 7 +------ src/modules/clock.cpp | 8 ++------ src/modules/cpu.cpp | 8 ++------ src/modules/custom.cpp | 12 ++---------- src/modules/memory.cpp | 8 ++------ src/modules/network.cpp | 8 ++------ src/modules/pulseaudio.cpp | 8 ++------ src/modules/sway/window.cpp | 6 +----- 19 files changed, 76 insertions(+), 99 deletions(-) create mode 100644 include/ALabel.hpp create mode 100644 src/ALabel.cpp diff --git a/include/ALabel.hpp b/include/ALabel.hpp new file mode 100644 index 0000000..9c1dc4c --- /dev/null +++ b/include/ALabel.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include +#include "IModule.hpp" + +namespace waybar { + +class ALabel : public IModule { + public: + ALabel(Json::Value); + virtual ~ALabel(); + virtual auto update() -> void; + virtual operator Gtk::Widget &(); + protected: + Gtk::Label label_; + Json::Value config_; +}; + +} diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 8263847..e5662a3 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include #include #include @@ -8,24 +7,21 @@ #include #include #include "util/chrono.hpp" -#include "IModule.hpp" +#include "ALabel.hpp" namespace waybar::modules { namespace fs = std::filesystem; -class Battery : public IModule { +class Battery : public ALabel { public: Battery(Json::Value); auto update() -> void; - operator Gtk::Widget&(); private: std::string getIcon(uint16_t percentage); static inline const fs::path data_dir_ = "/sys/class/power_supply/"; - Gtk::Label label_; - Json::Value config_; util::SleeperThread thread_; std::vector batteries_; }; diff --git a/include/modules/clock.hpp b/include/modules/clock.hpp index ae33274..2634e7b 100644 --- a/include/modules/clock.hpp +++ b/include/modules/clock.hpp @@ -1,22 +1,18 @@ #pragma once -#include #include #include "fmt/time.h" #include "util/chrono.hpp" -#include "IModule.hpp" +#include "ALabel.hpp" namespace waybar::modules { -class Clock : public IModule { +class Clock : public ALabel { public: Clock(Json::Value); auto update() -> void; - operator Gtk::Widget &(); private: - Gtk::Label label_; waybar::util::SleeperThread thread_; - Json::Value config_; }; } diff --git a/include/modules/cpu.hpp b/include/modules/cpu.hpp index 52f455f..e9c9fdf 100644 --- a/include/modules/cpu.hpp +++ b/include/modules/cpu.hpp @@ -1,22 +1,18 @@ #pragma once -#include #include #include #include "util/chrono.hpp" -#include "IModule.hpp" +#include "ALabel.hpp" namespace waybar::modules { -class Cpu : public IModule { +class Cpu : public ALabel { public: Cpu(Json::Value); auto update() -> void; - operator Gtk::Widget &(); private: - Gtk::Label label_; waybar::util::SleeperThread thread_; - Json::Value config_; }; } diff --git a/include/modules/custom.hpp b/include/modules/custom.hpp index 031d51c..5313b17 100644 --- a/include/modules/custom.hpp +++ b/include/modules/custom.hpp @@ -1,22 +1,18 @@ #pragma once -#include #include #include "util/chrono.hpp" -#include "IModule.hpp" +#include "ALabel.hpp" namespace waybar::modules { -class Custom : public IModule { +class Custom : public ALabel { public: Custom(const std::string&, Json::Value); auto update() -> void; - operator Gtk::Widget &(); private: const std::string name_; - Gtk::Label label_; waybar::util::SleeperThread thread_; - Json::Value config_; }; } diff --git a/include/modules/memory.hpp b/include/modules/memory.hpp index fbe147b..94bc6fd 100644 --- a/include/modules/memory.hpp +++ b/include/modules/memory.hpp @@ -1,22 +1,18 @@ #pragma once -#include #include #include #include "util/chrono.hpp" -#include "IModule.hpp" +#include "ALabel.hpp" namespace waybar::modules { -class Memory : public IModule { +class Memory : public ALabel { public: Memory(Json::Value); auto update() -> void; - operator Gtk::Widget &(); private: - Gtk::Label label_; waybar::util::SleeperThread thread_; - Json::Value config_; }; } diff --git a/include/modules/network.hpp b/include/modules/network.hpp index 2f65551..cbed929 100644 --- a/include/modules/network.hpp +++ b/include/modules/network.hpp @@ -5,18 +5,16 @@ #include #include #include -#include #include #include "util/chrono.hpp" -#include "IModule.hpp" +#include "ALabel.hpp" namespace waybar::modules { -class Network : public IModule { +class Network : public ALabel { public: Network(Json::Value); auto update() -> void; - operator Gtk::Widget &(); private: static uint64_t netlinkRequest(int, void*, uint32_t, uint32_t groups = 0); static uint64_t netlinkResponse(int, void*, uint32_t, uint32_t groups = 0); @@ -29,10 +27,7 @@ class Network : public IModule { bool associatedOrJoined(struct nlattr**); auto getInfo() -> void; - Gtk::Label label_; waybar::util::SleeperThread thread_; - Json::Value config_; - int ifid_; sa_family_t family_; int sock_fd_; diff --git a/include/modules/pulseaudio.hpp b/include/modules/pulseaudio.hpp index 5f813a1..65d4e0d 100644 --- a/include/modules/pulseaudio.hpp +++ b/include/modules/pulseaudio.hpp @@ -1,18 +1,16 @@ #pragma once #include -#include #include #include -#include "IModule.hpp" +#include "ALabel.hpp" namespace waybar::modules { -class Pulseaudio : public IModule { +class Pulseaudio : public ALabel { public: Pulseaudio(Json::Value); auto update() -> void; - operator Gtk::Widget &(); private: static void subscribeCb(pa_context*, pa_subscription_event_type_t, uint32_t, void*); @@ -22,8 +20,6 @@ class Pulseaudio : public IModule { std::string getIcon(uint16_t); - Gtk::Label label_; - Json::Value config_; pa_threaded_mainloop* mainloop_; pa_mainloop_api* mainloop_api_; pa_context* context_; diff --git a/include/modules/sway/window.hpp b/include/modules/sway/window.hpp index eaf247b..3a89825 100644 --- a/include/modules/sway/window.hpp +++ b/include/modules/sway/window.hpp @@ -5,23 +5,20 @@ #include "client.hpp" #include "util/chrono.hpp" #include "util/json.hpp" -#include "IModule.hpp" +#include "ALabel.hpp" namespace waybar::modules::sway { -class Window : public IModule { +class Window : public ALabel { public: Window(waybar::Bar&, Json::Value); auto update() -> void; - operator Gtk::Widget &(); private: std::string getFocusedNode(Json::Value nodes); void getFocusedWindow(); Bar& bar_; - Json::Value config_; waybar::util::SleeperThread thread_; - Gtk::Label label_; util::JsonParser parser_; int ipcfd_; int ipc_eventfd_; diff --git a/resources/config b/resources/config index ba3073f..a9e107b 100644 --- a/resources/config +++ b/resources/config @@ -19,6 +19,9 @@ // "5": "" // } }, + "sway/window": { + "max-length": 50 + }, "cpu": { "format": "{}% " }, diff --git a/src/ALabel.cpp b/src/ALabel.cpp new file mode 100644 index 0000000..f20bf21 --- /dev/null +++ b/src/ALabel.cpp @@ -0,0 +1,24 @@ +#include "ALabel.hpp" + +waybar::ALabel::ALabel(Json::Value config) + : config_(std::move(config)) +{ + if (config_["max-length"]) { + label_.set_max_width_chars(config_["max-length"].asUInt()); + label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END); + } +} + +waybar::ALabel::~ALabel() +{ + // Nothing here +} + +auto waybar::ALabel::update() -> void +{ + // Nothing here +} + +waybar::ALabel::operator Gtk::Widget &() { + return label_; +} diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index fe18b7b..0fa38b9 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -1,7 +1,7 @@ #include "modules/battery.hpp" waybar::modules::Battery::Battery(Json::Value config) - : config_(std::move(config)) + : ALabel(std::move(config)) { try { for (auto &node : fs::directory_iterator(data_dir_)) { @@ -83,8 +83,3 @@ std::string waybar::modules::Battery::getIcon(uint16_t percentage) auto idx = std::clamp(percentage / (100 / size), 0U, size - 1); return config_["format-icons"][idx].asString(); } - -waybar::modules::Battery::operator Gtk::Widget &() -{ - return label_; -} diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index e24e64f..47900ad 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,7 +1,7 @@ #include "modules/clock.hpp" waybar::modules::Clock::Clock(Json::Value config) - : config_(std::move(config)) + : ALabel(std::move(config)) { label_.set_name("clock"); uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 60; @@ -12,7 +12,7 @@ waybar::modules::Clock::Clock(Json::Value config) + std::chrono::seconds(interval)); thread_.sleep_until(timeout); }; -}; +} auto waybar::modules::Clock::update() -> void { @@ -20,7 +20,3 @@ auto waybar::modules::Clock::update() -> void auto format = config_["format"] ? config_["format"].asString() : "{:%H:%M}"; label_.set_text(fmt::format(format, localtime)); } - -waybar::modules::Clock::operator Gtk::Widget &() { - return label_; -} diff --git a/src/modules/cpu.cpp b/src/modules/cpu.cpp index 9668c6a..5308539 100644 --- a/src/modules/cpu.cpp +++ b/src/modules/cpu.cpp @@ -1,7 +1,7 @@ #include "modules/cpu.hpp" waybar::modules::Cpu::Cpu(Json::Value config) - : config_(std::move(config)) + : ALabel(std::move(config)) { label_.set_name("cpu"); uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 10; @@ -9,7 +9,7 @@ waybar::modules::Cpu::Cpu(Json::Value config) Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Cpu::update)); thread_.sleep_for(chrono::seconds(interval)); }; -}; +} auto waybar::modules::Cpu::update() -> void { @@ -21,7 +21,3 @@ auto waybar::modules::Cpu::update() -> void label_.set_text(fmt::format(format, load)); } } - -waybar::modules::Cpu::operator Gtk::Widget &() { - return label_; -} diff --git a/src/modules/custom.cpp b/src/modules/custom.cpp index d9e9a4c..a76a88d 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -2,21 +2,17 @@ #include waybar::modules::Custom::Custom(const std::string &name, Json::Value config) - : name_(name), config_(std::move(config)) + : ALabel(std::move(config)), name_(name) { if (!config_["exec"]) { throw std::runtime_error(name_ + " has no exec path."); } - if (config_["max-length"]) { - label_.set_max_width_chars(config_["max-length"].asUInt()); - label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END); - } uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 30; thread_ = [this, interval] { Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Custom::update)); thread_.sleep_for(chrono::seconds(interval)); }; -}; +} auto waybar::modules::Custom::update() -> void { @@ -52,7 +48,3 @@ auto waybar::modules::Custom::update() -> void label_.show(); } } - -waybar::modules::Custom::operator Gtk::Widget &() { - return label_; -} diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp index 4c7c18b..f44f6cd 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -1,7 +1,7 @@ #include "modules/memory.hpp" waybar::modules::Memory::Memory(Json::Value config) - : config_(std::move(config)) + : ALabel(std::move(config)) { label_.set_name("memory"); uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 30; @@ -9,7 +9,7 @@ waybar::modules::Memory::Memory(Json::Value config) Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Memory::update)); thread_.sleep_for(chrono::seconds(interval)); }; -}; +} auto waybar::modules::Memory::update() -> void { @@ -24,7 +24,3 @@ auto waybar::modules::Memory::update() -> void label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1)); } } - -waybar::modules::Memory::operator Gtk::Widget &() { - return label_; -} diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 17ea13c..775aff0 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -1,7 +1,7 @@ #include "modules/network.hpp" waybar::modules::Network::Network(Json::Value config) - : config_(std::move(config)), family_(AF_INET), + : ALabel(std::move(config)), family_(AF_INET), signal_strength_dbm_(0), signal_strength_(0) { sock_fd_ = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); @@ -70,7 +70,7 @@ waybar::modules::Network::Network(Json::Value config) Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Network::update)); } }; -}; +} auto waybar::modules::Network::update() -> void { @@ -381,7 +381,3 @@ auto waybar::modules::Network::getInfo() -> void nl_send_sync(sk, msg); nl_socket_free(sk); } - -waybar::modules::Network::operator Gtk::Widget &() { - return label_; -} diff --git a/src/modules/pulseaudio.cpp b/src/modules/pulseaudio.cpp index 80ada27..0083769 100644 --- a/src/modules/pulseaudio.cpp +++ b/src/modules/pulseaudio.cpp @@ -1,7 +1,7 @@ #include "modules/pulseaudio.hpp" waybar::modules::Pulseaudio::Pulseaudio(Json::Value config) - : config_(std::move(config)), mainloop_(nullptr), mainloop_api_(nullptr), + : ALabel(std::move(config)), mainloop_(nullptr), mainloop_api_(nullptr), context_(nullptr), sink_idx_(0), volume_(0), muted_(false) { label_.set_name("pulseaudio"); @@ -26,7 +26,7 @@ waybar::modules::Pulseaudio::Pulseaudio(Json::Value config) throw std::runtime_error("pa_mainloop_run() failed."); } pa_threaded_mainloop_unlock(mainloop_); -}; +} void waybar::modules::Pulseaudio::contextStateCb(pa_context *c, void *data) { @@ -123,7 +123,3 @@ std::string waybar::modules::Pulseaudio::getIcon(uint16_t percentage) auto idx = std::clamp(percentage / (100 / size), 0U, size - 1); return config_["format-icons"][idx].asString(); } - -waybar::modules::Pulseaudio::operator Gtk::Widget &() { - return label_; -} diff --git a/src/modules/sway/window.cpp b/src/modules/sway/window.cpp index 1a41c01..639793d 100644 --- a/src/modules/sway/window.cpp +++ b/src/modules/sway/window.cpp @@ -2,7 +2,7 @@ #include "modules/sway/ipc/client.hpp" waybar::modules::sway::Window::Window(Bar &bar, Json::Value config) - : bar_(bar), config_(std::move(config)) + : ALabel(std::move(config)), bar_(bar) { label_.set_name("window"); std::string socketPath = getSocketPath(); @@ -59,7 +59,3 @@ void waybar::modules::sway::Window::getFocusedWindow() std::cerr << e.what() << std::endl; } } - -waybar::modules::sway::Window::operator Gtk::Widget &() { - return label_; -}