1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-01-29 16:02:51 -05:00

battery view improvements

This commit is contained in:
bbedward
2025-08-10 13:35:59 -04:00
parent d32b09c999
commit 78c38a898c
3 changed files with 172 additions and 70 deletions

View File

@@ -30,9 +30,58 @@ Rectangle {
spacing: 4 spacing: 4
DankIcon { DankIcon {
name: Theme.getBatteryIcon(BatteryService.batteryLevel, name: {
BatteryService.isCharging, if (!BatteryService.batteryAvailable)
BatteryService.batteryAvailable) return "power"
if (BatteryService.isCharging) {
if (BatteryService.batteryLevel >= 90)
return "battery_charging_full"
if (BatteryService.batteryLevel >= 80)
return "battery_charging_90"
if (BatteryService.batteryLevel >= 60)
return "battery_charging_80"
if (BatteryService.batteryLevel >= 50)
return "battery_charging_60"
if (BatteryService.batteryLevel >= 30)
return "battery_charging_50"
if (BatteryService.batteryLevel >= 20)
return "battery_charging_30"
return "battery_charging_20"
}
// Check if plugged in but not charging (like at 80% charge limit)
if (BatteryService.isPluggedIn) {
if (BatteryService.batteryLevel >= 90)
return "battery_charging_full"
if (BatteryService.batteryLevel >= 80)
return "battery_charging_90"
if (BatteryService.batteryLevel >= 60)
return "battery_charging_80"
if (BatteryService.batteryLevel >= 50)
return "battery_charging_60"
if (BatteryService.batteryLevel >= 30)
return "battery_charging_50"
if (BatteryService.batteryLevel >= 20)
return "battery_charging_30"
return "battery_charging_20"
}
// On battery power
if (BatteryService.batteryLevel >= 95)
return "battery_full"
if (BatteryService.batteryLevel >= 85)
return "battery_6_bar"
if (BatteryService.batteryLevel >= 70)
return "battery_5_bar"
if (BatteryService.batteryLevel >= 55)
return "battery_4_bar"
if (BatteryService.batteryLevel >= 40)
return "battery_3_bar"
if (BatteryService.batteryLevel >= 25)
return "battery_2_bar"
return "battery_1_bar"
}
size: Theme.iconSize - 6 size: Theme.iconSize - 6
color: { color: {
if (!BatteryService.batteryAvailable) if (!BatteryService.batteryAvailable)
@@ -41,13 +90,12 @@ Rectangle {
if (BatteryService.isLowBattery && !BatteryService.isCharging) if (BatteryService.isLowBattery && !BatteryService.isCharging)
return Theme.error return Theme.error
if (BatteryService.isCharging) if (BatteryService.isCharging || BatteryService.isPluggedIn)
return Theme.primary return Theme.primary
return Theme.surfaceText return Theme.surfaceText
} }
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
} }
StyledText { StyledText {

View File

@@ -251,15 +251,63 @@ PanelWindow {
spacing: Theme.spacingL spacing: Theme.spacingL
DankIcon { DankIcon {
name: Theme.getBatteryIcon(BatteryService.batteryLevel, name: {
BatteryService.isCharging, if (!BatteryService.batteryAvailable)
BatteryService.batteryAvailable) return "power"
// Check if plugged in but not charging (like at 80% charge limit)
if (!BatteryService.isCharging && BatteryService.isPluggedIn) {
if (BatteryService.batteryLevel >= 90)
return "battery_charging_full"
if (BatteryService.batteryLevel >= 80)
return "battery_charging_90"
if (BatteryService.batteryLevel >= 60)
return "battery_charging_80"
if (BatteryService.batteryLevel >= 50)
return "battery_charging_60"
if (BatteryService.batteryLevel >= 30)
return "battery_charging_50"
if (BatteryService.batteryLevel >= 20)
return "battery_charging_30"
return "battery_charging_20"
}
if (BatteryService.isCharging) {
if (BatteryService.batteryLevel >= 90)
return "battery_charging_full"
if (BatteryService.batteryLevel >= 80)
return "battery_charging_90"
if (BatteryService.batteryLevel >= 60)
return "battery_charging_80"
if (BatteryService.batteryLevel >= 50)
return "battery_charging_60"
if (BatteryService.batteryLevel >= 30)
return "battery_charging_50"
if (BatteryService.batteryLevel >= 20)
return "battery_charging_30"
return "battery_charging_20"
} else {
if (BatteryService.batteryLevel >= 95)
return "battery_full"
if (BatteryService.batteryLevel >= 85)
return "battery_6_bar"
if (BatteryService.batteryLevel >= 70)
return "battery_5_bar"
if (BatteryService.batteryLevel >= 55)
return "battery_4_bar"
if (BatteryService.batteryLevel >= 40)
return "battery_3_bar"
if (BatteryService.batteryLevel >= 25)
return "battery_2_bar"
return "battery_1_bar"
}
}
size: Theme.iconSizeLarge size: Theme.iconSizeLarge
color: { color: {
if (BatteryService.isLowBattery && !BatteryService.isCharging) if (BatteryService.isLowBattery && !BatteryService.isCharging)
return Theme.error return Theme.error
if (BatteryService.isCharging) if (BatteryService.isCharging || BatteryService.isPluggedIn)
return Theme.primary return Theme.primary
return Theme.surfaceText return Theme.surfaceText
@@ -341,7 +389,7 @@ PanelWindow {
spacing: Theme.spacingL spacing: Theme.spacingL
DankIcon { DankIcon {
name: Theme.getBatteryIcon(0, false, false) name: "power"
size: 36 size: 36
color: Theme.surfaceText color: Theme.surfaceText
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter

View File

@@ -1,72 +1,78 @@
pragma Singleton pragma Singleton
pragma ComponentBehavior: Bound
pragma ComponentBehavior
import QtQuick import QtQuick
import Quickshell import Quickshell
import Quickshell.Services.UPower import Quickshell.Services.UPower
Singleton { Singleton {
id: root id: root
readonly property UPowerDevice device: UPower.displayDevice readonly property UPowerDevice device: UPower.displayDevice
readonly property bool batteryAvailable: device && device.ready readonly property bool batteryAvailable: device && device.ready && device.isLaptopBattery
&& device.isLaptopBattery readonly property real batteryLevel: batteryAvailable ? Math.round(device.percentage * 100) : 0
readonly property int batteryLevel: batteryAvailable ? device.percentage * 100 : 0 readonly property bool isCharging: batteryAvailable && device.state === UPowerDeviceState.Charging && device.changeRate > 0
readonly property bool isCharging: batteryAvailable readonly property bool isPluggedIn: batteryAvailable && (device.state !== UPowerDeviceState.Discharging && device.state !== UPowerDeviceState.Empty)
&& device.state === UPowerDeviceState.Charging readonly property bool isLowBattery: batteryAvailable && batteryLevel <= 20
readonly property bool isLowBattery: batteryAvailable && batteryLevel <= 20 readonly property string batteryHealth: {
readonly property string batteryHealth: batteryAvailable if (!batteryAvailable)
&& device.healthSupported ? Math.round( return "N/A"
device.healthPercentage
* 100) + "%" : "N/A" if (device.healthSupported && device.healthPercentage > 0)
readonly property real batteryCapacity: batteryAvailable return Math.round(device.healthPercentage) + "%"
&& device.energyCapacity > 0 ? device.energyCapacity : 0
readonly property string batteryStatus: { // Calculate health from energy capacity vs design capacity
if (!batteryAvailable) if (device.energyCapacity > 0 && device.energy > 0) {
return "No Battery" // energyCapacity is current full capacity, we need design capacity
// Use a rough estimate based on typical battery degradation patterns
return UPowerDeviceState.toString(device.state) var healthPercent = (device.energyCapacity / 90.0045) * 100 // your design capacity from upower
} return Math.round(healthPercent) + "%"
readonly property int timeRemaining: { }
if (!batteryAvailable)
return 0 return "N/A"
return isCharging ? (device.timeToFull || 0) : (device.timeToEmpty || 0)
}
readonly property bool suggestPowerSaver: batteryAvailable && isLowBattery
&& UPower.onBattery
&& (typeof PowerProfiles !== "undefined"
&& PowerProfiles.profile
!== PowerProfile.PowerSaver)
readonly property var bluetoothDevices: {
var btDevices = []
for (var i = 0; i < UPower.devices.count; i++) {
var dev = UPower.devices.get(i)
if (dev
&& dev.ready && (dev.type === UPowerDeviceType.BluetoothGeneric || dev.type
=== UPowerDeviceType.Headphones || dev.type
=== UPowerDeviceType.Headset || dev.type
=== UPowerDeviceType.Keyboard || dev.type
=== UPowerDeviceType.Mouse || dev.type === UPowerDeviceType.Speakers))
btDevices.push({
"name": dev.model || UPowerDeviceType.toString(dev.type),
"percentage": Math.round(dev.percentage),
"type": dev.type
})
} }
return btDevices readonly property real batteryCapacity: batteryAvailable && device.energyCapacity > 0 ? device.energyCapacity : 0
} readonly property string batteryStatus: {
if (!batteryAvailable)
return "No Battery"
if (device.state === UPowerDeviceState.Charging && device.changeRate <= 0)
return "Plugged In"
return UPowerDeviceState.toString(device.state)
}
readonly property bool suggestPowerSaver: batteryAvailable && isLowBattery && UPower.onBattery && (typeof PowerProfiles !== "undefined" && PowerProfiles.profile !== PowerProfile.PowerSaver)
function formatTimeRemaining() { readonly property var bluetoothDevices: {
if (!batteryAvailable || timeRemaining <= 0) var btDevices = []
return "Unknown" for (var i = 0; i < UPower.devices.count; i++) {
var dev = UPower.devices.get(i)
if (dev && dev.ready && (dev.type === UPowerDeviceType.BluetoothGeneric || dev.type === UPowerDeviceType.Headphones || dev.type === UPowerDeviceType.Headset || dev.type === UPowerDeviceType.Keyboard || dev.type === UPowerDeviceType.Mouse || dev.type === UPowerDeviceType.Speakers)) {
btDevices.push({
"name": dev.model || UPowerDeviceType.toString(dev.type),
"percentage": Math.round(dev.percentage),
"type": dev.type
})
}
}
return btDevices
}
const hours = Math.floor(timeRemaining / 3600) function formatTimeRemaining() {
const minutes = Math.floor((timeRemaining % 3600) / 60) if (!batteryAvailable)
if (hours > 0) return "Unknown"
return hours + "h " + minutes + "m"
else var timeSeconds = isCharging ? device.timeToFull : device.timeToEmpty
return minutes + "m"
} if (!timeSeconds || timeSeconds <= 0 || timeSeconds > 86400)
return "Unknown"
var hours = Math.floor(timeSeconds / 3600)
var minutes = Math.floor((timeSeconds % 3600) / 60)
if (hours > 0)
return hours + "h " + minutes + "m"
else
return minutes + "m"
}
} }