Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/test-fixtures/ui/niri-integration.qml

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

Scope {
    id: root

    property var readiness: []
    property string opened: ""
    // The overlaid vault double records the focused application at each opening;
    // production Vault has no `context` or `opens`.
    readonly property var vaultDouble: featureRegistry.vault
    property bool pasteFixtureVisible: false

    function recordReadiness(): void {
        root.readiness = root.readiness.concat([
            {
                ready: niri.ready,
                windows: niri.windows.length,
                title: niri.focusedTitle
            }
        ]);
    }

    function tree(item: var): var {
        let items = [item];
        for (const child of item.children || [])
            items = items.concat(tree(child));
        return items;
    }

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

    // Screenshot reports from niri; ui-test waits for each before going on.
    property int notices: 0
    NotificationServer {
        onNotification: root.notices++
    }

    N.Theme {
        id: fixtureTheme
    }
    // Clipboard is a native feature here (niri-integration.mjs): its production
    // service gets the inputs shell.qml binds.
    N.Registry {
        id: featureRegistry
        theme: fixtureTheme
        surfaces: surfaceManager
        niriService: niri
    }
    N.Niri {
        id: niri
        outputPollMs: 250
        onReadyChanged: Qt.callLater(root.recordReadiness)
    }
    FloatingWindow {
        id: pasteWindow
        visible: root.pasteFixtureVisible
        title: "paste destination"
        implicitWidth: 480
        implicitHeight: 240
        color: fixtureTheme.content.backdrop
        TextInput {
            id: pasteInput
            anchors.fill: parent
            anchors.margins: 20
            color: fixtureTheme.content.normal
            focus: true
            wrapMode: TextInput.Wrap
        }
    }
    N.Usage {
        id: usage
        widgets: featureRegistry.chips
        snapshotted: true
        layoutOrder: ["vault", "clipboard"]
    }
    N.SurfaceManager {
        id: surfaceManager
        niriService: niri
        registry: featureRegistry
        // Records the Bar's request; the vault double and production clipboard open
        // as real popups, so the host's opening signal reaches the vault.
        function openRegisteredPopup(name) {
            root.opened = String(name);
            openPopup(name, Qt.rect(0, 0, 34, 34), "");
        }
    }
    N.Bar {
        id: bar
        theme: fixtureTheme
        niriService: niri
        usageService: usage
        surfaces: surfaceManager
        orientation: "horizontal"
        registry: featureRegistry
        privilegeService: null
    }
    PanelWindow {
        id: history
        anchors {
            bottom: true
            left: true
            right: true
        }
        implicitHeight: 260
        color: fixtureTheme.content.backdrop
        WlrLayershell.layer: WlrLayer.Top
        WlrLayershell.keyboardFocus: WlrKeyboardFocus.None
        WlrLayershell.namespace: "niri-integration-history"

        Column {
            id: rows
            x: 18
            y: 12
            spacing: 6

            Text {
                text: "native niri integration · " + (niri.ready ? "ready" : "disconnected")
                color: fixtureTheme.ui.accent
                font.pixelSize: 18
            }
            Text {
                text: "focus: " + niri.focusedTitle + " · " + niri.focusedAppId
                color: fixtureTheme.content.normal
            }
            Text {
                text: "vault context: " + root.vaultDouble.context
                color: fixtureTheme.content.normal
            }
        }
    }
    IpcHandler {
        target: "uiTest"

        function snapshot(): string {
            const subscription = Array.from(niri.data).find(child => child.command !== undefined && Array.from(child.command).includes("event-stream"));
            const pills = root.tree(bar.contentItem).filter(child => child.visible && child.idxStr !== undefined).map(child => ({
                        index: child.idxStr,
                        name: child.nameStr,
                        active: child.active,
                        rect: root.rect(child)
                    }));
            const vaultChip = root.tree(bar.contentItem).find(child => child.visible && child.popupName === "vault");
            const clipChip = root.tree(bar.contentItem).find(child => child.visible && child.popupName === "clipboard");
            const popup = Array.from(bar.data).find(child => child.activeName !== undefined);
            const popupItems = popup ? root.tree(popup.contentItem) : [];
            const search = popupItems.find(item => item.objectName === "searchField");
            const paste = popupItems.find(item => item.objectName === "clipboardPaste");
            let pasteRect = null;
            if (paste) {
                const point = paste.mapToItem(popup.contentItem, 0, 0);
                pasteRect = {
                    x: point.x,
                    y: point.y,
                    width: paste.width,
                    height: paste.height
                };
            }
            return JSON.stringify({
                notices: root.notices,
                niri: JSON.parse(niri.stateJson()),
                subscriptionPid: subscription ? subscription.processId : null,
                qmlPid: Quickshell.processId,
                readiness: root.readiness,
                pills: pills,
                vault: {
                    app: root.vaultDouble.context,
                    opens: root.vaultDouble.opens,
                    rect: vaultChip ? root.rect(vaultChip) : null,
                    opened: root.opened,
                    open: surfaceManager.popupOpen && surfaceManager.popupName === "vault"
                },
                paste: {
                    chip: clipChip ? root.rect(clipChip) : null,
                    button: pasteRect,
                    open: surfaceManager.popupOpen,
                    target: surfaceManager.returnWindowId,
                    searchFocused: !!search && search.input.activeFocus && !!popup.contentItem.Window.window && popup.contentItem.Window.window.active,
                    queryPreserved: surfaceManager.panelStates.clipboard.query === "focus-safe",
                    destinationMatches: pasteInput.text === "focus-safe paste",
                    destinationEmpty: pasteInput.text.length === 0,
                    destinationFocused: pasteInput.activeFocus,
                    pending: featureRegistry.clipboard.pendingPasteId.length > 0,
                    error: featureRegistry.clipboard.lastAction.error || ""
                },
                text: root.tree(rows).filter(child => child.visible && child.text !== undefined).map(child => child.text)
            });
        }

        function pasteFixture(): void {
            featureRegistry.clipboard.captureClipboard("focus-safe paste");
            root.pasteFixtureVisible = true;
        }

        function fixtureOutputs(enabled: bool): bool {
            const query = Array.from(niri.data).find(child => child.command !== undefined && Array.from(child.command).includes("outputs"));
            if (!query)
                return false;
            query.environment = {
                NIRI_SOCKET: enabled ? Quickshell.env("XDG_RUNTIME_DIR") + "/niri.integration.sock" : Quickshell.env("NIRI_SOCKET")
            };
            return true;
        }
    }
}