1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2025-12-06 05:25:41 -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
DankIcon {
name: Theme.getBatteryIcon(BatteryService.batteryLevel,
BatteryService.isCharging,
BatteryService.batteryAvailable)
name: {
if (!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
color: {
if (!BatteryService.batteryAvailable)
@@ -41,13 +90,12 @@ Rectangle {
if (BatteryService.isLowBattery && !BatteryService.isCharging)
return Theme.error
if (BatteryService.isCharging)
if (BatteryService.isCharging || BatteryService.isPluggedIn)
return Theme.primary
return Theme.surfaceText
}
anchors.verticalCenter: parent.verticalCenter
}
StyledText {

View File

@@ -251,15 +251,63 @@ PanelWindow {
spacing: Theme.spacingL
DankIcon {
name: Theme.getBatteryIcon(BatteryService.batteryLevel,
BatteryService.isCharging,
BatteryService.batteryAvailable)
name: {
if (!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
color: {
if (BatteryService.isLowBattery && !BatteryService.isCharging)
return Theme.error
if (BatteryService.isCharging)
if (BatteryService.isCharging || BatteryService.isPluggedIn)
return Theme.primary
return Theme.surfaceText
@@ -341,7 +389,7 @@ PanelWindow {
spacing: Theme.spacingL
DankIcon {
name: Theme.getBatteryIcon(0, false, false)
name: "power"
size: 36
color: Theme.surfaceText
anchors.verticalCenter: parent.verticalCenter

View File

@@ -1,72 +1,78 @@
pragma Singleton
pragma ComponentBehavior
pragma ComponentBehavior: Bound
import QtQuick
import Quickshell
import Quickshell.Services.UPower
Singleton {
id: root
id: root
readonly property UPowerDevice device: UPower.displayDevice
readonly property bool batteryAvailable: device && device.ready
&& device.isLaptopBattery
readonly property int batteryLevel: batteryAvailable ? device.percentage * 100 : 0
readonly property bool isCharging: batteryAvailable
&& device.state === UPowerDeviceState.Charging
readonly property bool isLowBattery: batteryAvailable && batteryLevel <= 20
readonly property string batteryHealth: batteryAvailable
&& device.healthSupported ? Math.round(
device.healthPercentage
* 100) + "%" : "N/A"
readonly property real batteryCapacity: batteryAvailable
&& device.energyCapacity > 0 ? device.energyCapacity : 0
readonly property string batteryStatus: {
if (!batteryAvailable)
return "No Battery"
return UPowerDeviceState.toString(device.state)
}
readonly property int timeRemaining: {
if (!batteryAvailable)
return 0
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
})
readonly property UPowerDevice device: UPower.displayDevice
readonly property bool batteryAvailable: device && device.ready && device.isLaptopBattery
readonly property real batteryLevel: batteryAvailable ? Math.round(device.percentage * 100) : 0
readonly property bool isCharging: batteryAvailable && device.state === UPowerDeviceState.Charging && device.changeRate > 0
readonly property bool isPluggedIn: batteryAvailable && (device.state !== UPowerDeviceState.Discharging && device.state !== UPowerDeviceState.Empty)
readonly property bool isLowBattery: batteryAvailable && batteryLevel <= 20
readonly property string batteryHealth: {
if (!batteryAvailable)
return "N/A"
if (device.healthSupported && device.healthPercentage > 0)
return Math.round(device.healthPercentage) + "%"
// Calculate health from energy capacity vs design capacity
if (device.energyCapacity > 0 && device.energy > 0) {
// energyCapacity is current full capacity, we need design capacity
// Use a rough estimate based on typical battery degradation patterns
var healthPercent = (device.energyCapacity / 90.0045) * 100 // your design capacity from upower
return Math.round(healthPercent) + "%"
}
return "N/A"
}
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() {
if (!batteryAvailable || timeRemaining <= 0)
return "Unknown"
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
}
const hours = Math.floor(timeRemaining / 3600)
const minutes = Math.floor((timeRemaining % 3600) / 60)
if (hours > 0)
return hours + "h " + minutes + "m"
else
return minutes + "m"
}
function formatTimeRemaining() {
if (!batteryAvailable)
return "Unknown"
var timeSeconds = isCharging ? device.timeToFull : device.timeToEmpty
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"
}
}