2024-09-27 21:14:58 -04:00
|
|
|
import { bind } from 'astal';
|
2024-09-26 23:55:06 -04:00
|
|
|
|
|
|
|
import AstalBattery from 'gi://AstalBattery';
|
|
|
|
const Battery = AstalBattery.get_default();
|
|
|
|
|
|
|
|
import Separator from '../../misc/separator';
|
|
|
|
|
|
|
|
|
|
|
|
const LOW_BATT = 20;
|
|
|
|
|
|
|
|
export default () => (
|
2024-09-27 12:37:14 -04:00
|
|
|
<box className="bar-item battery">
|
2024-09-26 23:55:06 -04:00
|
|
|
<icon
|
2024-09-27 21:14:58 -04:00
|
|
|
setup={(self) => {
|
2024-09-27 13:56:39 -04:00
|
|
|
const update = () => {
|
2024-09-27 12:37:14 -04:00
|
|
|
const percent = Math.round(Battery.get_percentage() * 100);
|
2024-09-27 13:56:39 -04:00
|
|
|
const level = Math.floor(percent / 10) * 10;
|
|
|
|
const isCharging = Battery.get_charging();
|
|
|
|
const charged = percent === 100 && isCharging;
|
|
|
|
const iconName = charged ?
|
|
|
|
'battery-level-100-charged-symbolic' :
|
|
|
|
`battery-level-${level}${isCharging ?
|
|
|
|
'-charging' :
|
|
|
|
''}-symbolic`;
|
|
|
|
|
|
|
|
self.set_icon(iconName);
|
|
|
|
|
|
|
|
self.toggleClassName('charging', isCharging);
|
|
|
|
self.toggleClassName('charged', charged);
|
2024-09-27 12:37:14 -04:00
|
|
|
self.toggleClassName('low', percent < LOW_BATT);
|
2024-09-27 13:56:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
update();
|
|
|
|
|
|
|
|
Battery.connect('notify::percentage', () => update());
|
2024-09-27 22:15:08 -04:00
|
|
|
Battery.connect('notify::icon-name', () => update());
|
|
|
|
Battery.connect('notify::battery-icon-name', () => update());
|
2024-09-26 23:55:06 -04:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
2024-09-27 21:14:58 -04:00
|
|
|
<Separator size={8} />
|
2024-09-26 23:55:06 -04:00
|
|
|
|
2024-09-27 12:37:14 -04:00
|
|
|
<label label={bind(Battery, 'percentage').as((v) => `${Math.round(v * 100)}%`)} />
|
2024-09-26 23:55:06 -04:00
|
|
|
</box>
|
|
|
|
);
|