Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/features/audio/AudioPanel.qml

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

Flickable {
    id: root

    required property Theme theme
    required property Audio audioService
    readonly property var sinks: audioService ? audioService.sinks : []
    readonly property var sources: audioService ? audioService.sources : []
    readonly property bool hasDevices: audioService ? audioService.hasDevices : false
    readonly property bool serviceReady: audioService ? audioService.pipewireReady : true
    property string expandedSink: ""
    property string expandedSource: ""
    // Opening focuses the default output's volume slider (FeatureEntry.qml).
    property Item initialFocus: null

    // Quiet: the open panel already shows the new value, so no volume overlay.
    function setVolume(id, ratio) {
        if (audioService)
            audioService.setVolume(id, Math.max(0, Math.min(1.5, ratio)), true);
    }

    function toggleMute(id) {
        if (audioService)
            audioService.toggleMute(id, true);
    }

    function selectSink(id) {
        if (audioService)
            audioService.setDefaultSink(id);
    }

    function selectSource(id) {
        if (audioService)
            audioService.setDefaultSource(id);
    }

    width: parent ? parent.width : 0
    contentWidth: width
    contentHeight: audioCol.height
    // The card fits the content; the entry height caps it (FeatureEntry.qml).
    readonly property real naturalHeight: contentHeight
    clip: true
    interactive: contentHeight > height
    boundsBehavior: Flickable.StopAtBounds
    flickableDirection: Flickable.VerticalFlick

    component DeviceSection: Column {
        id: section

        required property bool input
        required property var rows
        readonly property string expanded: input ? root.expandedSource : root.expandedSink

        function expand(id) {
            const next = expanded === id ? "" : id;
            if (input)
                root.expandedSource = next;
            else
                root.expandedSink = next;
        }

        spacing: 6

        StableListModel {
            id: devices
            rows: section.rows
        }

        NuguText {
            theme: root.theme
            variant: "title"
            text: section.input ? "input" : "output"
        }

        NuguText {
            theme: root.theme
            variant: "meta"
            visible: devices.count === 0
            text: section.input ? "no input devices" : "no output devices"
        }

        Repeater {
            model: devices

            delegate: Column {
                id: deviceRow

                required property var rowData
                readonly property bool revealed: section.expanded === rowData.id
                // A node without a description must not read as alsa_output.pci-….
                readonly property string name: Human.label(rowData.name, section.input ? "input device" : "output device")

                width: section.width
                spacing: 4

                PopupCard {
                    theme: root.theme
                    padding: 0
                    active: deviceRow.rowData.isDefault
                    revealed: deviceRow.revealed
                    interactive: true
                    Accessible.name: `${deviceRow.name} options`
                    Accessible.description: deviceRow.rowData.isDefault ? "default device" : ""
                    tooltip: `${deviceRow.name}${deviceRow.rowData.isDefault ? "\ndefault device" : ""}\n${deviceRow.revealed ? "hide" : "show"} device options`
                    disclosure: true
                    onControlFocusedChanged: if (controlFocused)
                        Util.revealIn(this, root)
                    onClicked: section.expand(deviceRow.rowData.id)
                    width: parent.width
                    height: 48

                    // Mute sits on the row; the speaker or microphone glyph shows its state.
                    PopupButton {
                        id: deviceGlyph

                        anchors.left: parent.left
                        anchors.leftMargin: 6
                        anchors.verticalCenter: parent.verticalCenter
                        theme: root.theme
                        width: 32
                        height: 32
                        role: "ghost"
                        showText: false
                        icon: deviceRow.rowData.glyph
                        checked: deviceRow.rowData.muted
                        accessibleName: `${deviceRow.rowData.muted ? "unmute" : "mute"} ${deviceRow.name}`
                        tooltip: deviceRow.rowData.muted ? "unmute" : "mute"
                        onControlFocusedChanged: if (controlFocused)
                            Util.revealIn(this, root)
                        onClicked: root.toggleMute(deviceRow.rowData.id)
                    }

                    NuguText {
                        theme: root.theme
                        text: `${deviceRow.name}${deviceRow.rowData.isDefault ? " · default" : ""}`
                        anchors.left: deviceGlyph.right
                        anchors.leftMargin: 6
                        anchors.right: deviceTrack.left
                        anchors.rightMargin: 10
                        anchors.verticalCenter: parent.verticalCenter
                        elide: Text.ElideRight
                        opacity: deviceRow.rowData.muted ? 0.5 : 1
                    }

                    NuguText {
                        id: trailing

                        anchors.right: optionsChevron.left
                        anchors.rightMargin: 8
                        anchors.verticalCenter: parent.verticalCenter
                        theme: root.theme
                        variant: "meta"
                        width: 48
                        horizontalAlignment: Text.AlignRight
                        text: deviceRow.rowData.muted ? "muted" : `${deviceRow.rowData.percent}%`
                        color: root.theme.roleColor(deviceRow.rowData.muted ? "ui.minor" : deviceRow.rowData.role)
                    }

                    // The row opens its remaining options; the chevron says so without hover.
                    Text {
                        id: optionsChevron

                        anchors.right: parent.right
                        anchors.rightMargin: 12
                        anchors.verticalCenter: parent.verticalCenter
                        width: 16
                        horizontalAlignment: Text.AlignHCenter
                        font.family: root.theme.fontIcon
                        font.pixelSize: 16
                        color: root.theme.ui.minor
                        text: deviceRow.revealed ? "\uF106" : "\uF107"
                        Accessible.ignored: true
                    }

                    PopupSlider {
                        id: deviceTrack

                        anchors.right: trailing.left
                        anchors.rightMargin: 10
                        anchors.verticalCenter: parent.verticalCenter
                        theme: root.theme
                        width: 120
                        Accessible.name: `${deviceRow.name} ${section.input ? "input" : "output"} volume`
                        onActiveFocusChanged: if (activeFocus)
                            Util.revealIn(this, root)
                        value: deviceRow.rowData.muted ? 0 : deviceRow.rowData.percent / 150
                        onMoved: value => root.setVolume(deviceRow.rowData.id, value * 1.5)

                        Binding {
                            when: !section.input && deviceRow.rowData.isDefault
                            target: root
                            property: "initialFocus"
                            value: deviceTrack
                            restoreMode: Binding.RestoreNone
                        }
                    }
                }

                Row {
                    visible: deviceRow.revealed
                    width: parent.width
                    height: visible ? 34 : 0
                    spacing: 8

                    PopupButton {
                        theme: root.theme
                        text: deviceRow.rowData.isDefault ? "default" : "set default"
                        enabled: !deviceRow.rowData.isDefault
                        checked: deviceRow.rowData.isDefault
                        onControlFocusedChanged: if (controlFocused)
                            Util.revealIn(this, root)
                        onClicked: {
                            section.expand(deviceRow.rowData.id);
                            if (section.input)
                                root.selectSource(deviceRow.rowData.id);
                            else
                                root.selectSink(deviceRow.rowData.id);
                        }
                    }
                }
            }
        }
    }

    Column {
        id: audioCol

        width: root.width
        spacing: 12

        DeviceSection {
            objectName: "outputSection"
            visible: root.hasDevices
            width: parent.width
            input: false
            rows: root.sinks
        }

        DeviceSection {
            objectName: "inputSection"
            visible: root.hasDevices
            width: parent.width
            input: true
            rows: root.sources
        }

        Item {
            objectName: "audioEmptyState"
            visible: !root.hasDevices
            width: parent.width
            height: 56

            Text {
                anchors.left: parent.left
                anchors.verticalCenter: parent.verticalCenter
                font.family: root.theme.fontIcon
                font.pixelSize: 26
                color: root.theme.ui.minor
                text: "\u{F0581}"
                Accessible.ignored: true
            }

            Column {
                anchors.left: parent.left
                anchors.leftMargin: 40
                anchors.right: parent.right
                anchors.verticalCenter: parent.verticalCenter
                spacing: 3

                NuguText {
                    theme: root.theme
                    variant: "title"
                    text: root.serviceReady ? "no audio devices" : "audio service not running"
                }

                NuguText {
                    width: parent.width
                    theme: root.theme
                    variant: "meta"
                    text: root.serviceReady ? "connect speakers or a microphone" : "start pipewire to control sound"
                    color: root.theme.ui.minor
                    wrapMode: Text.WordWrap
                }
            }
        }
    }
}