Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/features/noko/NokoPanel.qml

Raw
pragma ComponentBehavior: Bound
import QtQuick
import "../.."
import "../../Util.js" as Util

Column {
    id: root

    required property Theme theme
    readonly property Flickable scrollView: historyFlick
    required property Noko nokoService
    property int waveTick: 0
    property int copiedIndex: -1
    // Clearing discards history for good: the first press arms, the second clears,
    // and leaving the button disarms.
    property bool clearArmed: false
    signal configureRequested
    // Stopping from here hands the keyboard back, so the text lands where it was.
    signal closeRequested(string reason)
    readonly property Item initialFocus: nokoService.ready && !nokoService.installing ? captureButton : configure
    readonly property real naturalHeight: historyCard.y + historyCard.naturalHeight

    // How much speech a transcript holds, e.g. "2 s of speech".
    function speechLength(entry) {
        return entry.duration !== undefined ? `${Math.max(1, Math.round(Number(entry.duration)))} s of speech` : "local history";
    }

    function waveLevel(index) {
        // Low-power mode holds a still wave.
        const phase = ((theme.lowPowerMode ? 0 : waveTick) + index * 5) / (nokoService.listening ? 4 : 7);
        return 0.24 + Math.abs(Math.sin(phase)) * (nokoService.listening ? 0.7 : 0.42);
    }

    width: parent ? parent.width : 0
    height: parent ? parent.height : implicitHeight
    spacing: theme.spaceMd

    Timer {
        interval: 90
        repeat: true
        running: root.nokoService.dictating && !root.theme.lowPowerMode
        onTriggered: root.waveTick++
    }

    Timer {
        id: copyFeedbackTimer

        interval: 1200
        onTriggered: root.copiedIndex = -1
    }

    NuguText {
        objectName: "nokoStatusLine"
        theme: root.theme
        variant: "meta"
        text: root.nokoService.starting ? "starting" : (root.nokoService.listening ? "capturing" : (root.nokoService.transcribing ? "transcribing" : (root.nokoService.installing ? "setup running" : (root.nokoService.ready ? (root.nokoService.wakeListening ? "ready · wake word on" : "ready") : "setup needed"))))
    }

    NuguText {
        objectName: "nokoCaptureProblem"
        visible: root.nokoService.captureError.length > 0
        theme: root.theme
        text: root.nokoService.captureError + " · try again"
        color: root.theme.warning
        width: parent.width
        wrapMode: Text.Wrap
    }

    NuguText {
        objectName: "nokoTypingProblem"
        visible: root.nokoService.lastInsertError.length > 0
        theme: root.theme
        text: "typing failed · copy it below"
        color: root.theme.warning
        width: parent.width
        wrapMode: Text.Wrap
    }

    // The primary task: capture leads; below it, only while work runs, its wave
    // tells capturing from starting and transcribing, and the newest words show.
    Column {
        width: parent.width
        spacing: root.theme.spaceSm

        PopupButton {
            id: captureButton

            theme: root.theme
            text: root.nokoService.starting || root.nokoService.listening ? "stop capture" : (root.nokoService.transcribing ? "transcribing…" : "start capture")
            // Primary only while it can act: missing setup leaves its action the one
            // primary, and a waiting capture reads as a neutral, legible status.
            role: enabled ? "primary" : "normal"
            enabled: root.nokoService.ready && !root.nokoService.transcribing && !root.nokoService.installing
            width: parent.width
            onClicked: {
                const stopping = root.nokoService.starting || root.nokoService.listening;
                root.nokoService.trigger();
                if (stopping)
                    root.closeRequested("capture-stopped");
            }
        }

        PopupCard {
            objectName: "nokoWave"
            visible: root.nokoService.dictating
            theme: root.theme
            padding: 0
            active: root.nokoService.listening
            width: parent.width
            height: 58

            Row {
                anchors.centerIn: parent
                spacing: 5
                height: 36

                Repeater {
                    model: 31

                    Rectangle {
                        required property int index

                        anchors.verticalCenter: parent.verticalCenter
                        width: 4
                        height: Math.max(5, parent.height * root.waveLevel(index))
                        radius: width / 2
                        color: root.nokoService.listening ? root.theme.ui.accent : root.theme.ui.minor
                        opacity: 0.86

                        Behavior on height {
                            NumberAnimation {
                                duration: root.theme.lowPowerMode ? 0 : 120
                                easing.type: Easing.OutCubic
                            }
                        }
                    }
                }
            }
        }
    }

    // The newest words of the running dictation, eliding at the start.
    NuguText {
        objectName: "nokoLiveText"
        visible: root.nokoService.dictating && root.nokoService.liveText.length > 0
        theme: root.theme
        width: parent.width
        userText: true
        elide: Text.ElideLeft
        text: root.nokoService.liveText
    }

    PopupButton {
        id: configure
        // Setup that needs nothing stays in Config, out of the way of dictation.
        visible: !root.nokoService.ready || root.nokoService.installing
        theme: root.theme
        text: "configure noko"
        onClicked: root.configureRequested()
    }

    PopupCard {
        id: historyCard

        // Every transcript without scrolling; a tight popup still shows 150 px of it.
        readonly property real naturalHeight: 24 + historyHead.height + 7 + historyFlick.contentHeight

        theme: root.theme
        padding: 0
        width: parent.width
        height: Math.max(Math.min(150, naturalHeight), root.height - y)

        Column {
            anchors.fill: parent
            anchors.margins: 12
            spacing: 7

            Item {
                id: historyHead

                width: parent.width
                height: Math.max(historyTitle.height, clearButton.visible ? clearButton.height : 0)

                NuguText {
                    id: historyTitle

                    anchors.verticalCenter: parent.verticalCenter
                    theme: root.theme
                    variant: "title"
                    font.pixelSize: root.theme.textMeta
                    text: "last transcripts"
                }

                PopupButton {
                    id: clearButton

                    objectName: "clearHistory"
                    visible: root.nokoService.historyEntries.length > 0
                    anchors.right: parent.right
                    anchors.verticalCenter: parent.verticalCenter
                    theme: root.theme
                    height: 26
                    text: root.clearArmed ? "confirm clear" : "clear history"
                    role: root.clearArmed ? "destructive" : "normal"
                    onControlFocusedChanged: if (!controlFocused)
                        root.clearArmed = false
                    onClicked: {
                        if (root.clearArmed)
                            root.nokoService.clearHistory();
                        root.clearArmed = !root.clearArmed;
                    }
                }
            }

            Item {
                width: parent.width
                height: Math.max(1, parent.height - y)
                clip: true

                Flickable {
                    id: historyFlick

                    objectName: "nokoHistory"
                    anchors.fill: parent
                    anchors.rightMargin: contentHeight > height ? 8 : 0
                    clip: true
                    contentWidth: width
                    contentHeight: historyColumn.implicitHeight
                    boundsBehavior: Flickable.StopAtBounds
                    interactive: contentHeight > height

                    Column {
                        id: historyColumn
                        width: historyFlick.width
                        spacing: 7

                        Repeater {
                            model: root.nokoService.historyEntries

                            delegate: PopupCard {
                                id: transcriptEntry

                                required property int index
                                required property var modelData
                                readonly property string transcript: String(modelData.transcript || "")
                                readonly property bool copied: root.copiedIndex === index

                                objectName: `transcript${index}`
                                theme: root.theme
                                padding: 0
                                width: historyColumn.width
                                height: entryColumn.implicitHeight + 10

                                Column {
                                    id: entryColumn

                                    anchors.left: parent.left
                                    anchors.right: copyButton.left
                                    anchors.top: parent.top
                                    anchors.margins: 8
                                    spacing: 3

                                    NuguText {
                                        theme: root.theme
                                        width: parent.width
                                        wrapMode: Text.WordWrap
                                        userText: true
                                        text: transcriptEntry.transcript
                                        color: root.theme.content.normal
                                    }

                                    NuguText {
                                        theme: root.theme
                                        variant: "meta"
                                        width: parent.width
                                        text: root.speechLength(transcriptEntry.modelData)
                                        color: root.theme.ui.minor
                                    }
                                }

                                PopupButton {
                                    id: copyButton

                                    objectName: `copyTranscript${transcriptEntry.index}`
                                    anchors.right: parent.right
                                    anchors.top: parent.top
                                    anchors.margins: 8
                                    theme: root.theme
                                    width: 58
                                    height: 26
                                    text: transcriptEntry.copied ? "copied" : "copy"
                                    checked: transcriptEntry.copied
                                    onControlFocusedChanged: if (controlFocused)
                                        Util.revealIn(transcriptEntry, historyFlick)
                                    onClicked: if (transcriptEntry.transcript.length > 0) {
                                        root.nokoService.copyTranscript(transcriptEntry.transcript);
                                        root.copiedIndex = transcriptEntry.index;
                                        copyFeedbackTimer.restart();
                                    }
                                }
                            }
                        }

                        NuguText {
                            visible: root.nokoService.historyEntries.length === 0
                            theme: root.theme
                            width: parent.width
                            text: "no captures yet"
                            color: root.theme.content.normal
                        }
                    }
                }

                Rectangle {
                    visible: historyFlick.contentHeight > historyFlick.height
                    anchors.top: parent.top
                    anchors.bottom: parent.bottom
                    anchors.right: parent.right
                    width: 3
                    radius: width / 2
                    color: root.theme.withAlpha(root.theme.ui.minor, 0.14)

                    Rectangle {
                        width: parent.width
                        height: Math.max(18, parent.height * historyFlick["visibleArea"]["heightRatio"])
                        y: Math.max(0, Math.min(parent.height - height, parent.height * historyFlick["visibleArea"]["yPosition"]))
                        radius: parent.radius
                        color: root.theme.withAlpha(root.theme.ui.accent, 0.55)
                    }
                }
            }
        }
    }
}