feat(ags razer): add more props to monitor
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2024-02-26 01:23:19 -05:00
parent 235563aa0a
commit eeea68789c

View file

@ -1,63 +1,90 @@
import { execAsync, interval } from 'resource:///com/github/Aylur/ags/utils.js';
const { Box, Icon, Label } = Widget; const { Box, Icon, Label } = Widget;
const RAZER_POLL = 30000; const RAZER_POLL = 10000;
const LOW_BATT = 20; const LOW_BATT = 20;
const RazerBat = Variable({
// TODO: add charging indicator battery: 0,
const RazerBat = Variable(-1, { charging: false,
poll: [ sleeping: false,
RAZER_POLL, disconnected: true,
[
'bash',
'-c',
"polychromatic-cli -n 'Razer Naga Pro (Wired)' -k" +
' || ' +
"polychromatic-cli -n 'Razer Naga Pro (Wireless)' -k",
],
(out) => {
const batteryMatches = out.split('\n')
.filter((i) => i.includes('Battery'))[0]
.match(/[0-9]+/);
let battery = batteryMatches !== null ?
parseInt(batteryMatches[0]) :
-1;
// Don't set to zero when in sleep mode
if (battery === 0 && RazerBat.value > 10) {
battery = RazerBat.value;
}
return battery;
},
],
}); });
const fetchInfo = () => {
execAsync([
'bash',
'-c',
"polychromatic-cli -n 'Razer Naga Pro (Wired)' -k" +
' || ' +
"polychromatic-cli -n 'Razer Naga Pro (Wireless)' -k",
]).then((out) => {
const batteryMatches = out.split('\n')
.filter((i) => i.includes('Battery'))[0]
.match(/[0-9]+/);
let sleeping = false;
let battery = batteryMatches !== null ?
parseInt(batteryMatches[0]) :
0;
// Don't set to zero when in sleep mode
if (battery === 0 && RazerBat.value.battery > 10) {
battery = RazerBat.value.battery;
sleeping = true;
}
// If 'Wireless' isn't in the logs it means the cmd found 'Wired'
const charging = !out.includes('Wireless');
RazerBat.value = {
battery,
charging,
sleeping,
disconnected: false,
};
}).catch(() => {
RazerBat.value.disconnected = true;
});
};
interval(RAZER_POLL, fetchInfo);
// TODO: add charging indicator
export default () => Box({ export default () => Box({
class_name: 'razer', class_name: 'razer',
children: [ children: [
Icon({ Icon({
hpack: 'start', hpack: 'start',
icon: RazerBat.bind().transform((v) => { icon: RazerBat.bind().transform((v) => {
return v > -1 ? return v.disconnected ?
'mouse-razer-symbolic' : 'content-loading-symbolic' :
'content-loading-symbolic'; 'mouse-razer-symbolic';
}), }),
setup: (self) => { setup: (self) => {
self.hook(RazerBat, () => { self.hook(RazerBat, () => {
const v = RazerBat.value;
self.setCss(
v.disconnected ?
'margin-right: -5px;' :
'',
);
self.toggleClassName( self.toggleClassName(
'high', 'high',
RazerBat.value > 66, v.battery > 66,
); );
self.toggleClassName( self.toggleClassName(
'medium', 'medium',
RazerBat.value > LOW_BATT && RazerBat.value <= 66, v.battery > LOW_BATT && v.battery <= 66,
); );
self.toggleClassName( self.toggleClassName(
'low', 'low',
RazerBat.value <= LOW_BATT, v.battery <= LOW_BATT,
); );
}); });
}, },
@ -66,7 +93,12 @@ export default () => Box({
Label({ Label({
vpack: 'center', vpack: 'center',
label: RazerBat.bind().transform((v) => { label: RazerBat.bind().transform((v) => {
return v !== -1 ? `${v}%` : ''; if (!v.disconnected) {
// TODO: find better way to indicate charging
return v.battery + (v.charging ? '󱐋' : '%');
}
return '';
}), }),
}), }),
], ],