From 68644abcad270bd60dda04e2e602ba1b1518a077 Mon Sep 17 00:00:00 2001 From: Zeph T Date: Fri, 13 Jun 2025 15:11:02 +0800 Subject: [PATCH] Fix battery percent calculation This applet previously relied on the 'capacity' file to obtain percent charge remaning. However, with my 2012 macbook pro whose battery is not capable of reaching the design spec for full charge, this capacity is incorrect. Instead, the real effective charge percentage should be calculated using the 'charge_now' and 'charge_full' files. Matches default battery applet % charge now. --- .../batterymonitor@pdcurtis/5.4/applet.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/batterymonitor@pdcurtis/files/batterymonitor@pdcurtis/5.4/applet.js b/batterymonitor@pdcurtis/files/batterymonitor@pdcurtis/5.4/applet.js index e54755e5018..a95378055fc 100644 --- a/batterymonitor@pdcurtis/files/batterymonitor@pdcurtis/5.4/applet.js +++ b/batterymonitor@pdcurtis/files/batterymonitor@pdcurtis/5.4/applet.js @@ -464,9 +464,22 @@ MyApplet.prototype = { try { // Try to get the battery capacity as an int let capacityPath = this.batteryPath + '/capacity'; - let capacityValue = GLib.file_get_contents(capacityPath)[1]; - let capacityValueString = ByteArray.toString(capacityValue); - this.batteryPercentage = parseInt(capacityValueString); + + // Original code doesn't account for battery capacity drop due to ageing + // let capacityValue = GLib.file_get_contents(capacityPath)[1]; + // let capacityValueString = ByteArray.toString(capacityValue); + // this.batteryPercentage = parseInt(capacityValueString); + // this.batteryPercentage = 50; + + // New battery percentage calculated from actual attainable charge vs current charge + let batteryFullChargePath = this.batteryPath + '/charge_full'; + let batteryFullChargeValue = GLib.file_get_contents(batteryFullChargePath)[1]; + let batteryNowChargePath = this.batteryPath + '/charge_now'; + let batteryNowChargeValue = GLib.file_get_contents(batteryNowChargePath)[1]; + let chargeFullString = ByteArray.toString(batteryFullChargeValue); + let chargeNowString = ByteArray.toString(batteryNowChargeValue); + this.batteryPercentage = Math.round(100*parseInt(chargeNowString)/parseInt(chargeFullString)); + // Try to get the battery state as a lowercase string let statePath = this.batteryPath + '/status';