summaryrefslogtreecommitdiff
path: root/raveos-hyprland-theme/theme-data/DankMaterialShell/quickshell/Modules/ControlCenter/Details/BluetoothDetail.qml
blob: 580ea9f1c63d5e87e7fca62072cbadc68414132a (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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
import QtQuick
import QtQuick.Controls
import Quickshell
import Quickshell.Bluetooth
import qs.Common
import qs.Services
import qs.Widgets

Rectangle {
    id: root

    LayoutMirroring.enabled: I18n.isRtl
    LayoutMirroring.childrenInherit: true

    implicitHeight: {
        if (height > 0)
            return height;
        if (!BluetoothService.adapter?.enabled)
            return headerRow.height;
        return headerRow.height + bluetoothContent.height + Theme.spacingM;
    }
    radius: Theme.cornerRadius
    color: Theme.withAlpha(Theme.surfaceContainerHigh, Theme.popupTransparency)
    border.color: Qt.rgba(Theme.outline.r, Theme.outline.g, Theme.outline.b, 0.08)
    border.width: 0

    property var bluetoothCodecModalRef: null
    property var devicesBeingPaired: new Set()

    signal showCodecSelector(var device)

    function isDeviceBeingPaired(deviceAddress) {
        return devicesBeingPaired.has(deviceAddress);
    }

    function handlePairDevice(device) {
        if (!device)
            return;
        const deviceAddr = device.address;
        const pairingSet = devicesBeingPaired;

        pairingSet.add(deviceAddr);
        devicesBeingPairedChanged();

        BluetoothService.pairDevice(device, function (response) {
            pairingSet.delete(deviceAddr);
            devicesBeingPairedChanged();

            if (response.error) {
                ToastService.showError(I18n.tr("Pairing failed"), response.error);
                return;
            }
            if (!BluetoothService.enhancedPairingAvailable) {
                ToastService.showSuccess(I18n.tr("Device paired"));
            }
        });
    }

    function updateDeviceCodecDisplay(deviceAddress, codecName) {
        for (let i = 0; i < pairedRepeater.count; i++) {
            const item = pairedRepeater.itemAt(i);
            if (!item?.modelData)
                continue;
            if (item.modelData.address !== deviceAddress)
                continue;
            item.currentCodec = codecName;
            break;
        }
    }

    function normalizePinList(value) {
        if (Array.isArray(value))
            return value.filter(v => v);
        if (typeof value === "string" && value.length > 0)
            return [value];
        return [];
    }

    function getPinnedDevices() {
        const pins = SettingsData.bluetoothDevicePins || {};
        return normalizePinList(pins["preferredDevice"]);
    }

    Row {
        id: headerRow
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: parent.top
        anchors.leftMargin: Theme.spacingM
        anchors.rightMargin: Theme.spacingM
        anchors.topMargin: Theme.spacingS
        height: 40

        StyledText {
            id: headerText
            text: I18n.tr("Bluetooth Settings")
            font.pixelSize: Theme.fontSizeLarge
            color: Theme.surfaceText
            font.weight: Font.Medium
            anchors.verticalCenter: parent.verticalCenter
        }

        Item {
            width: Math.max(0, parent.width - headerText.implicitWidth - scanButton.width - Theme.spacingM)
            height: parent.height
        }

        Rectangle {
            id: scanButton

            readonly property bool adapterEnabled: BluetoothService.adapter?.enabled ?? false
            readonly property bool isDiscovering: BluetoothService.adapter?.discovering ?? false

            width: 100
            height: 36
            radius: 18
            color: scanMouseArea.containsMouse && adapterEnabled ? Theme.withAlpha(Theme.surfaceContainerHigh, Theme.popupTransparency) : "transparent"
            border.color: adapterEnabled ? Theme.primary : Qt.rgba(Theme.outline.r, Theme.outline.g, Theme.outline.b, 0.12)
            border.width: 0
            visible: adapterEnabled

            Row {
                anchors.centerIn: parent
                spacing: Theme.spacingXS

                DankIcon {
                    name: scanButton.isDiscovering ? "stop" : "bluetooth_searching"
                    size: 18
                    color: scanButton.adapterEnabled ? Theme.primary : Theme.surfaceVariantText
                    anchors.verticalCenter: parent.verticalCenter
                }

                StyledText {
                    text: scanButton.isDiscovering ? I18n.tr("Scanning") : I18n.tr("Scan")
                    color: scanButton.adapterEnabled ? Theme.primary : Theme.surfaceVariantText
                    font.pixelSize: Theme.fontSizeMedium
                    font.weight: Font.Medium
                    anchors.verticalCenter: parent.verticalCenter
                }
            }

            DankRipple {
                id: scanRipple
                cornerRadius: scanButton.radius
            }

            MouseArea {
                id: scanMouseArea
                anchors.fill: parent
                hoverEnabled: true
                enabled: scanButton.adapterEnabled
                cursorShape: enabled ? Qt.PointingHandCursor : Qt.ArrowCursor
                onPressed: mouse => scanRipple.trigger(mouse.x, mouse.y)
                onClicked: {
                    if (!BluetoothService.adapter)
                        return;
                    BluetoothService.adapter.discovering = !BluetoothService.adapter.discovering;
                }
            }
        }
    }

    DankFlickable {
        id: bluetoothContent
        anchors.top: headerRow.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        anchors.margins: Theme.spacingM
        anchors.topMargin: Theme.spacingM
        visible: BluetoothService.adapter?.enabled ?? false
        contentHeight: bluetoothColumn.height
        clip: true

        readonly property int maxPinnedDevices: 3

        Column {
            id: bluetoothColumn
            width: parent.width
            spacing: Theme.spacingS

            ScriptModel {
                id: pairedDevicesModel
                objectProp: "address"
                values: {
                    if (!BluetoothService.adapter?.devices)
                        return [];

                    const pinnedList = root.getPinnedDevices();
                    const devices = [...BluetoothService.adapter.devices.values.filter(dev => dev && (dev.paired || dev.trusted))];

                    devices.sort((a, b) => {
                        const aPinnedIndex = pinnedList.indexOf(a.address);
                        const bPinnedIndex = pinnedList.indexOf(b.address);

                        if (aPinnedIndex !== -1 || bPinnedIndex !== -1) {
                            if (aPinnedIndex === -1)
                                return 1;
                            if (bPinnedIndex === -1)
                                return -1;
                            return aPinnedIndex - bPinnedIndex;
                        }

                        if (a.connected !== b.connected)
                            return a.connected ? -1 : 1;

                        return (b.signalStrength || 0) - (a.signalStrength || 0);
                    });

                    return devices;
                }
            }

            Repeater {
                id: pairedRepeater
                model: pairedDevicesModel

                delegate: Rectangle {
                    id: pairedDelegate
                    required property var modelData
                    required property int index

                    readonly property string currentCodec: BluetoothService.deviceCodecs[modelData.address] || ""
                    readonly property bool isConnecting: modelData.state === BluetoothDeviceState.Connecting
                    readonly property bool isConnected: modelData.connected
                    readonly property bool isPinned: root.getPinnedDevices().includes(modelData.address)
                    readonly property string deviceName: modelData.name || modelData.deviceName || I18n.tr("Unknown Device")

                    width: parent.width
                    height: 50
                    radius: Theme.cornerRadius

                    Component.onCompleted: {
                        if (!isConnected)
                            return;
                        if (!BluetoothService.isAudioDevice(modelData))
                            return;
                        BluetoothService.refreshDeviceCodec(modelData);
                    }

                    color: {
                        if (isConnecting)
                            return Qt.rgba(Theme.warning.r, Theme.warning.g, Theme.warning.b, 0.12);
                        if (deviceMouseArea.containsMouse)
                            return Theme.primaryHoverLight;
                        return Theme.surfaceLight;
                    }

                    border.color: {
                        if (isConnecting)
                            return Theme.warning;
                        if (isConnected)
                            return Theme.primary;
                        return Theme.outlineLight;
                    }
                    border.width: (isConnecting || isConnected) ? 2 : 1

                    Row {
                        anchors.left: parent.left
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.leftMargin: Theme.spacingM
                        spacing: Theme.spacingS

                        DankIcon {
                            name: BluetoothService.getDeviceIcon(pairedDelegate.modelData)
                            size: Theme.iconSize - 4
                            anchors.verticalCenter: parent.verticalCenter
                            color: {
                                if (pairedDelegate.isConnecting)
                                    return Theme.warning;
                                if (pairedDelegate.isConnected)
                                    return Theme.primary;
                                return Theme.surfaceText;
                            }
                        }

                        Column {
                            anchors.verticalCenter: parent.verticalCenter
                            width: 200

                            StyledText {
                                text: pairedDelegate.deviceName
                                font.pixelSize: Theme.fontSizeMedium
                                color: Theme.surfaceText
                                font.weight: pairedDelegate.isConnected ? Font.Medium : Font.Normal
                                elide: Text.ElideRight
                                width: parent.width
                                horizontalAlignment: Text.AlignLeft
                            }

                            Row {
                                spacing: Theme.spacingXS

                                StyledText {
                                    text: {
                                        if (pairedDelegate.isConnecting)
                                            return I18n.tr("Connecting...");
                                        if (!pairedDelegate.isConnected)
                                            return I18n.tr("Paired");
                                        if (!pairedDelegate.currentCodec)
                                            return I18n.tr("Connected");
                                        return I18n.tr("Connected") + " • " + pairedDelegate.currentCodec;
                                    }
                                    font.pixelSize: Theme.fontSizeSmall
                                    color: pairedDelegate.isConnecting ? Theme.warning : Theme.surfaceVariantText
                                }

                                StyledText {
                                    readonly property var btBattery: {
                                        const name = pairedDelegate.deviceName;
                                        return BatteryService.bluetoothDevices.find(dev => dev.name === name || dev.name.toLowerCase().includes(name.toLowerCase()) || name.toLowerCase().includes(dev.name.toLowerCase()));
                                    }
                                    text: {
                                        if (pairedDelegate.modelData.batteryAvailable && pairedDelegate.modelData.battery > 0)
                                            return "• " + Math.round(pairedDelegate.modelData.battery * 100) + "%";
                                        if (btBattery)
                                            return "• " + btBattery.percentage + "%";
                                        return "";
                                    }
                                    font.pixelSize: Theme.fontSizeSmall
                                    color: Theme.surfaceVariantText
                                    visible: text.length > 0
                                }

                                StyledText {
                                    text: pairedDelegate.modelData.signalStrength > 0 ? "• " + pairedDelegate.modelData.signalStrength + "%" : ""
                                    font.pixelSize: Theme.fontSizeSmall
                                    color: Theme.surfaceVariantText
                                    visible: text.length > 0
                                }
                            }
                        }
                    }

                    Rectangle {
                        anchors.right: pairedOptionsButton.left
                        anchors.rightMargin: Theme.spacingS
                        anchors.verticalCenter: parent.verticalCenter
                        width: pinBluetoothRow.width + Theme.spacingS * 2
                        height: 28
                        radius: height / 2
                        color: pairedDelegate.isPinned ? Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.12) : Theme.withAlpha(Theme.surfaceText, 0.05)

                        Row {
                            id: pinBluetoothRow
                            anchors.centerIn: parent
                            spacing: 4

                            DankIcon {
                                name: "push_pin"
                                size: 16
                                color: pairedDelegate.isPinned ? Theme.primary : Theme.surfaceText
                                anchors.verticalCenter: parent.verticalCenter
                            }

                            StyledText {
                                text: pairedDelegate.isPinned ? I18n.tr("Pinned") : I18n.tr("Pin")
                                font.pixelSize: Theme.fontSizeSmall
                                color: pairedDelegate.isPinned ? Theme.primary : Theme.surfaceText
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }

                        MouseArea {
                            anchors.fill: parent
                            cursorShape: Qt.PointingHandCursor
                            onClicked: {
                                const pins = JSON.parse(JSON.stringify(SettingsData.bluetoothDevicePins || {}));
                                let pinnedList = root.normalizePinList(pins["preferredDevice"]);
                                const pinIndex = pinnedList.indexOf(pairedDelegate.modelData.address);

                                if (pinIndex !== -1) {
                                    pinnedList.splice(pinIndex, 1);
                                } else {
                                    pinnedList.unshift(pairedDelegate.modelData.address);
                                    if (pinnedList.length > bluetoothContent.maxPinnedDevices)
                                        pinnedList = pinnedList.slice(0, bluetoothContent.maxPinnedDevices);
                                }

                                if (pinnedList.length > 0) {
                                    pins["preferredDevice"] = pinnedList;
                                } else {
                                    delete pins["preferredDevice"];
                                }

                                SettingsData.set("bluetoothDevicePins", pins);
                            }
                        }
                    }

                    DankActionButton {
                        id: pairedOptionsButton
                        anchors.right: parent.right
                        anchors.rightMargin: Theme.spacingS
                        anchors.verticalCenter: parent.verticalCenter
                        iconName: "more_horiz"
                        buttonSize: 28
                        onClicked: {
                            if (bluetoothContextMenu.visible) {
                                bluetoothContextMenu.close();
                                return;
                            }
                            bluetoothContextMenu.currentDevice = pairedDelegate.modelData;
                            bluetoothContextMenu.popup(pairedOptionsButton, -bluetoothContextMenu.width + pairedOptionsButton.width, pairedOptionsButton.height + Theme.spacingXS);
                        }
                    }

                    DankRipple {
                        id: deviceRipple
                        cornerRadius: pairedDelegate.radius
                    }

                    MouseArea {
                        id: deviceMouseArea
                        anchors.fill: parent
                        anchors.rightMargin: pairedOptionsButton.width + Theme.spacingM + pinBluetoothRow.width + Theme.spacingS * 4
                        hoverEnabled: true
                        cursorShape: Qt.PointingHandCursor
                        onPressed: mouse => {
                            const pos = mapToItem(pairedDelegate, mouse.x, mouse.y);
                            deviceRipple.trigger(pos.x, pos.y);
                        }
                        onClicked: {
                            if (pairedDelegate.isConnected) {
                                pairedDelegate.modelData.disconnect();
                                return;
                            }
                            BluetoothService.connectDeviceWithTrust(pairedDelegate.modelData);
                        }
                    }
                }
            }

            Rectangle {
                width: parent.width
                height: 1
                color: Qt.rgba(Theme.outline.r, Theme.outline.g, Theme.outline.b, 0.12)
                visible: pairedRepeater.count > 0 && availableRepeater.count > 0
            }

            Item {
                width: parent.width
                height: 80
                visible: (BluetoothService.adapter?.discovering ?? false) && availableRepeater.count === 0

                DankIcon {
                    anchors.centerIn: parent
                    name: "sync"
                    size: 24
                    color: Qt.rgba(Theme.surfaceText.r || 0.8, Theme.surfaceText.g || 0.8, Theme.surfaceText.b || 0.8, 0.4)

                    RotationAnimation on rotation {
                        running: parent.visible
                        loops: Animation.Infinite
                        from: 0
                        to: 360
                        duration: 1500
                    }
                }
            }

            ScriptModel {
                id: availableDevicesModel
                objectProp: "address"
                values: {
                    if (!BluetoothService.adapter?.discovering)
                        return [];
                    if (!Bluetooth.devices)
                        return [];

                    const filtered = Bluetooth.devices.values.filter(dev => dev && !dev.paired && !dev.pairing && !dev.blocked && (dev.signalStrength === undefined || dev.signalStrength > 0));
                    return BluetoothService.sortDevices(filtered);
                }
            }

            Repeater {
                id: availableRepeater
                model: availableDevicesModel

                delegate: Rectangle {
                    id: availableDelegate
                    required property var modelData
                    required property int index

                    readonly property bool canConnect: BluetoothService.canConnect(modelData)
                    readonly property bool isBusy: BluetoothService.isDeviceBusy(modelData) || root.isDeviceBeingPaired(modelData.address)
                    readonly property bool isInteractive: canConnect && !isBusy
                    readonly property string deviceName: modelData.name || modelData.deviceName || I18n.tr("Unknown Device")

                    width: parent.width
                    height: 50
                    radius: Theme.cornerRadius
                    color: availableMouseArea.containsMouse && isInteractive ? Theme.primaryHoverLight : Theme.surfaceLight
                    border.color: Theme.outlineLight
                    border.width: 1
                    opacity: isInteractive ? 1 : 0.6

                    Row {
                        anchors.left: parent.left
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.leftMargin: Theme.spacingM
                        spacing: Theme.spacingS

                        DankIcon {
                            name: BluetoothService.getDeviceIcon(availableDelegate.modelData)
                            size: Theme.iconSize - 4
                            color: Theme.surfaceText
                            anchors.verticalCenter: parent.verticalCenter
                        }

                        Column {
                            anchors.verticalCenter: parent.verticalCenter
                            width: 200

                            StyledText {
                                text: availableDelegate.deviceName
                                font.pixelSize: Theme.fontSizeMedium
                                color: Theme.surfaceText
                                elide: Text.ElideRight
                                width: parent.width
                                horizontalAlignment: Text.AlignLeft
                            }

                            Row {
                                spacing: Theme.spacingXS

                                StyledText {
                                    text: {
                                        if (availableDelegate.modelData.pairing || availableDelegate.isBusy)
                                            return I18n.tr("Pairing...");
                                        if (availableDelegate.modelData.blocked)
                                            return I18n.tr("Blocked");
                                        return BluetoothService.getSignalStrength(availableDelegate.modelData);
                                    }
                                    font.pixelSize: Theme.fontSizeSmall
                                    color: Theme.surfaceVariantText
                                }

                                StyledText {
                                    text: availableDelegate.modelData.signalStrength > 0 ? "• " + availableDelegate.modelData.signalStrength + "%" : ""
                                    font.pixelSize: Theme.fontSizeSmall
                                    color: Theme.surfaceVariantText
                                    visible: text.length > 0 && !availableDelegate.modelData.pairing && !availableDelegate.modelData.blocked
                                }
                            }
                        }
                    }

                    StyledText {
                        anchors.right: parent.right
                        anchors.rightMargin: Theme.spacingM
                        anchors.verticalCenter: parent.verticalCenter
                        text: {
                            if (availableDelegate.isBusy)
                                return I18n.tr("Pairing...");
                            if (!availableDelegate.canConnect)
                                return I18n.tr("Cannot pair");
                            return I18n.tr("Pair");
                        }
                        font.pixelSize: Theme.fontSizeSmall
                        color: availableDelegate.isInteractive ? Theme.primary : Theme.surfaceVariantText
                        font.weight: Font.Medium
                    }

                    DankRipple {
                        id: availableRipple
                        cornerRadius: availableDelegate.radius
                    }

                    MouseArea {
                        id: availableMouseArea
                        anchors.fill: parent
                        hoverEnabled: true
                        cursorShape: availableDelegate.isInteractive ? Qt.PointingHandCursor : Qt.ArrowCursor
                        enabled: availableDelegate.isInteractive
                        onPressed: mouse => availableRipple.trigger(mouse.x, mouse.y)
                        onClicked: root.handlePairDevice(availableDelegate.modelData)
                    }
                }
            }

            Item {
                width: parent.width
                height: 60
                visible: !BluetoothService.adapter

                StyledText {
                    anchors.centerIn: parent
                    text: I18n.tr("No Bluetooth adapter found")
                    font.pixelSize: Theme.fontSizeMedium
                    color: Theme.surfaceVariantText
                }
            }
        }
    }

    Menu {
        id: bluetoothContextMenu
        width: 150
        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent

        property var currentDevice: null

        readonly property bool hasDevice: currentDevice !== null
        readonly property bool deviceConnected: currentDevice?.connected ?? false
        readonly property bool showCodecOption: hasDevice && deviceConnected && BluetoothService.isAudioDevice(currentDevice)

        background: Rectangle {
            color: Theme.withAlpha(Theme.surfaceContainer, Theme.popupTransparency)
            radius: Theme.cornerRadius
            border.width: 0
            border.color: Qt.rgba(Theme.outline.r, Theme.outline.g, Theme.outline.b, 0.12)
        }

        MenuItem {
            text: bluetoothContextMenu.deviceConnected ? I18n.tr("Disconnect") : I18n.tr("Connect")
            height: 32

            contentItem: StyledText {
                text: parent.text
                font.pixelSize: Theme.fontSizeSmall
                color: Theme.surfaceText
                leftPadding: Theme.spacingS
                verticalAlignment: Text.AlignVCenter
            }

            background: Rectangle {
                color: parent.hovered ? Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.08) : "transparent"
                radius: Theme.cornerRadius / 2
            }

            onTriggered: {
                if (!bluetoothContextMenu.hasDevice)
                    return;
                if (bluetoothContextMenu.deviceConnected) {
                    bluetoothContextMenu.currentDevice.disconnect();
                    return;
                }
                BluetoothService.connectDeviceWithTrust(bluetoothContextMenu.currentDevice);
            }
        }

        MenuItem {
            text: I18n.tr("Audio Codec")
            height: bluetoothContextMenu.showCodecOption ? 32 : 0
            visible: bluetoothContextMenu.showCodecOption

            contentItem: StyledText {
                text: parent.text
                font.pixelSize: Theme.fontSizeSmall
                color: Theme.surfaceText
                leftPadding: Theme.spacingS
                verticalAlignment: Text.AlignVCenter
            }

            background: Rectangle {
                color: parent.hovered ? Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.08) : "transparent"
                radius: Theme.cornerRadius / 2
            }

            onTriggered: {
                if (!bluetoothContextMenu.hasDevice)
                    return;
                if (!bluetoothContextMenu.currentDevice.connected)
                    return;
                if (!BluetoothService.isAudioDevice(bluetoothContextMenu.currentDevice))
                    return;
                showCodecSelector(bluetoothContextMenu.currentDevice);
            }
        }

        MenuItem {
            text: bluetoothContextMenu.currentDevice?.trusted ? I18n.tr("Untrust") : I18n.tr("Trust")
            height: 32

            contentItem: StyledText {
                text: parent.text
                font.pixelSize: Theme.fontSizeSmall
                color: Theme.surfaceText
                leftPadding: Theme.spacingS
                verticalAlignment: Text.AlignVCenter
            }

            background: Rectangle {
                color: parent.hovered ? Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.08) : "transparent"
                radius: Theme.cornerRadius / 2
            }

            onTriggered: {
                if (!bluetoothContextMenu.hasDevice)
                    return;
                bluetoothContextMenu.currentDevice.trusted = !bluetoothContextMenu.currentDevice.trusted;
            }
        }

        MenuItem {
            text: I18n.tr("Forget Device")
            height: 32

            contentItem: StyledText {
                text: parent.text
                font.pixelSize: Theme.fontSizeSmall
                color: Theme.error
                leftPadding: Theme.spacingS
                verticalAlignment: Text.AlignVCenter
            }

            background: Rectangle {
                color: parent.hovered ? Qt.rgba(Theme.error.r, Theme.error.g, Theme.error.b, 0.08) : "transparent"
                radius: Theme.cornerRadius / 2
            }

            onTriggered: {
                if (!bluetoothContextMenu.hasDevice)
                    return;
                if (!BluetoothService.enhancedPairingAvailable) {
                    bluetoothContextMenu.currentDevice.forget();
                    return;
                }

                const devicePath = BluetoothService.getDevicePath(bluetoothContextMenu.currentDevice);
                DMSService.bluetoothRemove(devicePath, response => {
                    if (!response.error)
                        return;
                    ToastService.showError(I18n.tr("Failed to remove device"), response.error);
                });
            }
        }
    }

    Connections {
        target: DMSService

        function onBluetoothPairingRequest(data) {
            const modal = PopoutService.bluetoothPairingModal;
            if (!modal)
                return;
            if (modal.token === data.token)
                return;
            modal.show(data);
        }
    }
}