From 36d3a50f217db5be164b7c8e1424c32e27ab8a3a Mon Sep 17 00:00:00 2001 From: LemmyCook Date: Sat, 6 Sep 2025 19:27:32 -0400 Subject: [PATCH] Brightness: brings back realtime brightness monitoring for internal(laptop) display. The pill will open and show the change in real time --- Modules/Bar/Widgets/Brightness.qml | 26 +++++------ Services/BrightnessService.qml | 72 ++++++++++++++++++++++++------ 2 files changed, 70 insertions(+), 28 deletions(-) diff --git a/Modules/Bar/Widgets/Brightness.qml b/Modules/Bar/Widgets/Brightness.qml index 4b6d91a..c16866c 100644 --- a/Modules/Bar/Widgets/Brightness.qml +++ b/Modules/Bar/Widgets/Brightness.qml @@ -37,28 +37,26 @@ Item { target: getMonitor() ignoreUnknownSignals: true function onBrightnessUpdated() { - Logger.log("Bar-Brightness", "OnBrightnessUpdated") - var monitor = getMonitor() - if (!monitor) - return - var currentBrightness = monitor.brightness - - // Ignore if this is the first time or if brightness hasn't actually changed + // Ignore if this is the first time we receive an update. + // Most likely service just kicked off. if (!firstBrightnessReceived) { firstBrightnessReceived = true - monitor.lastBrightness = currentBrightness return } - // Only show pill if brightness actually changed (not just loaded from settings) - if (Math.abs(currentBrightness - monitor.lastBrightness) > 0.1) { - pill.show() - } - - monitor.lastBrightness = currentBrightness + pill.show() + hideTimerAfterChange.restart() } } + Timer { + id: hideTimerAfterChange + interval: 2500 + running: false + repeat: false + onTriggered: pill.hide() + } + NPill { id: pill diff --git a/Services/BrightnessService.qml b/Services/BrightnessService.qml index 6cf1b5d..d14b166 100644 --- a/Services/BrightnessService.qml +++ b/Services/BrightnessService.qml @@ -110,9 +110,43 @@ Singleton { property real lastBrightness: 0 property real queuedBrightness: NaN + // For internal displays - store the backlight device path + property string backlightDevice: "" + property string brightnessPath: "" + property string maxBrightnessPath: "" + property int maxBrightness: 100 + property bool ignoreNextChange: false + // Signal for brightness changes signal brightnessUpdated(real newBrightness) + // FileView to watch for external brightness changes (internal displays only) + readonly property FileView brightnessWatcher: FileView { + id: brightnessWatcher + // Only set path for internal displays with a valid brightness path + path: (!monitor.isDdc && !monitor.isAppleDisplay && monitor.brightnessPath !== "") ? monitor.brightnessPath : "" + watchChanges: path !== "" + onFileChanged: { + reload() + if (monitor.ignoreNextChange) { + monitor.ignoreNextChange = false + return + } + if (text() === "") + return + var current = parseInt(text().trim()) + if (!isNaN(current) && monitor.maxBrightness > 0) { + var newBrightness = current / monitor.maxBrightness + // Only update if it's actually different (avoid feedback loops) + if (Math.abs(newBrightness - monitor.brightness) > 0.01) { + monitor.brightness = newBrightness + monitor.brightnessUpdated(monitor.brightness) + //Logger.log("Brightness", "External change detected:", monitor.modelData.name, monitor.brightness) + } + } + } + } + // Initialize brightness readonly property Process initProc: Process { stdout: StdioCollector { @@ -121,8 +155,8 @@ Singleton { if (dataText === "") { return } - Logger.log("Brightness", "Raw brightness data for", monitor.modelData.name + ":", dataText) + //Logger.log("Brightness", "Raw brightness data for", monitor.modelData.name + ":", dataText) if (monitor.isAppleDisplay) { var val = parseInt(dataText) if (!isNaN(val)) { @@ -140,14 +174,20 @@ Singleton { } } } else { - // Internal backlight - var parts = dataText.split(" ") - if (parts.length >= 2) { - var current = parseInt(parts[0]) - var max = parseInt(parts[1]) + // Internal backlight - parse the response which includes device path + var lines = dataText.split("\n") + if (lines.length >= 3) { + monitor.backlightDevice = lines[0] + monitor.brightnessPath = monitor.backlightDevice + "/brightness" + monitor.maxBrightnessPath = monitor.backlightDevice + "/max_brightness" + + var current = parseInt(lines[1]) + var max = parseInt(lines[2]) if (!isNaN(current) && !isNaN(max) && max > 0) { + monitor.maxBrightness = max monitor.brightness = current / max Logger.log("Brightness", "Internal brightness:", current + "/" + max + " =", monitor.brightness) + Logger.log("Brightness", "Using backlight device:", monitor.backlightDevice) } } } @@ -171,7 +211,7 @@ Singleton { function increaseBrightness(): void { var stepSize = Settings.data.brightness.brightnessStep / 100.0 - setBrightnessDebounced(brightness + stepSize) + setBrightnessDebounced(monitor.brightness + stepSize) } function decreaseBrightness(): void { @@ -183,22 +223,23 @@ Singleton { value = Math.max(0, Math.min(1, value)) var rounded = Math.round(value * 100) - if (Math.round(brightness * 100) === rounded) + if (Math.round(monitor.brightness * 100) === rounded) return if (isDdc && timer.running) { - queuedBrightness = value + monitor.queuedBrightness = value return } - brightness = value - brightnessUpdated(brightness) + monitor.brightness = value + brightnessUpdated(monitor.brightness) if (isAppleDisplay) { Quickshell.execDetached(["asdbctl", "set", rounded]) } else if (isDdc) { Quickshell.execDetached(["ddcutil", "-b", busNum, "setvcp", "10", rounded]) } else { + monitor.ignoreNextChange = true Quickshell.execDetached(["brightnessctl", "s", rounded + "%"]) } @@ -208,7 +249,7 @@ Singleton { } function setBrightnessDebounced(value: real): void { - queuedBrightness = value + monitor.queuedBrightness = value timer.restart() } @@ -218,8 +259,11 @@ Singleton { } else if (isDdc) { initProc.command = ["ddcutil", "-b", busNum, "getvcp", "10", "--brief"] } else { - // Internal backlight - try to find the first available backlight device - initProc.command = ["sh", "-c", "for dev in /sys/class/backlight/*; do if [ -f \"$dev/brightness\" ] && [ -f \"$dev/max_brightness\" ]; then echo \"$(cat $dev/brightness) $(cat $dev/max_brightness)\"; break; fi; done"] + // Internal backlight - find the first available backlight device and get its info + // This now returns: device_path, current_brightness, max_brightness (on separate lines) + initProc.command = ["sh", "-c", "for dev in /sys/class/backlight/*; do " + + " if [ -f \"$dev/brightness\" ] && [ -f \"$dev/max_brightness\" ]; then " + " echo \"$dev\"; " + + " cat \"$dev/brightness\"; " + " cat \"$dev/max_brightness\"; " + " break; " + " fi; " + "done"] } initProc.running = true }