diff --git a/Bar/Modules/Battery.qml b/Bar/Modules/Battery.qml index d4ccd2c..03af9c3 100644 --- a/Bar/Modules/Battery.qml +++ b/Bar/Modules/Battery.qml @@ -4,6 +4,7 @@ import Quickshell.Services.UPower import QtQuick.Layouts import qs.Components import qs.Settings +import "../../Helpers/Time.js" as Time Item { id: batteryWidget @@ -76,17 +77,37 @@ Item { positionAbove: false text: { let lines = []; - if (batteryWidget.isReady) { + if (!batteryWidget.isReady) { + return ""; + } + + if (batteryWidget.battery.timeToEmpty > 0) { + lines.push("Time left: " + Time.formatVagueHumanReadableTime(batteryWidget.battery.timeToEmpty)); + } + + if (batteryWidget.battery.timeToFull > 0) { + lines.push("Time until full: " + Time.formatVagueHumanReadableTime(batteryWidget.battery.timeToFull)); + } + + if (batteryWidget.battery.changeRate !== undefined) { + const rate = batteryWidget.battery.changeRate; + if (rate > 0) { + lines.push(batteryWidget.charging ? "Charging rate: " + rate.toFixed(2) + " W" : "Discharging rate: " + rate.toFixed(2) + " W"); + } + else if (rate < 0) { + lines.push("Discharging rate: " + Math.abs(rate).toFixed(2) + " W"); + } + else { + lines.push("Estimating..."); + } + } + else { lines.push(batteryWidget.charging ? "Charging" : "Discharging"); - lines.push(Math.round(batteryWidget.percent) + "%"); - if (batteryWidget.battery.changeRate !== undefined) - lines.push("Rate: " + batteryWidget.battery.changeRate.toFixed(2) + " W"); - if (batteryWidget.battery.timeToEmpty > 0) - lines.push("Time left: " + Math.floor(batteryWidget.battery.timeToEmpty / 60) + " min"); - if (batteryWidget.battery.timeToFull > 0) - lines.push("Time to full: " + Math.floor(batteryWidget.battery.timeToFull / 60) + " min"); - if (batteryWidget.battery.healthPercentage !== undefined) - lines.push("Health: " + Math.round(batteryWidget.battery.healthPercentage) + "%"); + } + + + if (batteryWidget.battery.healthPercentage !== undefined && batteryWidget.battery.healthPercentage > 0) { + lines.push("Health: " + Math.round(batteryWidget.battery.healthPercentage) + "%"); } return lines.join("\n"); } diff --git a/Components/StyledTooltip.qml b/Components/StyledTooltip.qml index fed3d1c..9c30baf 100644 --- a/Components/StyledTooltip.qml +++ b/Components/StyledTooltip.qml @@ -16,8 +16,8 @@ Window { color: "transparent" visible: false - minimumWidth: tooltipText.implicitWidth + 24 - minimumHeight: tooltipText.implicitHeight + 16 + minimumWidth: Math.max(50, tooltipText.implicitWidth + 24) + minimumHeight: Math.max(50, tooltipText.implicitHeight + 16) property var _timerObj: null onTooltipVisibleChanged: { diff --git a/Helpers/Time.js b/Helpers/Time.js new file mode 100644 index 0000000..7e1e316 --- /dev/null +++ b/Helpers/Time.js @@ -0,0 +1,18 @@ +function formatVagueHumanReadableTime(totalSeconds) { + const hours = Math.floor(totalSeconds / 3600); + const minutes = Math.floor((totalSeconds - (hours * 3600)) / 60); + const seconds = totalSeconds - (hours * 3600) - (minutes * 60); + + var str = ""; + if (hours) { + str += hours.toString() + "h"; + } + if (minutes) { + str += minutes.toString() + "m"; + } + if (!hours && !minutes) { + str += seconds.toString() + "s"; + } + return str; +} +