Add a warning color when battery is low

The threshold is controlled via BatteryWarnThreshold and the color via
battery-warning
This commit is contained in:
scorpion-26 2024-02-12 21:05:24 +01:00
parent e717e0e7ad
commit 12d1c59ef0
5 changed files with 27 additions and 9 deletions

View file

@ -231,7 +231,11 @@ highlight {
}
.battery-charging {
color: $orange
color: $orange;
}
.battery-warning {
color: $red;
}
.network-data-text {

View file

@ -39,6 +39,9 @@ ExitCommand: killall Hyprland
# The folder, where the battery sensors reside
BatteryFolder: /sys/class/power_supply/BAT1
# Threshold, when the battery is considered low and a different color (as specified by the 'battery-warning' CSS property) is applied
BatteryWarnThreshold: 20
# The partition to monitor with disk sensor
DiskPartition: /

View file

@ -107,6 +107,20 @@ namespace Bar
batteryText->RemoveClass("battery-charging");
wasCharging = false;
}
// Add warning if color falls below threshold
if (!isCharging && percentage * 100 <= Config::Get().batteryWarnThreshold)
{
sensor.AddClass("battery-warning");
if (batteryText)
batteryText->AddClass("battery-warning");
}
else
{
sensor.RemoveClass("battery-warning");
if (batteryText)
batteryText->RemoveClass("battery-warning");
}
return TimerResult::Ok;
}
@ -417,8 +431,7 @@ namespace Bar
#endif
}
void WidgetSensor(Widget& parent, TimerCallback<Sensor>&& callback, const std::string& sensorName, Text*& textPtr,
Side side)
void WidgetSensor(Widget& parent, TimerCallback<Sensor>&& callback, const std::string& sensorName, Text*& textPtr, Side side)
{
auto eventBox = Widget::Create<EventBox>();
Utils::SetTransform(*eventBox, {-1, false, SideToAlignment(side)});
@ -571,12 +584,8 @@ namespace Bar
box->AddClass("widget");
switch (type)
{
case AudioType::Input:
box->AddClass("mic");
break;
case AudioType::Output:
box->AddClass("audio");
break;
case AudioType::Input: box->AddClass("mic"); break;
case AudioType::Output: box->AddClass("audio"); break;
}
Utils::SetTransform(*box, {-1, false, SideToAlignment(side)});

View file

@ -294,6 +294,7 @@ void Config::Load(const std::string& overrideConfigLocation)
AddConfigVar("AudioScrollSpeed", config.audioScrollSpeed, lineView, foundProperty);
AddConfigVar("SensorSize", config.sensorSize, lineView, foundProperty);
AddConfigVar("NetworkIconSize", config.networkIconSize, lineView, foundProperty);
AddConfigVar("BatteryWarnThreshold", config.batteryWarnThreshold, lineView, foundProperty);
AddConfigVar("AudioMinVolume", config.audioMinVolume, lineView, foundProperty);
AddConfigVar("AudioMaxVolume", config.audioMaxVolume, lineView, foundProperty);

View file

@ -78,6 +78,7 @@ public:
uint32_t numWorkspaces = 9; // How many workspaces to display
uint32_t sensorSize = 24; // The size of the circular sensors
uint32_t networkIconSize = 24; // The size of the two network arrows
uint32_t batteryWarnThreshold = 20; // Threshold for color change when on battery
char location = 'T'; // The Location of the bar. Can be L,R,T,B