extract BacklightService singleton, deduplicate brightness logic
This commit is contained in:
parent
08d34ac5c7
commit
438362c6d1
4 changed files with 82 additions and 119 deletions
64
shell/services/BacklightService.qml
Normal file
64
shell/services/BacklightService.qml
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
pragma Singleton
|
||||
|
||||
import QtQuick
|
||||
import Quickshell
|
||||
import Quickshell.Io
|
||||
import "." as S
|
||||
|
||||
QtObject {
|
||||
id: root
|
||||
|
||||
property int percent: 0
|
||||
readonly property bool available: _blDev !== ""
|
||||
|
||||
function adjust(delta) {
|
||||
const step = S.Modules.backlight.step || 5;
|
||||
_adjProc.cmd = delta > 0 ? "light -A " + step : "light -U " + step;
|
||||
_adjProc.running = true;
|
||||
}
|
||||
|
||||
function setPercent(pct) {
|
||||
_adjProc.cmd = "light -S " + Math.round(Math.max(0, Math.min(100, pct)));
|
||||
_adjProc.running = true;
|
||||
}
|
||||
|
||||
property string _blDev: ""
|
||||
|
||||
property Process _detectProc: Process {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
property FileView _current: FileView {
|
||||
path: root._blDev ? root._blDev + "/brightness" : ""
|
||||
watchChanges: true
|
||||
onFileChanged: reload()
|
||||
onLoaded: root._update()
|
||||
}
|
||||
|
||||
property FileView _max: FileView {
|
||||
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)
|
||||
percent = Math.round((c / m) * 100);
|
||||
}
|
||||
|
||||
property Process _adjProc: Process {
|
||||
property string cmd: ""
|
||||
command: ["sh", "-c", cmd]
|
||||
onRunningChanged: if (!running && cmd !== "")
|
||||
root._current.reload()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue