icons & tweaks

This commit is contained in:
Flummi 2024-01-20 04:49:58 +01:00
parent c36eb8daae
commit de0ae352cb
Signed by: Flummi
GPG Key ID: AA2AEF822A6F4817
2 changed files with 24 additions and 23 deletions

View File

@ -1,22 +1,25 @@
function batteryColor(batteryPercent) { const colors = [
const colors = [
'#f00', '#e21c00', '#c83800', '#a50', '#8d7100', '#f00', '#e21c00', '#c83800', '#a50', '#8d7100',
'#718d00', '#6fa100', '#5c8000', '#498000', '#080' '#718d00', '#6fa100', '#5c8000', '#498000', '#080'
]; ];
let color;
switch(batteryPercent) { function batteryColor(batteryPercent) {
case -1: color = "green"; break; return colors[({
case -2: color = "red"; break; "-2": 9,
default: color = colors[~~(batteryPercent / 10 + 0.5) - 1]; break; "-1": 0
} })[batteryPercent] ?? ~~(batteryPercent / 10 + 0.5) - 1];
return color;
} }
function batteryLevel(batteryPercent) { function batteryLevel(batteryPercent) {
if(batteryPercent == -1) return ({
return "charging"; "-2": "not connected",
else if(batteryPercent == -2) "-1": "charging"
return "not connected"; })[batteryPercent] ?? batteryPercent + "%";
else }
return batteryPercent + "%";
function batteryIcon(batteryPercent) {
return ({
"-2": "action-unavailable",
"-1": "battery-ac-adapter"
})[batteryPercent] ?? "audio-headphones-symbolic";
} }

View File

@ -18,11 +18,9 @@ Item {
connectedSources: ["headsetcontrol -bc"] connectedSources: ["headsetcontrol -bc"]
interval: pollingrate * 1e3 interval: pollingrate * 1e3
onNewData: { onNewData: {
if(data['exit code'] > 0) { return batteryPercent = data['exit code'] > 0
batteryPercent = -2; ? -2 // not connected
return; : data['stdout'];
}
batteryPercent = data['stdout'];
} }
} }
@ -60,7 +58,7 @@ Item {
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
anchors.topMargin: 1 anchors.topMargin: 1
anchors.bottomMargin: 1 anchors.bottomMargin: 1
source: "audio-headphones-symbolic" source: Helper.batteryIcon(batteryPercent)
} }
} }
} }