repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nugu/ClipboardPicker.qml

Raw
import QtQuick
import Quickshell
import Quickshell.Io
import Quickshell.Wayland

PanelWindow {
    id: root

    required property QtObject theme
    required property QtObject commands
    property bool open: false
    property string query: ""
    property var entries: []

    // This replaces `cliphist list | hyprlauncher --dmenu | cliphist decode | wl-copy`.
    // Storage deliberately remains `cliphist`; only the picker frontend moves to QML.
    visible: open
    focusable: open
    anchors { top: true; left: true; right: true }
    margins { top: 62 }
    exclusiveZone: -1
    implicitHeight: 390
    color: "transparent"

    WlrLayershell {
        layer: WlrLayer.Overlay
        namespace: "nugu-clipboard-picker"
        keyboardFocus: root.open ? WlrKeyboardFocus.Exclusive : WlrKeyboardFocus.None
    }

    ScriptModel { id: matches }

    onQueryChanged: rebuild()
    onOpenChanged: {
        if (open) {
            refresh()
            Qt.callLater(search.forceActiveFocus)
        }
    }

    function show(initialQuery: string): string {
        query = initialQuery
        open = true
        return query
    }

    function close(): void {
        open = false
        query = ""
    }

    function matchCount(): int { return matches.values.length }
    function firstMatchPreview(): string { return matches.values.length > 0 ? matches.values[0].preview : "" }

    function refresh(): void {
        list.exec(["sh", "-c", "cliphist list | head -n 80"])
    }

    function rebuild(): void {
        const needle = query.toLowerCase().trim()
        matches.values = entries
            .filter(entry => needle.length === 0 || entry.search.indexOf(needle) !== -1)
            .slice(0, 16)
        results.currentIndex = matches.values.length > 0 ? 0 : -1
    }

    function chooseIndex(index: int): string {
        if (index < 0 || index >= matches.values.length) return ""
        const entry = matches.values[index]
        commands.copyClipboardEntry(entry.encoded, entry.preview)
        close()
        return entry.preview
    }

    function parseLine(line: string): var {
        const tab = line.indexOf("\t")
        const encoded = line
        const raw = tab >= 0 ? line.slice(tab + 1) : line
        const compact = raw.replace(/\s+/g, " ").trim()
        const preview = compact.length > 80 ? `${compact.slice(0, 77)}…` : compact
        return { "encoded": encoded, "preview": preview, "search": compact.toLowerCase() }
    }

    MouseArea {
        anchors.fill: parent
        onClicked: root.close()
    }

    Rectangle {
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter
        width: Math.min(parent.width - 32, 720)
        height: parent.height
        radius: 14
        color: `${theme.ui.backdrop}f2`
        border.width: 2
        border.color: theme.ui.accent

        Column {
            anchors.fill: parent
            anchors.margins: 14
            spacing: 10

            TextInput {
                id: search
                width: parent.width
                height: 42
                text: root.query
                onTextChanged: root.query = text
                color: theme.ui.important_global
                selectionColor: theme.ui.focus
                selectedTextColor: theme.content.backdrop
                font.family: "IBM Plex Mono"
                font.pixelSize: 18
                font.bold: true
                clip: true

                Rectangle {
                    anchors.fill: parent
                    anchors.margins: -6
                    z: -1
                    radius: 9
                    color: theme.content.backdrop
                    border.width: 1
                    border.color: theme.ui.unfocus
                }

                Keys.onPressed: event => {
                    if (event.key === Qt.Key_Escape) {
                        root.close()
                        event.accepted = true
                    } else if (event.key === Qt.Key_Down) {
                        results.currentIndex = Math.min(results.currentIndex + 1, matches.values.length - 1)
                        event.accepted = true
                    } else if (event.key === Qt.Key_Up) {
                        results.currentIndex = Math.max(results.currentIndex - 1, 0)
                        event.accepted = true
                    } else if (event.key === Qt.Key_Return || event.key === Qt.Key_Enter) {
                        root.chooseIndex(results.currentIndex)
                        event.accepted = true
                    }
                }
            }

            ListView {
                id: results
                width: parent.width
                height: parent.height - search.height - parent.spacing
                model: matches
                clip: true
                spacing: 4

                delegate: Rectangle {
                    id: row
                    required property var modelData
                    required property int index

                    width: results.width
                    height: 34
                    radius: 8
                    color: results.currentIndex === index ? theme.ui.focus : (rowMouse.containsMouse ? theme.ui.accent : "transparent")
                    clip: true

                    MouseArea {
                        id: rowMouse
                        anchors.fill: parent
                        hoverEnabled: true
                        onClicked: root.chooseIndex(row.index)
                    }

                    Text {
                        anchors.left: parent.left
                        anchors.right: parent.right
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.leftMargin: 10
                        anchors.rightMargin: 10
                        text: row.modelData.preview
                        color: (results.currentIndex === row.index || rowMouse.containsMouse) ? theme.content.backdrop : theme.ui.normal
                        font.family: "IBM Plex Mono"
                        font.pixelSize: 13
                        elide: Text.ElideRight
                    }
                }
            }

            Text {
                visible: matches.values.length === 0
                text: root.entries.length === 0 ? "clipboard empty" : "no clipboard match"
                color: theme.ui.minor
                font.family: "IBM Plex Mono"
                font.pixelSize: 13
            }
        }
    }

    Process {
        id: list
        stdout: StdioCollector {
            onStreamFinished: {
                const text = this.text.trim()
                root.entries = text.length === 0 ? [] : text.split("\n").map(line => root.parseLine(line))
                root.rebuild()
            }
        }
    }
}