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). solid-dark cards — the popup frame is light,
            // so a translucent tint fill washes to near-white. Identity is carried
            // by the left accent bar + border; default = ui.accent.
            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

                            Rectangle {
                                width: parent.width
                                height: 48
                                radius: 12
                                color: root.theme ? root.theme.withAlpha(root.theme.ui.backdrop, sinkRow.modelData.isDefault ? 0.82 : 0.55) : "transparent"
                                border.width: (sinkRow.modelData.isDefault || sinkRow.revealed) ? 2 : 1
                                border.color: root.theme ? (sinkRow.modelData.isDefault ? root.theme.ui.accent : (sinkRow.revealed ? root.theme.withAlpha(root.theme.ui.accent, 0.6) : root.theme.withAlpha(root.theme.ui.minor, 0.45))) : "transparent"

                                // tap the device (glyph/name area) opens its options drawer;
                                // the slider-track MouseArea (later sibling, higher z) wins on
                                // the track region so dragging volume never opens the drawer.
                                MouseArea {
                                    anchors.fill: parent
                                    onClicked: root.expandedSink = sinkRow.revealed ? "" : sinkRow.modelData.id
                                }

                                // left accent bar — only on the default device
                                Rectangle {
                                    anchors.left: parent.left
                                    anchors.top: parent.top
                                    anchors.bottom: parent.bottom
                                    anchors.margins: 2
                                    width: 3
                                    radius: 2
                                    color: root.theme ? root.theme.ui.accent : "transparent"
                                    opacity: sinkRow.modelData.isDefault ? 1 : 0
                                }

                                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)
                                }

                                // inline slider: 0..1.5 volume mapped onto the track (percent/150)
                                Rectangle {
                                    id: sinkTrack

                                    anchors.right: sinkTrailing.left
                                    anchors.rightMargin: 10
                                    anchors.verticalCenter: parent.verticalCenter
                                    width: 96
                                    height: 8
                                    radius: 4
                                    color: root.theme ? root.theme.withAlpha(root.theme.ui.minor, 0.28) : "transparent"

                                    Rectangle {
                                        anchors.left: parent.left
                                        anchors.top: parent.top
                                        anchors.bottom: parent.bottom
                                        width: parent.width * (sinkRow.modelData.percent / 150)
                                        radius: parent.radius
                                        color: root.roleColor(sinkRow.modelData.role)
                                        visible: !sinkRow.modelData.muted
                                    }

                                    MouseArea {
                                        anchors.fill: parent
                                        onClicked: event => {
                                            return root.setVolume(sinkRow.modelData.id, event.x / Math.max(1, width) * 1.5);
                                        }
                                        onPositionChanged: event => {
                                            if (pressed)
                                                root.setVolume(sinkRow.modelData.id, event.x / Math.max(1, width) * 1.5);
                                        }
                                    }
                                }
                            }

                            // device options drawer: set-default + mute. PipeWire
                            // exposes no port/profile routing, so those aren't offered.
                            Rectangle {
                                visible: sinkRow.revealed
                                width: parent.width
                                height: visible ? sinkOpts.height + 16 : 0
                                radius: 10
                                color: root.theme ? root.theme.withAlpha(root.theme.ui.backdrop, 0.55) : "#333333"
                                border.width: 1
                                border.color: root.theme ? root.theme.withAlpha(root.theme.ui.accent, 0.4) : "white"
                                clip: true

                                Row {
                                    id: sinkOpts

                                    x: 12
                                    y: 8
                                    spacing: 8

                                    Rectangle {
                                        visible: !sinkRow.modelData.isDefault
                                        width: 110
                                        height: 28
                                        radius: 9
                                        color: root.theme ? root.theme.withAlpha(root.theme.ui.accent, 0.18) : "transparent"
                                        border.width: 1
                                        border.color: root.theme ? root.theme.withAlpha(root.theme.ui.accent, 0.7) : "white"

                                        NuguText {
                                            anchors.centerIn: parent
                                            theme: root.theme
                                            variant: "meta"
                                            text: "set default"
                                        }

                                        MouseArea {
                                            anchors.fill: parent
                                            onClicked: {
                                                root.expandedSink = "";
                                                root.selectSink(sinkRow.modelData.id);
                                            }
                                        }
                                    }

                                    Rectangle {
                                        width: 92
                                        height: 28
                                        radius: 9
                                        color: root.theme ? root.theme.withAlpha(sinkRow.modelData.muted ? root.theme.warning : root.theme.ui.accent, 0.18) : "transparent"
                                        border.width: 1
                                        border.color: root.theme ? root.theme.withAlpha(sinkRow.modelData.muted ? root.theme.warning : root.theme.ui.accent, 0.7) : "white"

                                        NuguText {
                                            anchors.centerIn: parent
                                            theme: root.theme
                                            variant: "meta"
                                            text: sinkRow.modelData.muted ? "unmute" : "mute"
                                        }

                                        MouseArea {
                                            anchors.fill: parent
                                            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

                            Rectangle {
                                width: parent.width
                                height: 48
                                radius: 12
                                color: root.theme ? root.theme.withAlpha(root.theme.ui.backdrop, sourceRow.modelData.isDefault ? 0.82 : 0.55) : "transparent"
                                border.width: (sourceRow.modelData.isDefault || sourceRow.revealed) ? 2 : 1
                                border.color: root.theme ? (sourceRow.modelData.isDefault ? root.theme.ui.accent : (sourceRow.revealed ? root.theme.withAlpha(root.theme.ui.accent, 0.6) : root.theme.withAlpha(root.theme.ui.minor, 0.45))) : "transparent"

                                MouseArea {
                                    anchors.fill: parent
                                    onClicked: root.expandedSource = sourceRow.revealed ? "" : sourceRow.modelData.id
                                }

                                Rectangle {
                                    anchors.left: parent.left
                                    anchors.top: parent.top
                                    anchors.bottom: parent.bottom
                                    anchors.margins: 2
                                    width: 3
                                    radius: 2
                                    color: root.theme ? root.theme.ui.accent : "transparent"
                                    opacity: sourceRow.modelData.isDefault ? 1 : 0
                                }

                                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)
                                }

                                Rectangle {
                                    id: sourceTrack

                                    anchors.right: sourceTrailing.left
                                    anchors.rightMargin: 10
                                    anchors.verticalCenter: parent.verticalCenter
                                    width: 96
                                    height: 8
                                    radius: 4
                                    color: root.theme ? root.theme.withAlpha(root.theme.ui.minor, 0.28) : "transparent"

                                    Rectangle {
                                        anchors.left: parent.left
                                        anchors.top: parent.top
                                        anchors.bottom: parent.bottom
                                        width: parent.width * (sourceRow.modelData.percent / 150)
                                        radius: parent.radius
                                        color: root.roleColor(sourceRow.modelData.role)
                                        visible: !sourceRow.modelData.muted
                                    }

                                    MouseArea {
                                        anchors.fill: parent
                                        onClicked: event => {
                                            return root.setVolume(sourceRow.modelData.id, event.x / Math.max(1, width) * 1.5);
                                        }
                                        onPositionChanged: event => {
                                            if (pressed)
                                                root.setVolume(sourceRow.modelData.id, event.x / Math.max(1, width) * 1.5);
                                        }
                                    }
                                }
                            }

                            // device options drawer: set-default + mute. PipeWire
                            // exposes no port/profile routing, so those aren't offered.
                            Rectangle {
                                visible: sourceRow.revealed
                                width: parent.width
                                height: visible ? sourceOpts.height + 16 : 0
                                radius: 10
                                color: root.theme ? root.theme.withAlpha(root.theme.ui.backdrop, 0.55) : "#333333"
                                border.width: 1
                                border.color: root.theme ? root.theme.withAlpha(root.theme.ui.accent, 0.4) : "white"
                                clip: true

                                Row {
                                    id: sourceOpts

                                    x: 12
                                    y: 8
                                    spacing: 8

                                    Rectangle {
                                        visible: !sourceRow.modelData.isDefault
                                        width: 110
                                        height: 28
                                        radius: 9
                                        color: root.theme ? root.theme.withAlpha(root.theme.ui.accent, 0.18) : "transparent"
                                        border.width: 1
                                        border.color: root.theme ? root.theme.withAlpha(root.theme.ui.accent, 0.7) : "white"

                                        NuguText {
                                            anchors.centerIn: parent
                                            theme: root.theme
                                            variant: "meta"
                                            text: "set default"
                                        }

                                        MouseArea {
                                            anchors.fill: parent
                                            onClicked: {
                                                root.expandedSource = "";
                                                root.selectSource(sourceRow.modelData.id);
                                            }
                                        }
                                    }

                                    Rectangle {
                                        width: 92
                                        height: 28
                                        radius: 9
                                        color: root.theme ? root.theme.withAlpha(sourceRow.modelData.muted ? root.theme.warning : root.theme.ui.accent, 0.18) : "transparent"
                                        border.width: 1
                                        border.color: root.theme ? root.theme.withAlpha(sourceRow.modelData.muted ? root.theme.warning : root.theme.ui.accent, 0.7) : "white"

                                        NuguText {
                                            anchors.centerIn: parent
                                            theme: root.theme
                                            variant: "meta"
                                            text: sourceRow.modelData.muted ? "unmute" : "mute"
                                        }

                                        MouseArea {
                                            anchors.fill: parent
                                            onClicked: root.toggleMute(sourceRow.modelData.id)
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}