From de0ae352cb0fce4e08cbe479a3b7b0c557405270 Mon Sep 17 00:00:00 2001 From: Flummi Date: Sat, 20 Jan 2024 04:49:58 +0100 Subject: [PATCH] icons & tweaks --- package/contents/ui/lib/helper.js | 37 +++++++++++++++++-------------- package/contents/ui/main.qml | 10 ++++----- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/package/contents/ui/lib/helper.js b/package/contents/ui/lib/helper.js index 5beafee..4984a62 100644 --- a/package/contents/ui/lib/helper.js +++ b/package/contents/ui/lib/helper.js @@ -1,22 +1,25 @@ +const colors = [ + '#f00', '#e21c00', '#c83800', '#a50', '#8d7100', + '#718d00', '#6fa100', '#5c8000', '#498000', '#080' +]; + function batteryColor(batteryPercent) { - const colors = [ - '#f00', '#e21c00', '#c83800', '#a50', '#8d7100', - '#718d00', '#6fa100', '#5c8000', '#498000', '#080' - ]; - let color; - switch(batteryPercent) { - case -1: color = "green"; break; - case -2: color = "red"; break; - default: color = colors[~~(batteryPercent / 10 + 0.5) - 1]; break; - } - return color; + return colors[({ + "-2": 9, + "-1": 0 + })[batteryPercent] ?? ~~(batteryPercent / 10 + 0.5) - 1]; } function batteryLevel(batteryPercent) { - if(batteryPercent == -1) - return "charging"; - else if(batteryPercent == -2) - return "not connected"; - else - return batteryPercent + "%"; + return ({ + "-2": "not connected", + "-1": "charging" + })[batteryPercent] ?? batteryPercent + "%"; +} + +function batteryIcon(batteryPercent) { + return ({ + "-2": "action-unavailable", + "-1": "battery-ac-adapter" + })[batteryPercent] ?? "audio-headphones-symbolic"; } diff --git a/package/contents/ui/main.qml b/package/contents/ui/main.qml index bf190ab..b572b27 100644 --- a/package/contents/ui/main.qml +++ b/package/contents/ui/main.qml @@ -18,11 +18,9 @@ Item { connectedSources: ["headsetcontrol -bc"] interval: pollingrate * 1e3 onNewData: { - if(data['exit code'] > 0) { - batteryPercent = -2; - return; - } - batteryPercent = data['stdout']; + return batteryPercent = data['exit code'] > 0 + ? -2 // not connected + : data['stdout']; } } @@ -60,7 +58,7 @@ Item { anchors.horizontalCenter: parent.horizontalCenter anchors.topMargin: 1 anchors.bottomMargin: 1 - source: "audio-headphones-symbolic" + source: Helper.batteryIcon(batteryPercent) } } }