import QtQuick import Quickshell import Quickshell.Services.UPower import "../.." import "power.js" as Power import "../../human.js" as Human // Power service: battery/device status from UPower (native) + session actions. // Pure shaping and the action routing table live in power.js; this file owns // only what must touch native objects: reading UPower devices and executing // the chosen action (lock / niri / systemctl). DI per shell.qml — no singletons // declared here. Item { id: root property var upower: null property Lock lockService property Niri niriService property string pendingConfirmation: "" property var lastAction: ({}) readonly property ObjectModel deviceModel: upower ? upower.devices : null readonly property bool isLaptopBattery: !!(upower && upower.displayDevice && upower.displayDevice.ready && upower.displayDevice.isLaptopBattery) readonly property bool lowPowerMode: Power.isLowPower(batteryState) // System's wording groups the pending states with their active counterparts. readonly property string batteryStatus: !isLaptopBattery ? "" : batteryState === "charging" || batteryState === "pendingcharge" ? "charging" : batteryState === "full" ? "full" : lowPowerMode ? "on battery" : "" // One reading of the display device; the panel reads the typed battery // properties below, stateObject() reports the reading itself. readonly property var batteryReading: batteryObject() // Registry geometry inputs: the popup grows for a battery, device rows and // a pending confirmation. readonly property bool batteryPresent: batteryReading.present readonly property int batteryPercent: batteryReading.percent readonly property string batteryState: batteryReading.state // Theme token by charge level: "ui.accent", "warning" or "error". readonly property string batteryRole: batteryReading.role readonly property string batteryTimeEstimate: batteryReading.timeEstimate // Health percent, -1 when the battery does not report it. readonly property real batteryHealth: batteryReading.health // Rows of { id, name, type, percent, state }, one per present peripheral. readonly property var externalDevices: deviceRows() readonly property int externalDeviceCount: externalDevices.length readonly property var sessionActions: Power.SESSION_ACTIONS // Registry bar input: "" quiet, "warning" low, "error" critical battery. readonly property string batteryAttention: Power.batteryAttention(batteryReading) // --- Battery / devices (native UPower) --------------------------------- function nativeBattery() { const device = upower ? upower.displayDevice : null; if (!device || !device.ready) return { "id": "display", "percent": 0, "state": "unknown", "timeEstimate": "unknown", "health": -1 }; const state = stateName(device.state); return { "id": String(device.nativePath || "display"), "percent": Math.round(Number(device.percentage) * 100), "state": state, "timeEstimate": Power.timeEstimateLabel(state, device.timeToFull, device.timeToEmpty), "health": device.healthSupported ? Number(device.healthPercentage) : -1 }; } function batteryObject() { const b = nativeBattery(); b.present = Power.hasMainBattery(b); b.role = Power.batteryRole(b.percent); return b; } function deviceRows() { const rows = []; const devices = root.deviceModel ? root.deviceModel.values : []; for (let i = 0; i < devices.length; i++) { const device = devices[i]; if (!device || device.isLaptopBattery || !device.isPresent) continue; rows.push({ "id": String(device.nativePath || device.model || `device-${i}`), "name": Human.pick([device.model], String(UPowerDeviceType.toString(device.type)).toLowerCase()), "type": String(UPowerDeviceType.toString(device.type)).toLowerCase(), "percent": Math.round(Number(device.percentage) * 100), "state": stateName(device.state) }); } return rows; } function stateName(state) { switch (state) { case UPowerDeviceState.Charging: return "charging"; case UPowerDeviceState.Discharging: return "discharging"; case UPowerDeviceState.Empty: return "empty"; case UPowerDeviceState.FullyCharged: return "full"; case UPowerDeviceState.PendingCharge: return "pendingcharge"; case UPowerDeviceState.PendingDischarge: return "pendingdischarge"; default: return "unknown"; } } // --- State (IPC contract) ---------------------------------------------- function stateObject() { const battery = batteryReading; return { "battery": battery, "externalDevices": externalDevices, "sessionActions": sessionActions, "destructiveActions": Power.DESTRUCTIVE_ACTIONS, "compact": { "label": "power", "role": battery.role, "charging": battery.state === "charging" }, "pendingConfirmation": pendingConfirmation, "lastAction": lastAction }; } function stateJson() { return JSON.stringify(stateObject()); } // --- Session actions ---------------------------------------------------- // Dispatch a resolved action plan (from power.js) to the native sink. function runPlan(plan) { if (plan.via === "lock" && lockService) lockService.request(); else if (plan.via === "niri" && niriService) niriService.action(plan.args); else if (plan.via === "exec") Quickshell.execDetached(plan.argv); } function sessionAction(kind) { const action = String(kind); if (Power.requiresConfirmation(action)) { pendingConfirmation = action; lastAction = { "kind": action, "confirmed": false }; } else { pendingConfirmation = ""; runPlan(Power.actionPlan(action)); lastAction = { "kind": action, "confirmed": true }; } return stateJson(); } function confirmAction(kind) { const action = String(kind); const confirmed = pendingConfirmation === action && Power.requiresConfirmation(action); pendingConfirmation = ""; if (confirmed) runPlan(Power.actionPlan(action)); lastAction = { "kind": action, "confirmed": confirmed }; return stateJson(); } function cancelAction() { pendingConfirmation = ""; lastAction = { "kind": "cancel" }; return stateJson(); } }