Fork the desktop off Omarchy as a self-contained system
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
function batteryPercentage(device) {
|
||||
if (!device || !device.isPresent) return -1
|
||||
return Math.round(Number(device.percentage || 0) * 100)
|
||||
}
|
||||
|
||||
function isDischarging(device, onBattery, dischargingState) {
|
||||
return !!(device && device.isPresent && onBattery && device.state === dischargingState)
|
||||
}
|
||||
|
||||
function shouldWarnLowBattery(device, onBattery, dischargingState, threshold, alreadyNotified) {
|
||||
var level = batteryPercentage(device)
|
||||
if (level < 0) return { level: level, notify: false, notifiedLowBattery: false }
|
||||
|
||||
var low = isDischarging(device, onBattery, dischargingState) && level <= threshold
|
||||
return {
|
||||
level: level,
|
||||
notify: low && !alreadyNotified,
|
||||
notifiedLowBattery: low
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof module !== "undefined") {
|
||||
module.exports = {
|
||||
batteryPercentage: batteryPercentage,
|
||||
isDischarging: isDischarging,
|
||||
shouldWarnLowBattery: shouldWarnLowBattery
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
import QtQuick
|
||||
import Quickshell
|
||||
import Quickshell.Io
|
||||
import Quickshell.Services.UPower
|
||||
import "BatteryModel.js" as BatteryModel
|
||||
|
||||
Item {
|
||||
id: root
|
||||
|
||||
property var shell: null
|
||||
property string blobPath: Quickshell.env("BLOB_PATH")
|
||||
|
||||
readonly property int batteryThreshold: 10
|
||||
property string pendingPowerSource: ""
|
||||
|
||||
PersistentProperties {
|
||||
id: persisted
|
||||
reloadableId: "blob-battery"
|
||||
property bool notifiedLowBattery: false
|
||||
}
|
||||
|
||||
function batteryPercentage() {
|
||||
return BatteryModel.batteryPercentage(UPower.displayDevice)
|
||||
}
|
||||
|
||||
function isDischarging() {
|
||||
return BatteryModel.isDischarging(UPower.displayDevice, UPower.onBattery, UPowerDeviceState.Discharging)
|
||||
}
|
||||
|
||||
function checkBattery() {
|
||||
var state = BatteryModel.shouldWarnLowBattery(UPower.displayDevice, UPower.onBattery, UPowerDeviceState.Discharging, batteryThreshold, persisted.notifiedLowBattery)
|
||||
persisted.notifiedLowBattery = state.notifiedLowBattery
|
||||
if (state.notify) sendLowBatteryWarning(state.level)
|
||||
}
|
||||
|
||||
function sendLowBatteryWarning(level) {
|
||||
if (warningProcess.running) return
|
||||
warningProcess.command = [
|
||||
"blob-battery-low",
|
||||
String(level)
|
||||
]
|
||||
warningProcess.running = true
|
||||
}
|
||||
|
||||
function applyPowerProfile() {
|
||||
pendingPowerSource = UPower.onBattery ? "battery" : "ac"
|
||||
if (!powerProfileProcess.running) runPendingPowerProfile()
|
||||
}
|
||||
|
||||
function runPendingPowerProfile() {
|
||||
powerProfileProcess.command = ["blob-power-set", pendingPowerSource]
|
||||
pendingPowerSource = ""
|
||||
powerProfileProcess.running = true
|
||||
}
|
||||
|
||||
Process { id: warningProcess }
|
||||
|
||||
Process {
|
||||
id: powerProfileProcess
|
||||
onExited: if (root.pendingPowerSource !== "") root.runPendingPowerProfile()
|
||||
}
|
||||
|
||||
Timer {
|
||||
interval: 30000
|
||||
running: true
|
||||
repeat: true
|
||||
triggeredOnStart: true
|
||||
onTriggered: root.checkBattery()
|
||||
}
|
||||
|
||||
Connections {
|
||||
target: UPower
|
||||
function onOnBatteryChanged() {
|
||||
root.checkBattery()
|
||||
root.applyPowerProfile()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"id": "blob.battery",
|
||||
"name": "Battery",
|
||||
"version": "1.0.0",
|
||||
"author": "Blob",
|
||||
"description": "Low battery warning service",
|
||||
"kinds": [
|
||||
"service"
|
||||
],
|
||||
"entryPoints": {
|
||||
"service": "Service.qml"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user