Added CSS status classes

This commit is contained in:
Erik Reider 2022-03-19 19:22:00 +01:00
parent 5f19a54deb
commit 05effad18b
3 changed files with 37 additions and 0 deletions

View File

@ -37,6 +37,8 @@ class UPower : public AModule {
void removeDevices();
bool show_tooltip_callback(int, int, bool, const Glib::RefPtr<Gtk::Tooltip> &tooltip);
const std::string getDeviceStatus(UpDeviceState &state);
Gtk::Box box_;
Gtk::Image icon_;
Gtk::Label label_;
@ -54,6 +56,7 @@ class UPower : public AModule {
guint login1_id;
GDBusConnection *login1_connection;
UPowerTooltip *upower_tooltip;
std::string lastStatus;
};
} // namespace waybar::modules::upower

View File

@ -43,3 +43,10 @@ compatible devices in the tooltip.
}
```
# STYLE
- *#upower*
- *#upower.charging*
- *#upower.discharging*
- *#upower.unknown-status*

View File

@ -198,6 +198,21 @@ bool UPower::show_tooltip_callback(int, int, bool, const Glib::RefPtr<Gtk::Toolt
return true;
}
const std::string UPower::getDeviceStatus(UpDeviceState& state) {
switch (state) {
case UP_DEVICE_STATE_CHARGING:
case UP_DEVICE_STATE_PENDING_CHARGE:
return "charging";
case UP_DEVICE_STATE_EMPTY:
case UP_DEVICE_STATE_FULLY_CHARGED:
case UP_DEVICE_STATE_DISCHARGING:
case UP_DEVICE_STATE_PENDING_DISCHARGE:
return "discharging";
default:
return "unknown-status";
}
}
auto UPower::update() -> void {
std::lock_guard<std::mutex> guard(m_Mutex);
@ -230,6 +245,18 @@ auto UPower::update() -> void {
uint tooltipCount = 0;
// CSS status class
const std::string status = getDeviceStatus(state);
// Remove last status if it exists
if (!lastStatus.empty() && box_.get_style_context()->has_class(lastStatus)) {
box_.get_style_context()->remove_class(lastStatus);
}
// Add the new status class to the Box
if (!box_.get_style_context()->has_class(status)) {
box_.get_style_context()->add_class(status);
}
lastStatus = status;
if (devices.size() == 0 && !displayDeviceValid && hideIfEmpty) {
event_box_.set_visible(false);
goto update;