repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nuguland/Brightness.qml

Raw
import QtQuick
import Quickshell
import Quickshell.Io

Item {
    id: root

    property QtObject osdService
    property string devicePath: "/sys/class/backlight/intel_backlight"
    property var lastAction: ({
    })
    property var lastOsd: ({
    })
    readonly property var boundedActions: ["increase", "decrease", "set-level"]

    function clamp01(value) {
        const n = Number(value);
        if (!isFinite(n))
            return 0;

        return Math.max(0, Math.min(1, n));
    }

    function intText(text, fallback) {
        const n = parseInt(String(text || "").trim(), 10);
        return isFinite(n) ? n : fallback;
    }

    function nativeMax() {
        return Math.max(1, intText(maxFile.text(), 1));
    }

    function nativeBrightness() {
        return Math.max(1, intText(brightnessFile.text(), nativeMax()));
    }

    function levelFraction() {
        return clamp01(nativeBrightness() / nativeMax());
    }

    function osd(value) {
        const text = String(Math.round(clamp01(value) * 100));
        if (!osdService)
            return {
            "event": "brightness",
            "value": text
        };

        try {
            return JSON.parse(osdService.trigger("brightness", text));
        } catch (error) {
            return {
                "event": "brightness",
                "value": text
            };
        }
    }

    function stateObject() {
        const level = Math.round(levelFraction() * 100) / 100;
        return {
            "backend": "Brightness.qml",
            "devicePath": devicePath,
            "helperProcesses": ["sh"],
            "serviceOwnedActions": true,
            "boundedActions": boundedActions,
            "level": level,
            "compact": {
                "label": "Bright",
                "role": level <= 0.15 ? "ui.minor" : level >= 0.85 ? "warning" : "ui.accent"
            },
            "shader": {
                "kind": "brightness-gradient",
                "clipped": true,
                "source": "shaders/brightness-gradient.frag.qsb",
                "priorArt": "GLSL luminance gradients and smoothstep bar masks plus Inigo Quilez-style rounded-box SDF alpha mask",
                "values": [level, 1 - level, level >= 0.85 ? 1 : 0, level <= 0.15 ? 1 : 0],
                "colors": ["ui.accent", "ui.focus", "warning", "ui.backdrop"]
            },
            "lastAction": lastAction,
            "lastOsd": lastOsd
        };
    }

    function stateJson() {
        return JSON.stringify(stateObject());
    }

    function applyNative(level) {
        const value = clamp01(level);
        Quickshell.execDetached(["sh", "-c", `d='${devicePath}'; m=$(cat "$d/max_brightness"); n=$(awk -v v=${value} -v m=$m 'BEGIN { x=int(v*m); if (x<1) x=1; if (x>m) x=m; print x }'); printf '%s\n' "$n" > "$d/brightness"`]);
    }

    function setLevel(level) {
        const value = clamp01(level);
        applyNative(value);
        lastAction = {
            "kind": "set-level",
            "level": Math.round(value * 100) / 100
        };
        lastOsd = osd(value);
        return stateJson();
    }

    function increase() {
        return setLevel(levelFraction() + 0.05);
    }

    function decrease() {
        return setLevel(levelFraction() - 0.05);
    }

    FileView {
        id: brightnessFile

        path: `${root.devicePath}/brightness`
        blockLoading: true
        blockAllReads: true
        printErrors: false
    }

    FileView {
        id: maxFile

        path: `${root.devicePath}/max_brightness`
        blockLoading: true
        blockAllReads: true
        printErrors: false
    }

}