nova-shell/modules/Bluetooth.qml
2026-04-12 14:37:37 +02:00

73 lines
2.1 KiB
QML

import QtQuick
import Quickshell.Io
import "." as M
M.BarSection {
id: root
spacing: M.Theme.moduleSpacing
visible: M.Modules.bluetooth && root.state !== "unavailable"
tooltip: {
if (root.state === "off") return "Bluetooth: off";
if (root.state === "connected") return "Bluetooth: " + root.device;
return "Bluetooth: on";
}
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",
"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: root._parse(text)
}
}
Timer {
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 {
icon: "\uF294"
color: root.state === "connected" ? M.Theme.base0D
: root.state === "off" ? M.Theme.base04
: M.Theme.base05
anchors.verticalCenter: parent.verticalCenter
}
M.BarLabel {
visible: root.state === "connected"
label: root.device
anchors.verticalCenter: parent.verticalCenter
}
TapHandler {
cursorShape: Qt.PointingHandCursor
onTapped: {
toggle.cmd = root.state === "off" ? "on" : "off";
toggle.running = true;
}
}
}