import QtQuick import Quickshell import Quickshell.Io Item { id: root property var pluginApi: null property int chargeLimit: 100 property bool reading: false readonly property string sysPath: "/sys/class/power_supply/BAT1/charge_control_end_threshold" Component.onCompleted: readLimit() Timer { interval: 5000 running: true repeat: true onTriggered: readLimit() } function readLimit() { if (reading) return reading = true readProcess.running = true } function toggle() { var newVal = (chargeLimit >= 100) ? 80 : 100 writeStdout = "" writeStderr = "" writeProcess.command = ["pkexec", "tee", sysPath] writeProcess.running = true writeProcess.write(newVal + "\n") writeProcess.closeWriteChannel() } // --- read current value --- property string readStdout: "" Process { id: readProcess command: ["cat", root.sysPath] onExited: function(exitCode, exitStatus) { if (exitCode === 0) { var val = parseInt(readStdout.trim(), 10) if (!isNaN(val)) root.chargeLimit = val } readStdout = "" root.reading = false } stdout: SplitParser { onRead: data => root.readStdout += data } } // --- write new value --- property string writeStdout: "" property string writeStderr: "" Process { id: writeProcess onExited: function(exitCode, exitStatus) { if (exitCode === 0) { Qt.callLater(readLimit) } else { console.error("[battery-limit] pkexec tee failed: " + writeStderr) } writeStdout = "" writeStderr = "" } stdout: SplitParser { onRead: data => root.writeStdout += data } stderr: SplitParser { onRead: data => root.writeStderr += data } } }