Now shows the percentage and the correct icon

This commit is contained in:
Erik Reider 2022-03-15 20:22:04 +01:00
parent 4ee81c8dea
commit 5e9faeb2d0
2 changed files with 51 additions and 25 deletions

View File

@ -29,12 +29,15 @@ class UPower : public AModule {
void addDevice(UpDevice *device); void addDevice(UpDevice *device);
void setDisplayDevice(); void setDisplayDevice();
void resetDevices(); void resetDevices();
static void clicked();
Gtk::Box box_; Gtk::Box box_;
Gtk::Image icon_; Gtk::Image icon_;
Gtk::Label label_; Gtk::Label label_;
// Config
bool hideIfEmpty = true;
uint iconSize = 32;
UpClient *client = NULL; UpClient *client = NULL;
UpDevice *displayDevice = NULL; UpDevice *displayDevice = NULL;
std::map<std::string, UpDevice *> devices; std::map<std::string, UpDevice *> devices;

View File

@ -4,6 +4,9 @@
#include <map> #include <map>
#include <string> #include <string>
#include "gtkmm/enums.h"
#include "gtkmm/icontheme.h"
namespace waybar::modules { namespace waybar::modules {
UPower::UPower(const std::string& id, const Json::Value& config) UPower::UPower(const std::string& id, const Json::Value& config)
: AModule(config, "tray", id), : AModule(config, "tray", id),
@ -15,12 +18,16 @@ UPower::UPower(const std::string& id, const Json::Value& config)
box_.pack_start(label_); box_.pack_start(label_);
event_box_.add(box_); event_box_.add(box_);
icon_.set_pixel_size(iconSize);
GError* error = NULL; GError* error = NULL;
client = up_client_new_full(NULL, &error); client = up_client_new_full(NULL, &error);
if (client == NULL) { if (client == NULL) {
throw std::runtime_error("Unable to create UPower client!"); throw std::runtime_error("Unable to create UPower client!");
} }
// TODO: Connect to login1 prepare_for_sleep signal
g_signal_connect(client, "device-added", G_CALLBACK(deviceAdded_cb), this); g_signal_connect(client, "device-added", G_CALLBACK(deviceAdded_cb), this);
g_signal_connect(client, "device-removed", G_CALLBACK(deviceRemoved_cb), this); g_signal_connect(client, "device-removed", G_CALLBACK(deviceRemoved_cb), this);
g_signal_connect(client, "notify", G_CALLBACK(deviceNotify_cb), this); g_signal_connect(client, "notify", G_CALLBACK(deviceNotify_cb), this);
@ -32,8 +39,6 @@ UPower::UPower(const std::string& id, const Json::Value& config)
UPower::~UPower() {} UPower::~UPower() {}
void UPower::clicked() { printf("CLICK\n"); }
void UPower::deviceAdded_cb(UpClient* client, UpDevice* device, gpointer data) { void UPower::deviceAdded_cb(UpClient* client, UpDevice* device, gpointer data) {
UPower* up = static_cast<UPower*>(data); UPower* up = static_cast<UPower*>(data);
up->addDevice(device); up->addDevice(device);
@ -93,7 +98,10 @@ void UPower::resetDevices() {
} }
auto UPower::update() -> void { auto UPower::update() -> void {
printf("UPDATE!\n"); if (devices.size() == 0 && hideIfEmpty) {
box_.set_visible(false);
} else {
box_.set_visible(true);
UpDeviceKind kind; UpDeviceKind kind;
UpDeviceState state; UpDeviceState state;
@ -117,7 +125,22 @@ auto UPower::update() -> void {
&icon_name, &icon_name,
NULL); NULL);
printf("ICON: %s\n", icon_name); bool displayDeviceValid =
kind == UpDeviceKind::UP_DEVICE_KIND_BATTERY || kind == UpDeviceKind::UP_DEVICE_KIND_UPS;
// TODO: Tooltip
// Set percentage
std::string percent_string =
displayDeviceValid ? std::to_string(int(percentage) + 0.5) + "%" : "";
label_.set_text(percent_string);
// Set icon
if (!Gtk::IconTheme::get_default()->has_icon(icon_name)) {
icon_name = (char*)"battery-missing-symbolic";
}
icon_.set_from_icon_name(icon_name, Gtk::ICON_SIZE_INVALID);
}
// Call parent update // Call parent update
AModule::update(); AModule::update();