fix bluetooth

This commit is contained in:
Damocles 2026-04-12 11:11:34 +02:00
parent d208e264e2
commit 720d748b37

View file

@ -5,41 +5,68 @@ import "." as M
M.BarSection {
id: root
spacing: M.Theme.moduleSpacing
tooltip: root.status === "connected" ? "Bluetooth: " + root.device : "Bluetooth: on"
visible: root.state !== "unavailable"
tooltip: {
if (root.state === "off") return "Bluetooth: off";
if (root.state === "connected") return "Bluetooth: " + root.device;
return "Bluetooth: on";
}
property string status: "off"
property string state: "unavailable"
property string device: ""
function _parse(text) {
const t = text.trim();
const sep = t.indexOf(":");
root.state = sep === -1 ? t : t.slice(0, sep);
root.device = sep === -1 ? "" : t.slice(sep + 1);
}
Process {
id: proc
running: true
command: ["sh", "-c", "bluetoothctl info 2>/dev/null | awk -F': ' '/Name/ {n=$2} /Connected: yes/ {c=1} END {if (c) print n; else print \"\"}'"]
command: ["sh", "-c",
"s=$(bluetoothctl show 2>/dev/null); " +
"[ -z \"$s\" ] && echo unavailable && exit; " +
"echo \"$s\" | grep -q 'Powered: yes' || { echo off:; exit; }; " +
"d=$(bluetoothctl info 2>/dev/null | awk -F': ' '/\\tName:/{n=$2}/Connected: yes/{c=1}END{if(c)print n}'); " +
"[ -n \"$d\" ] && echo \"connected:$d\" || echo on:"
]
stdout: StdioCollector {
onStreamFinished: {
const t = text.trim();
root.device = t;
root.status = t ? "connected" : "on";
}
onStreamFinished: root._parse(text)
}
}
Timer {
interval: 10000
interval: 5000
running: true
repeat: true
onTriggered: proc.running = true
}
Process {
id: toggle
property string cmd: ""
command: ["bluetoothctl", "power", cmd]
onRunningChanged: if (!running && cmd !== "") proc.running = true
}
M.BarIcon {
visible: root.status === "connected"
icon: "\uF294"
color: root.state === "off" ? M.Theme.base04 : M.Theme.base05
anchors.verticalCenter: parent.verticalCenter
}
Text {
visible: root.status === "connected"
text: root.device
color: M.Theme.base05
font.pixelSize: M.Theme.fontSize + 1
font.family: M.Theme.fontFamily
M.BarLabel {
visible: root.state === "connected"
label: root.device
anchors.verticalCenter: parent.verticalCenter
}
MouseArea {
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: {
toggle.cmd = root.state === "off" ? "on" : "off";
toggle.running = true;
}
}
}