diff options
| author | sweenu <contact@sweenu.xyz> | 2025-09-02 05:23:15 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-02 13:23:15 +1000 |
| commit | 1066e467be365adb70c2168d8a7b536f5fab882e (patch) | |
| tree | eb52ae7ea5c33e7ab297ab586a22a2ecbf9765d7 /services | |
| parent | bar/workspaces: add option for ws name capitalisation (#543) (diff) | |
| download | caelestia-shell-1066e467be365adb70c2168d8a7b536f5fab882e.tar.gz caelestia-shell-1066e467be365adb70c2168d8a7b536f5fab882e.tar.bz2 caelestia-shell-1066e467be365adb70c2168d8a7b536f5fab882e.zip | |
brightness: add ipc handler for get/set (#546)
* services(brightness): add IpcHandler with brightnessctl like set function
* allow get/set for specific monitor
Also handle invalid formats
---------
Co-authored-by: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>
Diffstat (limited to 'services')
| -rw-r--r-- | services/Brightness.qml | 84 |
1 files changed, 78 insertions, 6 deletions
diff --git a/services/Brightness.qml b/services/Brightness.qml index fe564cc..ac905fd 100644 --- a/services/Brightness.qml +++ b/services/Brightness.qml @@ -17,22 +17,41 @@ Singleton { return monitors.find(m => m.modelData === screen); } + function getMonitor(query: string): var { + if (query === "active") { + return monitors.find(m => Hypr.monitorFor(m.modelData)?.focused); + } + + if (query.startsWith("model:")) { + const model = query.slice(6); + return monitors.find(m => m.modelData.model === model); + } + + if (query.startsWith("serial:")) { + const serial = query.slice(7); + return monitors.find(m => m.modelData.serialNumber === serial); + } + + if (query.startsWith("id:")) { + const id = parseInt(query.slice(3), 10); + return monitors.find(m => Hypr.monitorFor(m.modelData)?.id === id); + } + + return monitors.find(m => m.modelData.name === query); + } + function increaseBrightness(): void { - const focusedName = Hypr.focusedMonitor.name; - const monitor = monitors.find(m => focusedName === m.modelData.name); + const monitor = getMonitor("active"); if (monitor) monitor.setBrightness(monitor.brightness + 0.1); } function decreaseBrightness(): void { - const focusedName = Hypr.focusedMonitor.name; - const monitor = monitors.find(m => focusedName === m.modelData.name); + const monitor = getMonitor("active"); if (monitor) monitor.setBrightness(monitor.brightness - 0.1); } - reloadableId: "brightness" - onMonitorsChanged: { ddcMonitors = []; ddcProc.running = true; @@ -78,6 +97,59 @@ Singleton { onPressed: root.decreaseBrightness() } + IpcHandler { + target: "brightness" + + function get(): real { + return getFor("active"); + } + + // Allows searching by active/model/serial/id/name + function getFor(query: string): real { + return root.getMonitor(query)?.brightness ?? -1; + } + + function set(value: string): string { + return setFor("active", value); + } + + // Handles brightness value like brightnessctl: 0.1, +0.1, 0.1-, 10%, +10%, 10%- + function setFor(query: string, value: string): string { + const monitor = root.getMonitor(query); + if (!monitor) + return "Invalid monitor: " + query; + + let targetBrightness; + if (value.endsWith("%-")) { + const percent = parseFloat(value.slice(0, -2)); + targetBrightness = monitor.brightness - (percent / 100); + } else if (value.startsWith("+") && value.endsWith("%")) { + const percent = parseFloat(value.slice(1, -1)); + targetBrightness = monitor.brightness + (percent / 100); + } else if (value.endsWith("%")) { + const percent = parseFloat(value.slice(0, -1)); + targetBrightness = percent / 100; + } else if (value.startsWith("+")) { + const increment = parseFloat(value.slice(1)); + targetBrightness = monitor.brightness + increment; + } else if (value.endsWith("-")) { + const decrement = parseFloat(value.slice(0, -1)); + targetBrightness = monitor.brightness - decrement; + } else if (value.includes("%") || value.includes("-") || value.includes("+")) { + return `Invalid brightness format: ${value}\nExpected: 0.1, +0.1, 0.1-, 10%, +10%, 10%-`; + } else { + targetBrightness = parseFloat(value); + } + + if (isNaN(targetBrightness)) + return `Failed to parse value: ${value}\nExpected: 0.1, +0.1, 0.1-, 10%, +10%, 10%-`; + + monitor.setBrightness(targetBrightness); + + return `Set monitor ${monitor.modelData.name} brightness to ${+monitor.brightness.toFixed(2)}`; + } + } + component Monitor: QtObject { id: monitor |