Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/test-fixtures/ui/lifecycle.qml

Raw
pragma ComponentBehavior: Bound
import QtQuick
import Quickshell
import Quickshell.Io
import Quickshell.Services.Notifications
import Quickshell.Wayland
import "../.." as N

Scope {
    id: root

    // The overlaid double's fixture surface; production Resources has no gate counters.
    readonly property var resourcesDouble: featureRegistry.system

    // Walk visual QObject children, not production-only testing hooks.
    function tree(item: var): var {
        let items = [item];
        const children = item.children || [];
        for (let i = 0; i < children.length; i++)
            items = items.concat(tree(children[i]));
        return items;
    }

    function find(item: var, predicate: var): var {
        return tree(item).find(predicate) || null;
    }

    function rect(item: var, window: var): var {
        if (!item)
            return null;
        const point = item.mapToItem(window.contentItem, 0, 0);
        return {
            x: point.x,
            y: point.y,
            width: item.width,
            height: item.height
        };
    }

    function textRect(item: var, window: var, text: string): var {
        return rect(find(item, function (child) {
            return child.visible && child.text === text;
        }), window);
    }

    function visibleText(item: var, window: var): var {
        return tree(item).filter(function (child) {
            // Never serialize editable text, including when reveal is enabled.
            return child.visible && child.font !== undefined && child.text !== undefined && child.echoMode === undefined && String(child.text).length > 0;
        }).map(function (child) {
            return {
                text: String(child.text),
                rect: rect(child, window)
            };
        });
    }

    N.Theme {
        id: fixtureTheme
    }
    N.Privilege {
        id: privilege
    }
    N.Niri {
        id: niri
    }
    N.Registry {
        id: featureRegistry
        theme: fixtureTheme
        surfaces: surfaceManager
    }
    N.Diagnostics {
        id: diagnosticsService
        surfaces: surfaceManager
        screens: Quickshell.screens
        niriService: niri
        audioService: featureRegistry.audio
        networkService: featureRegistry.connectivity
        vaultService: featureRegistry.vault
        nokoService: featureRegistry.noko
    }
    N.SurfaceManager {
        id: surfaceManager
        screenWidth: desktop.width
        screenHeight: desktop.height
        barHeight: 48
        registry: featureRegistry
    }
    // Screenshot reports from niri; ui-test waits for each before going on.
    property int notices: 0
    NotificationServer {
        onNotification: root.notices++
    }

    PanelWindow {
        id: desktop
        anchors {
            top: true
            left: true
            right: true
            bottom: true
        }
        exclusionMode: ExclusionMode.Ignore
        WlrLayershell.layer: WlrLayer.Bottom
        WlrLayershell.keyboardFocus: WlrKeyboardFocus.None
        color: fixtureTheme.content.backdrop

        Repeater {
            model: ["clock", "system"]
            N.PopupButton {
                required property int index
                required property string modelData
                theme: fixtureTheme
                text: modelData
                x: surfaceManager.vertical ? 2 : 8 + index * 110
                y: surfaceManager.vertical ? 8 + index * 48 : 4
                width: surfaceManager.vertical ? 44 : 100
                height: 38
                onClicked: {
                    if (surfaceManager.popupOpen && surfaceManager.popupName === modelData)
                        surfaceManager.closePopup("bar-toggle");
                    else
                        surfaceManager.openPopup(modelData, Qt.rect(0, 0, 48, 48), "");
                }
            }
        }
    }

    N.PopupHost {
        id: host
        theme: fixtureTheme
        surfaces: surfaceManager
        barWindow: desktop
        privilegeService: privilege
        registry: featureRegistry
    }

    PanelWindow {
        id: authWindow
        visible: privilege.active || privilege.confirming || privilege.error.length > 0
        anchors {
            top: true
            left: true
            right: true
            bottom: true
        }
        exclusionMode: ExclusionMode.Ignore
        WlrLayershell.layer: WlrLayer.Overlay
        WlrLayershell.keyboardFocus: WlrKeyboardFocus.Exclusive
        color: "transparent"
        onVisibleChanged: if (visible)
            Qt.callLater(auth.focusSecret)

        N.PrivilegeSurface {
            id: auth
            anchors.fill: parent
            theme: fixtureTheme
            privilegeService: privilege
        }
    }

    // var parameters keep synthetic-only state out of production type contracts.
    // Only external service/layout events; all user actions use real input.
    function externalEvent(event: string, calendar: var, privilege: var): void {
        switch (event) {
        case "horizontal":
        case "vertical":
            surfaceManager.orientation = event;
            break;
        case "stored":
            calendar.failSave = false;
            calendar.finishSave();
            break;
        case "events":
            calendar.updateEvents();
            break;
        case "privilege":
            privilege.begin();
            break;
        case "privilege-settle":
            privilege.settle();
            break;
        }
    }

    function state(calendar: var, privilege: var): string {
        // The open popup's registry panel, never a feature-specific property.
        const feature = featureRegistry.popup(surfaceManager.popupName);
        const panel = feature ? root.find(host.contentItem, function (item) {
            return item.parent && item.parent.sourceComponent === feature.panel;
        }) : null;
        const card = root.find(host.contentItem, function (item) {
            return item.padding === 0 && item.radius === 14;
        });
        const url = root.find(host.contentItem, function (item) {
            return item.input !== undefined && item.accessibleName === "server url";
        });
        const user = root.find(host.contentItem, function (item) {
            return item.input !== undefined && item.accessibleName === "username";
        });
        const input = root.find(auth, function (item) {
            return item.echoMode !== undefined;
        });
        const shake = root.find(auth, function (item) {
            return item.objectName === "failureShakeSlot";
        });
        const shakeContent = root.find(shake, function (item) {
            return item.spacing === 7;
        });
        const authCard = auth.children[0];
        const reveal = root.find(auth, item => item.clicked !== undefined && item.accessibleName === (auth.secretVisible ? "hide password" : "show password"));
        const cancel = root.find(auth, item => item.clicked !== undefined && item.accessibleName === "cancel");
        const authorize = root.find(auth, item => item.clicked !== undefined && item.objectName === "authorizeButton");
        return JSON.stringify({
            notices: root.notices,
            width: desktop.width,
            height: desktop.height,
            orientation: surfaceManager.orientation,
            popup: JSON.parse(surfaceManager.popupState()),
            card: root.rect(card, host),
            panel: root.rect(panel, host),
            text: host.visible ? root.visibleText(host.contentItem, host) : [],
            buttons: {
                clock: root.textRect(desktop.contentItem, desktop, "clock"),
                system: root.textRect(desktop.contentItem, desktop, "system")
            },
            calendar: {
                refreshes: calendar.refreshes,
                saves: calendar.saves,
                savedDrafts: calendar.savedDrafts,
                draftsEdited: calendar.setupUrlDraft === "https://fixture.invalid/dav" && calendar.setupUsernameDraft === "fixture-user",
                url: root.rect(url, host),
                user: root.rect(user, host),
                save: root.textRect(host.contentItem, host, "save calendar"),
                configure: root.textRect(host.contentItem, host, "configure calendar"),
                todayItems: panel && panel.todayItems ? panel.todayItems.length : 0
            },
            // The registry system double's polling gate, released on every switch and close.
            resources: {
                open: root.resourcesDouble.popupOpen,
                opens: root.resourcesDouble.opens,
                closes: root.resourcesDouble.closes
            },
            auth: {
                width: authWindow.width,
                height: authWindow.height,
                visible: authWindow.visible,
                active: privilege.active,
                confirming: privilege.confirming,
                error: privilege.error,
                checking: auth.checking,
                inputReadOnly: input ? input.readOnly : false,
                authorizeEnabled: !!authorize && authorize.enabled && authorize.interactive,
                authorizeLabel: authorize ? authorize.accessibleName : "",
                submissions: privilege.submissions,
                cancellations: privilege.cancellations,
                receivedLiteral: privilege.receivedLiteral,
                revealed: auth.secretVisible,
                inputFocused: input ? input.activeFocus : false,
                hasInput: input ? input.text.length > 0 : false,
                inputMasked: input ? input.echoMode === TextInput.Password : false,
                input: root.rect(input, authWindow),
                card: root.rect(authCard, authWindow),
                shake: root.rect(shake, authWindow),
                shakeContentHeight: shakeContent ? shakeContent.implicitHeight : 0,
                reveal: root.rect(reveal, authWindow),
                cancel: root.rect(cancel, authWindow),
                revealFocused: !!reveal && reveal.controlFocused,
                cancelFocused: !!cancel && cancel.controlFocused,
                authorizeFocused: !!authorize && authorize.controlFocused,
                text: authWindow.visible ? root.visibleText(auth, authWindow) : []
            }
        });
    }

    IpcHandler {
        target: "nuguland"

        function diagnostics(): string {
            return diagnosticsService.snapshot();
        }
    }

    IpcHandler {
        target: "uiTest"

        function external(event: string): void {
            root.externalEvent(event, featureRegistry.clock, privilege);
        }

        function snapshot(): string {
            return root.state(featureRegistry.clock, privilege);
        }
    }
}