fix battery popup thresholds firing more than once per charging period

This commit is contained in:
2026-05-29 12:25:28 +02:00
parent 0819e8e2d1
commit d6102d9ebe
+30 -6
View File
@@ -11,16 +11,32 @@ Scope {
readonly property real currentPerc: UPower.displayDevice.percentage readonly property real currentPerc: UPower.displayDevice.percentage
readonly property list<var> popupThresholds: [...Config.general.battery.popupThresholds].sort((a, b) => b.perc - a.perc) readonly property list<var> popupThresholds: [...Config.general.battery.popupThresholds].sort((a, b) => b.perc - a.perc)
function nearestThresholdAbove(p: real): var {
const thresholds = [...root.popupThresholds];
for (const perc of thresholds) {
console.log(perc.message);
if (p < perc.perc)
return perc;
}
return null;
}
Connections { Connections {
function onOnBatteryChanged(): void { function onOnBatteryChanged(): void {
if (!UPower.displayDevice.ready)
return;
if (UPower.onBattery) { if (UPower.onBattery) {
if (Config.utilities.toasts.chargingChanged) if (Config.utilities.toasts.chargingChanged)
Toaster.toast(qsTr("Charger unplugged"), qsTr("Battery is discharging"), "power_off"); Toaster.toast(qsTr("Charger unplugged"), qsTr("Battery is discharging"), "power_off");
const p = root.currentPerc * 100;
const perc = root.nearestThresholdAbove(p);
if (perc)
Toaster.toast(perc.title ?? qsTr("Battery warning"), perc.message ?? qsTr("Battery is low"), perc.icon ?? "battery_android_alert", perc.critical ? Toast.Error : Toast.Warning);
} else { } else {
if (Config.utilities.toasts.chargingChanged) if (Config.utilities.toasts.chargingChanged)
Toaster.toast(qsTr("Charger plugged in"), qsTr("Battery is charging"), "power"); Toaster.toast(qsTr("Charger plugged in"), qsTr("Battery is charging"), "power");
for (const level of root.popupThresholds)
level.warned = false;
} }
} }
@@ -32,16 +48,24 @@ Scope {
if (!UPower.onBattery) if (!UPower.onBattery)
return; return;
const p = UPower.displayDevice.percentage * 100; const p = root.currentPerc * 100;
for (const perc of root.popupThresholds) { for (const perc of root.popupThresholds) {
if (p <= perc.perc && !perc.warned) { if (p == perc.perc) {
perc.warned = true;
console.log(perc.warned + "\n" + [...Config.general.battery.popupThresholds][0].warned);
Toaster.toast(perc.title ?? qsTr("Battery warning"), perc.message ?? qsTr("Battery perc is low"), perc.icon ?? "battery_android_alert", perc.critical ? Toast.Error : Toast.Warning); Toaster.toast(perc.title ?? qsTr("Battery warning"), perc.message ?? qsTr("Battery perc is low"), perc.icon ?? "battery_android_alert", perc.critical ? Toast.Error : Toast.Warning);
} }
} }
} }
function onReadyChanged(): void {
if (!UPower.displayDevice.ready)
return;
const p = root.currentPerc * 100;
const perc = root.nearestThresholdAbove(p);
if (perc)
Toaster.toast(perc.title ?? qsTr("Battery warning"), perc.message ?? qsTr("Battery is low"), perc.icon ?? "battery_android_alert", perc.critical ? Toast.Error : Toast.Warning);
}
target: UPower.displayDevice target: UPower.displayDevice
} }
} }