aboutsummaryrefslogtreecommitdiff
path: root/modules/system/quickshell/Volume.qml
blob: c4d16259f866efd7512728f015fa2e51000c0bbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import Quickshell
import Quickshell.Services.Pipewire

Item {
    id: root
    width: childrenRect.width
    height: parent.height

    // Properties bound directly to the Pipewire service
    readonly property PwNode sink: Pipewire.defaultAudioSink
    readonly property bool muted: sink && sink.audio ? sink.audio.muted : true
    readonly property real volume: sink && sink.audio ? sink.audio.volume : 0

    // Pipewire nodes only emit property updates while tracked.
    PwObjectTracker {
        objects: root.sink ? [root.sink] : []
    }

    function setVolume(value) {
        if (sink?.ready && sink?.audio) {
            sink.audio.muted = false;
            sink.audio.volume = value;
        } else {
            // Fallback for unbound nodes
            Quickshell.execDetached(["wpctl", "set-volume", "@DEFAULT_AUDIO_SINK@", value.toFixed(2)]);
        }
    }

    function toggleMute() {
        if (sink?.ready && sink?.audio) {
            sink.audio.muted = !sink.audio.muted;
        } else {
            Quickshell.execDetached(["wpctl", "set-mute", "@DEFAULT_AUDIO_SINK@", "toggle"]);
        }
    }

    function getDeviceIcon(node) {
        return node?.properties?.["device.icon-name"] ?? "audio-card"
    }

    Row {
        id: triggerRow
        anchors {
            verticalCenter: parent.verticalCenter
        }
        spacing: 4

        Image {
            width: 20
            height: 20
            source: Quickshell.iconPath(root.getDeviceIcon(root.sink) + "-symbolic")
            sourceSize: Qt.size(width, height)
            smooth: true
            mipmap: true
            opacity: root.muted ? 0.5 : 1.0
        }
    }

    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onClicked: mouse => {
            if (mouse.button === Qt.LeftButton) {
                GlobalState.toggle("Volume")
            } else if (mouse.button === Qt.RightButton) {
                root.toggleMute()
            }
        }
        onWheel: wheel => {
            const step = 0.05
            const next = wheel.angleDelta.y > 0 ? root.volume + step : root.volume - step
            root.setVolume(Math.max(0.0, Math.min(1.0, next)))
        }
    }

    PopupWindow {
        id: popup
        visible: GlobalState.activePopup === "Volume"
        grabFocus: true
        implicitWidth: bgRect.width
        implicitHeight: bgRect.height
        
        anchor {
            window: barWindow
            item: root
            edges: Edges.Bottom
            gravity: Edges.Bottom
            margins.top: Theme.popupGap
        }
        
        color: "transparent"
        
        onVisibleChanged: {
            if (visible) anchor.updateAnchor()
        }
        
        Squircle {
            id: bgRect
            width: 260
            height: contentCol.height + 24
            fillColor: Theme.bg
            strokeColor: Theme.border
            strokeWidth: 1
            cornerRadius: 8

            Column {
                id: contentCol
                anchors {
                    top: parent.top
                    left: parent.left
                    right: parent.right
                    margins: 12
                }
                spacing: 12

                Text {
                    text: "Sound"
                    color: Theme.text
                    font {
                        family: Theme.mainFont
                        pixelSize: 14
                        weight: Font.DemiBold
                    }
                }

                PillSlider {
                    id: volumeSlider
                    width: parent.width
                    value: root.volume
                    onMoved: root.setVolume(value)

                    Binding {
                        target: volumeSlider
                        property: "value"
                        value: root.volume
                        when: !volumeSlider.pressed
                    }
                }

                Rectangle { width: parent.width; height: 1; color: Theme.border }

                Text {
                    text: "Output Devices"
                    color: Theme.textMuted
                    font {
                        family: Theme.mainFont
                        pixelSize: 12
                        weight: Font.DemiBold
                    }
                    padding: 4
                }

                Repeater {
                    model: Pipewire.nodes
                    delegate: Item {
                        width: parent.width
                        property bool isOutput: modelData && modelData.isSink && !modelData.isStream && modelData.name !== "Dummy-Driver"
                        visible: isOutput
                        height: isOutput ? 40 : 0
                        
                        Squircle {
                            anchors.fill: parent
                            fillColor: mouseArea.containsMouse ? Theme.surfaceLighter : Theme.transparent
                            cornerRadius: 6
                            
                            RowLayout {
                                anchors.fill: parent
                                anchors.margins: 8
                                spacing: 12
                                
                                IconCircle {
                                    size: 24
                                    source: root.getDeviceIcon(modelData)
                                    active: root.sink && root.sink.id === modelData.id
                                }
                                
                                Text {
                                    Layout.fillWidth: true
                                    text: modelData.description || modelData.nickname || modelData.name
                                    color: (root.sink && root.sink.id === modelData.id) ? Theme.text : Theme.textDim
                                    font {
                                        family: Theme.mainFont
                                        pixelSize: 13
                                    }
                                    elide: Text.ElideRight
                                }
                            }
                            
                            MouseArea {
                                id: mouseArea
                                anchors.fill: parent
                                hoverEnabled: true
                                onClicked: {
                                    Quickshell.execDetached(["wpctl", "set-default", modelData.id.toString()])
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}