From 5e9faeb2d055b250f8d435c852de7c6492e96381 Mon Sep 17 00:00:00 2001 From: Erik Reider <35975961+ErikReider@users.noreply.github.com> Date: Tue, 15 Mar 2022 20:22:04 +0100 Subject: [PATCH] Now shows the percentage and the correct icon --- include/modules/upower/upower.hpp | 5 ++- src/modules/upower/upower.cpp | 71 ++++++++++++++++++++----------- 2 files changed, 51 insertions(+), 25 deletions(-) diff --git a/include/modules/upower/upower.hpp b/include/modules/upower/upower.hpp index ede6d7f..bce89e5 100644 --- a/include/modules/upower/upower.hpp +++ b/include/modules/upower/upower.hpp @@ -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 devices; diff --git a/src/modules/upower/upower.cpp b/src/modules/upower/upower.cpp index c455c52..1575c87 100644 --- a/src/modules/upower/upower.cpp +++ b/src/modules/upower/upower.cpp @@ -4,6 +4,9 @@ #include #include +#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(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();