78 lines
2 KiB
QML
78 lines
2 KiB
QML
import QtQuick
|
|
import Quickshell.Io
|
|
import "." as M
|
|
|
|
M.BarSection {
|
|
id: root
|
|
spacing: M.Theme.moduleSpacing
|
|
opacity: M.Modules.backlight && percent > 0 ? 1 : 0
|
|
visible: opacity > 0
|
|
tooltip: "Brightness: " + root.percent + "%"
|
|
|
|
property int percent: 0
|
|
onPercentChanged: if (percent > 0)
|
|
M.OsdState.show(percent / 100, "\uF185")
|
|
|
|
Process {
|
|
id: adjProc
|
|
property string cmd: ""
|
|
command: ["sh", "-c", cmd]
|
|
onRunningChanged: if (!running && cmd !== "")
|
|
current.reload()
|
|
}
|
|
|
|
function adjust(delta) {
|
|
adjProc.cmd = delta > 0 ? "light -A 5" : "light -U 5";
|
|
adjProc.running = true;
|
|
}
|
|
|
|
// Detect backlight device dynamically
|
|
property string _blDev: ""
|
|
Process {
|
|
id: detectBl
|
|
running: true
|
|
command: ["sh", "-c", "ls /sys/class/backlight/ 2>/dev/null | head -1"]
|
|
stdout: StdioCollector {
|
|
onStreamFinished: {
|
|
const dev = text.trim();
|
|
if (dev) root._blDev = "/sys/class/backlight/" + dev;
|
|
}
|
|
}
|
|
}
|
|
|
|
FileView {
|
|
id: current
|
|
path: root._blDev ? root._blDev + "/brightness" : ""
|
|
watchChanges: true
|
|
onFileChanged: reload()
|
|
onLoaded: root._update()
|
|
}
|
|
FileView {
|
|
id: max
|
|
path: root._blDev ? root._blDev + "/max_brightness" : ""
|
|
onLoaded: root._update()
|
|
}
|
|
|
|
function _update() {
|
|
const c = parseInt(current.text());
|
|
const m = parseInt(max.text());
|
|
if (m > 0)
|
|
root.percent = Math.round((c / m) * 100);
|
|
}
|
|
|
|
M.BarIcon {
|
|
icon: "\uF185"
|
|
color: M.Theme.base0A
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
}
|
|
M.BarLabel {
|
|
label: root.percent + "%"
|
|
minText: "100%"
|
|
color: M.Theme.base0A
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
}
|
|
|
|
WheelHandler {
|
|
onWheel: event => root.adjust(event.angleDelta.y)
|
|
}
|
|
}
|