Fix Bluetooth panel device list handling
This commit is contained in:
@@ -3,6 +3,18 @@ function deviceLabel(device) {
|
|||||||
return String(device.deviceName || device.name || "").trim()
|
return String(device.deviceName || device.name || "").trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function toArray(values) {
|
||||||
|
if (!values) return []
|
||||||
|
if (Array.isArray(values)) return values.slice()
|
||||||
|
|
||||||
|
var length = Number(values.length || 0)
|
||||||
|
if (!isFinite(length) || length <= 0) return []
|
||||||
|
|
||||||
|
var list = []
|
||||||
|
for (var i = 0; i < length; i++) list.push(values[i])
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
function isUuidLike(value) {
|
function isUuidLike(value) {
|
||||||
var text = String(value || "").trim()
|
var text = String(value || "").trim()
|
||||||
if (text === "") return false
|
if (text === "") return false
|
||||||
@@ -23,13 +35,13 @@ function hasHumanName(device) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function sortedByLabel(devices) {
|
function sortedByLabel(devices) {
|
||||||
var list = Array.isArray(devices) ? devices.slice() : []
|
var list = toArray(devices)
|
||||||
list.sort(function(a, b) { return deviceLabel(a).localeCompare(deviceLabel(b)) })
|
list.sort(function(a, b) { return deviceLabel(a).localeCompare(deviceLabel(b)) })
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
function deviceLists(devices) {
|
function deviceLists(devices) {
|
||||||
var values = Array.isArray(devices) ? devices : []
|
var values = toArray(devices)
|
||||||
var connected = []
|
var connected = []
|
||||||
var known = []
|
var known = []
|
||||||
var discovered = []
|
var discovered = []
|
||||||
@@ -86,6 +98,7 @@ function sectionDevices(lists, section) {
|
|||||||
if (typeof module !== "undefined") {
|
if (typeof module !== "undefined") {
|
||||||
module.exports = {
|
module.exports = {
|
||||||
deviceLabel: deviceLabel,
|
deviceLabel: deviceLabel,
|
||||||
|
toArray: toArray,
|
||||||
isUuidLike: isUuidLike,
|
isUuidLike: isUuidLike,
|
||||||
isAddressLike: isAddressLike,
|
isAddressLike: isAddressLike,
|
||||||
hasHumanName: hasHumanName,
|
hasHumanName: hasHumanName,
|
||||||
|
|||||||
@@ -219,9 +219,9 @@ Panel {
|
|||||||
|
|
||||||
function moveCursorH(delta) {
|
function moveCursorH(delta) {
|
||||||
if (!cursorActive) { cursorActive = true; return }
|
if (!cursorActive) { cursorActive = true; return }
|
||||||
if (focusSection !== "known") return
|
if (focusSection !== "known" && focusSection !== "connected") return
|
||||||
var dev = deviceAt(focusSection, selectedIndex)
|
var dev = deviceAt(focusSection, selectedIndex)
|
||||||
if (!dev || dev.connected) return
|
if (!dev || !dev.address) return
|
||||||
if (delta > 0) actionFocused = true
|
if (delta > 0) actionFocused = true
|
||||||
else if (delta < 0) actionFocused = false
|
else if (delta < 0) actionFocused = false
|
||||||
}
|
}
|
||||||
@@ -246,11 +246,10 @@ Panel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 'x' forgets remembered devices. Connected rows toggle connection via
|
// 'x' forgets remembered devices. For connected devices this first
|
||||||
// Enter/click, so the destructive forget action is intentionally unavailable
|
// disconnects, then removes the BlueZ pairing record via omarchy-bluetooth-device.
|
||||||
// while connected.
|
|
||||||
function deleteSelected() {
|
function deleteSelected() {
|
||||||
if (focusSection !== "known") return
|
if (focusSection !== "known" && focusSection !== "connected") return
|
||||||
var dev = deviceAt(focusSection, selectedIndex)
|
var dev = deviceAt(focusSection, selectedIndex)
|
||||||
if (!dev) return
|
if (!dev) return
|
||||||
forgetDevice(dev)
|
forgetDevice(dev)
|
||||||
@@ -626,7 +625,7 @@ Panel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
readonly property bool rowSelected: root.cursorActive && root.focusSection === sectionName && root.selectedIndex === rowIndex
|
readonly property bool rowSelected: root.cursorActive && root.focusSection === sectionName && root.selectedIndex === rowIndex
|
||||||
readonly property bool forgetAvailable: sectionName === "known" && !isConnected && !isDiscovered
|
readonly property bool forgetAvailable: (sectionName === "known" || sectionName === "connected") && !isDiscovered
|
||||||
readonly property bool showForgetButton: forgetAvailable && (rowMouse.containsMouse || rowSelected)
|
readonly property bool showForgetButton: forgetAvailable && (rowMouse.containsMouse || rowSelected)
|
||||||
|
|
||||||
hasCursor: rowSelected && !root.actionFocused
|
hasCursor: rowSelected && !root.actionFocused
|
||||||
|
|||||||
@@ -20,6 +20,17 @@ const devices = [
|
|||||||
{ name: 'Mouse', connected: false, trusted: true, address: '5' }
|
{ name: 'Mouse', connected: false, trusted: true, address: '5' }
|
||||||
]
|
]
|
||||||
|
|
||||||
|
const arrayLikeDevices = {
|
||||||
|
0: devices[0],
|
||||||
|
1: devices[1],
|
||||||
|
length: 2
|
||||||
|
}
|
||||||
|
assertDeepEqual(
|
||||||
|
bluetooth.toArray(arrayLikeDevices).map(bluetooth.deviceLabel),
|
||||||
|
['Speaker', 'Headphones'],
|
||||||
|
'bluetooth converts Quickshell QObjectList-style values into arrays'
|
||||||
|
)
|
||||||
|
|
||||||
const lists = bluetooth.deviceLists(devices)
|
const lists = bluetooth.deviceLists(devices)
|
||||||
assertDeepEqual(lists.connected.map(bluetooth.deviceLabel), ['Headphones'], 'bluetooth groups connected devices')
|
assertDeepEqual(lists.connected.map(bluetooth.deviceLabel), ['Headphones'], 'bluetooth groups connected devices')
|
||||||
assertDeepEqual(lists.known.map(bluetooth.deviceLabel), ['Mouse', 'Speaker'], 'bluetooth groups known devices by label')
|
assertDeepEqual(lists.known.map(bluetooth.deviceLabel), ['Mouse', 'Speaker'], 'bluetooth groups known devices by label')
|
||||||
@@ -27,6 +38,16 @@ assertDeepEqual(lists.discovered.map(bluetooth.deviceLabel), ['Keyboard'], 'blue
|
|||||||
assertDeepEqual(bluetooth.visibleSections(lists, true), ['connected', 'known', 'discovered'], 'bluetooth shows discovered section while scanning')
|
assertDeepEqual(bluetooth.visibleSections(lists, true), ['connected', 'known', 'discovered'], 'bluetooth shows discovered section while scanning')
|
||||||
assertDeepEqual(bluetooth.visibleSections(lists, false), ['connected', 'known'], 'bluetooth hides discovered section when not scanning')
|
assertDeepEqual(bluetooth.visibleSections(lists, false), ['connected', 'known'], 'bluetooth hides discovered section when not scanning')
|
||||||
|
|
||||||
|
const arrayLikeLists = bluetooth.deviceLists({
|
||||||
|
0: { name: 'Earbuds', connected: true, address: '6' },
|
||||||
|
1: { name: 'Trackpad', paired: true, address: '7' },
|
||||||
|
2: { name: 'Gamepad', address: '8' },
|
||||||
|
length: 3
|
||||||
|
})
|
||||||
|
assertDeepEqual(arrayLikeLists.connected.map(bluetooth.deviceLabel), ['Earbuds'], 'bluetooth groups connected devices from array-like values')
|
||||||
|
assertDeepEqual(arrayLikeLists.known.map(bluetooth.deviceLabel), ['Trackpad'], 'bluetooth groups known devices from array-like values')
|
||||||
|
assertDeepEqual(arrayLikeLists.discovered.map(bluetooth.deviceLabel), ['Gamepad'], 'bluetooth groups discovered devices from array-like values')
|
||||||
|
|
||||||
assertDeepEqual(
|
assertDeepEqual(
|
||||||
bluetooth.withPendingAction({ a: 'connecting' }, 'b', 'forgetting'),
|
bluetooth.withPendingAction({ a: 'connecting' }, 'b', 'forgetting'),
|
||||||
{ a: 'connecting', b: 'forgetting' },
|
{ a: 'connecting', b: 'forgetting' },
|
||||||
|
|||||||
Reference in New Issue
Block a user