diff --git a/Daemons/Battery.qml b/Daemons/Battery.qml index e3dbde1..be318a2 100644 --- a/Daemons/Battery.qml +++ b/Daemons/Battery.qml @@ -11,16 +11,32 @@ Scope { readonly property real currentPerc: UPower.displayDevice.percentage readonly property list 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 { function onOnBatteryChanged(): void { + if (!UPower.displayDevice.ready) + return; + if (UPower.onBattery) { if (Config.utilities.toasts.chargingChanged) 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 { if (Config.utilities.toasts.chargingChanged) 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) return; - const p = UPower.displayDevice.percentage * 100; + const p = root.currentPerc * 100; for (const perc of root.popupThresholds) { - if (p <= perc.perc && !perc.warned) { - perc.warned = true; - console.log(perc.warned + "\n" + [...Config.general.battery.popupThresholds][0].warned); + if (p == perc.perc) { 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 } }