| 1 | import QtQuick |
| 2 | import QtQuick.Layouts |
| 3 | import Quickshell.Services.UPower |
| 4 | |
| 5 | // Battery block (SysPage, settings app > Power): charge in Doto, state, a segment bar (accent when low), details. |
| 6 | // After glyph-rice's battery card (github.com/Nebelung-cloud/glyph-rice, GPL-3.0), bar/Bar.qml. |
| 7 | ColumnLayout { |
| 8 | id: root |
| 9 | readonly property var bat: UPower.displayDevice |
| 10 | readonly property bool hasBat: bat !== null && bat.isPresent |
| 11 | readonly property real batPct: hasBat ? (bat.percentage > 1 ? bat.percentage / 100 : bat.percentage) : 0 |
| 12 | readonly property bool charging: hasBat && bat.state === UPowerDeviceState.Charging |
| 13 | readonly property bool low: batPct <= Settings.batLow / 100 && !charging |
| 14 | function stateLine() { |
| 15 | if (!hasBat) return "" |
| 16 | switch (bat.state) { |
| 17 | case UPowerDeviceState.Charging: |
| 18 | return "charging" + (bat.timeToFull > 0 ? " · full in " + Theme.fmtTime(bat.timeToFull) : "") |
| 19 | case UPowerDeviceState.Discharging: |
| 20 | return "on battery" + (bat.timeToEmpty > 0 ? " · " + Theme.fmtTime(bat.timeToEmpty) + " left" : "") |
| 21 | case UPowerDeviceState.FullyCharged: return "on ac · full" |
| 22 | default: return UPower.onBattery ? "on battery" : "on ac · not charging" |
| 23 | } |
| 24 | } |
| 25 | visible: hasBat |
| 26 | Layout.fillWidth: true |
| 27 | spacing: 12 |
| 28 | |
| 29 | SectionLabel { text: "battery" } |
| 30 | RowLayout { |
| 31 | Layout.leftMargin: 6 |
| 32 | spacing: 14 |
| 33 | Num { |
| 34 | text: Math.round(root.batPct * 100) |
| 35 | font.pixelSize: 56 |
| 36 | } |
| 37 | ColumnLayout { |
| 38 | spacing: 6 |
| 39 | Lb { text: "% battery"; color: Theme.fg } |
| 40 | Lb { text: root.stateLine() } |
| 41 | } |
| 42 | } |
| 43 | Seg { |
| 44 | Layout.fillWidth: true |
| 45 | Layout.preferredWidth: 100 |
| 46 | Layout.preferredHeight: 6 |
| 47 | Layout.leftMargin: 6 |
| 48 | Layout.rightMargin: 6 |
| 49 | n: 24 |
| 50 | f: root.batPct |
| 51 | on: root.low ? Theme.err : Theme.fill |
| 52 | } |
| 53 | Lb { |
| 54 | Layout.fillWidth: true |
| 55 | Layout.leftMargin: 6 |
| 56 | wrapMode: Text.Wrap |
| 57 | lineHeight: 1.5 |
| 58 | px: 10 |
| 59 | text: { |
| 60 | if (!root.hasBat) return "" |
| 61 | var lines = [] |
| 62 | if (root.bat.changeRate !== undefined && Math.abs(root.bat.changeRate) > 0.05) |
| 63 | lines.push("power draw " + Math.abs(root.bat.changeRate).toFixed(1) + " w") |
| 64 | if (Settings.chargeMode !== "off" && Battery.supported) |
| 65 | lines.push("charge limit · conservation" + (Battery.held ? ", holding" : "")) |
| 66 | if (root.bat.healthSupported) |
| 67 | lines.push("health " + Math.round(root.bat.healthPercentage) + "%") |
| 68 | return lines.join("\n") |
| 69 | } |
| 70 | } |
| 71 | } |