feat(ALabel): Toggleable labels

This commit is contained in:
Alexis 2018-08-27 01:36:25 +02:00
parent e9478f548e
commit 53956d9d18
No known key found for this signature in database
GPG Key ID: 20B1EC2EBBA96BD9
13 changed files with 75 additions and 63 deletions

View File

@ -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_;
};
}

View File

@ -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<fs::path> batteries_;

View File

@ -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_;

View File

@ -22,6 +22,9 @@
"sway/window": {
"max-length": 50
},
"clock": {
"format-alt": "{:%Y-%m-%d}"
},
"cpu": {
"format": "{}% "
},

View File

@ -1,12 +1,22 @@
#include "ALabel.hpp"
waybar::ALabel::ALabel(const Json::Value& config)
: config_(config)
#include <iostream>
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_;
}

View File

@ -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();
}

View File

@ -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));
}

View File

@ -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));
}
}

View File

@ -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();

View File

@ -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));
}

View File

@ -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;

View File

@ -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_);
}

View File

@ -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_);
}