outsource lib

This commit is contained in:
Flummi 2024-01-20 03:03:58 +01:00
parent d51555ee47
commit ecf5c67ffb
Signed by: Flummi
GPG Key ID: AA2AEF822A6F4817
2 changed files with 26 additions and 2 deletions

View File

@ -0,0 +1,22 @@
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)]; break;
}
return color;
}
function batteryLevel(batteryPercent) {
if(batteryPercent == -1)
return "charging";
else if(batteryPercent == -2)
return "not connected";
else
return batteryPercent + "%";
}

View File

@ -4,6 +4,8 @@ import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.kirigami 2.14 as Kirigami import org.kde.kirigami 2.14 as Kirigami
import "./lib/helper.js" as Helper
Item { Item {
id: main id: main
@ -39,7 +41,7 @@ Item {
color: "transparent" color: "transparent"
border.color: PlasmaCore.Theme.textColor border.color: PlasmaCore.Theme.textColor
radius: 4 radius: 4
Plasmoid.toolTipMainText: "battery level: " + batteryLevel() Plasmoid.toolTipMainText: "battery level: " + Helper.batteryLevel(batteryPercent)
Plasmoid.toolTipSubText: "polling rate: " + pollingrate + " seconds" Plasmoid.toolTipSubText: "polling rate: " + pollingrate + " seconds"
Item { Item {
@ -49,7 +51,7 @@ Item {
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
color: batteryColor() color: Helper.batteryColor(batteryPercent)
width: parent.width * Math.max(0, Math.min(batteryPercent, 100)) / 100 width: parent.width * Math.max(0, Math.min(batteryPercent, 100)) / 100
} }
Kirigami.Icon { // headphones icon Kirigami.Icon { // headphones icon