repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nuguland/BrightnessPanel.qml

Raw
pragma ComponentBehavior: Bound
import QtQuick
import "Util.js" as Util

Column {
    id: root

    required property Theme theme
    required property Brightness brightnessService
    property var panelState: brightnessService ? Util.parseJson(brightnessService.stateJson(), {
        "displays": [],
        "global": {
            "mode": "none",
            "level": 0,
            "levels": []
        },
        "setup": {
            "needed": false,
            "available": false
        }
    }) : ({
            "displays": [],
            "global": {
                "mode": "none",
                "level": 0,
                "levels": []
            },
            "setup": {
                "needed": false,
                "available": false
            }
        })
    readonly property var displays: panelState.displays || []
    readonly property var global: panelState.global || ({
            "mode": "none",
            "level": 0,
            "levels": []
        })
    readonly property var setup: panelState.setup || ({
            "needed": false,
            "available": false
        })
    readonly property bool hasGlobal: global.mode !== "none"

    function refreshFrom(text) {
        panelState = Util.parseJson(text, panelState);
    }

    function setAllAt(x, width) {
        if (brightnessService && root.hasGlobal)
            refreshFrom(brightnessService.setAll(Math.max(0, Math.min(1, x / width))));
    }

    function setDisplayAt(id, x, width) {
        if (brightnessService)
            refreshFrom(brightnessService.setDisplay(id, Math.max(0, Math.min(1, x / width))));
    }

    width: parent ? parent.width : 0
    spacing: 12

    Row {
        width: parent.width
        height: root.setup.needed ? 34 : 0
        visible: root.setup.needed

        Item {
            width: Math.max(1, parent.width - setupButton.width)
            height: 1
        }

        PopupButton {
            id: setupButton
            theme: root.theme
            width: 112
            height: 34
            text: "setup ddc"
            role: "primary"
            onClicked: if (root.brightnessService)
                root.refreshFrom(root.brightnessService.setupDdc())
        }
    }

    PopupCard {
        width: parent.width
        height: root.hasGlobal ? 64 : 0
        visible: root.hasGlobal
        theme: root.theme

        PopupSlider {
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.verticalCenter: parent.verticalCenter
            theme: root.theme
            value: Number(root.global.level || 0)
            onMoved: value => {
                if (root.brightnessService)
                    root.refreshFrom(root.brightnessService.setAll(value));
            }
        }
    }

    Grid {
        id: presetGrid

        width: parent.width
        height: root.hasGlobal ? 36 : 0
        visible: root.hasGlobal
        columns: 4
        columnSpacing: 8

        Repeater {
            model: [0.25, 0.5, 0.75, 1]

            delegate: PopupButton {
                id: preset

                required property real modelData

                theme: root.theme
                width: Math.floor((presetGrid.width - presetGrid.columnSpacing * (presetGrid.columns - 1)) / presetGrid.columns)
                height: 36
                text: `${Math.round(preset.modelData * 100)}%`
                onClicked: if (root.brightnessService)
                    root.refreshFrom(root.brightnessService.setAll(preset.modelData))
            }
        }
    }

    Column {
        width: parent.width
        spacing: 8

        Repeater {
            model: root.displays

            delegate: PopupCard {
                id: displayRow

                theme: root.theme
                padding: 0

                required property var modelData
                readonly property bool controllable: !!modelData.controllable
                readonly property string issue: String(modelData.issue || "")
                readonly property real level: Math.max(0, Math.min(1, Number(modelData.level || 0)))

                width: root.width
                height: 38
                attention: issue.length > 0
                enabled: controllable

                NuguText {
                    id: displayName

                    anchors.left: parent.left
                    anchors.leftMargin: 10
                    anchors.verticalCenter: parent.verticalCenter
                    width: 128
                    theme: root.theme
                    text: String(displayRow.modelData.name || "display")
                    color: root.theme ? root.theme.content.normal : "white"
                    elide: Text.ElideRight
                }

                PopupSlider {
                    id: displaySlider

                    anchors.left: displayName.right
                    anchors.leftMargin: 10
                    anchors.right: displayPercent.left
                    anchors.rightMargin: 10
                    anchors.verticalCenter: parent.verticalCenter
                    theme: root.theme
                    enabled: displayRow.controllable
                    value: displayRow.level
                    onMoved: value => root.setDisplayAt(String(displayRow.modelData.id || ""), value, 1)
                }

                NuguText {
                    id: displayPercent

                    anchors.right: issueBadge.visible ? issueBadge.left : parent.right
                    anchors.rightMargin: issueBadge.visible ? 10 : 10
                    anchors.verticalCenter: parent.verticalCenter
                    width: 44
                    theme: root.theme
                    variant: "meta"
                    text: displayRow.controllable ? `${Math.round(displayRow.level * 100)}%` : "—"
                    color: root.theme ? root.theme.content.normal : "white"
                    horizontalAlignment: Text.AlignRight
                }

                NuguText {
                    id: issueBadge

                    visible: displayRow.issue.length > 0
                    anchors.right: parent.right
                    anchors.rightMargin: 10
                    anchors.verticalCenter: parent.verticalCenter
                    width: 70
                    theme: root.theme
                    variant: "meta"
                    text: displayRow.issue.toLowerCase()
                    color: root.theme ? root.theme.warning : "white"
                    horizontalAlignment: Text.AlignRight
                }
            }
        }
    }
}