repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nuguland/AudioPanel.qml

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

Column {
    id: root

    required property Theme theme
    required property Audio audioService
    property var panelState: audioService ? Util.parseJson(audioService.stateJson(), {
        "sinks": [],
        "sources": [],
        "streams": [],
        "defaults": {}
    }) : ({
            "sinks": [],
            "sources": [],
            "streams": [],
            "defaults": {}
        })
    readonly property var sinks: panelState.sinks || []
    readonly property var sources: panelState.sources || []
    readonly property var streams: panelState.streams || []
    // device id whose options drawer is open ("" = none), per section.
    property string expandedSink: ""
    property string expandedSource: ""

    function refreshFrom(text) {
        panelState = Util.parseJson(text, panelState);
    }

    function setVolume(id, ratio) {
        if (audioService)
            refreshFrom(audioService.setVolume(id, Math.max(0, Math.min(1.5, ratio))));
    }

    function toggleMute(id) {
        if (audioService)
            refreshFrom(audioService.toggleMute(id));
    }

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

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

    function roleColor(role) {
        if (!theme)
            return "white";

        if (role === "warning")
            return theme.warning;

        if (role === "error")
            return theme.error;

        if (role === "ui.minor")
            return theme.ui.minor;

        return theme.ui.accent;
    }

    spacing: 10
    width: parent ? parent.width : 0

    // One scroll pane for the whole panel so device option drawers grow
    // without clipping and a long device list scrolls. Idle (no overflow)
    // => not interactive.
    Flickable {
        width: parent.width
        height: root.height
        contentWidth: width
        contentHeight: audioCol.height
        clip: true
        interactive: contentHeight > height
        boundsBehavior: Flickable.StopAtBounds
        flickableDirection: Flickable.VerticalFlick

        Column {
            id: audioCol

            width: parent.width
            spacing: 10

            // output devices (sinks). shared cards carry default/revealed state.
            Column {
                objectName: "outputSection"
                width: parent.width
                spacing: 6

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

                Column {
                    width: parent.width
                    spacing: 6

                    Repeater {
                        model: root.sinks

                        delegate: Column {
                            id: sinkRow

                            required property var modelData
                            readonly property bool revealed: root.expandedSink === modelData.id

                            width: parent.width
                            spacing: 4

                            PopupCard {
                                theme: root.theme
                                padding: 0
                                active: sinkRow.modelData.isDefault
                                revealed: sinkRow.revealed
                                hoverable: true
                                externallyPressed: sinkArea.pressed
                                width: parent.width
                                height: 48

                                MouseArea {
                                    id: sinkArea
                                    anchors.fill: parent
                                    hoverEnabled: true
                                    cursorShape: Qt.PointingHandCursor
                                    onClicked: root.expandedSink = sinkRow.revealed ? "" : sinkRow.modelData.id
                                }

                                Text {
                                    id: sinkGlyph

                                    anchors.left: parent.left
                                    anchors.leftMargin: 12
                                    anchors.verticalCenter: parent.verticalCenter
                                    font.family: "Symbols Nerd Font Mono"
                                    font.pixelSize: 18
                                    text: sinkRow.modelData.glyph
                                    color: root.roleColor(sinkRow.modelData.role)
                                    opacity: sinkRow.modelData.muted ? 0.5 : 1
                                }

                                NuguText {
                                    theme: root.theme
                                    text: sinkRow.modelData.name
                                    anchors.left: sinkGlyph.right
                                    anchors.leftMargin: 10
                                    anchors.right: sinkTrack.left
                                    anchors.rightMargin: 10
                                    anchors.verticalCenter: parent.verticalCenter
                                    elide: Text.ElideRight
                                    opacity: sinkRow.modelData.muted ? 0.5 : 1
                                }

                                NuguText {
                                    id: sinkTrailing

                                    anchors.right: parent.right
                                    anchors.rightMargin: 12
                                    anchors.verticalCenter: parent.verticalCenter
                                    theme: root.theme
                                    variant: "meta"
                                    text: sinkRow.modelData.muted ? "muted" : `${sinkRow.modelData.percent}%`
                                    color: root.roleColor(sinkRow.modelData.muted ? "ui.minor" : sinkRow.modelData.role)
                                }

                                PopupSlider {
                                    id: sinkTrack

                                    anchors.right: sinkTrailing.left
                                    anchors.rightMargin: 10
                                    anchors.verticalCenter: parent.verticalCenter
                                    theme: root.theme
                                    width: 96
                                    value: sinkRow.modelData.muted ? 0 : sinkRow.modelData.percent / 150
                                    onMoved: value => root.setVolume(sinkRow.modelData.id, value * 1.5)
                                }
                            }

                            // device options drawer: set-default + mute. PipeWire
                            // exposes no port/profile routing, so those aren't offered.
                            PopupCard {
                                visible: sinkRow.revealed
                                theme: root.theme
                                padding: 0
                                revealed: true
                                width: parent.width
                                height: visible ? sinkOpts.height + 16 : 0

                                Row {
                                    id: sinkOpts

                                    x: 12
                                    y: 8
                                    spacing: 8

                                    PopupButton {
                                        visible: !sinkRow.modelData.isDefault
                                        theme: root.theme
                                        width: 110
                                        height: 28
                                        text: "set default"
                                        role: "primary"
                                        onClicked: {
                                            root.expandedSink = "";
                                            root.selectSink(sinkRow.modelData.id);
                                        }
                                    }

                                    PopupButton {
                                        theme: root.theme
                                        width: 92
                                        height: 28
                                        text: sinkRow.modelData.muted ? "unmute" : "mute"
                                        checked: sinkRow.modelData.muted
                                        onClicked: root.toggleMute(sinkRow.modelData.id)
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // input devices (sources)
            Column {
                objectName: "inputSection"
                width: parent.width
                spacing: 6

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

                Column {
                    width: parent.width
                    spacing: 6

                    Repeater {
                        model: root.sources

                        delegate: Column {
                            id: sourceRow

                            required property var modelData
                            readonly property bool revealed: root.expandedSource === modelData.id

                            width: parent.width
                            spacing: 4

                            PopupCard {
                                theme: root.theme
                                padding: 0
                                active: sourceRow.modelData.isDefault
                                revealed: sourceRow.revealed
                                hoverable: true
                                externallyPressed: sourceArea.pressed
                                width: parent.width
                                height: 48

                                MouseArea {
                                    id: sourceArea
                                    anchors.fill: parent
                                    hoverEnabled: true
                                    cursorShape: Qt.PointingHandCursor
                                    onClicked: root.expandedSource = sourceRow.revealed ? "" : sourceRow.modelData.id
                                }

                                Text {
                                    id: sourceGlyph

                                    anchors.left: parent.left
                                    anchors.leftMargin: 12
                                    anchors.verticalCenter: parent.verticalCenter
                                    font.family: "Symbols Nerd Font Mono"
                                    font.pixelSize: 18
                                    text: sourceRow.modelData.glyph
                                    color: root.roleColor(sourceRow.modelData.role)
                                    opacity: sourceRow.modelData.muted ? 0.5 : 1
                                }

                                NuguText {
                                    theme: root.theme
                                    text: sourceRow.modelData.name
                                    anchors.left: sourceGlyph.right
                                    anchors.leftMargin: 10
                                    anchors.right: sourceTrack.left
                                    anchors.rightMargin: 10
                                    anchors.verticalCenter: parent.verticalCenter
                                    elide: Text.ElideRight
                                    opacity: sourceRow.modelData.muted ? 0.5 : 1
                                }

                                NuguText {
                                    id: sourceTrailing

                                    anchors.right: parent.right
                                    anchors.rightMargin: 12
                                    anchors.verticalCenter: parent.verticalCenter
                                    theme: root.theme
                                    variant: "meta"
                                    text: sourceRow.modelData.muted ? "muted" : `${sourceRow.modelData.percent}%`
                                    color: root.roleColor(sourceRow.modelData.muted ? "ui.minor" : sourceRow.modelData.role)
                                }

                                PopupSlider {
                                    id: sourceTrack

                                    anchors.right: sourceTrailing.left
                                    anchors.rightMargin: 10
                                    anchors.verticalCenter: parent.verticalCenter
                                    theme: root.theme
                                    width: 96
                                    value: sourceRow.modelData.muted ? 0 : sourceRow.modelData.percent / 150
                                    onMoved: value => root.setVolume(sourceRow.modelData.id, value * 1.5)
                                }
                            }

                            // device options drawer: set-default + mute. PipeWire
                            // exposes no port/profile routing, so those aren't offered.
                            PopupCard {
                                visible: sourceRow.revealed
                                theme: root.theme
                                padding: 0
                                revealed: true
                                width: parent.width
                                height: visible ? sourceOpts.height + 16 : 0

                                Row {
                                    id: sourceOpts

                                    x: 12
                                    y: 8
                                    spacing: 8

                                    PopupButton {
                                        visible: !sourceRow.modelData.isDefault
                                        theme: root.theme
                                        width: 110
                                        height: 28
                                        text: "set default"
                                        role: "primary"
                                        onClicked: {
                                            root.expandedSource = "";
                                            root.selectSource(sourceRow.modelData.id);
                                        }
                                    }

                                    PopupButton {
                                        theme: root.theme
                                        width: 92
                                        height: 28
                                        text: sourceRow.modelData.muted ? "unmute" : "mute"
                                        checked: sourceRow.modelData.muted
                                        onClicked: root.toggleMute(sourceRow.modelData.id)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}