aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/island/HeadphonesPanel.qml
blob: 315bc6bcd4c6af9a636fdcf372c7a2227a7823e9 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

import Quickshell
import Quickshell.Io
import Quickshell.Widgets
import Quickshell.Services.Pipewire
import QtQuick
import ".."
import "Logic.js" as Logic

// The Sony headphones: battery, noise control tiles and ambient level
// through sonyctl, the headphones' own volume, power off and refresh.
// Battery is BlueZ's and volume PipeWire's; only the mode needs the Sony
// protocol. Glyphs are Material Design codepoints checked against the
// Inconsolata Nerd Font cmap (AGENTS.md).
Column {
    id: panel

    required property var device        // the connected BluetoothDevice
    property bool open: false
    signal close()

    readonly property string sonyctl: Qt.resolvedUrl("sonyctl").toString().replace(/^file:\/\//, "")
    property string mode: ""            // "nc", "ambient", "off"; "" until read
    property int level: 10
    property bool voice: false
    property bool busy: false
    property string error: ""

    // The headphones' sink rather than the default one, so the slider is
    // theirs even while another output is selected.
    readonly property PwNode sink: {
        const prefix = "bluez_output." + (device?.address ?? "").replace(/:/g, "_");
        return Pipewire.nodes.values.find(n => n.name.startsWith(prefix)) ?? null;
    }
    readonly property var audio: sink?.audio ?? null
    PwObjectTracker { objects: panel.sink ? [panel.sink] : [] }

    readonly property real battery: device?.batteryAvailable ? device.battery : -1

    function glyph(cp) { return String.fromCodePoint(cp); }

    // Read on every open: the headphones' own button changes the mode too,
    // and nothing pushes it here.
    onOpenChanged: if (open) run(["status"])

    // One run at a time: each opens its own RFCOMM link, and a second one
    // while the first is up is refused by the headphones. A fresh Process per
    // run (a running one ignores another start, AGENTS.md).
    function run(args) {
        if (busy || !device) return;
        busy = true;
        op.createObject(panel, { command: [sonyctl, device.address].concat(args) }).running = true;
    }

    Component {
        id: op
        Process {
            id: proc
            stdout: StdioCollector {
                onStreamFinished: {
                    let r;
                    try { r = JSON.parse(text); }
                    catch (e) { r = { ok: false, error: "sonyctl: " + (text.trim() || "no output") }; }
                    panel.error = r.ok ? "" : r.error;
                    if (r.ok && r.mode) { panel.mode = r.mode; panel.level = r.level; panel.voice = r.voice; }
                    panel.busy = false;
                    proc.destroy();
                }
            }
        }
    }

    // The ambient level is sent once the slider or wheel settles: each set
    // is a ~2s round trip, and a send per wheel notch would queue up.
    property int pendingLevel: -1
    readonly property int shownLevel: pendingLevel > 0 ? pendingLevel : level
    Timer {
        id: levelSend
        interval: 500
        onTriggered: {
            const v = panel.pendingLevel;
            if (panel.busy) { restart(); return; }
            panel.pendingLevel = -1;
            if (v > 0 && v !== panel.level) { panel.level = v; panel.run(["ambient", String(v)]); }
        }
    }

    spacing: 14

    // --- header ----------------------------------------------------------

    Item {
        width: panel.width
        height: 26

        Text {
            id: headGlyph
            anchors.verticalCenter: parent.verticalCenter
            text: panel.glyph(0xF02CB)  // md-headphones
            font { family: Theme.iconFamily; pixelSize: 22 }
            color: Theme.accent
        }

        Rectangle {
            id: headSep
            anchors { left: headGlyph.right; leftMargin: 10; verticalCenter: parent.verticalCenter }
            width: 1
            height: 18
            color: Qt.alpha(Theme.text, 0.15)
        }

        Text {
            anchors {
                left: headSep.right; leftMargin: 10; right: closeGlyph.left; rightMargin: 8
                verticalCenter: parent.verticalCenter
            }
            elide: Text.ElideRight
            text: (panel.device?.deviceName || panel.device?.name || "Headphones").toUpperCase()
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 1; letterSpacing: 2 }
            color: Theme.text
        }

        Text {
            id: closeGlyph
            anchors { right: parent.right; verticalCenter: parent.verticalCenter }
            text: panel.glyph(0xF0156)  // md-close
            font { family: Theme.iconFamily; pixelSize: 18 }
            color: closeHit.containsMouse ? Theme.text : Theme.subtext

            MouseArea {
                id: closeHit
                anchors { fill: parent; margins: -4 }
                hoverEnabled: true
                cursorShape: Qt.PointingHandCursor
                onClicked: panel.close()
            }
        }
    }

    // --- battery ---------------------------------------------------------

    Row {
        visible: panel.battery >= 0
        width: panel.width
        spacing: 10

        Text {
            id: batGlyph
            anchors.verticalCenter: parent.verticalCenter
            // md-battery_10 .. md-battery_90 are consecutive, md-battery is full.
            text: panel.glyph(panel.battery >= 0.95 ? 0xF0079
                : 0xF007A + Math.max(0, Math.min(8, Math.round(panel.battery * 10) - 1)))
            font { family: Theme.iconFamily; pixelSize: 20 }
            color: batBar.tint
        }

        Rectangle {
            id: batBar
            readonly property color tint: panel.battery < 0.2 ? Theme.red : Theme.accent
            anchors.verticalCenter: parent.verticalCenter
            width: parent.width - batGlyph.width - batPct.width - parent.spacing * 2
            height: 8
            radius: 4
            color: Theme.surface

            Rectangle {
                height: parent.height
                radius: parent.radius
                width: parent.width * Math.max(0, panel.battery)
                color: batBar.tint
            }
        }

        Text {
            id: batPct
            anchors.verticalCenter: parent.verticalCenter
            width: 44
            horizontalAlignment: Text.AlignRight
            text: Math.round(panel.battery * 100) + "%"
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 1; bold: true }
            color: batBar.tint
        }
    }

    // --- noise control ---------------------------------------------------

    Text {
        text: "NOISE CONTROL"
        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 5; bold: true; letterSpacing: 1.5 }
        color: Theme.accent
    }

    Row {
        id: tiles
        spacing: 8

        Repeater {
            model: [
                { id: "nc", label: "ANC", cp: 0xF147D },          // md-waveform
                { id: "ambient", label: "Ambient", cp: 0xF05CB }, // md-account_voice
                { id: "off", label: "Off", cp: 0xF073A }          // md-cancel
            ]

            delegate: Rectangle {
                id: tile
                required property var modelData
                readonly property bool current: panel.mode === modelData.id
                width: (panel.width - tiles.spacing * 2) / 3
                height: 84
                radius: 12
                color: current ? Qt.alpha(Theme.accent, 0.14)
                     : tileHit.containsMouse ? Qt.alpha(Theme.surface, 0.8) : Qt.alpha(Theme.surface, 0.4)
                border.width: current ? 1.5 : 1
                border.color: current ? Theme.accent : Qt.alpha(Theme.text, 0.1)
                opacity: panel.busy && !current ? 0.6 : 1

                Column {
                    anchors.centerIn: parent
                    spacing: 6

                    Text {
                        anchors.horizontalCenter: parent.horizontalCenter
                        text: panel.glyph(tile.modelData.cp)
                        font { family: Theme.iconFamily; pixelSize: 26 }
                        color: tile.current ? Theme.accent : Theme.text
                    }

                    Text {
                        anchors.horizontalCenter: parent.horizontalCenter
                        text: tile.modelData.label
                        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
                        color: tile.current ? Theme.text : Theme.subtext
                    }

                    Rectangle {
                        anchors.horizontalCenter: parent.horizontalCenter
                        width: 6
                        height: 6
                        radius: 3
                        color: tile.current ? Theme.accent : Theme.overlay
                    }
                }

                MouseArea {
                    id: tileHit
                    anchors.fill: parent
                    hoverEnabled: true
                    enabled: !panel.busy && !tile.current
                    cursorShape: Qt.PointingHandCursor
                    onClicked: panel.run([tile.modelData.id])
                }
            }
        }
    }

    // --- sliders ---------------------------------------------------------

    // Ambient level 1-20, while in ambient mode. Wheel or drag, sent once it
    // settles (levelSend above).
    Slider {
        visible: panel.mode === "ambient"
        cp: 0xF07C5  // md-ear_hearing
        value: panel.shownLevel / 20
        label: String(panel.shownLevel)
        onMoved: v => { panel.pendingLevel = Math.max(1, Math.min(20, Math.round(v * 20))); levelSend.restart(); }
        onStepped: d => {
            panel.pendingLevel = Math.max(1, Math.min(20, panel.shownLevel + d));
            levelSend.restart();
        }
    }

    // Focus on voice, an ambient-only flag: the headphones keep it in the
    // other modes but it does nothing there.
    Item {
        visible: panel.mode === "ambient"
        width: panel.width
        height: voiceSwitch.height

        Text {
            id: voiceGlyph
            anchors.verticalCenter: parent.verticalCenter
            text: panel.glyph(0xF1308)  // md-account_tie_voice
            font { family: Theme.iconFamily; pixelSize: 20 }
            color: Theme.subtext
        }

        Text {
            anchors { left: voiceGlyph.right; leftMargin: 10; verticalCenter: parent.verticalCenter }
            text: "Focus on voice"
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
            color: Theme.text
        }

        Switch {
            id: voiceSwitch
            anchors { right: parent.right; verticalCenter: parent.verticalCenter }
            enabled: !panel.busy
            checked: panel.voice
            onToggled: on => panel.run(["voice", on ? "on" : "off"])
        }
    }

    // The headphones' volume; the icon mutes, the wheel steps 5%.
    Slider {
        enabled: panel.audio !== null
        icon: Logic.volumeIcon(panel.audio?.volume ?? 0, panel.audio?.muted ?? false)
        value: panel.audio?.volume ?? 0
        muted: panel.audio?.muted ?? false
        label: !panel.audio ? "—" : panel.audio.muted ? "muted" : Math.round(panel.audio.volume * 100) + "%"
        onIconClicked: panel.audio.muted = !panel.audio.muted
        onMoved: v => panel.audio.volume = v
        onStepped: d => panel.audio.volume = Math.max(0, Math.min(1, panel.audio.volume + d * 0.05))
    }

    Text {
        visible: panel.error !== ""
        width: panel.width
        wrapMode: Text.Wrap
        text: panel.error
        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 }
        color: Theme.red
    }

    // --- footer ----------------------------------------------------------

    Rectangle {
        width: panel.width
        height: 1
        color: Qt.alpha(Theme.text, 0.1)
    }

    Row {
        id: footer
        width: panel.width

        Repeater {
            model: [
                { label: "Power off", cp: 0xF0425, tint: "red", args: ["poweroff"] },  // md-power
                { label: "Refresh", cp: 0xF0450, tint: "text", args: ["status"] }      // md-refresh
            ]

            delegate: Item {
                id: action
                required property var modelData
                required property int index
                width: footer.width / 2
                height: 36

                Rectangle {
                    id: actionBox
                    anchors { left: parent.left; leftMargin: action.index ? 12 : 0; verticalCenter: parent.verticalCenter }
                    width: 36
                    height: 36
                    radius: 10
                    color: actionHit.containsMouse ? Qt.alpha(Theme[action.modelData.tint], 0.18)
                                                   : Qt.alpha(Theme.surface, 0.6)
                    border.width: 1
                    border.color: Qt.alpha(Theme[action.modelData.tint], 0.25)

                    Text {
                        id: actionGlyph
                        anchors.centerIn: parent
                        text: panel.glyph(action.modelData.cp)
                        font { family: Theme.iconFamily; pixelSize: 20 }
                        color: Theme[action.modelData.tint]
                        // The refresh glyph turns while sonyctl runs.
                        RotationAnimation on rotation {
                            running: panel.busy && action.index === 1
                            from: 0; to: 360; duration: 900; loops: Animation.Infinite
                            onStopped: actionGlyph.rotation = 0
                        }
                    }
                }

                Text {
                    anchors { left: actionBox.right; leftMargin: 10; verticalCenter: parent.verticalCenter }
                    text: action.modelData.label
                    font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
                    color: Theme.text
                }

                Rectangle {
                    visible: action.index === 1
                    anchors { left: parent.left; verticalCenter: parent.verticalCenter }
                    width: 1
                    height: 22
                    color: Qt.alpha(Theme.text, 0.15)
                }

                MouseArea {
                    id: actionHit
                    anchors.fill: parent
                    hoverEnabled: true
                    enabled: !panel.busy
                    cursorShape: Qt.PointingHandCursor
                    onClicked: panel.run(action.modelData.args)
                }
            }
        }
    }

    // One slider row: a glyph (cp) or a named icon (icon), a track, a label.
    // moved(v) gives 0..1 from a click or drag; stepped(+1|-1) one wheel notch.
    component Slider: Row {
        id: row
        property int cp: 0
        property string icon: ""
        property real value: 0
        property bool muted: false
        property string label: ""
        signal moved(real v)
        signal stepped(int d)
        signal iconClicked()

        width: panel.width
        spacing: 10

        Item {
            anchors.verticalCenter: parent.verticalCenter
            width: 20
            height: 20

            Text {
                visible: row.icon === ""
                anchors.centerIn: parent
                text: panel.glyph(row.cp)
                font { family: Theme.iconFamily; pixelSize: 20 }
                color: Theme.subtext
            }

            IconImage {
                visible: row.icon !== ""
                anchors.centerIn: parent
                implicitSize: 18
                source: row.icon !== "" ? Quickshell.iconPath(row.icon, true) : ""
            }

            MouseArea {
                anchors.fill: parent
                enabled: row.enabled
                cursorShape: Qt.PointingHandCursor
                onClicked: row.iconClicked()
            }
        }

        Rectangle {
            anchors.verticalCenter: parent.verticalCenter
            width: row.width - 20 - 44 - row.spacing * 2
            height: 8
            radius: 4
            color: Theme.surface

            Rectangle {
                height: parent.height
                radius: parent.radius
                width: parent.width * Math.max(0, Math.min(1, row.value))
                color: row.muted ? Theme.red : Theme.accent
                opacity: row.muted ? 0.5 : 1
            }

            // Taller than the track so it is easy to hit. A MouseArea for the
            // wheel, not a WheelHandler (AGENTS.md); one step per notch (120),
            // since a touchpad sends many small deltas.
            MouseArea {
                anchors { fill: parent; topMargin: -8; bottomMargin: -8 }
                enabled: row.enabled
                preventStealing: true
                cursorShape: Qt.PointingHandCursor
                property real acc: 0
                function at(x) { return Math.max(0, Math.min(1, x / width)); }
                onPressed: mouse => row.moved(at(mouse.x))
                onPositionChanged: mouse => { if (pressed) row.moved(at(mouse.x)); }
                onWheel: wheel => {
                    acc += wheel.angleDelta.y;
                    while (Math.abs(acc) >= 120) {
                        row.stepped(acc > 0 ? 1 : -1);
                        acc -= acc > 0 ? 120 : -120;
                    }
                    wheel.accepted = true;
                }
            }
        }

        Text {
            anchors.verticalCenter: parent.verticalCenter
            width: 44
            horizontalAlignment: Text.AlignRight
            text: row.label
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
            color: row.muted ? Theme.red : Theme.subtext
        }
    }
}