2023-09-06 17:33:47 -04:00
|
|
|
const { Label, Icon, Stack, ProgressBar, Overlay, Box } = ags.Widget;
|
|
|
|
import { Separator } from '../common.js';
|
|
|
|
const { exec } = ags.Utils;
|
|
|
|
|
|
|
|
const icons = charging => ([
|
2023-09-08 14:14:10 -04:00
|
|
|
...Array.from({ length: 10 }, (_, i) => i * 10).map(i => ([
|
2023-09-06 17:33:47 -04:00
|
|
|
`${i}`, Icon({
|
|
|
|
className: `${i} ${charging ? 'charging' : 'discharging'}`,
|
|
|
|
icon: `battery-level-${i}${charging ? '-charging' : ''}-symbolic`,
|
|
|
|
}),
|
|
|
|
])),
|
|
|
|
['100', Icon({
|
|
|
|
className: `100 ${charging ? 'charging' : 'discharging'}`,
|
|
|
|
icon: `battery-level-100${charging ? '-charged' : ''}-symbolic`,
|
|
|
|
})],
|
|
|
|
]);
|
|
|
|
|
|
|
|
const Indicators = charging => Stack({
|
|
|
|
items: icons(charging),
|
|
|
|
connections: [[1000, stack => {
|
|
|
|
stack.shown = `${Math.floor(exec('cat /sys/class/power_supply/BAT0/capacity') / 10) * 10}`;
|
|
|
|
}]],
|
|
|
|
});
|
|
|
|
|
2023-09-08 14:14:10 -04:00
|
|
|
const Indicator = ({
|
2023-09-06 17:33:47 -04:00
|
|
|
charging = Indicators(true),
|
|
|
|
discharging = Indicators(false),
|
|
|
|
...props
|
|
|
|
} = {}) => Stack({
|
|
|
|
...props,
|
|
|
|
className: 'battery-indicator',
|
|
|
|
items: [
|
|
|
|
['true', charging],
|
|
|
|
['false', discharging],
|
|
|
|
],
|
|
|
|
connections: [[1000, stack => {
|
|
|
|
const charging = exec('cat /sys/class/power_supply/BAT0/status') == 'Charging';
|
|
|
|
const charged = exec('cat /sys/class/power_supply/BAT0/capacity') == 100;
|
|
|
|
stack.shown = `${charging || charged}`;
|
|
|
|
stack.toggleClassName('charging', charging);
|
|
|
|
stack.toggleClassName('charged', charged);
|
|
|
|
stack.toggleClassName('low', exec('cat /sys/class/power_supply/BAT0/capacity') < 30);
|
|
|
|
}]],
|
|
|
|
});
|
|
|
|
|
2023-09-08 14:14:10 -04:00
|
|
|
const LevelLabel = props => Label({
|
2023-09-06 17:33:47 -04:00
|
|
|
...props,
|
|
|
|
className: 'label',
|
|
|
|
connections: [[1000, label => label.label = `${exec('cat /sys/class/power_supply/BAT0/capacity')}%`]],
|
|
|
|
});
|
|
|
|
|
2023-09-08 14:14:10 -04:00
|
|
|
const BatteryLabel = () => Box({
|
2023-09-06 17:33:47 -04:00
|
|
|
className: 'toggle-off battery',
|
|
|
|
children: [
|
|
|
|
Indicator(),
|
|
|
|
Separator(5),
|
|
|
|
LevelLabel(),
|
|
|
|
],
|
|
|
|
});
|
2023-09-08 14:14:10 -04:00
|
|
|
|
|
|
|
export const Batt = BatteryLabel();
|