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 setDisplayDevice();
void resetDevices();
static void clicked();
Gtk::Box box_;
Gtk::Image icon_;
Gtk::Label label_;
// Config
bool hideIfEmpty = true;
uint iconSize = 32;
UpClient *client = NULL;
UpDevice *displayDevice = NULL;
std::map<std::string, UpDevice *> devices;

View File

@ -4,6 +4,9 @@
#include <map>
#include <string>
#include "gtkmm/enums.h"
#include "gtkmm/icontheme.h"
namespace waybar::modules {
UPower::UPower(const std::string& id, const Json::Value& config)
: AModule(config, "tray", id),
@ -15,12 +18,16 @@ UPower::UPower(const std::string& id, const Json::Value& config)
box_.pack_start(label_);
event_box_.add(box_);
icon_.set_pixel_size(iconSize);
GError* error = NULL;
client = up_client_new_full(NULL, &error);
if (client == NULL) {
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-removed", G_CALLBACK(deviceRemoved_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() {}
void UPower::clicked() { printf("CLICK\n"); }
void UPower::deviceAdded_cb(UpClient* client, UpDevice* device, gpointer data) {
UPower* up = static_cast<UPower*>(data);
up->addDevice(device);
@ -93,31 +98,49 @@ void UPower::resetDevices() {
}
auto UPower::update() -> void {
printf("UPDATE!\n");
if (devices.size() == 0 && hideIfEmpty) {
box_.set_visible(false);
} else {
box_.set_visible(true);
UpDeviceKind kind;
UpDeviceState state;
double percentage;
gboolean is_power_supply;
gboolean is_present;
gchar* icon_name;
UpDeviceKind kind;
UpDeviceState state;
double percentage;
gboolean is_power_supply;
gboolean is_present;
gchar* icon_name;
g_object_get(displayDevice,
"kind",
&kind,
"state",
&state,
"is-present",
&is_present,
"power-supply",
&is_power_supply,
"percentage",
&percentage,
"icon-name",
&icon_name,
NULL);
g_object_get(displayDevice,
"kind",
&kind,
"state",
&state,
"is-present",
&is_present,
"power-supply",
&is_power_supply,
"percentage",
&percentage,
"icon-name",
&icon_name,
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
AModule::update();