nova-shell/modules/Battery.qml
2026-04-12 16:17:36 +02:00

40 lines
1.7 KiB
QML

import QtQuick
import Quickshell.Services.UPower
import "." as M
M.BarSection {
id: root
spacing: M.Theme.moduleSpacing
visible: M.Modules.battery && (UPower.displayDevice?.isLaptopBattery ?? false)
tooltip: {
const state = root.charging ? "Charging" : "Discharging";
const t = root.charging ? root.dev?.timeToFull : root.dev?.timeToEmpty;
const mins = t ? Math.round(t / 60) : 0;
const timeStr = mins > 0 ? "\n" + Math.floor(mins / 60) + "h " + (mins % 60) + "m " + (root.charging ? "until full" : "remaining") : "";
return state + " \u2014 " + Math.round(root.pct) + "%" + timeStr;
}
readonly property var dev: UPower.displayDevice
readonly property real pct: (dev?.percentage ?? 0) * 100
readonly property bool charging: dev?.state === UPowerDeviceState.Charging
readonly property color _stateColor: charging ? M.Theme.base0B : pct < 15 ? M.Theme.base08 : pct < 30 ? M.Theme.base09 : M.Theme.base0B
Behavior on _stateColor { ColorAnimation { duration: 300 } }
M.BarIcon {
icon: {
if (root.charging)
return "\uDB80\uDC84";
const icons = ["\uDB80\uDC8E", "\uDB80\uDC7A", "\uDB80\uDC7B", "\uDB80\uDC7C", "\uDB80\uDC7D", "\uDB80\uDC7E", "\uDB80\uDC7F", "\uDB80\uDC80", "\uDB80\uDC81", "\uDB80\uDC82", "\uDB85\uDFE2"];
return icons[Math.min(10, Math.floor(root.pct / 10))];
}
color: root._stateColor
font.pixelSize: M.Theme.fontSize + 2
anchors.verticalCenter: parent.verticalCenter
}
M.BarLabel {
label: Math.round(root.pct) + "%"
color: root._stateColor
anchors.verticalCenter: parent.verticalCenter
}
}