56 lines
2.1 KiB
QML
56 lines
2.1 KiB
QML
pragma Singleton
|
|
|
|
import QtQuick
|
|
import Quickshell.Services.UPower
|
|
import "." as S
|
|
import NovaStats as NS
|
|
|
|
QtObject {
|
|
id: root
|
|
|
|
readonly property var dev: UPower.displayDevice
|
|
readonly property bool available: dev?.isLaptopBattery ?? false
|
|
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 real changeRate: dev?.changeRate ?? 0
|
|
readonly property int timeToFull: dev?.timeToFull ?? 0
|
|
readonly property int timeToEmpty: dev?.timeToEmpty ?? 0
|
|
readonly property bool healthSupported: dev?.healthSupported ?? false
|
|
readonly property real healthPercent: (dev?.healthPercentage ?? 1) * 100
|
|
|
|
readonly property int critThresh: NS.ModulesService.batteryCritical || 15
|
|
readonly property int warnThresh: NS.ModulesService.batteryWarning || 25
|
|
readonly property bool critical: percent < critThresh && !charging
|
|
|
|
readonly property color stateColor: charging ? NS.ThemeService.base0B : critical ? NS.ThemeService.base09 : percent < warnThresh ? NS.ThemeService.base0A : NS.ThemeService.base05
|
|
|
|
// 24h history (1440 samples @ 60s)
|
|
property var history: []
|
|
// Wattage history (60 samples @ 60s = 1h), signed: positive = charging
|
|
property var rateHistory: []
|
|
|
|
property var _histTimer: Timer {
|
|
interval: 60000
|
|
running: root.available
|
|
repeat: true
|
|
triggeredOnStart: true
|
|
onTriggered: {
|
|
const h = root.history.concat([root.percent]);
|
|
root.history = h.length > 1440 ? h.slice(h.length - 1440) : h;
|
|
const w = root.charging ? root.changeRate : -root.changeRate;
|
|
const rh = root.rateHistory.concat([w]);
|
|
root.rateHistory = rh.length > 60 ? rh.slice(rh.length - 60) : rh;
|
|
}
|
|
}
|
|
|
|
function fmtTime(secs) {
|
|
if (!secs || secs <= 0)
|
|
return "";
|
|
const h = Math.floor(secs / 3600);
|
|
const m = Math.floor((secs % 3600) / 60);
|
|
return h > 0 ? h + "h " + m + "m" : m + "m";
|
|
}
|
|
}
|