From 438362c6d198452dda86cd0450ecc4f578369074 Mon Sep 17 00:00:00 2001 From: Damocles Date: Sat, 18 Apr 2026 10:28:08 +0200 Subject: [PATCH] extract BacklightService singleton, deduplicate brightness logic --- shell/lock/LockWidgets.qml | 54 ++----------------- shell/modules/BacklightModule.qml | 82 +++++------------------------ shell/services/BacklightService.qml | 64 ++++++++++++++++++++++ shell/services/qmldir | 1 + 4 files changed, 82 insertions(+), 119 deletions(-) create mode 100644 shell/services/BacklightService.qml diff --git a/shell/lock/LockWidgets.qml b/shell/lock/LockWidgets.qml index 01fede3..bee4c52 100644 --- a/shell/lock/LockWidgets.qml +++ b/shell/lock/LockWidgets.qml @@ -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) } } } diff --git a/shell/modules/BacklightModule.qml b/shell/modules/BacklightModule.qml index 6893a37..67ab22f 100644 --- a/shell/modules/BacklightModule.qml +++ b/shell/modules/BacklightModule.qml @@ -1,6 +1,5 @@ import QtQuick import Quickshell -import Quickshell.Io import "." as M import "../services" as S import "../applets" as C @@ -8,11 +7,11 @@ import "../applets" as C M.BarSection { id: root 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 tooltip: "" - property int percent: 0 + property int percent: S.BacklightService.percent property bool _osdActive: false property bool _percentInit: false readonly property bool _showPanel: root._hovered || hoverPanel.panelHovered || _osdActive @@ -37,71 +36,8 @@ M.BarSection { 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 { - onWheel: event => root.adjust(event.angleDelta.y) + onWheel: event => S.BacklightService.adjust(event.angleDelta.y) } M.HoverPanel { @@ -118,7 +54,17 @@ M.BarSection { width: parent.width percent: root.percent 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 + } } diff --git a/shell/services/BacklightService.qml b/shell/services/BacklightService.qml new file mode 100644 index 0000000..caa4cd8 --- /dev/null +++ b/shell/services/BacklightService.qml @@ -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() + } +} diff --git a/shell/services/qmldir b/shell/services/qmldir index d23b48f..a5af319 100644 --- a/shell/services/qmldir +++ b/shell/services/qmldir @@ -7,3 +7,4 @@ singleton PowerProfileService 1.0 PowerProfileService.qml singleton NotifService 1.0 NotifService.qml NotifItem 1.0 NotifItem.qml singleton LockService 1.0 LockService.qml +singleton BacklightService 1.0 BacklightService.qml