Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/PrivilegeSurface.qml

Raw
pragma ComponentBehavior: Bound
import QtQuick
import QtQuick.Window
import "privilege.js" as PrivilegeUi

Rectangle {
    id: root

    required property Theme theme
    required property Privilege privilegeService
    // The service's typed state, bound; the specialized controller owns it.
    readonly property bool active: privilegeService ? privilegeService.active : false
    // A granted request, shown briefly before the surface closes.
    readonly property bool confirming: privilegeService ? privilegeService.confirming : false
    readonly property string error: privilegeService ? privilegeService.error : ""
    readonly property var request: privilegeService ? privilegeService.request : ({})
    // A submitted password is being checked: the field holds, authorize waits.
    readonly property bool checking: active && !!privilegeService && privilegeService.awaitingCompletion
    property bool secretVisible: false
    // Only the submitted password's length survives submission: checking shows it
    // as masked dots, so the field never looks emptied while the secret is gone.
    property int submittedLength: 0

    function submitSecret(secret) {
        if (privilegeService && !checking) {
            submittedLength = String(secret).length;
            privilegeService.submit(secret);
        }
    }

    function deny() {
        if (privilegeService)
            privilegeService.deny();
    }

    function cancel() {
        if (privilegeService)
            privilegeService.cancel();
    }

    function focusSecret() {
        if (root.visible && root.active) {
            root.forceActiveFocus();
            secretInput.forceActiveFocus();
        }
    }

    color: theme ? theme.withAlpha(theme.ui.backdrop, 0.94) : "black"
    visible: active || error.length > 0 || confirming
    focus: visible
    onVisibleChanged: {
        if (visible && active)
            Qt.callLater(root.focusSecret);
    }
    // A new request takes the password field.
    onActiveChanged: {
        if (active)
            Qt.callLater(root.focusSecret);
    }
    Component.onCompleted: Qt.callLater(root.focusSecret)

    Connections {
        target: root.Window.window

        function onActiveChanged() {
            if (root.Window.window.active && root.visible && root.active)
                Qt.callLater(root.focusSecret);
        }
    }

    Keys.onEscapePressed: event => {
        event.accepted = true;
        root.cancel();
    }

    Rectangle {
        anchors.centerIn: parent
        width: Math.min(parent.width - 48, 680)
        height: cardColumn.implicitHeight + 44
        radius: 24
        color: root.theme ? root.theme.withAlpha(root.theme.ui.backdrop, 0.98) : "#181818"
        border.width: 1
        border.color: root.theme ? root.theme.withAlpha(root.error ? root.theme.error : root.theme.ui.accent, 0.32) : "transparent"

        Column {
            id: cardColumn

            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.margins: 22
            spacing: 16

            Column {
                width: parent.width
                spacing: 10

                // Success says it once: a check and "authorized" in the title,
                // above the request it answered.
                Row {
                    id: successTitle

                    width: parent.width
                    spacing: 8

                    Text {
                        visible: root.confirming
                        anchors.verticalCenter: parent.verticalCenter
                        text: "✓"
                        color: root.theme ? root.theme.info : "white"
                        font.family: "IBM Plex Mono"
                        font.pixelSize: 16
                        font.bold: true
                        Accessible.ignored: true
                    }

                    NuguText {
                        theme: root.theme
                        variant: "title"
                        text: root.confirming ? "authorized" : (root.error && !root.active ? "authentication unavailable" : "authorization required")
                        color: root.confirming && root.theme ? root.theme.info : (root.theme ? root.theme.content.normal : "white")
                        width: parent.width - (root.confirming ? 24 : 0)
                        elide: Text.ElideRight
                    }
                }

                Rectangle {
                    width: parent.width
                    height: commandText.implicitHeight + 20
                    radius: 16
                    color: root.theme ? root.theme.withAlpha(root.theme.ui.minor, 0.08) : "transparent"

                    Text {
                        id: commandText

                        anchors.left: parent.left
                        anchors.right: parent.right
                        anchors.top: parent.top
                        anchors.margins: 10
                        text: PrivilegeUi.commandMarkdown(root.request.message || "a system change needs your password")
                        color: root.theme ? root.theme.content.normal : "white"
                        textFormat: Text.RichText
                        wrapMode: Text.WordWrap
                        elide: Text.ElideNone
                    }
                }
            }

            PrivilegeFailureShake {
                theme: root.theme
                objectName: "failureShakeSlot"
                visible: !root.confirming
                // Every rejection shakes, also a repeated one after checking.
                active: !root.checking && !!root.error && root.error.length > 0
                width: parent.width
                height: inputColumn.implicitHeight

                Column {
                    id: inputColumn

                    width: parent.width
                    spacing: 7

                    // The field and its reveal button, drawn like PopupTextField and the
                    // vault's master password: the auth harness reads the plain TextInput.
                    Row {
                        width: parent.width
                        spacing: 6

                        Rectangle {
                            id: secretBox

                            objectName: "secretPromptField"
                            width: parent.width - revealButton.width - parent.spacing
                            height: 40
                            radius: root.theme ? root.theme.radiusControl : 8
                            opacity: root.checking ? 0.6 : 1
                            color: root.theme ? root.theme.withAlpha(root.theme.content.backdrop, secretInput.activeFocus ? 0.9 : 0.68) : "transparent"
                            border.width: secretInput.activeFocus ? 2 : 1
                            // A rejection keeps an error edge; focus draws the shared focus border.
                            border.color: !root.theme ? "transparent" : (root.error ? root.theme.error : (secretInput.activeFocus ? root.theme.content.focus : root.theme.withAlpha(root.theme.ui.minor, 0.3)))

                            NuguText {
                                anchors.left: parent.left
                                anchors.leftMargin: root.theme ? root.theme.spaceMd : 12
                                anchors.verticalCenter: parent.verticalCenter
                                theme: root.theme
                                variant: "meta"
                                text: root.request.prompt || "password"
                                visible: secretInput.text.length === 0 && !root.checking
                                // Opaque, at least 4.5:1 on every field fill.
                                color: root.theme ? root.theme.ui.minor : "grey"
                            }

                            // The submitted password, masked and read-only, while it is checked.
                            Text {
                                objectName: "checkingSecretMask"
                                anchors.left: parent.left
                                anchors.right: parent.right
                                anchors.leftMargin: root.theme ? root.theme.spaceMd : 12
                                anchors.rightMargin: root.theme ? root.theme.spaceMd : 12
                                anchors.verticalCenter: parent.verticalCenter
                                visible: root.checking && secretInput.text.length === 0
                                clip: true
                                color: root.theme ? root.theme.content.normal : "white"
                                font: secretInput.font
                                text: secretInput.passwordCharacter.repeat(root.submittedLength)
                                Accessible.ignored: true
                            }

                            MouseArea {
                                anchors.fill: parent
                                cursorShape: Qt.IBeamCursor
                                onClicked: secretInput.forceActiveFocus()
                            }

                            TextInput {
                                id: secretInput

                                anchors.fill: parent
                                anchors.leftMargin: root.theme ? root.theme.spaceMd : 12
                                anchors.rightMargin: root.theme ? root.theme.spaceMd : 12
                                verticalAlignment: TextInput.AlignVCenter
                                clip: true
                                color: root.theme ? root.theme.content.normal : "white"
                                selectionColor: root.theme ? root.theme.ui.accent : "white"
                                selectedTextColor: root.theme ? root.theme.content.backdrop : "black"
                                font.family: "IBM Plex Sans"
                                font.pixelSize: 14
                                echoMode: root.secretVisible || root.request.responseVisible ? TextInput.Normal : TextInput.Password
                                // Read-only keeps focus here, so a rejection lets the user retype at once.
                                readOnly: root.checking
                                inputMethodHints: Qt.ImhSensitiveData
                                activeFocusOnTab: true
                                KeyNavigation.tab: revealButton
                                KeyNavigation.backtab: authorizeButton
                                Keys.onReturnPressed: event => {
                                    event.accepted = true;
                                    if (root.checking)
                                        return;
                                    root.submitSecret(text);
                                    text = "";
                                }
                                Keys.onEnterPressed: event => {
                                    event.accepted = true;
                                    if (root.checking)
                                        return;
                                    root.submitSecret(text);
                                    text = "";
                                }
                                Keys.onEscapePressed: event => {
                                    event.accepted = true;
                                    root.cancel();
                                    text = "";
                                }
                            }
                        }

                        PopupButton {
                            id: revealButton

                            theme: root.theme
                            width: 40
                            height: 40
                            icon: root.secretVisible ? "" : ""
                            text: root.secretVisible ? "hide password" : "show password"
                            accessibleName: text
                            tooltip: text
                            showText: false
                            checked: root.secretVisible
                            KeyNavigation.tab: cancelButton
                            KeyNavigation.backtab: secretInput
                            onClicked: root.secretVisible = !root.secretVisible
                        }
                    }

                    NuguText {
                        // The line under the field names a rejection; checking says so once,
                        // on the authorize button, and blanks a previous rejection while
                        // keeping its line, so a retry never moves the buttons.
                        visible: !!root.error && root.error.length > 0
                        theme: root.theme
                        variant: "meta"
                        text: root.checking ? "" : root.error === "Authentication failed" ? "password rejected" : (root.error === "no-active-polkit-flow" ? "no active authentication request" : root.error)
                        width: parent.width
                        color: root.theme ? root.theme.error : "white"
                        elide: Text.ElideRight
                    }
                }
            }

            Row {
                objectName: "denyCancelConfirmRow"
                visible: !root.confirming
                width: parent.width
                spacing: 10

                PopupButton {
                    id: cancelButton

                    theme: root.theme
                    width: Math.floor((parent.width - 10) * 0.38)
                    height: 40
                    icon: ""
                    text: "cancel"
                    accessibleName: text
                    tooltip: text
                    role: "ghost"
                    KeyNavigation.tab: authorizeButton
                    KeyNavigation.backtab: revealButton
                    onClicked: {
                        root.cancel();
                        secretInput.text = "";
                    }
                }

                PopupButton {
                    id: authorizeButton

                    objectName: "authorizeButton"
                    theme: root.theme
                    width: parent.width - Math.floor((parent.width - 10) * 0.38) - 10
                    height: 40
                    // nf-fa-hourglass_half while checking, nf-fa-check otherwise.
                    icon: root.checking ? "\uF252" : ""
                    text: root.checking ? "checking…" : "authorize"
                    accessibleName: text
                    tooltip: text
                    role: "primary"
                    enabled: root.active
                    // A status while checking: legible at full strength, never clickable.
                    interactive: !root.checking
                    KeyNavigation.tab: secretInput
                    KeyNavigation.backtab: cancelButton
                    onClicked: {
                        // Focus waits in the field, so a rejection lets the user retype at once.
                        secretInput.forceActiveFocus();
                        root.submitSecret(secretInput.text);
                        secretInput.text = "";
                    }
                }
            }
        }
    }

    component PrivilegeFailureShake: Item {
        id: shakeRoot

        required property Theme theme
        default property alias content: contentItem.data
        property bool active: false
        property real boundedDistance: 12
        readonly property var shakeOffsets: [0, -boundedDistance, boundedDistance, -boundedDistance / 2, boundedDistance / 2, 0]

        // The caller supplies width and height; content only fills that frame.
        onActiveChanged: {
            if (active)
                shake.restart();
        }

        Item {
            id: contentItem

            anchors.fill: parent
        }

        SequentialAnimation {
            id: shake

            running: false
            alwaysRunToEnd: true

            ScriptAction {
                script: translation.x = shakeRoot.shakeOffsets[0]
            }

            NumberAnimation {
                target: translation
                property: "x"
                to: shakeRoot.shakeOffsets[1]
                duration: shakeRoot.theme.lowPowerMode ? 0 : 50
                easing.type: Easing.OutCubic
            }

            NumberAnimation {
                target: translation
                property: "x"
                to: shakeRoot.shakeOffsets[2]
                duration: shakeRoot.theme.lowPowerMode ? 0 : 50
                easing.type: Easing.OutCubic
            }

            NumberAnimation {
                target: translation
                property: "x"
                to: shakeRoot.shakeOffsets[3]
                duration: shakeRoot.theme.lowPowerMode ? 0 : 40
                easing.type: Easing.OutCubic
            }

            NumberAnimation {
                target: translation
                property: "x"
                to: shakeRoot.shakeOffsets[4]
                duration: shakeRoot.theme.lowPowerMode ? 0 : 40
                easing.type: Easing.OutCubic
            }

            NumberAnimation {
                target: translation
                property: "x"
                to: shakeRoot.shakeOffsets[5]
                duration: shakeRoot.theme.lowPowerMode ? 0 : 30
                easing.type: Easing.OutCubic
            }
        }

        transform: Translate {
            id: translation

            x: 0
        }
    }
}