repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nuguland/Lock.qml

Raw
import QtQuick
import Quickshell
import Quickshell.Services.Pam
import Quickshell.Wayland

Item {
    id: root

    property QtObject theme
    property QtObject timeService
    property QtObject mediaService
    property QtObject notificationService
    property QtObject powerService
    readonly property bool testHarness: Quickshell.env("NUGULAND_SMOKE") === "1"
    readonly property bool nativeActionRoute: !safeMode
    property bool safeMode: testHarness
    property bool active: false
    property bool logicalLocked: false
    property bool unlocking: false
    property bool unlocked: false
    property bool teardownClean: true
    property string failure: ""
    property int inputLength: 0
    property var lastAction: ({
    })
    readonly property string pamConfig: "nugu-lock"
    readonly property var boundedActions: ["request", "type", "submit", "fail", "unlock", "cancel"]

    function lockSurfacePanelContract() {
        return {
            "source": "Quickshell lockscreen LockSurface.qml LockContext.qml plus Nuguland lock Content.qml PasswordInput.qml NotifGroup.qml",
            "ownedBy": "Lock.qml inline LockSurface",
            "nativeSessionLockSurface": true,
            "centerPasswordRail": true,
            "safeInfoRails": true,
            "secretInputField": true,
            "failureShakeSlot": true,
            "cleanTeardownState": true,
            "serviceFedOnly": true,
            "noDecorativeDistraction": true,
            "railParts": ["time", "secret-input", "safe-media", "notification-summary", "battery", "resources"]
        };
    }

    function surfaceContract() {
        return {
            "sharedSurfaceContext": true,
            "perScreenSurfaces": true,
            "activeMonitorEntryOnly": true,
            "source": "Quickshell lockscreen example plus Nuguland lock Content.qml",
            "centerRail": {
                "role": "password-and-clock",
                "passwordInput": true,
                "clockVisible": true,
                "dateVisible": true
            },
            "sideRails": {
                "safeMedia": "transport-only",
                "notificationSummary": "body-hidden",
                "resourcesVisible": true
            }
        };
    }

    function passwordInputContract() {
        return {
            "passwordInput": true,
            "echoMode": "Password",
            "sensitiveInputHints": true,
            "focusReclaim": true,
            "failureShake": failure.length > 0,
            "enterAction": "submit",
            "volatilePamBuffer": true,
            "serializedSecret": false,
            "widthMorphsWithBuffer": true,
            "iconStates": ["lock", "unlocking", "fingerprint", "fingerprint_off"]
        };
    }

    function stateObject() {
        return {
            "backend": "Lock.qml",
            "usesNativeSessionLock": true,
            "usesPam": true,
            "pamConfig": pamConfig,
            "safeMode": safeMode,
            "testHarness": testHarness,
            "nativeActionRoute": nativeActionRoute,
            "helperProcesses": [],
            "serviceOwnedActions": true,
            "boundedActions": boundedActions,
            "surface": "native-session-lock-surface",
            "keyboardFocus": true,
            "stealsFocusBack": true,
            "secretStored": false,
            "active": active,
            "locked": logicalLocked,
            "nativeLocked": sessionLock.locked,
            "secure": sessionLock.secure,
            "unlocking": unlocking,
            "unlocked": unlocked,
            "teardownClean": teardownClean,
            "failure": failure,
            "inputLength": inputLength,
            "safeMedia": {
                "enabled": true,
                "bodyHidden": true,
                "transportOnly": true
            },
            "notificationSummary": {
                "count": 0,
                "bodiesHidden": true
            },
            "lockSurfacePanelContract": lockSurfacePanelContract(),
            "surfaceContract": surfaceContract(),
            "passwordInput": passwordInputContract(),
            "lastAction": lastAction
        };
    }

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

    function request() {
        active = true;
        logicalLocked = true;
        unlocking = false;
        unlocked = false;
        teardownClean = false;
        failure = "";
        inputLength = 0;
        lastAction = {
            "kind": "request",
            "nativeActionRoute": nativeActionRoute,
            "testHarness": testHarness
        };
        if (!safeMode)
            pam.active = true;

        return stateJson();
    }

    function typeSecret(secret) {
        inputLength = String(secret).length;
        failure = "";
        lastAction = {
            "kind": "type",
            "secretLength": inputLength
        };
        return stateJson();
    }

    function fail(message) {
        active = true;
        logicalLocked = true;
        unlocking = false;
        unlocked = false;
        teardownClean = false;
        inputLength = 0;
        failure = String(message);
        lastAction = {
            "kind": "fail",
            "message": failure
        };
        return stateJson();
    }

    function finishUnlock(action) {
        active = false;
        logicalLocked = false;
        unlocking = false;
        unlocked = true;
        teardownClean = true;
        failure = "";
        inputLength = 0;
        if (!safeMode)
            sessionLock.unlock();

        lastAction = action;
    }

    function submit(secret) {
        const length = String(secret).length;
        unlocking = true;
        if (safeMode) {
            finishUnlock({
                "kind": "submit",
                "secretLength": length,
                "nativeActionRoute": nativeActionRoute,
                "testHarness": testHarness
            });
        } else {
            pam.start();
            pam.respond(String(secret));
            lastAction = {
                "kind": "submit",
                "secretLength": length,
                "nativeActionRoute": nativeActionRoute,
                "testHarness": testHarness
            };
        }
        return stateJson();
    }

    function unlock() {
        finishUnlock({
            "kind": "unlock",
            "nativeActionRoute": nativeActionRoute,
            "testHarness": testHarness
        });
        return stateJson();
    }

    function cancel() {
        if (!safeMode)
            pam.abort();

        active = false;
        logicalLocked = false;
        unlocking = false;
        unlocked = false;
        teardownClean = true;
        failure = "";
        inputLength = 0;
        lastAction = {
            "kind": "cancel"
        };
        return stateJson();
    }

    WlSessionLock {
        id: sessionLock

        locked: !root.safeMode && root.logicalLocked

        WlSessionLockSurface {
            color: root.theme ? root.theme.ui.backdrop : "black"

            LockSurface {
                anchors.fill: parent
                theme: root.theme
                lockService: root
                timeService: root.timeService
                mediaService: root.mediaService
                notificationService: root.notificationService
                powerService: root.powerService
            }

        }

    }

    PamContext {
        id: pam

        configDirectory: Qt.resolvedUrl("pam")
        config: root.pamConfig
        onPamMessage: {
            if (this.responseRequired)
                this.respond("");

        }
        onCompleted: (result) => {
            if (result === PamResult.Success)
                root.finishUnlock({
                "kind": "pam-unlock"
            });
            else
                root.fail("Incorrect password");
        }
        onError: (error) => {
            return root.fail(PamError.toString(error));
        }
    }

    component NuguText: Text {
        required property QtObject theme
        property string variant: "body"
        property bool selected: false

        color: !theme ? "transparent" : (selected ? theme.content.backdrop : (variant === "title" ? theme.ui.important_global : (variant === "meta" ? theme.ui.normal : theme.content.normal)))
        font.family: variant === "mono" ? "IBM Plex Mono" : "IBM Plex Sans"
        font.pixelSize: variant === "title" ? 15 : (variant === "meta" ? 13 : 14)
        font.bold: variant === "title"
        elide: Text.ElideRight
    }

    component FailureShake: Item {
        id: shakeRoot

        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]

        implicitWidth: contentItem.implicitWidth
        implicitHeight: contentItem.implicitHeight
        onActiveChanged: {
            if (active)
                shake.restart();

        }

        Item {
            id: contentItem

            anchors.fill: parent
            implicitWidth: childrenRect.width
            implicitHeight: childrenRect.height
        }

        SequentialAnimation {
            id: shake

            running: false
            alwaysRunToEnd: true

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

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

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

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

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

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

        }

        transform: Translate {
            id: translation

            x: 0
        }

    }

    component LockSurface: Rectangle {
        id: root

        required property QtObject theme
        required property QtObject lockService
        property QtObject timeService
        property QtObject mediaService
        property QtObject notificationService
        property QtObject powerService
        property var lockState: lockService ? parseJson(lockService.stateJson(), {
            "lockSurfacePanelContract": {
            },
            "passwordInput": {
            },
            "safeMedia": {
            },
            "notificationSummary": {
            }
        }) : ({
            "lockSurfacePanelContract": {
            },
            "passwordInput": {
            },
            "safeMedia": {
            },
            "notificationSummary": {
            }
        })
        readonly property var panel: lockState.lockSurfacePanelContract || ({
        })
        readonly property var timeState: timeService ? parseJson(timeService.stateJson(), {
            "clock": {
            },
            "date": {
            }
        }) : ({
            "clock": {
            },
            "date": {
            }
        })
        readonly property var mediaState: mediaService ? parseJson(mediaService.stateJson(), {
            "activePlayer": {
            },
            "compact": {
            }
        }) : ({
            "activePlayer": {
            },
            "compact": {
            }
        })
        readonly property var notificationsState: notificationService ? parseJson(notificationService.stateJson(), {
            "activeCount": 0,
            "unreadCount": 0
        }) : ({
            "activeCount": 0,
            "unreadCount": 0
        })
        readonly property var powerState: powerService ? parseJson(powerService.stateJson(), {
            "displayBattery": {
            },
            "activeProfile": ""
        }) : ({
            "displayBattery": {
            },
            "activeProfile": ""
        })
        readonly property string source: "Quickshell lockscreen LockSurface.qml LockContext.qml plus Nuguland lock Content.qml PasswordInput.qml NotifGroup.qml"
        readonly property bool nativeSessionLockSurface: true
        readonly property bool centerPasswordRail: true
        readonly property bool safeInfoRails: true
        readonly property bool secretInputField: true
        readonly property bool failureShakeSlot: true
        readonly property bool cleanTeardownState: true
        readonly property bool serviceFedOnly: true

        function parseJson(text, valueWhenInvalid) {
            try {
                return JSON.parse(String(text));
            } catch (error) {
                return valueWhenInvalid;
            }
        }

        function refreshFrom(text) {
            lockState = parseJson(text, lockState);
        }

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

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

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

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

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

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

            return theme.ui.accent;
        }

        color: theme ? theme.ui.backdrop : "black"

        Rectangle {
            anchors.fill: parent
            anchors.margins: 28
            radius: 22
            color: root.theme ? root.theme.withAlpha(root.theme.ui.backdrop, 0.92) : "transparent"
            border.width: 1
            border.color: root.theme ? root.theme.ui.accent : "transparent"
        }

        Row {
            anchors.fill: parent
            anchors.margins: 48
            spacing: 28

            Column {
                objectName: "safeInfoRails"
                width: Math.max(220, Math.floor((parent.width - 360 - parent.spacing * 2) / 2))
                anchors.verticalCenter: parent.verticalCenter
                spacing: 12

                NuguText {
                    theme: root.theme
                    variant: "meta"
                    text: "Session locked"
                    width: parent.width
                    wrapMode: Text.WordWrap
                }

                Rectangle {
                    width: parent.width
                    height: 1
                    color: root.theme ? root.theme.ui.accent : "transparent"
                }

                NuguText {
                    theme: root.theme
                    variant: "title"
                    text: root.mediaState.activePlayer ? root.mediaState.activePlayer.title || "media idle" : "media idle"
                    width: parent.width
                    elide: Text.ElideRight
                }

                NuguText {
                    theme: root.theme
                    text: root.lockState.safeMedia && root.lockState.safeMedia.transportOnly ? "transport-only media · bodies hidden" : "safe media"
                    width: parent.width
                    elide: Text.ElideRight
                }

                Rectangle {
                    width: parent.width
                    height: 52
                    radius: 14
                    color: root.theme ? root.theme.withAlpha(root.theme.ui.accent, 0.12) : "transparent"
                    border.width: 1
                    border.color: root.theme ? root.theme.ui.accent : "transparent"

                    NuguText {
                        anchors.centerIn: parent
                        theme: root.theme
                        variant: "meta"
                        text: "previous · play/pause · next"
                    }

                }

            }

            Column {
                objectName: "centerPasswordRail"
                width: 360
                anchors.verticalCenter: parent.verticalCenter
                spacing: 14

                NuguText {
                    theme: root.theme
                    variant: "title"
                    text: root.timeState.clock ? root.timeState.clock.display || "--:--" : "--:--"
                    width: parent.width
                    horizontalAlignment: Text.AlignHCenter
                }

                NuguText {
                    theme: root.theme
                    text: root.timeState.date ? root.timeState.date.line || "locked" : "locked"
                    width: parent.width
                    horizontalAlignment: Text.AlignHCenter
                }

                FailureShake {
                    objectName: "failureShakeSlot"
                    active: root.lockState.failure && root.lockState.failure.length > 0
                    width: parent.width
                    height: inputCard.height

                    Rectangle {
                        id: inputCard

                        objectName: "secretInputField"
                        width: parent.width
                        height: 46
                        radius: 23
                        color: root.theme ? root.theme.withAlpha(root.lockState.failure ? root.theme.error : root.theme.ui.focus, root.lockState.failure ? 0.18 : 0.14) : "transparent"
                        border.width: 1
                        border.color: root.theme ? (root.lockState.failure ? root.theme.error : root.theme.ui.focus) : "transparent"

                        NuguText {
                            anchors.left: parent.left
                            anchors.leftMargin: 14
                            anchors.verticalCenter: parent.verticalCenter
                            theme: root.theme
                            variant: "meta"
                            text: root.lockState.unlocking ? "unlocking" : "lock"
                            opacity: 0.72
                        }

                        TextInput {
                            id: secretInput

                            anchors.left: parent.left
                            anchors.leftMargin: 90
                            anchors.right: submitButton.left
                            anchors.rightMargin: 10
                            anchors.verticalCenter: parent.verticalCenter
                            color: root.theme ? root.theme.content.normal : "white"
                            selectionColor: root.theme ? root.theme.ui.focus : "white"
                            selectedTextColor: root.theme ? root.theme.content.backdrop : "black"
                            font.family: "IBM Plex Mono"
                            echoMode: TextInput.Password
                            inputMethodHints: Qt.ImhSensitiveData
                            enabled: !root.lockState.unlocking
                            focus: root.lockState.active
                            onActiveFocusChanged: {
                                if (!activeFocus && root.lockState.active)
                                    forceActiveFocus();

                            }
                            onTextChanged: {
                                if (root.lockService)
                                    root.refreshFrom(root.lockService.typeSecret(text));

                            }
                            Keys.onReturnPressed: (event) => {
                                event.accepted = true;
                                if (root.lockService) {
                                    root.refreshFrom(root.lockService.submit(text));
                                    text = "";
                                }
                            }
                            Keys.onEnterPressed: (event) => {
                                event.accepted = true;
                                if (root.lockService) {
                                    root.refreshFrom(root.lockService.submit(text));
                                    text = "";
                                }
                            }
                        }

                        Rectangle {
                            id: submitButton

                            anchors.right: parent.right
                            anchors.rightMargin: 6
                            anchors.verticalCenter: parent.verticalCenter
                            width: 34
                            height: 34
                            radius: 17
                            color: root.theme ? root.theme.ui.accent : "white"
                            opacity: secretInput.text.length > 0 ? 1 : 0.46

                            NuguText {
                                anchors.centerIn: parent
                                theme: root.theme
                                variant: "meta"
                                selected: true
                                text: "→"
                            }

                            MouseArea {
                                anchors.fill: parent
                                enabled: secretInput.text.length > 0
                                onClicked: {
                                    root.refreshFrom(root.lockService.submit(secretInput.text));
                                    secretInput.text = "";
                                }
                            }

                        }

                    }

                }

                NuguText {
                    theme: root.theme
                    variant: "meta"
                    text: root.lockState.failure || (root.lockState.teardownClean ? "cleanTeardownState" : "session locked")
                    width: parent.width
                    horizontalAlignment: Text.AlignHCenter
                    color: root.lockState.failure ? root.roleColor("error") : (root.theme ? root.theme.content.normal : "white")
                }

            }

            Column {
                width: Math.max(220, Math.floor((parent.width - 360 - parent.spacing * 2) / 2))
                anchors.verticalCenter: parent.verticalCenter
                spacing: 12

                NuguText {
                    theme: root.theme
                    variant: "title"
                    text: "private"
                    width: parent.width
                    elide: Text.ElideRight
                }

                NuguText {
                    theme: root.theme
                    text: `${root.notificationsState.unreadCount || 0} unread · bodies hidden`
                    width: parent.width
                    elide: Text.ElideRight
                }

                Rectangle {
                    width: parent.width
                    height: 52
                    radius: 14
                    color: root.theme ? root.theme.withAlpha(root.theme.ui.minor, 0.1) : "transparent"
                    border.width: 1
                    border.color: root.theme ? root.theme.ui.minor : "transparent"

                    NuguText {
                        anchors.centerIn: parent
                        theme: root.theme
                        variant: "meta"
                        text: root.powerState.displayBattery ? `battery ${root.powerState.displayBattery.percent || 0}%` : "battery"
                    }

                }

                NuguText {
                    theme: root.theme
                    variant: "meta"
                    text: "no notification bodies · no secrets serialized"
                    width: parent.width
                    wrapMode: Text.WordWrap
                }

            }

        }

    }

}