repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nuguland/PowerPanel.qml

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

Column {
    id: root

    required property Theme theme
    required property Power powerService
    property var panelState: powerService ? Util.parseJson(powerService.stateJson(), {
        "battery": {},
        "externalDevices": []
    }) : ({
            "battery": {},
            "externalDevices": []
        })
    readonly property var battery: panelState.battery || ({})
    readonly property var devices: panelState.externalDevices || []
    readonly property var sessionActions: panelState.sessionActions || ["lock", "suspend", "monitor-off", "monitor-on", "logout", "reboot", "poweroff"]
    readonly property bool hasMainBattery: !!(battery && battery.present)
    readonly property var safeActionNames: ["lock", "suspend", "monitor-off"]
    readonly property var destructiveActionNames: ["logout", "reboot", "poweroff"]
    readonly property var safeActions: actionSubset(safeActionNames)
    readonly property var destructiveActions: actionSubset(destructiveActionNames)

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

    function runSession(action) {
        if (powerService)
            refreshFrom(powerService.sessionAction(action));
    }

    function confirm(action) {
        if (powerService)
            refreshFrom(powerService.confirmAction(action));
    }

    function cancel() {
        if (powerService)
            refreshFrom(powerService.cancelAction());
    }

    function roleColor(role) {
        if (!theme)
            return "white";

        if (role === "warning")
            return theme.warning;

        if (role === "error")
            return theme.error;

        if (role === "ui.minor")
            return theme.ui.minor;

        return theme.ui.accent;
    }

    function actionSubset(names) {
        const rows = [];
        for (let i = 0; i < names.length; i++) {
            if (sessionActions.indexOf(names[i]) >= 0)
                rows.push(names[i]);
        }
        return rows;
    }

    function actionLabel(action) {
        if (action === "suspend")
            return "sleep";

        if (action === "monitor-off")
            return "display off";

        if (action === "poweroff")
            return "power off";

        return action;
    }

    function batteryMeta() {
        const parts = [];
        if (battery.timeEstimate && battery.timeEstimate !== "unknown")
            parts.push(battery.timeEstimate);
        if (battery.health >= 0)
            parts.push(`health ${battery.health}%`);
        return parts.join(" · ");
    }

    spacing: 12
    width: parent ? parent.width : 0
    focus: true
    Keys.onEscapePressed: root.cancel()
    Keys.onReturnPressed: root.panelState.pendingConfirmation ? root.confirm(root.panelState.pendingConfirmation) : root.runSession("lock")
    Keys.onEnterPressed: root.panelState.pendingConfirmation ? root.confirm(root.panelState.pendingConfirmation) : root.runSession("lock")

    PopupCard {
        objectName: "batteryHero"
        theme: root.theme
        padding: 0
        active: true
        attention: root.battery.role === "warning"
        destructive: root.battery.role === "error"
        width: parent.width
        height: root.hasMainBattery ? 116 : 0
        visible: root.hasMainBattery

        Rectangle {
            id: batteryDial

            anchors.left: parent.left
            anchors.leftMargin: 12
            anchors.verticalCenter: parent.verticalCenter
            width: 92
            height: 92
            radius: 46
            color: root.theme ? root.theme.withAlpha(root.roleColor(root.battery.role || "ui.accent"), 0.18) : "transparent"
            border.width: 1
            border.color: root.theme ? root.roleColor(root.battery.role || "ui.accent") : "transparent"

            NuguText {
                anchors.centerIn: parent
                theme: root.theme
                variant: "title"
                font.pixelSize: 25
                text: `${root.battery.percent || 0}%`
                color: root.theme ? root.theme.content.normal : "white"
            }
        }

        Column {
            anchors.left: batteryDial.right
            anchors.leftMargin: 14
            anchors.right: parent.right
            anchors.rightMargin: 14
            anchors.verticalCenter: parent.verticalCenter
            spacing: 8

            NuguText {
                theme: root.theme
                variant: "title"
                width: parent.width
                text: root.battery.state || "unknown"
                color: root.theme ? root.roleColor(root.battery.role || "ui.accent") : "white"
                elide: Text.ElideRight
            }

            NuguText {
                theme: root.theme
                variant: "meta"
                width: parent.width
                visible: root.batteryMeta().length > 0
                text: root.batteryMeta()
                color: root.theme ? root.theme.ui.minor : "white"
                elide: Text.ElideRight
            }
        }
    }

    Flow {
        id: deviceFlow

        objectName: "externalDeviceBatteryChips"
        width: parent.width
        height: visible ? implicitHeight : 0
        spacing: 8
        visible: root.devices.length > 0

        Repeater {
            model: root.devices

            delegate: PopupCard {
                id: deviceChip

                required property var modelData

                theme: root.theme
                padding: 0
                width: Math.floor((root.width - 8) / 2)
                height: 32

                NuguText {
                    anchors.left: parent.left
                    anchors.leftMargin: 9
                    anchors.right: devicePercent.left
                    anchors.rightMargin: 8
                    anchors.verticalCenter: parent.verticalCenter
                    theme: root.theme
                    variant: "meta"
                    text: `${deviceChip.modelData.name}`
                    color: root.theme ? root.theme.ui.minor : "white"
                    elide: Text.ElideRight
                }

                NuguText {
                    id: devicePercent

                    anchors.right: parent.right
                    anchors.rightMargin: 9
                    anchors.verticalCenter: parent.verticalCenter
                    theme: root.theme
                    variant: "meta"
                    text: `${deviceChip.modelData.percent}%`
                    color: root.theme ? root.theme.content.normal : "white"
                }
            }
        }
    }

    Grid {
        id: safeGrid

        objectName: "safeActionGrid"
        width: parent.width
        columns: 3
        columnSpacing: 8
        rowSpacing: 8

        Repeater {
            model: root.safeActions

            delegate: PopupButton {
                id: safeButton

                required property string modelData
                readonly property bool isPrimary: modelData === "lock"

                theme: root.theme
                width: Math.floor((root.width - safeGrid.columnSpacing * (safeGrid.columns - 1)) / safeGrid.columns)
                height: 42
                text: root.actionLabel(modelData)
                role: isPrimary ? "primary" : "normal"
                onClicked: root.runSession(modelData)
            }
        }
    }

    PopupCard {
        objectName: "dangerRegion"
        theme: root.theme
        padding: 0
        attention: !!root.panelState.pendingConfirmation
        destructive: !root.panelState.pendingConfirmation
        width: parent.width
        height: root.panelState.pendingConfirmation ? 62 : 58

        Grid {
            id: dangerGrid

            anchors.fill: parent
            anchors.margins: 10
            columns: 3
            columnSpacing: 8
            visible: !root.panelState.pendingConfirmation

            Repeater {
                model: root.destructiveActions

                delegate: PopupButton {
                    id: dangerButton

                    required property string modelData

                    theme: root.theme
                    width: Math.floor((dangerGrid.width - dangerGrid.columnSpacing * 2) / 3)
                    height: 38
                    text: root.actionLabel(modelData)
                    role: "destructive"
                    onClicked: root.runSession(modelData)
                }
            }
        }

        Row {
            anchors.fill: parent
            anchors.margins: 10
            spacing: 8
            visible: !!root.panelState.pendingConfirmation

            NuguText {
                anchors.verticalCenter: parent.verticalCenter
                theme: root.theme
                variant: "title"
                text: root.actionLabel(root.panelState.pendingConfirmation)
                color: root.theme ? root.theme.warning : "white"
                width: parent.width - confirmButton.width - cancelButton.width - 16
                elide: Text.ElideRight
            }

            PopupButton {
                id: confirmButton
                anchors.verticalCenter: parent.verticalCenter
                theme: root.theme
                width: 82
                height: 38
                text: "confirm"
                role: "destructive"
                onClicked: root.confirm(root.panelState.pendingConfirmation)
            }

            PopupButton {
                id: cancelButton
                anchors.verticalCenter: parent.verticalCenter
                theme: root.theme
                width: 74
                height: 38
                text: "cancel"
                onClicked: root.cancel()
            }
        }
    }
}