fix: avoid crash on system with no battey in battery segment

This commit is contained in:
lnu 2021-01-05 08:23:11 +01:00 committed by Jan De Dobbeleer
parent 86d2c3c0fa
commit c57368ce7d

View file

@ -37,11 +37,6 @@ const (
func (b *batt) enabled() bool { func (b *batt) enabled() bool {
bt, err := b.env.getBatteryInfo() bt, err := b.env.getBatteryInfo()
display := b.props.getBool(DisplayCharging, true)
if !display && (bt.State == battery.Charging || bt.State == battery.Full) {
return false
}
displayError := b.props.getBool(DisplayError, false) displayError := b.props.getBool(DisplayError, false)
if err != nil && displayError { if err != nil && displayError {
b.percentageText = "BATT ERR" b.percentageText = "BATT ERR"
@ -58,6 +53,12 @@ func (b *batt) enabled() bool {
State: battery.Full, State: battery.Full,
} }
} }
display := b.props.getBool(DisplayCharging, true)
if !display && (bt.State == battery.Charging || bt.State == battery.Full) {
return false
}
batteryPercentage := bt.Current / bt.Full * 100 batteryPercentage := bt.Current / bt.Full * 100
batteryPercentage = math.Min(100, batteryPercentage) batteryPercentage = math.Min(100, batteryPercentage)
percentageText := fmt.Sprintf("%.0f", batteryPercentage) percentageText := fmt.Sprintf("%.0f", batteryPercentage)