Pull low battery warning into shell

This commit is contained in:
David Heinemeier Hansson committed 2026-05-19 21:28:32 +02:00
1 parent 15f461c1df
commit 1a630b1239
12 files changed
+119 -69

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
import QtQuick
import Quickshell
import Quickshell.Io
import Quickshell.Services.UPower
Item {
id: root
property var shell: null
property string omarchyPath: Quickshell.env("OMARCHY_PATH")
readonly property int batteryThreshold: 10
PersistentProperties {
id: persisted
reloadableId: "omarchy-battery-monitor"
property bool notifiedLowBattery: false
}
function batteryPercentage() {
var device = UPower.displayDevice
if (!device || !device.isPresent) return -1
return Math.round(Number(device.percentage || 0) * 100)
}
function isDischarging() {
var device = UPower.displayDevice
return !!(device && device.isPresent
&& UPower.onBattery
&& device.state === UPowerDeviceState.Discharging)
}
function checkBattery() {
var level = batteryPercentage()
if (level < 0) {
persisted.notifiedLowBattery = false
return
}
if (isDischarging() && level <= batteryThreshold) {
if (!persisted.notifiedLowBattery) {
persisted.notifiedLowBattery = true
sendLowBatteryWarning(level)
}
} else {
persisted.notifiedLowBattery = false
}
}
function sendLowBatteryWarning(level) {
if (warningProcess.running) return
warningProcess.command = [
"omarchy-battery-low",
String(level)
]
warningProcess.running = true
}
Process { id: warningProcess }
Timer {
interval: 30000
running: true
repeat: true
triggeredOnStart: true
onTriggered: root.checkBattery()
}
Connections {
target: UPower
function onOnBatteryChanged() { root.checkBattery() }
}
}
@@ -0,0 +1,14 @@
{
"schemaVersion": 1,
"id": "omarchy.battery-monitor",
"name": "Battery Monitor",
"version": "1.0.0",
"author": "Omarchy",
"description": "Low battery warning service",
"kinds": [
"service"
],
"entryPoints": {
"service": "Service.qml"
}
}