import QtQuick import Quickshell import Quickshell.Services.UPower Item { id: root property QtObject lockService property QtObject niriService property string smokeProfile: "balanced" property string pendingConfirmation: "" property var lastAction: ({ }) property var smokeDisplayBattery: ({ "id": "BAT0", "percent": 73, "state": "discharging", "timeEstimate": "3h 12m", "health": 91, "profile": "balanced" }) property var smokeExternalDevices: [{ "id": "mouse", "name": "Nugu Mouse", "type": "pointer", "percent": 62, "state": "discharging" }, { "id": "headphones", "name": "Nugu Headphones", "type": "audio", "percent": 87, "state": "discharging" }] readonly property bool testHarness: Quickshell.env("NUGULAND_SMOKE") === "1" readonly property bool nativeActionRoute: !testHarness readonly property bool usesUPower: true readonly property var boundedActions: ["lock", "logout", "suspend", "reboot", "poweroff", "monitor-off", "monitor-on", "set-profile", "confirm", "cancel"] readonly property var destructiveActions: ["logout", "reboot", "poweroff"] function normalizeProfile(value) { const text = String(value).toLowerCase(); if (text.indexOf("performance") >= 0) return "performance"; if (text.indexOf("saver") >= 0) return "power-saver"; return "balanced"; } function currentProfile() { return testHarness ? smokeProfile : normalizeProfile(PowerProfile.toString(PowerProfiles.profile)); } function deviceState(device) { return String(UPowerDeviceState.toString(device.state || UPowerDeviceState.Unknown)).toLowerCase().replace("fullycharged", "full"); } function minutesToLabel(seconds) { const value = Math.max(0, Math.round(Number(seconds || 0) / 60)); if (value <= 0) return "unknown"; const hours = Math.floor(value / 60); const minutes = value % 60; if (hours <= 0) return `${minutes}m`; if (minutes <= 0) return `${hours}h`; return `${hours}h ${minutes}m`; } function nativeBatteryObject() { const device = UPower.displayDevice; if (!device || !device.ready) return smokeDisplayBattery; const state = deviceState(device); const timeEstimate = state === "charging" ? minutesToLabel(device.timeToFull) : minutesToLabel(device.timeToEmpty); return { "id": String(device.nativePath || "display"), "percent": Math.round(Number(device.percentage || 0)), "state": state, "timeEstimate": timeEstimate, "health": device.healthSupported ? Math.round(Number(device.healthPercentage || 0)) : -1, "profile": currentProfile() }; } function displayBattery() { return testHarness ? Object.assign({ }, smokeDisplayBattery, { "profile": smokeProfile }) : nativeBatteryObject(); } function nativeExternalDevices() { const rows = []; const devices = UPower.devices.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": String(device.model || UPowerDeviceType.toString(device.type)), "type": String(UPowerDeviceType.toString(device.type)).toLowerCase(), "percent": Math.round(Number(device.percentage || 0)), "state": deviceState(device) }); } return rows; } function externalDevices() { return testHarness ? smokeExternalDevices : nativeExternalDevices(); } function externalDevicesById() { const indexed = { }; const devices = externalDevices(); for (let i = 0; i < devices.length; i++) { const device = devices[i]; indexed[device.id] = { "id": device.id, "name": device.name, "type": device.type, "percent": device.percent, "state": device.state }; } return indexed; } function batteryRole(battery) { if (battery.percent <= 10) return "error"; if (battery.percent <= 25) return "warning"; return "ui.accent"; } function batteryObject() { const battery = displayBattery(); return { "id": battery.id, "percent": battery.percent, "state": battery.state, "timeEstimate": battery.timeEstimate, "health": battery.health, "profile": battery.profile, "role": batteryRole(battery) }; } function powerPanelContract() { return { "source": "Nuguland session Content.qml plus Nuguland SessionScreen.qml plus Nuguland Power.qml", "ownedBy": "PopoutHost.qml inline PowerPanel", "upowerSessionPanel": true, "batteryHero": true, "externalDeviceBatteryRows": true, "powerProfileSelector": true, "keyboardActionGrid": true, "confirmationStrip": true, "destructiveTwoStep": true, "serviceActionsOnly": true, "safeDefault": "lock", "actions": ["lock", "suspend", "monitor-off", "monitor-on", "logout", "reboot", "poweroff"], "profileRows": ["power-saver", "balanced", "performance"] }; } function stateObject() { const battery = batteryObject(); const devices = externalDevices(); return { "backend": "Power.qml", "usesUPower": usesUPower, "testHarness": testHarness, "nativeActionRoute": nativeActionRoute, "helperProcesses": [], "serviceOwnedActions": true, "boundedActions": boundedActions, "powerPanelContract": powerPanelContract(), "profile": currentProfile(), "battery": battery, "externalDevices": devices.map((device) => { return ({ "id": device.id, "name": device.name, "type": device.type, "percent": device.percent, "state": device.state }); }), "externalDevicesById": externalDevicesById(), "session": { "safeDefault": "lock", "requiresConfirmation": destructiveActions, "keyboardFocus": true, "escapeCancels": true, "routes": { "lock": "Lock.qml", "monitor": "Niri.qml" } }, "compact": { "label": `${battery.percent}%`, "role": battery.role, "charging": battery.state === "charging" }, "pendingConfirmation": pendingConfirmation, "lastAction": lastAction }; } function stateJson() { return JSON.stringify(stateObject()); } function setProfile(name) { const selected = normalizeProfile(name); if (testHarness) { smokeProfile = selected; smokeDisplayBattery = Object.assign({ }, smokeDisplayBattery, { "profile": smokeProfile }); } else { if (selected === "performance") PowerProfiles.profile = PowerProfile.Performance; else if (selected === "power-saver") PowerProfiles.profile = PowerProfile.PowerSaver; else PowerProfiles.profile = PowerProfile.Balanced; } pendingConfirmation = ""; lastAction = { "kind": "set-profile", "profile": selected, "nativeActionRoute": nativeActionRoute, "testHarness": testHarness }; return stateJson(); } function requiresConfirmation(kind) { return destructiveActions.indexOf(kind) >= 0; } function executeSessionAction(actionName) { if (testHarness) return ; if (actionName === "lock" && lockService) lockService.request(); else if (actionName === "monitor-off" && niriService) niriService.action(["power-off-monitors"]); else if (actionName === "monitor-on" && niriService) niriService.action(["power-on-monitors"]); } function sessionAction(kind) { const actionName = String(kind); if (requiresConfirmation(actionName)) { pendingConfirmation = actionName; lastAction = { "kind": actionName, "nativeActionRoute": nativeActionRoute, "testHarness": testHarness, "confirmed": false }; } else { pendingConfirmation = ""; executeSessionAction(actionName); lastAction = { "kind": actionName, "nativeActionRoute": nativeActionRoute, "testHarness": testHarness, "confirmed": true }; } return stateJson(); } function confirmAction(kind) { const actionName = String(kind); const confirmed = pendingConfirmation === actionName && requiresConfirmation(actionName); pendingConfirmation = ""; if (confirmed) executeSessionAction(actionName); lastAction = { "kind": actionName, "nativeActionRoute": nativeActionRoute, "testHarness": testHarness, "confirmed": confirmed }; return stateJson(); } function cancelAction() { pendingConfirmation = ""; lastAction = { "kind": "cancel" }; return stateJson(); } }