diff --git a/Bar/Modules/Brightness.qml b/Bar/Modules/Brightness.qml index 2616646..5aa5fd7 100644 --- a/Bar/Modules/Brightness.qml +++ b/Bar/Modules/Brightness.qml @@ -1,41 +1,95 @@ import QtQuick +import Quickshell import Quickshell.Io -import qs.Settings import qs.Components +import qs.Settings Item { id: brightnessDisplay property int brightness: -1 + property int previousBrightness: -1 + property var screen: (typeof modelData !== 'undefined' ? modelData : null) + property string monitorName: screen ? screen.name : "DP-1" + property bool isSettingBrightness: false + property bool hasPendingSet: false + property int pendingSetValue: -1 width: pill.width height: pill.height - - FileView { - id: brightnessFile - path: "/tmp/brightness_osd_level" - watchChanges: true - blockLoading: true - - onLoaded: updateBrightness() - onFileChanged: { - brightnessFile.reload() - updateBrightness() - } - - function updateBrightness() { - const val = parseInt(brightnessFile.text()) - if (!isNaN(val) && val !== brightnessDisplay.brightness) { - brightnessDisplay.brightness = val - pill.text = brightness + "%" - pill.show() + + Process { + id: getBrightnessProcess + command: [Quickshell.shellDir + "/Programs/zigbrightness", "get", monitorName] + + stdout: StdioCollector { + onStreamFinished: { + const output = this.text.trim() + const val = parseInt(output) + + if (!isNaN(val) && val >= 0 && val !== previousBrightness) { + previousBrightness = brightness + brightness = val + pill.text = brightness + "%" + pill.show() + } } } } + + function getBrightness() { + if (isSettingBrightness) { + return + } + + getBrightnessProcess.running = true + } + + Process { + id: setBrightnessProcess + property int targetValue: -1 + command: [Quickshell.shellDir + "/Programs/zigbrightness", "set", monitorName, targetValue.toString()] + + stdout: StdioCollector { + onStreamFinished: { + const output = this.text.trim() + const val = parseInt(output) + + if (!isNaN(val) && val >= 0) { + brightness = val + pill.text = brightness + "%" + pill.show() + } + + isSettingBrightness = false + + if (hasPendingSet) { + hasPendingSet = false + const pendingValue = pendingSetValue + pendingSetValue = -1 + setBrightness(pendingValue) + } + } + } + } + + function setBrightness(newValue) { + newValue = Math.max(0, Math.min(100, newValue)) + + if (isSettingBrightness) { + hasPendingSet = true + pendingSetValue = newValue + return + } + + isSettingBrightness = true + setBrightnessProcess.targetValue = newValue + setBrightnessProcess.running = true + } PillIndicator { id: pill icon: "brightness_high" - text: brightness >= 0 ? brightness + "%" : "" + text: brightness >= 0 ? brightness + "%" : "--" pillColor: Theme.surfaceVariant iconCircleColor: Theme.accentPrimary iconTextColor: Theme.backgroundPrimary @@ -43,8 +97,17 @@ Item { MouseArea { anchors.fill: parent hoverEnabled: true - onEntered: brightnessTooltip.tooltipVisible = true + onEntered: { + getBrightness() + brightnessTooltip.tooltipVisible = true + } onExited: brightnessTooltip.tooltipVisible = false + + onWheel: function(wheel) { + const delta = wheel.angleDelta.y > 0 ? 5 : -5 + const newBrightness = brightness + delta + setBrightness(newBrightness) + } } StyledTooltip { id: brightnessTooltip @@ -56,8 +119,9 @@ Item { } Component.onCompleted: { + getBrightness() if (brightness >= 0) { pill.show() } } -} +} \ No newline at end of file diff --git a/Programs/zigbrightness b/Programs/zigbrightness new file mode 100755 index 0000000..30a8f43 Binary files /dev/null and b/Programs/zigbrightness differ diff --git a/README.md b/README.md index 07d42bb..d9adbf3 100644 --- a/README.md +++ b/README.md @@ -177,11 +177,11 @@ You will need to install a few things to get everything working: - `swww` to add fancy wallpaper animations (optional) - `wallust` to theme the setup based on wallpaper (optional) -## zigstat is bundled - source can be found [here](https://git.pika-os.com/wm-packages/pikabar/src/branch/main/src/zigstat). +## zigstat and zigbrightnesss is bundled - source can be found [here](https://git.pika-os.com/wm-packages/pikabar/src/t). ## Known issues -Currently the brightness indicator is very opiniated (using ddcutil with a script to log current brightness). This will be fixed **asap**! +It is perfect now ---