Personal noctalia plugins collection
1import QtQuick
2import Quickshell
3import Quickshell.Io
4
5Item {
6 id: root
7 property var pluginApi: null
8
9 property int chargeLimit: 100
10 property bool reading: false
11
12 readonly property string sysPath: "/sys/class/power_supply/BAT1/charge_control_end_threshold"
13
14 Component.onCompleted: readLimit()
15
16 Timer {
17 interval: 5000
18 running: true
19 repeat: true
20 onTriggered: readLimit()
21 }
22
23 function readLimit() {
24 if (reading) return
25 reading = true
26 readProcess.running = true
27 }
28
29 function toggle() {
30 var newVal = (chargeLimit >= 100) ? 80 : 100
31 writeStdout = ""
32 writeStderr = ""
33 writeProcess.command = ["pkexec", "tee", sysPath]
34 writeProcess.running = true
35 writeProcess.write(newVal + "\n")
36 writeProcess.closeWriteChannel()
37 }
38
39 // --- read current value ---
40 property string readStdout: ""
41
42 Process {
43 id: readProcess
44 command: ["cat", root.sysPath]
45 onExited: function(exitCode, exitStatus) {
46 if (exitCode === 0) {
47 var val = parseInt(readStdout.trim(), 10)
48 if (!isNaN(val)) root.chargeLimit = val
49 }
50 readStdout = ""
51 root.reading = false
52 }
53 stdout: SplitParser {
54 onRead: data => root.readStdout += data
55 }
56 }
57
58 // --- write new value ---
59 property string writeStdout: ""
60 property string writeStderr: ""
61
62 Process {
63 id: writeProcess
64 onExited: function(exitCode, exitStatus) {
65 if (exitCode === 0) {
66 Qt.callLater(readLimit)
67 } else {
68 console.error("[battery-limit] pkexec tee failed: " + writeStderr)
69 }
70 writeStdout = ""
71 writeStderr = ""
72 }
73 stdout: SplitParser {
74 onRead: data => root.writeStdout += data
75 }
76 stderr: SplitParser {
77 onRead: data => root.writeStderr += data
78 }
79 }
80}