Brightness: brings back realtime brightness monitoring for internal(laptop) display.

The pill will open and show the change in real time
This commit is contained in:
LemmyCook 2025-09-06 19:27:32 -04:00
parent 9bc6479c92
commit 36d3a50f21
2 changed files with 70 additions and 28 deletions

View file

@ -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

View file

@ -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
}