Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/features/power/PowerPanel.qml

Raw
pragma ComponentBehavior: Bound
import QtQuick
import "../.."
import "../../Util.js" as Util
import "../../human.js" as Human
import "power.js" as PowerModel

Flickable {
    id: root

    required property Theme theme
    required property Power powerService
    readonly property var devices: powerService ? powerService.externalDevices : []
    readonly property var sessionActions: powerService ? powerService.sessionActions : []
    readonly property bool hasMainBattery: powerService ? powerService.batteryPresent : false
    readonly property int batteryPercent: powerService ? powerService.batteryPercent : 0
    readonly property string batteryState: powerService ? powerService.batteryState : "unknown"
    readonly property string pendingConfirmation: powerService ? powerService.pendingConfirmation : ""
    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)
    // Opening focuses the first action, lock (FeatureEntry.qml).
    property Item initialFocus: null

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

    function restoreActionFocus(action) {
        const index = safeActions.concat(destructiveActions).indexOf(action);
        const button = actionRepeater.itemAt(index);
        if (button)
            button.forceActiveFocus(Qt.TabFocusReason);
    }

    // Arrows move focus within the action grid, row-aware; moving never
    // activates or arms an action.
    function moveActionFocus(index, key) {
        const direction = key === Qt.Key_Left ? "left" : (key === Qt.Key_Right ? "right" : (key === Qt.Key_Up ? "up" : (key === Qt.Key_Down ? "down" : "")));
        if (!direction)
            return false;
        const button = actionRepeater.itemAt(PowerModel.gridStep(index, actionRepeater.count, actionGrid.columns, direction));
        if (button)
            button.forceActiveFocus(Qt.TabFocusReason);
        return true;
    }

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

    function cancel() {
        const action = pendingConfirmation;
        if (powerService)
            powerService.cancelAction();
        restoreActionFocus(action);
    }

    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 actionIcon(action) {
        if (action === "lock")
            return "\uF023";
        if (action === "suspend")
            return "\uF186";
        if (action === "monitor-off" || action === "monitor-on")
            return "\uF108";
        if (action === "logout")
            return "\uF08B";
        if (action === "reboot")
            return "\uF01E";
        return "\uF011";
    }

    function actionLabel(action) {
        if (action === "suspend")
            return "sleep";
        if (action === "monitor-off")
            return "display off";
        if (action === "poweroff")
            return "power off";
        if (action === "logout")
            return "log out";
        return action;
    }

    width: parent ? parent.width : 0
    contentWidth: width
    contentHeight: powerCol.height
    // The card fits the content; the entry height caps it (FeatureEntry.qml).
    readonly property real naturalHeight: contentHeight
    clip: true
    interactive: contentHeight > height
    boundsBehavior: Flickable.StopAtBounds
    flickableDirection: Flickable.VerticalFlick

    Column {
        id: powerCol

        width: root.width
        spacing: 12

        Item {
            objectName: "batteryStatus"
            width: parent.width
            height: 48
            visible: root.hasMainBattery

            Rectangle {
                id: batteryIcon

                anchors.left: parent.left
                anchors.verticalCenter: parent.verticalCenter
                width: 34
                height: 20
                radius: 4
                color: "transparent"
                border.width: 1
                border.color: root.theme.ui.minor
                Accessible.ignored: true

                Rectangle {
                    x: 4
                    y: 4
                    width: 26 * Math.max(0, Math.min(1, root.batteryPercent / 100))
                    height: 12
                    radius: 1
                    color: root.theme.roleColor(root.powerService ? root.powerService.batteryRole : "")
                }

                Rectangle {
                    x: 34
                    y: 6
                    width: 3
                    height: 8
                    radius: 1
                    color: root.theme.ui.minor
                }
            }

            Column {
                anchors.left: batteryIcon.right
                anchors.leftMargin: 16
                anchors.right: parent.right
                anchors.verticalCenter: parent.verticalCenter
                spacing: 2

                Row {
                    spacing: 10

                    NuguText {
                        id: batteryPercent
                        theme: root.theme
                        variant: "title"
                        font.pixelSize: root.theme.textTitle
                        text: `${root.batteryPercent}%`
                    }

                    NuguText {
                        anchors.baseline: batteryPercent.baseline
                        theme: root.theme
                        variant: "meta"
                        text: root.batteryState || "unknown"
                        color: root.theme.ui.minor
                    }
                }

                NuguText {
                    theme: root.theme
                    variant: "meta"
                    width: parent.width
                    visible: text.length > 0
                    text: root.powerService ? PowerModel.batteryMeta({
                        "state": root.batteryState,
                        "timeEstimate": root.powerService.batteryTimeEstimate,
                        "health": root.powerService.batteryHealth
                    }) : ""
                    color: root.theme.ui.minor
                    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"
                        // A device without a model reads as its kind, never as its bus path.
                        text: Human.label(deviceChip.modelData.name, deviceChip.modelData.type || "device")
                        color: root.theme.content.normal
                        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.content.normal
                    }
                }
            }
        }

        Grid {
            id: actionGrid

            objectName: "powerActionGrid"
            width: parent.width
            columns: 3
            spacing: 8

            Repeater {
                id: actionRepeater
                model: root.safeActions.concat(root.destructiveActions)

                delegate: PopupButton {
                    id: actionButton

                    required property string modelData
                    required property int index
                    readonly property bool risky: root.destructiveActionNames.indexOf(modelData) >= 0
                    // Every action shares one tile; danger shows only while hovered or armed.
                    readonly property color labelColor: risky && (hovered || root.pendingConfirmation === modelData) ? root.theme.error : root.theme.content.normal

                    Binding {
                        when: actionButton.index === 0
                        target: root
                        property: "initialFocus"
                        value: actionButton
                        restoreMode: Binding.RestoreNone
                    }

                    theme: root.theme
                    width: Math.floor((root.width - actionGrid.spacing * 2) / 3)
                    height: 66
                    text: root.actionLabel(modelData)
                    showText: false
                    // Each tile's visible label says it all; no tooltip repeats it.
                    tooltip: ""
                    onControlFocusedChanged: if (controlFocused)
                        Util.revealIn(this, root)
                    onClicked: root.runSession(modelData)
                    Keys.onPressed: event => {
                        event.accepted = root.moveActionFocus(index, event.key);
                    }

                    Column {
                        anchors.centerIn: parent
                        spacing: 6

                        Text {
                            anchors.horizontalCenter: parent.horizontalCenter
                            font.family: root.theme.fontIcon
                            font.pixelSize: 20
                            color: actionButton.labelColor
                            text: root.actionIcon(actionButton.modelData)
                            Accessible.ignored: true
                        }

                        NuguText {
                            anchors.horizontalCenter: parent.horizontalCenter
                            theme: root.theme
                            variant: "meta"
                            color: actionButton.labelColor
                            text: actionButton.text
                            Accessible.ignored: true
                        }
                    }
                }
            }
        }

        Item {
            objectName: "powerConfirmation"
            width: parent.width
            height: 104
            visible: root.pendingConfirmation.length > 0

            Rectangle {
                width: parent.width
                height: 1
                color: root.theme.withAlpha(root.theme.ui.minor, 0.2)
            }

            Column {
                y: 13
                width: parent.width
                spacing: 2

                NuguText {
                    theme: root.theme
                    variant: "title"
                    text: `${root.actionLabel(root.pendingConfirmation)}?`
                }

                NuguText {
                    theme: root.theme
                    variant: "meta"
                    text: "unsaved work may be lost"
                    color: root.theme.ui.minor
                }
            }

            Row {
                anchors.right: parent.right
                anchors.bottom: parent.bottom
                spacing: 8

                PopupButton {
                    theme: root.theme
                    text: "cancel"
                    onControlFocusedChanged: if (controlFocused)
                        Util.revealIn(this, root)
                    onClicked: root.cancel()
                }

                PopupButton {
                    theme: root.theme
                    text: root.actionLabel(root.pendingConfirmation)
                    accessibleName: `confirm ${text}`
                    tooltip: ""
                    role: "destructive"
                    primary: true
                    onControlFocusedChanged: if (controlFocused)
                        Util.revealIn(this, root)
                    onClicked: root.confirm(root.pendingConfirmation)
                }
            }
        }
    }
}