feat(ALabel): Toggleable labels
This commit is contained in:
		
							parent
							
								
									e9478f548e
								
							
						
					
					
						commit
						53956d9d18
					
				|  | @ -7,13 +7,20 @@ namespace waybar { | ||||||
| 
 | 
 | ||||||
| class ALabel : public IModule { | class ALabel : public IModule { | ||||||
|   public: |   public: | ||||||
|     ALabel(const Json::Value&); |     ALabel(const Json::Value&, const std::string format); | ||||||
|     virtual ~ALabel() = default; |     virtual ~ALabel() = default; | ||||||
|     virtual auto update() -> void; |     virtual auto update() -> void; | ||||||
|  |     virtual std::string getIcon(uint16_t percentage); | ||||||
|     virtual operator Gtk::Widget &(); |     virtual operator Gtk::Widget &(); | ||||||
|   protected: |   protected: | ||||||
|  |     Gtk::EventBox event_box_; | ||||||
|     Gtk::Label label_; |     Gtk::Label label_; | ||||||
|     const Json::Value& config_; |     const Json::Value& config_; | ||||||
|  |     std::string format_; | ||||||
|  |   private: | ||||||
|  |     bool handleToggle(GdkEventButton* const& ev); | ||||||
|  |     bool alt = false; | ||||||
|  |     const std::string default_format_; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -22,7 +22,6 @@ class Battery : public ALabel { | ||||||
|     static inline const fs::path data_dir_ = "/sys/class/power_supply/"; |     static inline const fs::path data_dir_ = "/sys/class/power_supply/"; | ||||||
|    |    | ||||||
|     void worker(); |     void worker(); | ||||||
|     std::string getIcon(uint16_t percentage); |  | ||||||
| 
 | 
 | ||||||
|     util::SleeperThread thread_; |     util::SleeperThread thread_; | ||||||
|     std::vector<fs::path> batteries_; |     std::vector<fs::path> batteries_; | ||||||
|  |  | ||||||
|  | @ -19,8 +19,6 @@ class Pulseaudio : public ALabel { | ||||||
|     static void sinkInfoCb(pa_context*, const pa_sink_info*, int, void*); |     static void sinkInfoCb(pa_context*, const pa_sink_info*, int, void*); | ||||||
|     static void serverInfoCb(pa_context*, const pa_server_info*, void*); |     static void serverInfoCb(pa_context*, const pa_server_info*, void*); | ||||||
| 
 | 
 | ||||||
|     std::string getIcon(uint16_t); |  | ||||||
| 
 |  | ||||||
|     pa_threaded_mainloop* mainloop_; |     pa_threaded_mainloop* mainloop_; | ||||||
|     pa_mainloop_api* mainloop_api_; |     pa_mainloop_api* mainloop_api_; | ||||||
|     pa_context* context_; |     pa_context* context_; | ||||||
|  |  | ||||||
|  | @ -22,6 +22,9 @@ | ||||||
|     "sway/window": { |     "sway/window": { | ||||||
|         "max-length": 50 |         "max-length": 50 | ||||||
|     }, |     }, | ||||||
|  |     "clock": { | ||||||
|  |         "format-alt": "{:%Y-%m-%d}" | ||||||
|  |     }, | ||||||
|     "cpu": { |     "cpu": { | ||||||
|         "format": "{}% " |         "format": "{}% " | ||||||
|     }, |     }, | ||||||
|  |  | ||||||
|  | @ -1,12 +1,22 @@ | ||||||
| #include "ALabel.hpp" | #include "ALabel.hpp" | ||||||
| 
 | 
 | ||||||
| waybar::ALabel::ALabel(const Json::Value& config) | #include <iostream> | ||||||
|   : config_(config) | 
 | ||||||
|  | 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"]) { | 	if (config_["max-length"]) { | ||||||
|     label_.set_max_width_chars(config_["max-length"].asUInt()); |     label_.set_max_width_chars(config_["max-length"].asUInt()); | ||||||
|     label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END); |     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 | auto waybar::ALabel::update() -> void | ||||||
|  | @ -14,6 +24,28 @@ auto waybar::ALabel::update() -> void | ||||||
|   // Nothing here
 |   // Nothing here
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| waybar::ALabel::operator Gtk::Widget &() { | bool waybar::ALabel::handleToggle(GdkEventButton* const& ev) | ||||||
|   return label_; | { | ||||||
|  |   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_; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| #include "modules/battery.hpp" | #include "modules/battery.hpp" | ||||||
| 
 | 
 | ||||||
| waybar::modules::Battery::Battery(const Json::Value& config) | waybar::modules::Battery::Battery(const Json::Value& config) | ||||||
|   : ALabel(config) |   : ALabel(config, "{capacity}%") | ||||||
| { | { | ||||||
|   try { |   try { | ||||||
|     for (auto &node : fs::directory_iterator(data_dir_)) { |     for (auto &node : fs::directory_iterator(data_dir_)) { | ||||||
|  | @ -62,9 +62,7 @@ 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"] |     label_.set_text(fmt::format(format_, fmt::arg("capacity", 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); | ||||||
|     bool charging = status == "Charging"; |     bool charging = status == "Charging"; | ||||||
|  | @ -83,13 +81,3 @@ auto waybar::modules::Battery::update() -> void | ||||||
|     std::cerr << e.what() << std::endl; |     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(); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| #include "modules/clock.hpp" | #include "modules/clock.hpp" | ||||||
| 
 | 
 | ||||||
| waybar::modules::Clock::Clock(const Json::Value& config) | waybar::modules::Clock::Clock(const Json::Value& config) | ||||||
|   : ALabel(config) |   : ALabel(config, "{:%H:%M}") | ||||||
| { | { | ||||||
|   label_.set_name("clock"); |   label_.set_name("clock"); | ||||||
|   uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 60; |   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 waybar::modules::Clock::update() -> void | ||||||
| { | { | ||||||
|   auto localtime = fmt::localtime(std::time(nullptr)); |   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)); |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| #include "modules/cpu.hpp" | #include "modules/cpu.hpp" | ||||||
| 
 | 
 | ||||||
| waybar::modules::Cpu::Cpu(const Json::Value& config) | waybar::modules::Cpu::Cpu(const Json::Value& config) | ||||||
|   : ALabel(config) |   : ALabel(config, "{}%") | ||||||
| { | { | ||||||
|   label_.set_name("cpu"); |   label_.set_name("cpu"); | ||||||
|   uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 10; |   uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 10; | ||||||
|  | @ -17,7 +17,6 @@ auto waybar::modules::Cpu::update() -> void | ||||||
|   if (sysinfo(&info) == 0) { |   if (sysinfo(&info) == 0) { | ||||||
|     float f_load = 1.f / (1u << SI_LOAD_SHIFT); |     float f_load = 1.f / (1u << SI_LOAD_SHIFT); | ||||||
|     uint16_t load = info.loads[0] * f_load * 100 / get_nprocs(); |     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)); |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -2,7 +2,7 @@ | ||||||
| 
 | 
 | ||||||
| waybar::modules::Custom::Custom(const std::string name, | waybar::modules::Custom::Custom(const std::string name, | ||||||
|   const Json::Value& config) |   const Json::Value& config) | ||||||
|   : ALabel(config), name_(name) |   : ALabel(config, "{}"), name_(name) | ||||||
| { | { | ||||||
|   if (!config_["exec"]) { |   if (!config_["exec"]) { | ||||||
|     throw std::runtime_error(name_ + " has no exec path."); |     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) { |   if (res.out.empty() || res.exit_code != 0) { | ||||||
|     label_.hide(); |     label_.hide(); | ||||||
|   } else { |   } 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_text(str); | ||||||
|     label_.set_tooltip_text(str); |     label_.set_tooltip_text(str); | ||||||
|     label_.show(); |     label_.show(); | ||||||
|  |  | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| #include "modules/memory.hpp" | #include "modules/memory.hpp" | ||||||
| 
 | 
 | ||||||
| waybar::modules::Memory::Memory(const Json::Value& config) | waybar::modules::Memory::Memory(const Json::Value& config) | ||||||
|   : ALabel(config) |   : ALabel(config, "{}%") | ||||||
| { | { | ||||||
|   label_.set_name("memory"); |   label_.set_name("memory"); | ||||||
|   uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 30; |   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 total = info.totalram * info.mem_unit; | ||||||
|     auto freeram = info.freeram * info.mem_unit; |     auto freeram = info.freeram * info.mem_unit; | ||||||
|     int used_ram_percentage = 100 * (total - freeram) / total; |     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); |     auto used_ram_gigabytes = (total - freeram) / std::pow(1024, 3); | ||||||
|     label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1)); |     label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1)); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| #include "modules/network.hpp" | #include "modules/network.hpp" | ||||||
| 
 | 
 | ||||||
| waybar::modules::Network::Network(const Json::Value& config) | 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) |     signal_strength_dbm_(0), signal_strength_(0) | ||||||
| { | { | ||||||
|   sock_fd_ = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |   sock_fd_ = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); | ||||||
|  | @ -82,7 +82,7 @@ waybar::modules::Network::~Network() | ||||||
| 
 | 
 | ||||||
| auto waybar::modules::Network::update() -> void | auto waybar::modules::Network::update() -> void | ||||||
| { | { | ||||||
|   auto format = config_["format"] ? config_["format"].asString() : "{ifname}"; |   auto format = format_; | ||||||
|   if (ifid_ <= 0) { |   if (ifid_ <= 0) { | ||||||
|     format = config_["format-disconnected"] |     format = config_["format-disconnected"] | ||||||
|       ? config_["format-disconnected"].asString() : format; |       ? config_["format-disconnected"].asString() : format; | ||||||
|  |  | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| #include "modules/pulseaudio.hpp" | #include "modules/pulseaudio.hpp" | ||||||
| 
 | 
 | ||||||
| waybar::modules::Pulseaudio::Pulseaudio(const Json::Value& config) | 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) |     context_(nullptr), sink_idx_(0), volume_(0), muted_(false) | ||||||
| { | { | ||||||
|   label_.set_name("pulseaudio"); |   label_.set_name("pulseaudio"); | ||||||
|  | @ -100,33 +100,22 @@ void waybar::modules::Pulseaudio::sinkInfoCb(pa_context* /*context*/, | ||||||
| void waybar::modules::Pulseaudio::serverInfoCb(pa_context *context, | void waybar::modules::Pulseaudio::serverInfoCb(pa_context *context, | ||||||
|   const pa_server_info *i, void *data) |   const pa_server_info *i, void *data) | ||||||
| { | { | ||||||
|     pa_context_get_sink_info_by_name(context, i->default_sink_name, |   pa_context_get_sink_info_by_name(context, i->default_sink_name, | ||||||
|       sinkInfoCb, data); |     sinkInfoCb, data); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| auto waybar::modules::Pulseaudio::update() -> void | auto waybar::modules::Pulseaudio::update() -> void | ||||||
| { | { | ||||||
| 	  auto format = |   auto format = 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, |  | ||||||
|       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 size = config_["format-icons"].size(); |   label_.set_label(fmt::format(format, | ||||||
|   auto idx = std::clamp(percentage / (100 / size), 0U, size - 1); |     fmt::arg("volume", volume_), | ||||||
|   return config_["format-icons"][idx].asString(); |     fmt::arg("icon", getIcon(volume_)))); | ||||||
|  |   label_.set_tooltip_text(desc_); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| #include "modules/sway/window.hpp" | #include "modules/sway/window.hpp" | ||||||
| 
 | 
 | ||||||
| waybar::modules::sway::Window::Window(Bar &bar, const Json::Value& config) | waybar::modules::sway::Window::Window(Bar &bar, const Json::Value& config) | ||||||
|   : ALabel(config), bar_(bar) |   : ALabel(config, "{}"), bar_(bar) | ||||||
| { | { | ||||||
|   label_.set_name("window"); |   label_.set_name("window"); | ||||||
|   ipc_.connect(); |   ipc_.connect(); | ||||||
|  | @ -30,7 +30,7 @@ void waybar::modules::sway::Window::worker() | ||||||
| 
 | 
 | ||||||
| auto waybar::modules::sway::Window::update() -> void | auto waybar::modules::sway::Window::update() -> void | ||||||
| { | { | ||||||
|   label_.set_text(window_); |   label_.set_text(fmt::format(format_, window_)); | ||||||
|   label_.set_tooltip_text(window_); |   label_.set_tooltip_text(window_); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue