2023-10-02 12:06:35 -04:00
|
|
|
import { Battery, Widget } from '../../imports.js';
|
|
|
|
const { Label, Icon, Stack, Box } = Widget;
|
2023-09-06 17:33:47 -04:00
|
|
|
|
2023-10-17 13:47:02 -04:00
|
|
|
import Separator from '../misc/separator.js';
|
2023-09-11 19:57:21 -04:00
|
|
|
|
2023-09-06 17:33:47 -04:00
|
|
|
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`,
|
|
|
|
})],
|
|
|
|
]);
|
|
|
|
|
2023-10-02 12:06:35 -04:00
|
|
|
|
2023-09-06 17:33:47 -04:00
|
|
|
const Indicators = charging => Stack({
|
|
|
|
items: icons(charging),
|
2023-09-26 13:32:24 -04:00
|
|
|
connections: [[Battery, stack => {
|
|
|
|
stack.shown = `${Math.floor(Battery.percent / 10) * 10}`;
|
2023-09-06 17:33:47 -04:00
|
|
|
}]],
|
|
|
|
});
|
|
|
|
|
2023-09-08 14:14:10 -04:00
|
|
|
const Indicator = ({
|
2023-09-06 17:33:47 -04:00
|
|
|
charging = Indicators(true),
|
|
|
|
discharging = Indicators(false),
|
2023-10-17 13:47:02 -04:00
|
|
|
...props
|
2023-09-06 17:33:47 -04:00
|
|
|
} = {}) => Stack({
|
2023-10-17 13:47:02 -04:00
|
|
|
...props,
|
2023-09-06 17:33:47 -04:00
|
|
|
className: 'battery-indicator',
|
|
|
|
items: [
|
|
|
|
['true', charging],
|
|
|
|
['false', discharging],
|
|
|
|
],
|
2023-09-26 13:32:24 -04:00
|
|
|
connections: [[Battery, stack => {
|
|
|
|
stack.shown = `${Battery.charging || Battery.charged}`;
|
|
|
|
stack.toggleClassName('charging', Battery.charging);
|
|
|
|
stack.toggleClassName('charged', Battery.charged);
|
|
|
|
stack.toggleClassName('low', Battery.percent < 20);
|
2023-09-06 17:33:47 -04:00
|
|
|
}]],
|
|
|
|
});
|
|
|
|
|
2023-10-17 13:47:02 -04:00
|
|
|
const LevelLabel = props => Label({
|
|
|
|
...props,
|
2023-09-06 17:33:47 -04:00
|
|
|
className: 'label',
|
2023-10-17 13:47:02 -04:00
|
|
|
connections: [[Battery, self => self.label = `${Battery.percent}%`]],
|
2023-09-06 17:33:47 -04:00
|
|
|
});
|
|
|
|
|
2023-10-17 13:47:02 -04:00
|
|
|
export default () => Box({
|
2023-09-06 17:33:47 -04:00
|
|
|
className: 'toggle-off battery',
|
|
|
|
children: [
|
|
|
|
Indicator(),
|
|
|
|
Separator(5),
|
|
|
|
LevelLabel(),
|
|
|
|
],
|
|
|
|
});
|