fix battery NaN: guard percentage and fix dead signal handler
This commit is contained in:
parent
660b34fde5
commit
6f385130ff
1 changed files with 11 additions and 8 deletions
|
|
@ -11,19 +11,22 @@ QtObject {
|
||||||
|
|
||||||
readonly property var dev: UPower.displayDevice
|
readonly property var dev: UPower.displayDevice
|
||||||
readonly property bool available: dev?.isLaptopBattery ?? false
|
readonly property bool available: dev?.isLaptopBattery ?? false
|
||||||
readonly property real pct: (dev?.percentage ?? 0) * 100
|
readonly property real percent: {
|
||||||
|
const p = dev?.percentage;
|
||||||
|
return (p !== undefined && p !== null && !isNaN(p)) ? p * 100 : 0;
|
||||||
|
}
|
||||||
readonly property bool charging: dev?.state === UPowerDeviceState.Charging
|
readonly property bool charging: dev?.state === UPowerDeviceState.Charging
|
||||||
readonly property real changeRate: dev?.changeRate ?? 0
|
readonly property real changeRate: dev?.changeRate ?? 0
|
||||||
readonly property int timeToFull: dev?.timeToFull ?? 0
|
readonly property int timeToFull: dev?.timeToFull ?? 0
|
||||||
readonly property int timeToEmpty: dev?.timeToEmpty ?? 0
|
readonly property int timeToEmpty: dev?.timeToEmpty ?? 0
|
||||||
readonly property bool healthSupported: dev?.healthSupported ?? false
|
readonly property bool healthSupported: dev?.healthSupported ?? false
|
||||||
readonly property real healthPct: (dev?.healthPercentage ?? 1) * 100
|
readonly property real healthPercent: (dev?.healthPercentage ?? 1) * 100
|
||||||
|
|
||||||
readonly property int critThresh: S.Modules.battery.critical || 15
|
readonly property int critThresh: S.Modules.battery.critical || 15
|
||||||
readonly property int warnThresh: S.Modules.battery.warning || 25
|
readonly property int warnThresh: S.Modules.battery.warning || 25
|
||||||
readonly property bool critical: pct < critThresh && !charging
|
readonly property bool critical: percent < critThresh && !charging
|
||||||
|
|
||||||
readonly property color stateColor: charging ? S.Theme.base0B : critical ? S.Theme.base09 : pct < warnThresh ? S.Theme.base0A : S.Theme.base05
|
readonly property color stateColor: charging ? S.Theme.base0B : critical ? S.Theme.base09 : percent < warnThresh ? S.Theme.base0A : S.Theme.base05
|
||||||
|
|
||||||
// 24h history (1440 samples @ 60s)
|
// 24h history (1440 samples @ 60s)
|
||||||
property var history: []
|
property var history: []
|
||||||
|
|
@ -34,7 +37,7 @@ QtObject {
|
||||||
repeat: true
|
repeat: true
|
||||||
triggeredOnStart: true
|
triggeredOnStart: true
|
||||||
onTriggered: {
|
onTriggered: {
|
||||||
const h = root.history.concat([root.pct]);
|
const h = root.history.concat([root.percent]);
|
||||||
root.history = h.length > 1440 ? h.slice(h.length - 1440) : h;
|
root.history = h.length > 1440 ? h.slice(h.length - 1440) : h;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -49,15 +52,15 @@ QtObject {
|
||||||
root._warnSent = false;
|
root._warnSent = false;
|
||||||
root._critSent = false;
|
root._critSent = false;
|
||||||
}
|
}
|
||||||
function onPctChanged() {
|
function onPercentChanged() {
|
||||||
if (root.charging)
|
if (root.charging)
|
||||||
return;
|
return;
|
||||||
if (root.pct < root.critThresh && !root._critSent) {
|
if (root.percent < root.critThresh && !root._critSent) {
|
||||||
root._critSent = true;
|
root._critSent = true;
|
||||||
root._warnSent = true;
|
root._warnSent = true;
|
||||||
_notifProc.command = ["notify-send", "--urgency=critical", "--icon=battery-low", "--category=device", "Very Low Battery", "Connect to power now!"];
|
_notifProc.command = ["notify-send", "--urgency=critical", "--icon=battery-low", "--category=device", "Very Low Battery", "Connect to power now!"];
|
||||||
_notifProc.running = true;
|
_notifProc.running = true;
|
||||||
} else if (root.pct < root.warnThresh && !root._warnSent) {
|
} else if (root.percent < root.warnThresh && !root._warnSent) {
|
||||||
root._warnSent = true;
|
root._warnSent = true;
|
||||||
_notifProc.command = ["notify-send", "--icon=battery-caution", "--category=device", "Low Battery"];
|
_notifProc.command = ["notify-send", "--icon=battery-caution", "--category=device", "Low Battery"];
|
||||||
_notifProc.running = true;
|
_notifProc.running = true;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue