Added format and format-alt config properties
parent
c4282cfacc
commit
0764e9ad6e
|
@ -7,6 +7,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "ALabel.hpp"
|
#include "ALabel.hpp"
|
||||||
|
#include "glibconfig.h"
|
||||||
#include "gtkmm/box.h"
|
#include "gtkmm/box.h"
|
||||||
#include "gtkmm/image.h"
|
#include "gtkmm/image.h"
|
||||||
#include "gtkmm/label.h"
|
#include "gtkmm/label.h"
|
||||||
|
@ -23,6 +24,9 @@ class UPower : public AModule {
|
||||||
private:
|
private:
|
||||||
typedef std::unordered_map<std::string, UpDevice *> Devices;
|
typedef std::unordered_map<std::string, UpDevice *> Devices;
|
||||||
|
|
||||||
|
const std::string DEFAULT_FORMAT = "{percentage}";
|
||||||
|
const std::string DEFAULT_FORMAT_ALT = "{percentage} {time}";
|
||||||
|
|
||||||
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);
|
||||||
|
@ -36,6 +40,8 @@ class UPower : public AModule {
|
||||||
void resetDevices();
|
void resetDevices();
|
||||||
void removeDevices();
|
void removeDevices();
|
||||||
bool show_tooltip_callback(int, int, bool, const Glib::RefPtr<Gtk::Tooltip> &tooltip);
|
bool show_tooltip_callback(int, int, bool, const Glib::RefPtr<Gtk::Tooltip> &tooltip);
|
||||||
|
bool handleToggle(GdkEventButton *const &);
|
||||||
|
std::string timeToString(gint64 time);
|
||||||
|
|
||||||
const std::string getDeviceStatus(UpDeviceState &state);
|
const std::string getDeviceStatus(UpDeviceState &state);
|
||||||
|
|
||||||
|
@ -48,6 +54,8 @@ class UPower : public AModule {
|
||||||
bool tooltip_enabled = true;
|
bool tooltip_enabled = true;
|
||||||
uint tooltip_spacing = 4;
|
uint tooltip_spacing = 4;
|
||||||
uint iconSize = 20;
|
uint iconSize = 20;
|
||||||
|
std::string format = DEFAULT_FORMAT;
|
||||||
|
std::string format_alt = DEFAULT_FORMAT_ALT;
|
||||||
|
|
||||||
Devices devices;
|
Devices devices;
|
||||||
std::mutex m_Mutex;
|
std::mutex m_Mutex;
|
||||||
|
@ -57,6 +65,7 @@ class UPower : public AModule {
|
||||||
GDBusConnection *login1_connection;
|
GDBusConnection *login1_connection;
|
||||||
UPowerTooltip *upower_tooltip;
|
UPowerTooltip *upower_tooltip;
|
||||||
std::string lastStatus;
|
std::string lastStatus;
|
||||||
|
bool showAltText;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace waybar::modules::upower
|
} // namespace waybar::modules::upower
|
||||||
|
|
|
@ -16,6 +16,16 @@ compatible devices in the tooltip.
|
||||||
default: 20 ++
|
default: 20 ++
|
||||||
Defines the size of the icons.
|
Defines the size of the icons.
|
||||||
|
|
||||||
|
*format*: ++
|
||||||
|
typeof: string ++
|
||||||
|
default: {percentage} ++
|
||||||
|
The text format.
|
||||||
|
|
||||||
|
*format-alt*: ++
|
||||||
|
typeof: string ++
|
||||||
|
default: {percentage} {time} ++
|
||||||
|
The text format when toggled.
|
||||||
|
|
||||||
*hide-if-empty*: ++
|
*hide-if-empty*: ++
|
||||||
typeof: bool ++
|
typeof: bool ++
|
||||||
default: true ++
|
default: true ++
|
||||||
|
@ -32,6 +42,13 @@ compatible devices in the tooltip.
|
||||||
Defines the spacing between the tooltip device name and device battery ++
|
Defines the spacing between the tooltip device name and device battery ++
|
||||||
status.
|
status.
|
||||||
|
|
||||||
|
# FORMAT REPLACEMENTS
|
||||||
|
|
||||||
|
*{percentage}*: The battery capacity in percentage
|
||||||
|
|
||||||
|
*{time}*: An estimated time either until empty or until fully charged ++
|
||||||
|
depending on the charging state.
|
||||||
|
|
||||||
# EXAMPLES
|
# EXAMPLES
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
#include "modules/upower/upower.hpp"
|
#include "modules/upower/upower.hpp"
|
||||||
|
|
||||||
|
#include <fmt/core.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "gtkmm/icontheme.h"
|
#include "gtkmm/icontheme.h"
|
||||||
#include "gtkmm/label.h"
|
#include "gtkmm/label.h"
|
||||||
#include "gtkmm/tooltip.h"
|
#include "gtkmm/tooltip.h"
|
||||||
|
@ -13,7 +17,8 @@ UPower::UPower(const std::string& id, const Json::Value& config)
|
||||||
label_(),
|
label_(),
|
||||||
devices(),
|
devices(),
|
||||||
m_Mutex(),
|
m_Mutex(),
|
||||||
client() {
|
client(),
|
||||||
|
showAltText(false) {
|
||||||
box_.pack_start(icon_);
|
box_.pack_start(icon_);
|
||||||
box_.pack_start(label_);
|
box_.pack_start(label_);
|
||||||
event_box_.add(box_);
|
event_box_.add(box_);
|
||||||
|
@ -29,6 +34,16 @@ UPower::UPower(const std::string& id, const Json::Value& config)
|
||||||
hideIfEmpty = config_["hide-if-empty"].asBool();
|
hideIfEmpty = config_["hide-if-empty"].asBool();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Format
|
||||||
|
if (config_["format"].isString()) {
|
||||||
|
format = config_["format"].asString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format Alt
|
||||||
|
if (config_["format-alt"].isString()) {
|
||||||
|
format_alt = config_["format-alt"].asString();
|
||||||
|
}
|
||||||
|
|
||||||
// Tooltip Spacing
|
// Tooltip Spacing
|
||||||
if (config_["tooltip-spacing"].isUInt()) {
|
if (config_["tooltip-spacing"].isUInt()) {
|
||||||
tooltip_spacing = config_["tooltip-spacing"].asUInt();
|
tooltip_spacing = config_["tooltip-spacing"].asUInt();
|
||||||
|
@ -69,6 +84,8 @@ UPower::UPower(const std::string& id, const Json::Value& config)
|
||||||
NULL);
|
NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
event_box_.signal_button_press_event().connect(sigc::mem_fun(*this, &UPower::handleToggle));
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
|
@ -213,14 +230,33 @@ const std::string UPower::getDeviceStatus(UpDeviceState& state) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool UPower::handleToggle(GdkEventButton* const& event) {
|
||||||
|
std::lock_guard<std::mutex> guard(m_Mutex);
|
||||||
|
showAltText = !showAltText;
|
||||||
|
dp.emit();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string UPower::timeToString(gint64 time) {
|
||||||
|
if (time == 0) return "";
|
||||||
|
float hours = (float)time / 3600;
|
||||||
|
float hours_fixed = static_cast<float>(static_cast<int>(hours * 10)) / 10;
|
||||||
|
float minutes = static_cast<float>(static_cast<int>(hours * 60 * 10)) / 10;
|
||||||
|
if (hours_fixed >= 1) {
|
||||||
|
return fmt::format("{H} h", fmt::arg("H", hours_fixed));
|
||||||
|
} else {
|
||||||
|
return fmt::format("{M} min", fmt::arg("M", minutes));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto UPower::update() -> void {
|
auto UPower::update() -> void {
|
||||||
std::lock_guard<std::mutex> guard(m_Mutex);
|
std::lock_guard<std::mutex> guard(m_Mutex);
|
||||||
|
|
||||||
UpDeviceKind kind;
|
UpDeviceKind kind;
|
||||||
UpDeviceState state;
|
UpDeviceState state;
|
||||||
double percentage;
|
double percentage;
|
||||||
gboolean is_power_supply;
|
gint64 time_empty;
|
||||||
gboolean is_present;
|
gint64 time_full;
|
||||||
gchar* icon_name;
|
gchar* icon_name;
|
||||||
|
|
||||||
g_object_get(displayDevice,
|
g_object_get(displayDevice,
|
||||||
|
@ -228,14 +264,14 @@ auto UPower::update() -> void {
|
||||||
&kind,
|
&kind,
|
||||||
"state",
|
"state",
|
||||||
&state,
|
&state,
|
||||||
"is-present",
|
|
||||||
&is_present,
|
|
||||||
"power-supply",
|
|
||||||
&is_power_supply,
|
|
||||||
"percentage",
|
"percentage",
|
||||||
&percentage,
|
&percentage,
|
||||||
"icon-name",
|
"icon-name",
|
||||||
&icon_name,
|
&icon_name,
|
||||||
|
"time-to-empty",
|
||||||
|
&time_empty,
|
||||||
|
"time-to-full",
|
||||||
|
&time_full,
|
||||||
NULL);
|
NULL);
|
||||||
|
|
||||||
bool displayDeviceValid =
|
bool displayDeviceValid =
|
||||||
|
@ -245,6 +281,22 @@ auto UPower::update() -> void {
|
||||||
|
|
||||||
uint tooltipCount = 0;
|
uint tooltipCount = 0;
|
||||||
|
|
||||||
|
std::string time_full_format = timeToString(time_full);
|
||||||
|
std::string time_empty_format = timeToString(time_full);
|
||||||
|
std::string time_format = "";
|
||||||
|
switch (state) {
|
||||||
|
case UP_DEVICE_STATE_CHARGING:
|
||||||
|
case UP_DEVICE_STATE_PENDING_CHARGE:
|
||||||
|
time_format = time_full_format;
|
||||||
|
break;
|
||||||
|
case UP_DEVICE_STATE_DISCHARGING:
|
||||||
|
case UP_DEVICE_STATE_PENDING_DISCHARGE:
|
||||||
|
time_format = time_empty_format;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// CSS status class
|
// CSS status class
|
||||||
const std::string status = getDeviceStatus(state);
|
const std::string status = getDeviceStatus(state);
|
||||||
// Remove last status if it exists
|
// Remove last status if it exists
|
||||||
|
@ -275,7 +327,11 @@ auto UPower::update() -> void {
|
||||||
if (displayDeviceValid) {
|
if (displayDeviceValid) {
|
||||||
percentString = std::to_string(int(percentage + 0.5)) + "%";
|
percentString = std::to_string(int(percentage + 0.5)) + "%";
|
||||||
}
|
}
|
||||||
label_.set_text(percentString);
|
|
||||||
|
// Label format
|
||||||
|
label_.set_markup(fmt::format(showAltText ? format_alt : format,
|
||||||
|
fmt::arg("percentage", percentString),
|
||||||
|
fmt::arg("time", time_format)));
|
||||||
|
|
||||||
// Set icon
|
// Set icon
|
||||||
if (!Gtk::IconTheme::get_default()->has_icon(icon_name)) {
|
if (!Gtk::IconTheme::get_default()->has_icon(icon_name)) {
|
||||||
|
|
Loading…
Reference in New Issue