| 1 | pragma Singleton |
| 2 | import QtQuick |
| 3 | import Quickshell |
| 4 | import Quickshell.Io |
| 5 | |
| 6 | // Backlight via brightnessctl. Hides itself if brightnessctl isn't installed. |
| 7 | Singleton { |
| 8 | id: root |
| 9 | property int percent: -1 |
| 10 | readonly property bool available: percent >= 0 |
| 11 | property int pending: -1 |
| 12 | |
| 13 | property bool stepping: false |
| 14 | signal stepped() // a step() finished and percent is fresh (drives the OSD) |
| 15 | |
| 16 | function refresh() { getProc.running = true } |
| 17 | // media keys: ~/.local/bin/brictl picks laptop backlight vs ddcutil for the focused monitor; |
| 18 | // plain brightnessctl if the script is missing |
| 19 | function step(dir) { |
| 20 | if (stepProc.running) return |
| 21 | stepProc.command = ["sh", "-c", |
| 22 | '[ -x "$1" ] && exec "$1" "$2"; exec brightnessctl -e4 -n2 set 5%"$2"', |
| 23 | "sh", Quickshell.env("HOME") + "/.local/bin/brictl", dir > 0 ? "+" : "-"] |
| 24 | stepProc.running = true |
| 25 | } |
| 26 | function setPercent(v) { |
| 27 | percent = Math.round(v) |
| 28 | pending = percent |
| 29 | debounce.restart() |
| 30 | } |
| 31 | |
| 32 | Timer { |
| 33 | id: debounce |
| 34 | interval: 70 |
| 35 | onTriggered: { |
| 36 | if (setProc.running) { debounce.restart(); return } |
| 37 | setProc.command = ["brightnessctl", "set", Math.max(1, root.pending) + "%"] |
| 38 | setProc.running = true |
| 39 | } |
| 40 | } |
| 41 | Process { id: setProc } |
| 42 | Process { |
| 43 | id: stepProc |
| 44 | onExited: { root.stepping = true; getProc.running = true } |
| 45 | } |
| 46 | Process { |
| 47 | id: getProc |
| 48 | command: ["brightnessctl", "-m"] |
| 49 | running: true |
| 50 | stdout: StdioCollector { |
| 51 | onStreamFinished: { |
| 52 | var p = text.trim().split(",") |
| 53 | if (p.length >= 4) root.percent = parseInt(p[3]) |
| 54 | if (root.stepping) { root.stepping = false; root.stepped() } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |