Add shell plugin model tests
This commit is contained in:
@@ -5,6 +5,7 @@ import Quickshell.Io
|
||||
import Quickshell.Bluetooth
|
||||
import qs.Ui
|
||||
import qs.Commons
|
||||
import "BluetoothModel.js" as BluetoothModel
|
||||
|
||||
Panel {
|
||||
id: root
|
||||
@@ -20,66 +21,25 @@ Panel {
|
||||
readonly property var devices: Bluetooth.devices ? Bluetooth.devices.values : []
|
||||
|
||||
function deviceLabel(device) {
|
||||
if (!device) return ""
|
||||
return String(device.deviceName || device.name || "").trim()
|
||||
return BluetoothModel.deviceLabel(device)
|
||||
}
|
||||
|
||||
function isUuidLike(value) {
|
||||
var text = (value || "").trim()
|
||||
if (text === "") return false
|
||||
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(text)
|
||||
|| /^[0-9a-f]{32}$/i.test(text)
|
||||
|| /^0x[0-9a-f]{4,32}$/i.test(text)
|
||||
|| /^0000[0-9a-f]{4}-0000-1000-8000-00805f9b34fb$/i.test(text)
|
||||
return BluetoothModel.isUuidLike(value)
|
||||
}
|
||||
|
||||
function isAddressLike(value) {
|
||||
var text = (value || "").trim()
|
||||
return /^([0-9a-f]{2}[:-]){5}[0-9a-f]{2}$/i.test(text)
|
||||
return BluetoothModel.isAddressLike(value)
|
||||
}
|
||||
|
||||
function hasHumanName(device) {
|
||||
var label = deviceLabel(device)
|
||||
return label !== "" && !isUuidLike(label) && !isAddressLike(label)
|
||||
return BluetoothModel.hasHumanName(device)
|
||||
}
|
||||
|
||||
readonly property var connectedDevices: {
|
||||
var list = []
|
||||
for (var i = 0; i < devices.length; i++) {
|
||||
var d = devices[i]
|
||||
if (d && d.connected && hasHumanName(d)) list.push(d)
|
||||
}
|
||||
list.sort(function(a, b) {
|
||||
return deviceLabel(a).localeCompare(deviceLabel(b))
|
||||
})
|
||||
return list
|
||||
}
|
||||
|
||||
readonly property var knownDevices: {
|
||||
var list = []
|
||||
for (var i = 0; i < devices.length; i++) {
|
||||
var d = devices[i]
|
||||
if (d && hasHumanName(d) && !d.connected && (d.paired || d.bonded || d.trusted)) list.push(d)
|
||||
}
|
||||
list.sort(function(a, b) {
|
||||
return deviceLabel(a).localeCompare(deviceLabel(b))
|
||||
})
|
||||
return list
|
||||
}
|
||||
|
||||
readonly property var discoveredDevices: {
|
||||
var list = []
|
||||
for (var i = 0; i < devices.length; i++) {
|
||||
var d = devices[i]
|
||||
if (!d || !hasHumanName(d)) continue
|
||||
if (d.connected || d.paired || d.bonded || d.trusted) continue
|
||||
list.push(d)
|
||||
}
|
||||
list.sort(function(a, b) {
|
||||
return deviceLabel(a).localeCompare(deviceLabel(b))
|
||||
})
|
||||
return list
|
||||
}
|
||||
readonly property var deviceGroups: BluetoothModel.deviceLists(devices)
|
||||
readonly property var connectedDevices: deviceGroups.connected || []
|
||||
readonly property var knownDevices: deviceGroups.known || []
|
||||
readonly property var discoveredDevices: deviceGroups.discovered || []
|
||||
|
||||
readonly property string icon: {
|
||||
if (!adapter) return ""
|
||||
@@ -145,18 +105,11 @@ Panel {
|
||||
}
|
||||
|
||||
readonly property var visibleSections: {
|
||||
var list = []
|
||||
if (sectionVisible("connected")) list.push("connected")
|
||||
if (sectionVisible("known")) list.push("known")
|
||||
if (sectionVisible("discovered")) list.push("discovered")
|
||||
return list
|
||||
return BluetoothModel.visibleSections(deviceGroups, adapter && adapter.discovering)
|
||||
}
|
||||
|
||||
function devicesForSection(section) {
|
||||
if (section === "connected") return connectedDevices
|
||||
if (section === "known") return knownDevices
|
||||
if (section === "discovered") return discoveredDevices
|
||||
return []
|
||||
return BluetoothModel.sectionDevices(deviceGroups, section)
|
||||
}
|
||||
|
||||
function deviceAt(section, index) {
|
||||
@@ -165,21 +118,16 @@ Panel {
|
||||
}
|
||||
|
||||
function cloneMap(map) {
|
||||
var next = ({})
|
||||
for (var key in map) next[key] = map[key]
|
||||
return next
|
||||
return BluetoothModel.cloneMap(map)
|
||||
}
|
||||
|
||||
function pendingAction(address) {
|
||||
return address && pendingActions[address] ? pendingActions[address] : ""
|
||||
return BluetoothModel.pendingAction(pendingActions, address)
|
||||
}
|
||||
|
||||
function setPendingAction(address, action) {
|
||||
if (!address) return
|
||||
var next = cloneMap(pendingActions)
|
||||
if (action) next[address] = action
|
||||
else delete next[address]
|
||||
pendingActions = next
|
||||
pendingActions = BluetoothModel.withPendingAction(pendingActions, address, action)
|
||||
if (action) pendingTimeout.restart()
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
function deviceLabel(device) {
|
||||
if (!device) return ""
|
||||
return String(device.deviceName || device.name || "").trim()
|
||||
}
|
||||
|
||||
function isUuidLike(value) {
|
||||
var text = String(value || "").trim()
|
||||
if (text === "") return false
|
||||
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(text)
|
||||
|| /^[0-9a-f]{32}$/i.test(text)
|
||||
|| /^0x[0-9a-f]{4,32}$/i.test(text)
|
||||
|| /^0000[0-9a-f]{4}-0000-1000-8000-00805f9b34fb$/i.test(text)
|
||||
}
|
||||
|
||||
function isAddressLike(value) {
|
||||
var text = String(value || "").trim()
|
||||
return /^([0-9a-f]{2}[:-]){5}[0-9a-f]{2}$/i.test(text)
|
||||
}
|
||||
|
||||
function hasHumanName(device) {
|
||||
var label = deviceLabel(device)
|
||||
return label !== "" && !isUuidLike(label) && !isAddressLike(label)
|
||||
}
|
||||
|
||||
function sortedByLabel(devices) {
|
||||
var list = Array.isArray(devices) ? devices.slice() : []
|
||||
list.sort(function(a, b) { return deviceLabel(a).localeCompare(deviceLabel(b)) })
|
||||
return list
|
||||
}
|
||||
|
||||
function deviceLists(devices) {
|
||||
var values = Array.isArray(devices) ? devices : []
|
||||
var connected = []
|
||||
var known = []
|
||||
var discovered = []
|
||||
|
||||
for (var i = 0; i < values.length; i++) {
|
||||
var d = values[i]
|
||||
if (!d || !hasHumanName(d)) continue
|
||||
if (d.connected) connected.push(d)
|
||||
else if (d.paired || d.bonded || d.trusted) known.push(d)
|
||||
else discovered.push(d)
|
||||
}
|
||||
|
||||
return {
|
||||
connected: sortedByLabel(connected),
|
||||
known: sortedByLabel(known),
|
||||
discovered: sortedByLabel(discovered)
|
||||
}
|
||||
}
|
||||
|
||||
function cloneMap(map) {
|
||||
var next = ({})
|
||||
for (var key in map || {}) next[key] = map[key]
|
||||
return next
|
||||
}
|
||||
|
||||
function pendingAction(actions, address) {
|
||||
return address && actions && actions[address] ? actions[address] : ""
|
||||
}
|
||||
|
||||
function withPendingAction(actions, address, action) {
|
||||
var next = cloneMap(actions)
|
||||
if (!address) return next
|
||||
if (action) next[address] = action
|
||||
else delete next[address]
|
||||
return next
|
||||
}
|
||||
|
||||
function visibleSections(lists, discovering) {
|
||||
var sections = []
|
||||
if (lists && lists.connected && lists.connected.length > 0) sections.push("connected")
|
||||
if (lists && lists.known && lists.known.length > 0) sections.push("known")
|
||||
if (discovering && lists && lists.discovered && lists.discovered.length > 0) sections.push("discovered")
|
||||
return sections
|
||||
}
|
||||
|
||||
function sectionDevices(lists, section) {
|
||||
if (!lists) return []
|
||||
if (section === "connected") return lists.connected || []
|
||||
if (section === "known") return lists.known || []
|
||||
if (section === "discovered") return lists.discovered || []
|
||||
return []
|
||||
}
|
||||
|
||||
if (typeof module !== "undefined") {
|
||||
module.exports = {
|
||||
deviceLabel: deviceLabel,
|
||||
isUuidLike: isUuidLike,
|
||||
isAddressLike: isAddressLike,
|
||||
hasHumanName: hasHumanName,
|
||||
sortedByLabel: sortedByLabel,
|
||||
deviceLists: deviceLists,
|
||||
cloneMap: cloneMap,
|
||||
pendingAction: pendingAction,
|
||||
withPendingAction: withPendingAction,
|
||||
visibleSections: visibleSections,
|
||||
sectionDevices: sectionDevices
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user