1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
import QtQuick 2.15
import Qt.labs.folderlistmodel 2.15
Row {
id: batteryIndicator
height: root.font.pointSize * 1.6
spacing: root.font.pointSize * 0.5
property int batteryLevel: -1
property bool isCharging: false
property bool isFull: false
property string batteryPath: ""
visible: batteryLevel >= 0
property var tryPaths: [
"/sys/class/power_supply/BAT0/",
"/sys/class/power_supply/BAT1/",
"/sys/class/power_supply/BAT2/",
"/sys/class/power_supply/battery/",
"/sys/class/power_supply/BATT/"
]
FolderListModel {
id: powerSupplyScanner
folder: "file:///sys/class/power_supply"
showDirs: true
showFiles: false
onCountChanged: scan()
onFolderChanged: scan()
function scan() {
var defaults = [
"/sys/class/power_supply/BAT0/",
"/sys/class/power_supply/BAT1/",
"/sys/class/power_supply/BAT2/",
"/sys/class/power_supply/battery/",
"/sys/class/power_supply/BATT/"
]
for (var i = 0; i < count; i++) {
var name = get(i, "fileName")
if (name && name !== "." && name !== "..") {
var isLaptopBat = /^bat/i.test(name) || /^battery/i.test(name)
if (isLaptopBat) {
var fullPath = "/sys/class/power_supply/" + name + "/"
if (defaults.indexOf(fullPath) === -1) {
defaults.push(fullPath)
}
}
}
}
tryPaths = defaults
detectBattery(0)
}
}
function readFile(path, cb) {
var xhr = new XMLHttpRequest()
xhr.open("GET", "file://" + path, true)
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE)
cb(xhr.responseText && xhr.responseText.trim() !== "" ? xhr.responseText.trim() : null)
}
xhr.send()
}
function detectBattery(idx) {
if (idx >= tryPaths.length) { batteryLevel = -1; return }
readFile(tryPaths[idx] + "capacity", function(cap) {
if (cap !== null) {
batteryPath = tryPaths[idx]
batteryLevel = Math.max(0, Math.min(100, parseInt(cap) || 0))
readFile(batteryPath + "status", function(st) {
var s = st ? st.toLowerCase() : ""
isFull = (s === "full")
isCharging = (s === "charging") || isFull
batteryCanvas.requestPaint()
})
} else {
detectBattery(idx + 1)
}
})
}
function refresh() {
if (batteryPath !== "") {
readFile(batteryPath + "capacity", function(cap) {
if (cap !== null) {
batteryLevel = Math.max(0, Math.min(100, parseInt(cap) || 0))
readFile(batteryPath + "status", function(st) {
var s = st ? st.toLowerCase() : ""
isFull = (s === "full")
isCharging = (s === "charging") || isFull
batteryCanvas.requestPaint()
})
}
})
} else {
powerSupplyScanner.scan()
}
}
Component.onCompleted: powerSupplyScanner.scan()
Timer {
interval: 30000
repeat: true
running: true
onTriggered: batteryIndicator.refresh()
}
function levelColor(lvl, charging) {
if (charging) return "#4ade80" // zöld – tölt
if (lvl > 60) return "#ffffff" // fehér – ok
if (lvl > 30) return "#facc15" // sárga – közepes
if (lvl > 15) return "#fb923c" // narancs – alacsony
return "#f87171" // piros – kritikus
}
Canvas {
id: batteryCanvas
width: root.font.pointSize * 3.2
height: root.font.pointSize * 1.6
anchors.verticalCenter: parent.verticalCenter
onPaint: {
var ctx = getContext("2d")
ctx.clearRect(0, 0, width, height)
function rr(c, x, y, w, h, r) {
r = Math.min(Math.max(r, 0), Math.min(w, h) / 2)
c.beginPath()
c.moveTo(x + r, y)
c.arcTo(x + w, y, x + w, y + h, r)
c.arcTo(x + w, y + h, x, y + h, r)
c.arcTo(x, y + h, x, y, r)
c.arcTo(x, y, x + w, y, r)
c.closePath()
}
var lvl = batteryIndicator.batteryLevel
var charging = batteryIndicator.isCharging
var full = batteryIndicator.isFull
var col = batteryIndicator.levelColor(lvl, charging)
var bw = width * 0.84 // akksi test szélessége
var bh = height * 0.72 // akksi test magassága
var bx = 0
var by = (height - bh) / 2
var r = bh * 0.22 // sarokkerekítés
var tw = width * 0.07 // terminál szélessége
var th = bh * 0.44 // terminál magassága
var tx = bw // terminál x
var ty = (height - th) / 2
rr(ctx, tx, ty, tw, th, height * 0.15)
ctx.fillStyle = col
ctx.globalAlpha = 0.7
ctx.fill()
ctx.globalAlpha = 1.0
rr(ctx, bx, by, bw, bh, r)
ctx.strokeStyle = col
ctx.lineWidth = height * 0.09
ctx.stroke()
var padding = height * 0.18
var fillW = Math.max(0, (bw - padding * 2) * lvl / 100)
if (fillW > 0) {
rr(ctx, bx + padding, by + padding,
fillW, bh - padding * 2,
Math.max(0, r - padding * 0.5))
ctx.fillStyle = col
ctx.globalAlpha = 0.85
ctx.fill()
ctx.globalAlpha = 1.0
}
if (charging && !full) {
var cx = bw / 2
var cy = height / 2
var s = bh * 0.52
ctx.beginPath()
ctx.moveTo(cx + s * 0.18, cy - s * 0.5)
ctx.lineTo(cx - s * 0.08, cy + s * 0.05)
ctx.lineTo(cx + s * 0.10, cy + s * 0.05)
ctx.lineTo(cx - s * 0.18, cy + s * 0.5)
ctx.lineTo(cx + s * 0.08, cy - s * 0.05)
ctx.lineTo(cx - s * 0.10, cy - s * 0.05)
ctx.closePath()
ctx.fillStyle = "#ffffff"
ctx.globalAlpha = 0.95
ctx.fill()
ctx.globalAlpha = 1.0
}
if (full) {
ctx.beginPath()
ctx.moveTo(bw * 0.32, by + bh / 2)
ctx.lineTo(bw * 0.46, by + bh * 0.68)
ctx.lineTo(bw * 0.68, by + bh * 0.28)
ctx.strokeStyle = "#ffffff"
ctx.lineWidth = height * 0.11
ctx.lineCap = "round"
ctx.lineJoin = "round"
ctx.globalAlpha = 0.95
ctx.stroke()
ctx.globalAlpha = 1.0
}
}
}
Text {
id: batteryText
anchors.verticalCenter: parent.verticalCenter
text: {
if (batteryIndicator.isFull) return "100%"
if (batteryIndicator.isCharging) return batteryIndicator.batteryLevel + "% ↑"
return batteryIndicator.batteryLevel + "%"
}
color: batteryIndicator.levelColor(batteryIndicator.batteryLevel, batteryIndicator.isCharging)
font.pointSize: root.font.pointSize * 0.82
font.family: root.font.family
font.bold: batteryIndicator.batteryLevel <= 15 && !batteryIndicator.isCharging
SequentialAnimation on opacity {
running: batteryIndicator.batteryLevel <= 15 && !batteryIndicator.isCharging
loops: Animation.Infinite
NumberAnimation { to: 0.3; duration: 700; easing.type: Easing.InOutSine }
NumberAnimation { to: 1.0; duration: 700; easing.type: Easing.InOutSine }
}
}
onBatteryLevelChanged: batteryCanvas.requestPaint()
onIsChargingChanged: batteryCanvas.requestPaint()
onIsFullChanged: batteryCanvas.requestPaint()
}
|