1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-04-14 01:32:29 -04:00

bluetooth: improve performance of details

This commit is contained in:
bbedward
2026-02-03 11:30:38 -05:00
parent 3c4749ead0
commit 24ce41935e
2 changed files with 281 additions and 233 deletions

View File

@@ -16,9 +16,15 @@ Item {
property string currentCodec: "" property string currentCodec: ""
property bool isLoading: false property bool isLoading: false
readonly property bool deviceValid: device !== null && device.connected && BluetoothService.isAudioDevice(device)
signal codecSelected(string deviceAddress, string codecName) signal codecSelected(string deviceAddress, string codecName)
function show(bluetoothDevice) { function show(bluetoothDevice) {
if (!bluetoothDevice?.connected)
return;
if (!BluetoothService.isAudioDevice(bluetoothDevice))
return;
device = bluetoothDevice; device = bluetoothDevice;
isLoading = true; isLoading = true;
availableCodecs = []; availableCodecs = [];
@@ -35,14 +41,22 @@ Item {
modalVisible = false; modalVisible = false;
Qt.callLater(() => { Qt.callLater(() => {
visible = false; visible = false;
device = null;
}); });
} }
function queryCodecs() { function queryCodecs() {
if (!device) if (!deviceValid) {
hide();
return; return;
}
BluetoothService.getAvailableCodecs(device, function (codecs, current) { const capturedDevice = device;
const capturedAddress = device.address;
BluetoothService.getAvailableCodecs(capturedDevice, function (codecs, current) {
if (!root.deviceValid || root.device?.address !== capturedAddress)
return;
availableCodecs = codecs; availableCodecs = codecs;
currentCodec = current; currentCodec = current;
isLoading = false; isLoading = false;
@@ -50,27 +64,40 @@ Item {
} }
function selectCodec(profileName) { function selectCodec(profileName) {
if (!device || isLoading) if (!deviceValid || isLoading)
return; return;
let selectedCodec = availableCodecs.find(c => c.profile === profileName); const capturedDevice = device;
if (selectedCodec && device) { const capturedAddress = device.address;
BluetoothService.updateDeviceCodec(device.address, selectedCodec.name);
codecSelected(device.address, selectedCodec.name); const selectedCodec = availableCodecs.find(c => c.profile === profileName);
} if (!selectedCodec)
return;
BluetoothService.updateDeviceCodec(capturedAddress, selectedCodec.name);
codecSelected(capturedAddress, selectedCodec.name);
isLoading = true; isLoading = true;
BluetoothService.switchCodec(device, profileName, function (success, message) { BluetoothService.switchCodec(capturedDevice, profileName, function (success, message) {
if (!root.device || root.device.address !== capturedAddress)
return;
isLoading = false; isLoading = false;
if (success) { if (success) {
ToastService.showToast(message, ToastService.levelInfo); ToastService.showToast(message, ToastService.levelInfo);
Qt.callLater(root.hide); Qt.callLater(root.hide);
} else { return;
ToastService.showToast(message, ToastService.levelError);
} }
ToastService.showToast(message, ToastService.levelError);
}); });
} }
onDeviceValidChanged: {
if (modalVisible && !deviceValid) {
hide();
}
}
visible: false visible: false
anchors.fill: parent anchors.fill: parent
z: 2000 z: 2000

View File

@@ -5,7 +5,6 @@ import Quickshell.Bluetooth
import qs.Common import qs.Common
import qs.Services import qs.Services
import qs.Widgets import qs.Widgets
import qs.Modals
Rectangle { Rectangle {
id: root id: root
@@ -14,10 +13,11 @@ Rectangle {
LayoutMirroring.childrenInherit: true LayoutMirroring.childrenInherit: true
implicitHeight: { implicitHeight: {
if (height > 0) { if (height > 0)
return height return height;
} if (!BluetoothService.adapter?.enabled)
return BluetoothService.adapter && BluetoothService.adapter.enabled ? headerRow.height + bluetoothContent.height + Theme.spacingM : headerRow.height return headerRow.height;
return headerRow.height + bluetoothContent.height + Theme.spacingM;
} }
radius: Theme.cornerRadius radius: Theme.cornerRadius
color: Theme.withAlpha(Theme.surfaceContainerHigh, Theme.popupTransparency) color: Theme.withAlpha(Theme.surfaceContainerHigh, Theme.popupTransparency)
@@ -30,41 +30,56 @@ Rectangle {
signal showCodecSelector(var device) signal showCodecSelector(var device)
function isDeviceBeingPaired(deviceAddress) { function isDeviceBeingPaired(deviceAddress) {
return devicesBeingPaired.has(deviceAddress) return devicesBeingPaired.has(deviceAddress);
} }
function handlePairDevice(device) { function handlePairDevice(device) {
if (!device) return if (!device)
return;
const deviceAddr = device.address;
const pairingSet = devicesBeingPaired;
const deviceAddr = device.address pairingSet.add(deviceAddr);
const pairingSet = devicesBeingPaired devicesBeingPairedChanged();
pairingSet.add(deviceAddr) BluetoothService.pairDevice(device, function (response) {
devicesBeingPairedChanged() pairingSet.delete(deviceAddr);
devicesBeingPairedChanged();
BluetoothService.pairDevice(device, function(response) {
pairingSet.delete(deviceAddr)
devicesBeingPairedChanged()
if (response.error) { if (response.error) {
ToastService.showError(I18n.tr("Pairing failed"), response.error) ToastService.showError(I18n.tr("Pairing failed"), response.error);
} else if (!BluetoothService.enhancedPairingAvailable) { return;
ToastService.showSuccess(I18n.tr("Device paired"))
} }
}) if (!BluetoothService.enhancedPairingAvailable) {
ToastService.showSuccess(I18n.tr("Device paired"));
}
});
} }
function updateDeviceCodecDisplay(deviceAddress, codecName) { function updateDeviceCodecDisplay(deviceAddress, codecName) {
for (let i = 0; i < pairedRepeater.count; i++) { for (let i = 0; i < pairedRepeater.count; i++) {
let item = pairedRepeater.itemAt(i) const item = pairedRepeater.itemAt(i);
if (item && item.modelData && item.modelData.address === deviceAddress) { if (!item?.modelData)
item.currentCodec = codecName continue;
break 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 { Row {
id: headerRow id: headerRow
@@ -92,32 +107,32 @@ Rectangle {
Rectangle { Rectangle {
id: scanButton id: scanButton
readonly property bool adapterEnabled: BluetoothService.adapter?.enabled ?? false
readonly property bool isDiscovering: BluetoothService.adapter?.discovering ?? false
width: 100 width: 100
height: 36 height: 36
radius: 18 radius: 18
color: { color: scanMouseArea.containsMouse && adapterEnabled ? Theme.withAlpha(Theme.surfaceContainerHigh, Theme.popupTransparency) : "transparent"
if (!BluetoothService.adapter || !BluetoothService.adapter.enabled) border.color: adapterEnabled ? Theme.primary : Qt.rgba(Theme.outline.r, Theme.outline.g, Theme.outline.b, 0.12)
return Theme.withAlpha(Theme.surfaceContainerHigh, Theme.popupTransparency)
return scanMouseArea.containsMouse ? Theme.withAlpha(Theme.surfaceContainerHigh, Theme.popupTransparency) : "transparent"
}
border.color: BluetoothService.adapter && BluetoothService.adapter.enabled ? Theme.primary : Qt.rgba(Theme.outline.r, Theme.outline.g, Theme.outline.b, 0.12)
border.width: 0 border.width: 0
visible: BluetoothService.adapter && BluetoothService.adapter.enabled visible: adapterEnabled
Row { Row {
anchors.centerIn: parent anchors.centerIn: parent
spacing: Theme.spacingXS spacing: Theme.spacingXS
DankIcon { DankIcon {
name: BluetoothService.adapter && BluetoothService.adapter.discovering ? "stop" : "bluetooth_searching" name: scanButton.isDiscovering ? "stop" : "bluetooth_searching"
size: 18 size: 18
color: BluetoothService.adapter && BluetoothService.adapter.enabled ? Theme.primary : Theme.surfaceVariantText color: scanButton.adapterEnabled ? Theme.primary : Theme.surfaceVariantText
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
} }
StyledText { StyledText {
text: BluetoothService.adapter && BluetoothService.adapter.discovering ? I18n.tr("Scanning") : I18n.tr("Scan") text: scanButton.isDiscovering ? I18n.tr("Scanning") : I18n.tr("Scan")
color: BluetoothService.adapter && BluetoothService.adapter.enabled ? Theme.primary : Theme.surfaceVariantText color: scanButton.adapterEnabled ? Theme.primary : Theme.surfaceVariantText
font.pixelSize: Theme.fontSizeMedium font.pixelSize: Theme.fontSizeMedium
font.weight: Font.Medium font.weight: Font.Medium
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
@@ -128,11 +143,12 @@ Rectangle {
id: scanMouseArea id: scanMouseArea
anchors.fill: parent anchors.fill: parent
hoverEnabled: true hoverEnabled: true
enabled: BluetoothService.adapter && BluetoothService.adapter.enabled enabled: scanButton.adapterEnabled
cursorShape: enabled ? Qt.PointingHandCursor : Qt.ArrowCursor cursorShape: enabled ? Qt.PointingHandCursor : Qt.ArrowCursor
onClicked: { onClicked: {
if (BluetoothService.adapter) if (!BluetoothService.adapter)
BluetoothService.adapter.discovering = !BluetoothService.adapter.discovering return;
BluetoothService.adapter.discovering = !BluetoothService.adapter.discovering;
} }
} }
} }
@@ -146,88 +162,92 @@ Rectangle {
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
anchors.margins: Theme.spacingM anchors.margins: Theme.spacingM
anchors.topMargin: Theme.spacingM anchors.topMargin: Theme.spacingM
visible: BluetoothService.adapter && BluetoothService.adapter.enabled visible: BluetoothService.adapter?.enabled ?? false
contentHeight: bluetoothColumn.height contentHeight: bluetoothColumn.height
clip: true clip: true
property int maxPinnedDevices: 3 readonly property int maxPinnedDevices: 3
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"])
}
Column { Column {
id: bluetoothColumn id: bluetoothColumn
width: parent.width width: parent.width
spacing: Theme.spacingS 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 { Repeater {
id: pairedRepeater id: pairedRepeater
model: { model: pairedDevicesModel
if (!BluetoothService.adapter || !BluetoothService.adapter.devices)
return []
const pinnedList = bluetoothContent.getPinnedDevices()
let devices = [...BluetoothService.adapter.devices.values.filter(dev => dev && (dev.paired || dev.trusted))]
devices.sort((a, b) => {
// Pinned device first
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
}
// Then connected devices
if (a.connected && !b.connected) return -1
if (!a.connected && b.connected) return 1
// Then by signal strength
return (b.signalStrength || 0) - (a.signalStrength || 0)
})
return devices
}
delegate: Rectangle { delegate: Rectangle {
id: pairedDelegate
required property var modelData required property var modelData
required property int index required property int index
property string currentCodec: BluetoothService.deviceCodecs[modelData.address] || "" 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 width: parent.width
height: 50 height: 50
radius: Theme.cornerRadius radius: Theme.cornerRadius
border.width: 0
Component.onCompleted: { Component.onCompleted: {
if (modelData.connected && BluetoothService.isAudioDevice(modelData)) { if (!isConnected)
BluetoothService.refreshDeviceCodec(modelData) return;
} if (!BluetoothService.isAudioDevice(modelData))
return;
BluetoothService.refreshDeviceCodec(modelData);
} }
color: { color: {
if (modelData.state === BluetoothDeviceState.Connecting) if (isConnecting)
return Qt.rgba(Theme.warning.r, Theme.warning.g, Theme.warning.b, 0.12) return Qt.rgba(Theme.warning.r, Theme.warning.g, Theme.warning.b, 0.12);
if (deviceMouseArea.containsMouse) if (deviceMouseArea.containsMouse)
return Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.08) return Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.08);
return Theme.withAlpha(Theme.surfaceContainerHighest, Theme.popupTransparency) return Theme.withAlpha(Theme.surfaceContainerHighest, Theme.popupTransparency);
} }
border.color: { border.color: {
if (modelData.state === BluetoothDeviceState.Connecting) if (isConnecting)
return Theme.warning return Theme.warning;
if (modelData.connected) if (isConnected)
return Theme.primary return Theme.primary;
return Qt.rgba(Theme.outline.r, Theme.outline.g, Theme.outline.b, 0.12) return Qt.rgba(Theme.outline.r, Theme.outline.g, Theme.outline.b, 0.12);
} }
border.width: 0
Row { Row {
anchors.left: parent.left anchors.left: parent.left
@@ -236,16 +256,16 @@ Rectangle {
spacing: Theme.spacingS spacing: Theme.spacingS
DankIcon { DankIcon {
name: BluetoothService.getDeviceIcon(modelData) name: BluetoothService.getDeviceIcon(pairedDelegate.modelData)
size: Theme.iconSize - 4 size: Theme.iconSize - 4
color: {
if (modelData.state === BluetoothDeviceState.Connecting)
return Theme.warning
if (modelData.connected)
return Theme.primary
return Theme.surfaceText
}
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
color: {
if (pairedDelegate.isConnecting)
return Theme.warning;
if (pairedDelegate.isConnected)
return Theme.primary;
return Theme.surfaceText;
}
} }
Column { Column {
@@ -253,10 +273,10 @@ Rectangle {
width: 200 width: 200
StyledText { StyledText {
text: modelData.name || modelData.deviceName || I18n.tr("Unknown Device") text: pairedDelegate.deviceName
font.pixelSize: Theme.fontSizeMedium font.pixelSize: Theme.fontSizeMedium
color: Theme.surfaceText color: Theme.surfaceText
font.weight: modelData.connected ? Font.Medium : Font.Normal font.weight: pairedDelegate.isConnected ? Font.Medium : Font.Normal
elide: Text.ElideRight elide: Text.ElideRight
width: parent.width width: parent.width
horizontalAlignment: Text.AlignLeft horizontalAlignment: Text.AlignLeft
@@ -267,36 +287,29 @@ Rectangle {
StyledText { StyledText {
text: { text: {
if (modelData.state === BluetoothDeviceState.Connecting) if (pairedDelegate.isConnecting)
return I18n.tr("Connecting...") return I18n.tr("Connecting...");
if (modelData.connected) { if (!pairedDelegate.isConnected)
let status = I18n.tr("Connected") return I18n.tr("Paired");
if (currentCodec) { if (!pairedDelegate.currentCodec)
status += " • " + currentCodec return I18n.tr("Connected");
} return I18n.tr("Connected") + " • " + pairedDelegate.currentCodec;
return status
}
return I18n.tr("Paired")
} }
font.pixelSize: Theme.fontSizeSmall font.pixelSize: Theme.fontSizeSmall
color: { color: pairedDelegate.isConnecting ? Theme.warning : Theme.surfaceVariantText
if (modelData.state === BluetoothDeviceState.Connecting)
return Theme.warning
return Theme.surfaceVariantText
}
} }
StyledText { 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: { text: {
if (modelData.batteryAvailable && modelData.battery > 0) if (pairedDelegate.modelData.batteryAvailable && pairedDelegate.modelData.battery > 0)
return "• " + Math.round(modelData.battery * 100) + "%" return "• " + Math.round(pairedDelegate.modelData.battery * 100) + "%";
if (btBattery)
var btBattery = BatteryService.bluetoothDevices.find(dev => { return "• " + btBattery.percentage + "%";
return dev.name === (modelData.name || modelData.deviceName) || return "";
dev.name.toLowerCase().includes((modelData.name || modelData.deviceName).toLowerCase()) ||
(modelData.name || modelData.deviceName).toLowerCase().includes(dev.name.toLowerCase())
})
return btBattery ? "• " + btBattery.percentage + "%" : ""
} }
font.pixelSize: Theme.fontSizeSmall font.pixelSize: Theme.fontSizeSmall
color: Theme.surfaceVariantText color: Theme.surfaceVariantText
@@ -304,7 +317,7 @@ Rectangle {
} }
StyledText { StyledText {
text: modelData.signalStrength !== undefined && modelData.signalStrength > 0 ? "• " + modelData.signalStrength + "%" : "" text: pairedDelegate.modelData.signalStrength > 0 ? "• " + pairedDelegate.modelData.signalStrength + "%" : ""
font.pixelSize: Theme.fontSizeSmall font.pixelSize: Theme.fontSizeSmall
color: Theme.surfaceVariantText color: Theme.surfaceVariantText
visible: text.length > 0 visible: text.length > 0
@@ -320,10 +333,7 @@ Rectangle {
width: pinBluetoothRow.width + Theme.spacingS * 2 width: pinBluetoothRow.width + Theme.spacingS * 2
height: 28 height: 28
radius: height / 2 radius: height / 2
color: { color: pairedDelegate.isPinned ? Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.12) : Theme.withAlpha(Theme.surfaceText, 0.05)
const isThisDevicePinned = bluetoothContent.getPinnedDevices().includes(modelData.address)
return isThisDevicePinned ? Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.12) : Theme.withAlpha(Theme.surfaceText, 0.05)
}
Row { Row {
id: pinBluetoothRow id: pinBluetoothRow
@@ -333,23 +343,14 @@ Rectangle {
DankIcon { DankIcon {
name: "push_pin" name: "push_pin"
size: 16 size: 16
color: { color: pairedDelegate.isPinned ? Theme.primary : Theme.surfaceText
const isThisDevicePinned = bluetoothContent.getPinnedDevices().includes(modelData.address)
return isThisDevicePinned ? Theme.primary : Theme.surfaceText
}
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
} }
StyledText { StyledText {
text: { text: pairedDelegate.isPinned ? I18n.tr("Pinned") : I18n.tr("Pin")
const isThisDevicePinned = bluetoothContent.getPinnedDevices().includes(modelData.address)
return isThisDevicePinned ? I18n.tr("Pinned") : I18n.tr("Pin")
}
font.pixelSize: Theme.fontSizeSmall font.pixelSize: Theme.fontSizeSmall
color: { color: pairedDelegate.isPinned ? Theme.primary : Theme.surfaceText
const isThisDevicePinned = bluetoothContent.getPinnedDevices().includes(modelData.address)
return isThisDevicePinned ? Theme.primary : Theme.surfaceText
}
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
} }
} }
@@ -358,24 +359,25 @@ Rectangle {
anchors.fill: parent anchors.fill: parent
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: { onClicked: {
const pins = JSON.parse(JSON.stringify(SettingsData.bluetoothDevicePins || {})) const pins = JSON.parse(JSON.stringify(SettingsData.bluetoothDevicePins || {}));
let pinnedList = bluetoothContent.normalizePinList(pins["preferredDevice"]) let pinnedList = root.normalizePinList(pins["preferredDevice"]);
const pinIndex = pinnedList.indexOf(modelData.address) const pinIndex = pinnedList.indexOf(pairedDelegate.modelData.address);
if (pinIndex !== -1) { if (pinIndex !== -1) {
pinnedList.splice(pinIndex, 1) pinnedList.splice(pinIndex, 1);
} else { } else {
pinnedList.unshift(modelData.address) pinnedList.unshift(pairedDelegate.modelData.address);
if (pinnedList.length > bluetoothContent.maxPinnedDevices) if (pinnedList.length > bluetoothContent.maxPinnedDevices)
pinnedList = pinnedList.slice(0, bluetoothContent.maxPinnedDevices) pinnedList = pinnedList.slice(0, bluetoothContent.maxPinnedDevices);
} }
if (pinnedList.length > 0) if (pinnedList.length > 0) {
pins["preferredDevice"] = pinnedList pins["preferredDevice"] = pinnedList;
else } else {
delete pins["preferredDevice"] delete pins["preferredDevice"];
}
SettingsData.set("bluetoothDevicePins", pins) SettingsData.set("bluetoothDevicePins", pins);
} }
} }
} }
@@ -389,11 +391,11 @@ Rectangle {
buttonSize: 28 buttonSize: 28
onClicked: { onClicked: {
if (bluetoothContextMenu.visible) { if (bluetoothContextMenu.visible) {
bluetoothContextMenu.close() bluetoothContextMenu.close();
} else { return;
bluetoothContextMenu.currentDevice = modelData
bluetoothContextMenu.popup(pairedOptionsButton, -bluetoothContextMenu.width + pairedOptionsButton.width, pairedOptionsButton.height + Theme.spacingXS)
} }
bluetoothContextMenu.currentDevice = pairedDelegate.modelData;
bluetoothContextMenu.popup(pairedOptionsButton, -bluetoothContextMenu.width + pairedOptionsButton.width, pairedOptionsButton.height + Theme.spacingXS);
} }
} }
@@ -404,11 +406,11 @@ Rectangle {
hoverEnabled: true hoverEnabled: true
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: { onClicked: {
if (modelData.connected) { if (pairedDelegate.isConnected) {
modelData.disconnect() pairedDelegate.modelData.disconnect();
} else { return;
BluetoothService.connectDeviceWithTrust(modelData)
} }
BluetoothService.connectDeviceWithTrust(pairedDelegate.modelData);
} }
} }
} }
@@ -421,11 +423,10 @@ Rectangle {
visible: pairedRepeater.count > 0 && availableRepeater.count > 0 visible: pairedRepeater.count > 0 && availableRepeater.count > 0
} }
Item { Item {
width: parent.width width: parent.width
height: 80 height: 80
visible: BluetoothService.adapter && BluetoothService.adapter.discovering && availableRepeater.count === 0 visible: (BluetoothService.adapter?.discovering ?? false) && availableRepeater.count === 0
DankIcon { DankIcon {
anchors.centerIn: parent anchors.centerIn: parent
@@ -434,7 +435,7 @@ Rectangle {
color: Qt.rgba(Theme.surfaceText.r || 0.8, Theme.surfaceText.g || 0.8, Theme.surfaceText.b || 0.8, 0.4) color: Qt.rgba(Theme.surfaceText.r || 0.8, Theme.surfaceText.g || 0.8, Theme.surfaceText.b || 0.8, 0.4)
RotationAnimation on rotation { RotationAnimation on rotation {
running: parent.visible && BluetoothService.adapter && BluetoothService.adapter.discovering && availableRepeater.count === 0 running: parent.visible
loops: Animation.Infinite loops: Animation.Infinite
from: 0 from: 0
to: 360 to: 360
@@ -443,33 +444,41 @@ Rectangle {
} }
} }
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 { Repeater {
id: availableRepeater id: availableRepeater
model: { model: availableDevicesModel
if (!BluetoothService.adapter || !BluetoothService.adapter.discovering || !Bluetooth.devices)
return []
var filtered = Bluetooth.devices.values.filter(dev => {
return dev && !dev.paired && !dev.pairing && !dev.blocked &&
(dev.signalStrength === undefined || dev.signalStrength > 0)
})
return BluetoothService.sortDevices(filtered)
}
delegate: Rectangle { delegate: Rectangle {
id: availableDelegate
required property var modelData required property var modelData
required property int index required property int index
property bool canConnect: BluetoothService.canConnect(modelData) readonly property bool canConnect: BluetoothService.canConnect(modelData)
property bool isBusy: BluetoothService.isDeviceBusy(modelData) || isDeviceBeingPaired(modelData.address) 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 width: parent.width
height: 50 height: 50
radius: Theme.cornerRadius radius: Theme.cornerRadius
color: availableMouseArea.containsMouse && !isBusy ? Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.08) : Theme.withAlpha(Theme.surfaceContainerHighest, Theme.popupTransparency) color: availableMouseArea.containsMouse && isInteractive ? Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.08) : Theme.withAlpha(Theme.surfaceContainerHighest, Theme.popupTransparency)
border.color: Qt.rgba(Theme.outline.r, Theme.outline.g, Theme.outline.b, 0.12) border.color: Qt.rgba(Theme.outline.r, Theme.outline.g, Theme.outline.b, 0.12)
border.width: 0 border.width: 0
opacity: (canConnect && !isBusy) ? 1 : 0.6 opacity: isInteractive ? 1 : 0.6
Row { Row {
anchors.left: parent.left anchors.left: parent.left
@@ -478,7 +487,7 @@ Rectangle {
spacing: Theme.spacingS spacing: Theme.spacingS
DankIcon { DankIcon {
name: BluetoothService.getDeviceIcon(modelData) name: BluetoothService.getDeviceIcon(availableDelegate.modelData)
size: Theme.iconSize - 4 size: Theme.iconSize - 4
color: Theme.surfaceText color: Theme.surfaceText
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
@@ -489,7 +498,7 @@ Rectangle {
width: 200 width: 200
StyledText { StyledText {
text: modelData.name || modelData.deviceName || I18n.tr("Unknown Device") text: availableDelegate.deviceName
font.pixelSize: Theme.fontSizeMedium font.pixelSize: Theme.fontSizeMedium
color: Theme.surfaceText color: Theme.surfaceText
elide: Text.ElideRight elide: Text.ElideRight
@@ -502,19 +511,21 @@ Rectangle {
StyledText { StyledText {
text: { text: {
if (modelData.pairing || isBusy) return I18n.tr("Pairing...") if (availableDelegate.modelData.pairing || availableDelegate.isBusy)
if (modelData.blocked) return I18n.tr("Blocked") return I18n.tr("Pairing...");
return BluetoothService.getSignalStrength(modelData) if (availableDelegate.modelData.blocked)
return I18n.tr("Blocked");
return BluetoothService.getSignalStrength(availableDelegate.modelData);
} }
font.pixelSize: Theme.fontSizeSmall font.pixelSize: Theme.fontSizeSmall
color: Theme.surfaceVariantText color: Theme.surfaceVariantText
} }
StyledText { StyledText {
text: modelData.signalStrength !== undefined && modelData.signalStrength > 0 ? "• " + modelData.signalStrength + "%" : "" text: availableDelegate.modelData.signalStrength > 0 ? "• " + availableDelegate.modelData.signalStrength + "%" : ""
font.pixelSize: Theme.fontSizeSmall font.pixelSize: Theme.fontSizeSmall
color: Theme.surfaceVariantText color: Theme.surfaceVariantText
visible: text.length > 0 && !modelData.pairing && !modelData.blocked visible: text.length > 0 && !availableDelegate.modelData.pairing && !availableDelegate.modelData.blocked
} }
} }
} }
@@ -525,12 +536,14 @@ Rectangle {
anchors.rightMargin: Theme.spacingM anchors.rightMargin: Theme.spacingM
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
text: { text: {
if (isBusy) return I18n.tr("Pairing...") if (availableDelegate.isBusy)
if (!canConnect) return I18n.tr("Cannot pair") return I18n.tr("Pairing...");
return I18n.tr("Pair") if (!availableDelegate.canConnect)
return I18n.tr("Cannot pair");
return I18n.tr("Pair");
} }
font.pixelSize: Theme.fontSizeSmall font.pixelSize: Theme.fontSizeSmall
color: (canConnect && !isBusy) ? Theme.primary : Theme.surfaceVariantText color: availableDelegate.isInteractive ? Theme.primary : Theme.surfaceVariantText
font.weight: Font.Medium font.weight: Font.Medium
} }
@@ -538,13 +551,10 @@ Rectangle {
id: availableMouseArea id: availableMouseArea
anchors.fill: parent anchors.fill: parent
hoverEnabled: true hoverEnabled: true
cursorShape: canConnect && !isBusy ? Qt.PointingHandCursor : Qt.ArrowCursor cursorShape: availableDelegate.isInteractive ? Qt.PointingHandCursor : Qt.ArrowCursor
enabled: canConnect && !isBusy enabled: availableDelegate.isInteractive
onClicked: { onClicked: root.handlePairDevice(availableDelegate.modelData)
root.handlePairDevice(modelData)
}
} }
} }
} }
@@ -570,6 +580,10 @@ Rectangle {
property var currentDevice: null 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 { background: Rectangle {
color: Theme.withAlpha(Theme.surfaceContainer, Theme.popupTransparency) color: Theme.withAlpha(Theme.surfaceContainer, Theme.popupTransparency)
radius: Theme.cornerRadius radius: Theme.cornerRadius
@@ -578,7 +592,7 @@ Rectangle {
} }
MenuItem { MenuItem {
text: bluetoothContextMenu.currentDevice && bluetoothContextMenu.currentDevice.connected ? I18n.tr("Disconnect") : I18n.tr("Connect") text: bluetoothContextMenu.deviceConnected ? I18n.tr("Disconnect") : I18n.tr("Connect")
height: 32 height: 32
contentItem: StyledText { contentItem: StyledText {
@@ -595,20 +609,20 @@ Rectangle {
} }
onTriggered: { onTriggered: {
if (bluetoothContextMenu.currentDevice) { if (!bluetoothContextMenu.hasDevice)
if (bluetoothContextMenu.currentDevice.connected) { return;
bluetoothContextMenu.currentDevice.disconnect() if (bluetoothContextMenu.deviceConnected) {
} else { bluetoothContextMenu.currentDevice.disconnect();
BluetoothService.connectDeviceWithTrust(bluetoothContextMenu.currentDevice) return;
}
} }
BluetoothService.connectDeviceWithTrust(bluetoothContextMenu.currentDevice);
} }
} }
MenuItem { MenuItem {
text: I18n.tr("Audio Codec") text: I18n.tr("Audio Codec")
height: bluetoothContextMenu.currentDevice && BluetoothService.isAudioDevice(bluetoothContextMenu.currentDevice) && bluetoothContextMenu.currentDevice.connected ? 32 : 0 height: bluetoothContextMenu.showCodecOption ? 32 : 0
visible: bluetoothContextMenu.currentDevice && BluetoothService.isAudioDevice(bluetoothContextMenu.currentDevice) && bluetoothContextMenu.currentDevice.connected visible: bluetoothContextMenu.showCodecOption
contentItem: StyledText { contentItem: StyledText {
text: parent.text text: parent.text
@@ -624,9 +638,13 @@ Rectangle {
} }
onTriggered: { onTriggered: {
if (bluetoothContextMenu.currentDevice) { if (!bluetoothContextMenu.hasDevice)
showCodecSelector(bluetoothContextMenu.currentDevice) return;
} if (!bluetoothContextMenu.currentDevice.connected)
return;
if (!BluetoothService.isAudioDevice(bluetoothContextMenu.currentDevice))
return;
showCodecSelector(bluetoothContextMenu.currentDevice);
} }
} }
@@ -648,18 +666,19 @@ Rectangle {
} }
onTriggered: { onTriggered: {
if (bluetoothContextMenu.currentDevice) { if (!bluetoothContextMenu.hasDevice)
if (BluetoothService.enhancedPairingAvailable) { return;
const devicePath = BluetoothService.getDevicePath(bluetoothContextMenu.currentDevice) if (!BluetoothService.enhancedPairingAvailable) {
DMSService.bluetoothRemove(devicePath, response => { bluetoothContextMenu.currentDevice.forget();
if (response.error) { return;
ToastService.showError(I18n.tr("Failed to remove device"), response.error)
}
})
} else {
bluetoothContextMenu.currentDevice.forget()
}
} }
const devicePath = BluetoothService.getDevicePath(bluetoothContextMenu.currentDevice);
DMSService.bluetoothRemove(devicePath, response => {
if (!response.error)
return;
ToastService.showError(I18n.tr("Failed to remove device"), response.error);
});
} }
} }
} }
@@ -668,10 +687,12 @@ Rectangle {
target: DMSService target: DMSService
function onBluetoothPairingRequest(data) { function onBluetoothPairingRequest(data) {
const modal = PopoutService.bluetoothPairingModal const modal = PopoutService.bluetoothPairingModal;
if (modal && modal.token !== data.token) { if (!modal)
modal.show(data) return;
} if (modal.token === data.token)
return;
modal.show(data);
} }
} }
} }