Added custom tooltip with device icon, device name and battery status

This commit is contained in:
Erik Reider 2022-03-19 17:09:55 +01:00
parent 4196e0d5d1
commit 7fac2afb85
6 changed files with 229 additions and 18 deletions

View File

@ -10,8 +10,9 @@
#include "gtkmm/box.h" #include "gtkmm/box.h"
#include "gtkmm/image.h" #include "gtkmm/image.h"
#include "gtkmm/label.h" #include "gtkmm/label.h"
#include "modules/upower/upower_tooltip.hpp"
namespace waybar::modules { namespace waybar::modules::upower {
class UPower : public AModule { class UPower : public AModule {
public: public:
@ -19,9 +20,9 @@ class UPower : public AModule {
~UPower(); ~UPower();
auto update() -> void; auto update() -> void;
private:
typedef std::unordered_map<std::string, UpDevice *> Devices; typedef std::unordered_map<std::string, UpDevice *> Devices;
private:
static void deviceAdded_cb(UpClient *client, UpDevice *device, gpointer data); static void deviceAdded_cb(UpClient *client, UpDevice *device, gpointer data);
static void deviceRemoved_cb(UpClient *client, const gchar *objectPath, gpointer data); static void deviceRemoved_cb(UpClient *client, const gchar *objectPath, gpointer data);
static void deviceNotify_cb(UpDevice *device, GParamSpec *pspec, gpointer user_data); static void deviceNotify_cb(UpDevice *device, GParamSpec *pspec, gpointer user_data);
@ -34,6 +35,7 @@ class UPower : public AModule {
void setDisplayDevice(); void setDisplayDevice();
void resetDevices(); void resetDevices();
void removeDevices(); void removeDevices();
bool show_tooltip_callback(int, int, bool, const Glib::RefPtr<Gtk::Tooltip> &tooltip);
Gtk::Box box_; Gtk::Box box_;
Gtk::Image icon_; Gtk::Image icon_;
@ -41,6 +43,8 @@ class UPower : public AModule {
// Config // Config
bool hideIfEmpty = true; bool hideIfEmpty = true;
bool tooltip_enabled = true;
uint tooltip_spacing = 4;
uint iconSize = 20; uint iconSize = 20;
Devices devices; Devices devices;
@ -49,6 +53,7 @@ class UPower : public AModule {
UpDevice *displayDevice; UpDevice *displayDevice;
guint login1_id; guint login1_id;
GDBusConnection *login1_connection; GDBusConnection *login1_connection;
UPowerTooltip *upower_tooltip;
}; };
} // namespace waybar::modules } // namespace waybar::modules::upower

View File

@ -0,0 +1,29 @@
#pragma once
#include <libupower-glib/upower.h>
#include "gtkmm/box.h"
#include "gtkmm/label.h"
#include "gtkmm/window.h"
namespace waybar::modules::upower {
class UPowerTooltip : public Gtk::Window {
private:
typedef std::unordered_map<std::string, UpDevice*> Devices;
std::string getDeviceIcon(UpDeviceKind& kind);
Gtk::Box* contentBox;
uint iconSize;
uint tooltipSpacing;
public:
UPowerTooltip(uint iconSize, uint tooltipSpacing);
~UPowerTooltip();
uint updateTooltip(Devices& devices);
};
} // namespace waybar::modules::upower

View File

@ -207,6 +207,7 @@ endif
if (upower_glib.found() and giounix.found() and not get_option('logind').disabled()) if (upower_glib.found() and giounix.found() and not get_option('logind').disabled())
add_project_arguments('-DHAVE_UPOWER', language: 'cpp') add_project_arguments('-DHAVE_UPOWER', language: 'cpp')
src_files += 'src/modules/upower/upower.cpp' src_files += 'src/modules/upower/upower.cpp'
src_files += 'src/modules/upower/upower_tooltip.cpp'
endif endif
if libpulse.found() if libpulse.found()

View File

@ -14,7 +14,7 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
#endif #endif
#ifdef HAVE_UPOWER #ifdef HAVE_UPOWER
if (ref == "upower") { if (ref == "upower") {
return new waybar::modules::UPower(id, config_[name]); return new waybar::modules::upower::UPower(id, config_[name]);
} }
#endif #endif
#ifdef HAVE_SWAY #ifdef HAVE_SWAY

View File

@ -1,8 +1,11 @@
#include "modules/upower/upower.hpp" #include "modules/upower/upower.hpp"
#include "gtkmm/icontheme.h" #include "gtkmm/icontheme.h"
#include "gtkmm/label.h"
#include "gtkmm/tooltip.h"
#include "modules/upower/upower_tooltip.hpp"
namespace waybar::modules { namespace waybar::modules::upower {
UPower::UPower(const std::string& id, const Json::Value& config) UPower::UPower(const std::string& id, const Json::Value& config)
: AModule(config, "upower", id), : AModule(config, "upower", id),
box_(Gtk::ORIENTATION_HORIZONTAL, 0), box_(Gtk::ORIENTATION_HORIZONTAL, 0),
@ -26,6 +29,23 @@ UPower::UPower(const std::string& id, const Json::Value& config)
hideIfEmpty = config_["hide-if-empty"].asBool(); hideIfEmpty = config_["hide-if-empty"].asBool();
} }
// Tooltip Spacing
if (config_["tooltip-spacing"].isUInt()) {
tooltip_spacing = config_["tooltip-spacing"].asUInt();
}
// Tooltip
if (config_["tooltip"].isBool()) {
tooltip_enabled = config_["tooltip"].asBool();
}
box_.set_has_tooltip(tooltip_enabled);
if (tooltip_enabled) {
// Sets the window to use when showing the tooltip
upower_tooltip = new UPowerTooltip(iconSize, tooltip_spacing);
box_.set_tooltip_window(*upower_tooltip);
box_.signal_query_tooltip().connect(sigc::mem_fun(*this, &UPower::show_tooltip_callback));
}
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) {
@ -174,6 +194,10 @@ void UPower::resetDevices() {
dp.emit(); dp.emit();
} }
bool UPower::show_tooltip_callback(int, int, bool, const Glib::RefPtr<Gtk::Tooltip>& tooltip) {
return true;
}
auto UPower::update() -> void { auto UPower::update() -> void {
std::lock_guard<std::mutex> guard(m_Mutex); std::lock_guard<std::mutex> guard(m_Mutex);
@ -204,7 +228,7 @@ auto UPower::update() -> void {
std::string percentString = ""; std::string percentString = "";
std::string tooltip = ""; uint tooltipCount = 0;
if (devices.size() == 0 && !displayDeviceValid && hideIfEmpty) { if (devices.size() == 0 && !displayDeviceValid && hideIfEmpty) {
event_box_.set_visible(false); event_box_.set_visible(false);
@ -213,18 +237,12 @@ auto UPower::update() -> void {
event_box_.set_visible(true); event_box_.set_visible(true);
// TODO: Tooltip // Tooltip
if (!devices.empty()) { if (tooltip_enabled) {
for (auto& e : devices) { tooltipCount = upower_tooltip->updateTooltip(devices);
const gchar* objectPath = up_device_get_object_path(e.second); // Disable the tooltip if there aren't any devices in the tooltip
double percentage; box_.set_has_tooltip(!devices.empty() && tooltipCount > 0);
g_object_get(e.second, "percentage", &percentage, NULL);
printf("Device: %s, VALID: %f\n", objectPath, percentage);
} }
} else {
printf("No devices\n");
}
// box_.set_tooltip
// Set percentage // Set percentage
if (displayDeviceValid) { if (displayDeviceValid) {
@ -243,4 +261,4 @@ update:
AModule::update(); AModule::update();
} }
} // namespace waybar::modules } // namespace waybar::modules::upower

View File

@ -0,0 +1,158 @@
#include "modules/upower/upower_tooltip.hpp"
#include "gtkmm/box.h"
#include "gtkmm/enums.h"
#include "gtkmm/icontheme.h"
#include "gtkmm/image.h"
#include "gtkmm/label.h"
namespace waybar::modules::upower {
UPowerTooltip::UPowerTooltip(uint iconSize_, uint tooltipSpacing_)
: Gtk::Window(), iconSize(iconSize_), tooltipSpacing(tooltipSpacing_) {
contentBox = new Gtk::Box(Gtk::ORIENTATION_VERTICAL);
add(*contentBox);
contentBox->show();
}
UPowerTooltip::~UPowerTooltip() {}
uint UPowerTooltip::updateTooltip(Devices& devices) {
// Removes all old devices
for (auto child : contentBox->get_children()) {
child->~Widget();
}
uint deviceCount = 0;
// Adds all valid devices
for (auto pair : devices) {
UpDevice* device = pair.second;
std::string objectPath = pair.first;
if (!G_IS_OBJECT(device)) continue;
Gtk::Box* box = new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, tooltipSpacing);
UpDeviceKind kind;
double percentage;
gchar* native_path;
gchar* model;
gchar* icon_name;
g_object_get(device,
"kind",
&kind,
"percentage",
&percentage,
"native-path",
&native_path,
"model",
&model,
"icon-name",
&icon_name,
NULL);
// Skip Line_Power and BAT0 devices
if (kind == UP_DEVICE_KIND_LINE_POWER || strcmp(native_path, "BAT0") == 0) continue;
Gtk::Box* modelBox = new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL);
box->add(*modelBox);
// Set device icon
std::string deviceIconName = getDeviceIcon(kind);
Gtk::Image* deviceIcon = new Gtk::Image();
deviceIcon->set_pixel_size(iconSize);
if (!Gtk::IconTheme::get_default()->has_icon(deviceIconName)) {
deviceIconName = "battery-missing-symbolic";
}
deviceIcon->set_from_icon_name(deviceIconName, Gtk::ICON_SIZE_INVALID);
modelBox->add(*deviceIcon);
// Set model
Gtk::Label* modelLabel = new Gtk::Label(model);
modelBox->add(*modelLabel);
Gtk::Box* chargeBox = new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL);
box->add(*chargeBox);
// Set icon
Gtk::Image* icon = new Gtk::Image();
icon->set_pixel_size(iconSize);
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);
chargeBox->add(*icon);
// Set percentage
std::string percentString = std::to_string(int(percentage + 0.5)) + "%";
Gtk::Label* percentLabel = new Gtk::Label(percentString);
chargeBox->add(*percentLabel);
contentBox->add(*box);
deviceCount++;
}
contentBox->show_all();
return deviceCount;
}
std::string UPowerTooltip::getDeviceIcon(UpDeviceKind& kind) {
switch (kind) {
case UP_DEVICE_KIND_LINE_POWER:
return "ac-adapter-symbolic";
case UP_DEVICE_KIND_BATTERY:
return "battery";
case UP_DEVICE_KIND_UPS:
return "uninterruptible-power-supply-symbolic";
case UP_DEVICE_KIND_MONITOR:
return "video-display-symbolic";
case UP_DEVICE_KIND_MOUSE:
return "input-mouse-symbolic";
case UP_DEVICE_KIND_KEYBOARD:
return "input-keyboard-symbolic";
case UP_DEVICE_KIND_PDA:
return "pda-symbolic";
case UP_DEVICE_KIND_PHONE:
return "phone-symbolic";
case UP_DEVICE_KIND_MEDIA_PLAYER:
return "multimedia-player-symbolic";
case UP_DEVICE_KIND_TABLET:
return "computer-apple-ipad-symbolic";
case UP_DEVICE_KIND_COMPUTER:
return "computer-symbolic";
case UP_DEVICE_KIND_GAMING_INPUT:
return "input-gaming-symbolic";
case UP_DEVICE_KIND_PEN:
return "input-tablet-symbolic";
case UP_DEVICE_KIND_TOUCHPAD:
return "input-touchpad-symbolic";
case UP_DEVICE_KIND_MODEM:
return "modem-symbolic";
case UP_DEVICE_KIND_NETWORK:
return "network-wired-symbolic";
case UP_DEVICE_KIND_HEADSET:
return "audio-headset-symbolic";
case UP_DEVICE_KIND_HEADPHONES:
return "audio-headphones-symbolic";
case UP_DEVICE_KIND_OTHER_AUDIO:
case UP_DEVICE_KIND_SPEAKERS:
return "audio-speakers-symbolic";
case UP_DEVICE_KIND_VIDEO:
return "camera-web-symbolic";
case UP_DEVICE_KIND_PRINTER:
return "printer-symbolic";
case UP_DEVICE_KIND_SCANNER:
return "scanner-symbolic";
case UP_DEVICE_KIND_CAMERA:
return "camera-photo-symbolic";
case UP_DEVICE_KIND_BLUETOOTH_GENERIC:
return "bluetooth-active-symbolic";
case UP_DEVICE_KIND_TOY:
case UP_DEVICE_KIND_REMOTE_CONTROL:
case UP_DEVICE_KIND_WEARABLE:
case UP_DEVICE_KIND_LAST:
default:
return "battery-symbolic";
}
}
} // namespace waybar::modules::upower