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 QtQuick
import Quickshell
import Quickshell.Io
import Quickshell.Services.Mpris import Quickshell.Services.Mpris
import Quickshell.Services.Pipewire import Quickshell.Services.Pipewire
import "../services" as S 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) 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.color: Qt.rgba(S.Theme.base03.r, S.Theme.base03.g, S.Theme.base03.b, 0.3)
border.width: 1 border.width: 1
visible: _blDev !== "" visible: S.BacklightService.available
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()
}
C.BacklightApplet { C.BacklightApplet {
id: _backlightContent id: _backlightContent
@ -165,12 +120,9 @@ Item {
anchors.right: parent.right anchors.right: parent.right
anchors.top: parent.top anchors.top: parent.top
anchors.topMargin: 4 anchors.topMargin: 4
percent: _backlightCard._percent percent: S.BacklightService.percent
accentColor: S.Theme.base0A accentColor: S.Theme.base0A
onSetPercent: pct => { onSetPercent: pct => S.BacklightService.setPercent(pct)
_blAdj.cmd = "light -S " + Math.round(Math.max(0, Math.min(100, pct)));
_blAdj.running = true;
}
} }
} }
} }

View file

@ -1,6 +1,5 @@
import QtQuick import QtQuick
import Quickshell import Quickshell
import Quickshell.Io
import "." as M import "." as M
import "../services" as S import "../services" as S
import "../applets" as C import "../applets" as C
@ -8,11 +7,11 @@ import "../applets" as C
M.BarSection { M.BarSection {
id: root id: root
spacing: S.Theme.moduleSpacing spacing: S.Theme.moduleSpacing
opacity: S.Modules.backlight.enable && percent > 0 ? 1 : 0 opacity: S.Modules.backlight.enable && S.BacklightService.available ? 1 : 0
visible: opacity > 0 visible: opacity > 0
tooltip: "" tooltip: ""
property int percent: 0 property int percent: S.BacklightService.percent
property bool _osdActive: false property bool _osdActive: false
property bool _percentInit: false property bool _percentInit: false
readonly property bool _showPanel: root._hovered || hoverPanel.panelHovered || _osdActive readonly property bool _showPanel: root._hovered || hoverPanel.panelHovered || _osdActive
@ -37,71 +36,8 @@ M.BarSection {
onTriggered: root._osdActive = false onTriggered: root._osdActive = false
} }
Process {
id: adjProc
property string cmd: ""
command: ["sh", "-c", cmd]
onRunningChanged: if (!running && cmd !== "")
current.reload()
}
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: ""
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"
anchors.verticalCenter: parent.verticalCenter
}
M.BarLabel {
label: root.percent + "%"
minText: "100%"
anchors.verticalCenter: parent.verticalCenter
}
WheelHandler { WheelHandler {
onWheel: event => root.adjust(event.angleDelta.y) onWheel: event => S.BacklightService.adjust(event.angleDelta.y)
} }
M.HoverPanel { M.HoverPanel {
@ -118,7 +54,17 @@ M.BarSection {
width: parent.width width: parent.width
percent: root.percent percent: root.percent
accentColor: root.accentColor accentColor: root.accentColor
onSetPercent: pct => root.setPercent(pct) onSetPercent: pct => S.BacklightService.setPercent(pct)
} }
} }
M.BarIcon {
icon: "\uF185"
anchors.verticalCenter: parent.verticalCenter
}
M.BarLabel {
label: root.percent + "%"
minText: "100%"
anchors.verticalCenter: parent.verticalCenter
}
} }

View 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()
}
}

View file

@ -7,3 +7,4 @@ singleton PowerProfileService 1.0 PowerProfileService.qml
singleton NotifService 1.0 NotifService.qml singleton NotifService 1.0 NotifService.qml
NotifItem 1.0 NotifItem.qml NotifItem 1.0 NotifItem.qml
singleton LockService 1.0 LockService.qml singleton LockService 1.0 LockService.qml
singleton BacklightService 1.0 BacklightService.qml