From ad310ec74bed19b6204e0fcc403efb56708e128e Mon Sep 17 00:00:00 2001 From: notiant <238434866+notiant@users.noreply.github.com> Date: Thu, 29 Jan 2026 21:24:35 +0100 Subject: [PATCH] Battery: fix low battery warning & fallback handling --- Assets/settings-widgets-default.json | 2 +- Modules/Bar/Widgets/Battery.qml | 12 ++++----- Modules/Panels/Battery/BatteryPanel.qml | 4 +-- .../Bar/WidgetSettings/BatterySettings.qml | 8 ++---- Services/Hardware/BatteryService.qml | 25 +++++++++---------- Services/UI/BarWidgetRegistry.qml | 2 +- 6 files changed, 24 insertions(+), 29 deletions(-) diff --git a/Assets/settings-widgets-default.json b/Assets/settings-widgets-default.json index 9afd1b408..58f8196a8 100644 --- a/Assets/settings-widgets-default.json +++ b/Assets/settings-widgets-default.json @@ -16,7 +16,7 @@ "Battery": { "displayMode": "onhover", "warningThreshold": 30, - "deviceNativePath": "", + "deviceNativePath": "__default__", "showPowerProfiles": false, "showNoctaliaPerformance": false, "hideIfNotDetected": true, diff --git a/Modules/Bar/Widgets/Battery.qml b/Modules/Bar/Widgets/Battery.qml index 97ccf8109..f83e6656b 100644 --- a/Modules/Bar/Widgets/Battery.qml +++ b/Modules/Bar/Widgets/Battery.qml @@ -50,8 +50,8 @@ Item { readonly property int testPercent: 35 readonly property bool testCharging: false readonly property bool testPluggedIn: false - readonly property string deviceNativePath: widgetSettings.deviceNativePath || "__default__" + readonly property string deviceNativePath: widgetSettings.deviceNativePath !== undefined ? widgetSettings.deviceNativePath : widgetMetadata.deviceNativePath readonly property var selectedBattery: BatteryService.findUPowerDevice(deviceNativePath) readonly property var selectedBluetoothDevice: BatteryService.findBluetoothDevice(deviceNativePath) readonly property var selectedDevice: { @@ -61,7 +61,7 @@ Item { if (BatteryService.isDevicePresent(selectedBattery)) { return selectedBattery; } - return BatteryService.primaryDevice; + return null; } // Check if selected device is actually present/connected @@ -95,13 +95,13 @@ Item { target: selectedDevice?.type === UPowerDeviceType.Battery ? selectedDevice : null function onPercentageChanged() { - maybeNotify(BatteryService.getPercentage(selectedBattery), isCharging, isPluggedIn, isReady); + maybeNotify(BatteryService.getPercentage(selectedDevice), isCharging, isPluggedIn, isReady); } function onStateChanged() { if (isCharging || isPluggedIn) { hasNotifiedLowBattery = false; } - maybeNotify(BatteryService.getPercentage(selectedBattery), isCharging, isPluggedIn, isReady); + maybeNotify(BatteryService.getPercentage(selectedDevice), isCharging, isPluggedIn, isReady); } } @@ -109,7 +109,7 @@ Item { target: selectedDevice?.batteryAvailable ? selectedDevice : null function onBatteryChanged() { - maybeNotify(BatteryService.getPercentage(selectedBluetoothDevice), isCharging, isPluggedIn, isReady); + maybeNotify(BatteryService.getPercentage(selectedDevice), isCharging, isPluggedIn, isReady); } } @@ -156,7 +156,7 @@ Item { if (!isReady || !isPresent) { return I18n.tr("battery.no-battery-detected"); } - const isInternal = selectedDevice === BatteryService.primaryDevice && BatteryService.isLaptopBattery; + const isInternal = selectedDevice.type === UPowerDeviceType.Battery && BatteryService.isLaptopBattery; if (isInternal) { let timeText = BatteryService.getTimeRemainingText(selectedDevice); diff --git a/Modules/Panels/Battery/BatteryPanel.qml b/Modules/Panels/Battery/BatteryPanel.qml index b0f5fb851..53c87e59b 100644 --- a/Modules/Panels/Battery/BatteryPanel.qml +++ b/Modules/Panels/Battery/BatteryPanel.qml @@ -35,7 +35,7 @@ SmartPanel { if (BatteryService.isDevicePresent(selectedBattery)) { return selectedBattery; } - return BatteryService.primaryDevice; + return null; } // Check if selected device is actually present/connected @@ -49,7 +49,7 @@ SmartPanel { readonly property int healthPercent: (isReady && selectedBattery && selectedBattery.healthSupported) ? Math.round(selectedBattery.healthPercentage) : BatteryService.healthPercent readonly property string deviceName: BatteryService.getDeviceName(selectedDevice) - readonly property string panelTitle: deviceName ? `${I18n.tr("common.battery")} - ${deviceName}` : I18n.tr("common.battery") + readonly property string panelTitle: deviceName ? `${deviceName}` : I18n.tr("common.battery") readonly property var allDevices: { var list = []; diff --git a/Modules/Panels/Settings/Bar/WidgetSettings/BatterySettings.qml b/Modules/Panels/Settings/Bar/WidgetSettings/BatterySettings.qml index f1e1af23c..33efd8906 100644 --- a/Modules/Panels/Settings/Bar/WidgetSettings/BatterySettings.qml +++ b/Modules/Panels/Settings/Bar/WidgetSettings/BatterySettings.qml @@ -18,7 +18,7 @@ ColumnLayout { // Local state property string valueDisplayMode: widgetData.displayMode !== undefined ? widgetData.displayMode : widgetMetadata.displayMode property int valueWarningThreshold: widgetData.warningThreshold !== undefined ? widgetData.warningThreshold : widgetMetadata.warningThreshold - property string valueDeviceNativePath: widgetData.deviceNativePath !== undefined ? widgetData.deviceNativePath : "" + property string valueDeviceNativePath: widgetData.deviceNativePath !== undefined ? widgetData.deviceNativePath : "__default__" property bool valueShowPowerProfiles: widgetData.showPowerProfiles !== undefined ? widgetData.showPowerProfiles : widgetMetadata.showPowerProfiles property bool valueShowNoctaliaPerformance: widgetData.showNoctaliaPerformance !== undefined ? widgetData.showNoctaliaPerformance : widgetMetadata.showNoctaliaPerformance property bool valueHideIfNotDetected: widgetData.hideIfNotDetected !== undefined ? widgetData.hideIfNotDetected : widgetMetadata.hideIfNotDetected @@ -35,11 +35,7 @@ ColumnLayout { settings.showNoctaliaPerformance = valueShowNoctaliaPerformance; settings.hideIfNotDetected = valueHideIfNotDetected; settings.hideIfIdle = valueHideIfIdle; - if (valueDeviceNativePath && valueDeviceNativePath !== "") { - settings.deviceNativePath = valueDeviceNativePath; - } else { - delete settings.deviceNativePath; - } + settings.deviceNativePath = valueDeviceNativePath; return settings; } diff --git a/Services/Hardware/BatteryService.qml b/Services/Hardware/BatteryService.qml index 63bdc1384..81fc8a032 100644 --- a/Services/Hardware/BatteryService.qml +++ b/Services/Hardware/BatteryService.qml @@ -59,6 +59,18 @@ Singleton { return null; } + readonly property var externalBatteries: { + var list = []; + var devices = BluetoothService.devices ? (BluetoothService.devices.values || []) : []; + for (var i = 0; i < devices.length; i++) { + var device = devices[i]; + if (device && device.connected && device.batteryAvailable) { + list.push(device); + } + } + return list; + } + readonly property var _bluetoothBattery: externalBatteries.length > 0 ? externalBatteries[0] : null // MARK: BatterySettings @@ -318,19 +330,6 @@ Singleton { return I18n.tr("common.idle"); } - // MARK: BatteryPanel - readonly property var externalBatteries: { - var list = []; - var devices = BluetoothService.devices ? (BluetoothService.devices.values || []) : []; - for (var i = 0; i < devices.length; i++) { - var device = devices[i]; - if (device && device.connected && device.batteryAvailable) { - list.push(device); - } - } - return list; - } - // MARK: getTimeRemainingText function getTimeRemainingText(device) { if (!isDeviceReady(device)) { diff --git a/Services/UI/BarWidgetRegistry.qml b/Services/UI/BarWidgetRegistry.qml index d87be7a82..59ffe9905 100644 --- a/Services/UI/BarWidgetRegistry.qml +++ b/Services/UI/BarWidgetRegistry.qml @@ -86,7 +86,7 @@ Singleton { "Battery": { "displayMode": "onhover", "warningThreshold": 30, - "deviceNativePath": "", + "deviceNativePath": "__default__", "showPowerProfiles": false, "showNoctaliaPerformance": false, "hideIfNotDetected": true,