diff --git a/include/ALabel.hpp b/include/ALabel.hpp new file mode 100644 index 00000000..9c1dc4cf --- /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 82638476..e5662a3d 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 ae332741..2634e7b2 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 52f455f0..e9c9fdfd 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 031d51ca..5313b177 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 fbe147b3..94bc6fd9 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 2f655514..cbed9298 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 5f813a16..65d4e0d5 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 eaf247bf..3a89825d 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 ba3073fc..a9e107bc 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 00000000..f20bf214 --- /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 fe18b7b0..0fa38b95 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 e24e64ff..47900ada 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 9668c6a8..53085393 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 d9e9a4cf..a76a88df 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 4c7c18ba..f44f6cd3 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 17ea13cf..775aff06 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 80ada279..00837698 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 1a41c016..639793d7 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_; -}