extract BacklightService singleton, deduplicate brightness logic

This commit is contained in:
Damocles 2026-04-18 10:28:08 +02:00
parent 08d34ac5c7
commit 438362c6d1
4 changed files with 82 additions and 119 deletions

View file

@ -1,6 +1,4 @@
import QtQuick
import Quickshell
import Quickshell.Io
import Quickshell.Services.Mpris
import Quickshell.Services.Pipewire
import "../services" as S
@ -114,50 +112,7 @@ Item {
color: Qt.rgba(S.Theme.base01.r, S.Theme.base01.g, S.Theme.base01.b, 0.7)
border.color: Qt.rgba(S.Theme.base03.r, S.Theme.base03.g, S.Theme.base03.b, 0.3)
border.width: 1
visible: _blDev !== ""
property string _blDev: ""
property int _percent: 0
Process {
running: true
command: ["sh", "-c", "ls /sys/class/backlight/ 2>/dev/null | head -1"]
stdout: StdioCollector {
onStreamFinished: {
const dev = text.trim();
if (dev)
_backlightCard._blDev = "/sys/class/backlight/" + dev;
}
}
}
FileView {
id: _blCurrent
path: _backlightCard._blDev ? _backlightCard._blDev + "/brightness" : ""
watchChanges: true
onFileChanged: reload()
onLoaded: _backlightCard._updatePercent()
}
FileView {
id: _blMax
path: _backlightCard._blDev ? _backlightCard._blDev + "/max_brightness" : ""
onLoaded: _backlightCard._updatePercent()
}
function _updatePercent() {
const c = parseInt(_blCurrent.text());
const m = parseInt(_blMax.text());
if (m > 0)
_percent = Math.round((c / m) * 100);
}
Process {
id: _blAdj
property string cmd: ""
command: ["sh", "-c", cmd]
onRunningChanged: if (!running && cmd !== "")
_blCurrent.reload()
}
visible: S.BacklightService.available
C.BacklightApplet {
id: _backlightContent
@ -165,12 +120,9 @@ Item {
anchors.right: parent.right
anchors.top: parent.top
anchors.topMargin: 4
percent: _backlightCard._percent
percent: S.BacklightService.percent
accentColor: S.Theme.base0A
onSetPercent: pct => {
_blAdj.cmd = "light -S " + Math.round(Math.max(0, Math.min(100, pct)));
_blAdj.running = true;
}
onSetPercent: pct => S.BacklightService.setPercent(pct)
}
}
}