From 2c7038c504abab245f1ec16117159a49cc161ef6 Mon Sep 17 00:00:00 2001 From: loner <2788892716@qq.com> Date: Thu, 25 Sep 2025 10:18:09 +0800 Subject: [PATCH] Fix brightness sync after external command changes Fix brightness sync after external command changes, improve brightness module compatibility --- Services/BrightnessService.qml | 73 ++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 20 deletions(-) diff --git a/Services/BrightnessService.qml b/Services/BrightnessService.qml index 81f6ac6a..794ca4ea 100644 --- a/Services/BrightnessService.qml +++ b/Services/BrightnessService.qml @@ -119,6 +119,51 @@ Singleton { // Signal for brightness changes signal brightnessUpdated(real newBrightness) + // Execute a system command to get the current brightness value directly + readonly property Process refreshProc: Process { + stdout: StdioCollector { + onStreamFinished: { + var dataText = text.trim() + if (dataText === "") { + return + } + + var lines = dataText.split(" +") + if (lines.length >= 2) { + var current = parseInt(lines[0].trim()) + var max = parseInt(lines[1].trim()) + if (!isNaN(current) && !isNaN(max) && max > 0) { + var newBrightness = current / max + // Update internal value to match system state + monitor.brightness = newBrightness + monitor.brightnessUpdated(monitor.brightness) + //Logger.log("Brightness", "Refreshed brightness from system:", monitor.modelData.name, monitor.brightness) + } + } + } + } + } + + // Function to actively refresh the brightness from system + function refreshBrightnessFromSystem() { + if (!monitor.isDdc && !monitor.isAppleDisplay) { + // For internal displays, query the system directly + refreshProc.command = ["sh", "-c", + "cat " + monitor.brightnessPath + " && " + + "cat " + monitor.maxBrightnessPath] + refreshProc.running = true + } else if (monitor.isDdc) { + // For DDC displays, get the current value + refreshProc.command = ["ddcutil", "-b", monitor.busNum, "getvcp", "10", "--brief"] + refreshProc.running = true + } else if (monitor.isAppleDisplay) { + // For Apple displays, get the current value + refreshProc.command = ["asdbctl", "get"] + refreshProc.running = true + } + } + // FileView to watch for external brightness changes (internal displays only) readonly property FileView brightnessWatcher: FileView { id: brightnessWatcher @@ -126,23 +171,11 @@ Singleton { 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) - } - } + // When a file change is detected, actively refresh from system + // to ensure we get the most up-to-date value + Qt.callLater(function() { + monitor.refreshBrightnessFromSystem() + }) } } @@ -229,20 +262,20 @@ Singleton { value = Math.max(0, Math.min(1, value)) var rounded = Math.round(value * 100) - if (Math.round(monitor.brightness * 100) === rounded) - return - if (timer.running) { monitor.queuedBrightness = value return } + // Update internal value and trigger UI feedback monitor.brightness = value brightnessUpdated(monitor.brightness) if (isAppleDisplay) { + monitor.ignoreNextChange = true Quickshell.execDetached(["asdbctl", "set", rounded]) } else if (isDdc) { + monitor.ignoreNextChange = true Quickshell.execDetached(["ddcutil", "-b", busNum, "setvcp", "10", rounded]) } else { monitor.ignoreNextChange = true