network/bluetooth menus: add radio toggle header, fix contentWidth

This commit is contained in:
Damocles 2026-04-14 00:58:24 +02:00
parent 86003d8eaa
commit edcc78483c
2 changed files with 170 additions and 22 deletions

View file

@ -10,15 +10,19 @@ M.HoverPanel {
contentWidth: 250 contentWidth: 250
property var _devices: [] property var _devices: []
property bool _btEnabled: true
property Process _scanner: Process { property Process _scanner: Process {
id: scanner id: scanner
running: true running: true
command: ["sh", "-c", "bluetoothctl devices Paired 2>/dev/null | while read -r _ mac name; do " + "info=$(bluetoothctl info \"$mac\" 2>/dev/null); " + "conn=$(echo \"$info\" | grep -c 'Connected: yes'); " + "bat=$(echo \"$info\" | awk -F'[(): ]' '/Battery Percentage/{for(i=1;i<=NF;i++) if($i+0==$i && $i!=\"\") print $i}'); " + "echo \"$mac:$conn:${bat:-}:$name\"; " + "done"] command: ["sh", "-c", "bluetoothctl show 2>/dev/null | awk '/Powered:/{print $2; exit}';" + "echo '---DEVICES---';" + "bluetoothctl devices Paired 2>/dev/null | while read -r _ mac name; do " + "info=$(bluetoothctl info \"$mac\" 2>/dev/null); " + "conn=$(echo \"$info\" | grep -c 'Connected: yes'); " + "bat=$(echo \"$info\" | awk -F'[(): ]' '/Battery Percentage/{for(i=1;i<=NF;i++) if($i+0==$i && $i!=\"\") print $i}'); " + "echo \"$mac:$conn:${bat:-}:$name\"; " + "done"]
stdout: StdioCollector { stdout: StdioCollector {
onStreamFinished: { onStreamFinished: {
const sections = text.split("---DEVICES---");
menuWindow._btEnabled = (sections[0] || "").trim() === "yes";
const devs = []; const devs = [];
for (const line of text.trim().split("\n")) { for (const line of (sections[1] || "").trim().split("\n")) {
if (!line) if (!line)
continue; continue;
const i1 = line.indexOf(":"); const i1 = line.indexOf(":");
@ -27,10 +31,10 @@ M.HoverPanel {
if (i3 < 0) if (i3 < 0)
continue; continue;
devs.push({ devs.push({
mac: line.slice(0, i1), "mac": line.slice(0, i1),
connected: line.slice(i1 + 1, i2) === "1", "connected": line.slice(i1 + 1, i2) === "1",
battery: parseInt(line.slice(i2 + 1, i3)) || -1, "battery": parseInt(line.slice(i2 + 1, i3)) || -1,
name: line.slice(i3 + 1) "name": line.slice(i3 + 1)
}); });
} }
devs.sort((a, b) => { devs.sort((a, b) => {
@ -43,6 +47,14 @@ M.HoverPanel {
} }
} }
property Process _powerProc: Process {
id: powerProc
property string _action: ""
command: ["bluetoothctl", "power", _action]
onRunningChanged: if (!running)
scanner.running = true
}
property Process _toggleProc: Process { property Process _toggleProc: Process {
id: toggleProc id: toggleProc
property string action: "" property string action: ""
@ -52,6 +64,70 @@ M.HoverPanel {
scanner.running = true scanner.running = true
} }
// Bluetooth radio toggle header
Item {
width: menuWindow.contentWidth
height: 36
Rectangle {
anchors.fill: parent
anchors.leftMargin: 4
anchors.rightMargin: 4
color: btHeaderArea.containsMouse ? M.Theme.base02 : "transparent"
radius: M.Theme.radius
}
Text {
id: btHeaderIcon
anchors.left: parent.left
anchors.leftMargin: 12
anchors.verticalCenter: parent.verticalCenter
text: "\uF294"
color: menuWindow._btEnabled ? menuWindow.accentColor : M.Theme.base04
font.pixelSize: M.Theme.fontSize + 1
font.family: M.Theme.iconFontFamily
}
Text {
anchors.left: btHeaderIcon.right
anchors.leftMargin: 8
anchors.verticalCenter: parent.verticalCenter
text: "Bluetooth"
color: menuWindow._btEnabled ? M.Theme.base05 : M.Theme.base04
font.pixelSize: M.Theme.fontSize
font.family: M.Theme.fontFamily
font.bold: true
}
Text {
anchors.right: parent.right
anchors.rightMargin: 12
anchors.verticalCenter: parent.verticalCenter
text: "\uF011"
color: menuWindow._btEnabled ? menuWindow.accentColor : M.Theme.base04
font.pixelSize: M.Theme.fontSize
font.family: M.Theme.iconFontFamily
}
MouseArea {
id: btHeaderArea
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: {
powerProc._action = menuWindow._btEnabled ? "off" : "on";
powerProc.running = true;
}
}
}
Rectangle {
width: menuWindow.contentWidth - 16
height: 1
anchors.horizontalCenter: parent.horizontalCenter
color: M.Theme.base03
}
Repeater { Repeater {
model: menuWindow._devices model: menuWindow._devices
@ -60,7 +136,7 @@ M.HoverPanel {
required property var modelData required property var modelData
required property int index required property int index
width: menuWindow.panelWidth width: menuWindow.contentWidth
height: 32 height: 32
Rectangle { Rectangle {
@ -125,11 +201,11 @@ M.HoverPanel {
Text { Text {
visible: menuWindow._devices.length === 0 visible: menuWindow._devices.length === 0
width: menuWindow.panelWidth width: menuWindow.contentWidth
height: 32 height: 32
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter verticalAlignment: Text.AlignVCenter
text: "No paired devices" text: menuWindow._btEnabled ? "No paired devices" : "Bluetooth is off"
color: M.Theme.base04 color: M.Theme.base04
font.pixelSize: M.Theme.fontSize font.pixelSize: M.Theme.fontSize
font.family: M.Theme.fontFamily font.family: M.Theme.fontFamily

View file

@ -10,18 +10,21 @@ M.HoverPanel {
contentWidth: 250 contentWidth: 250
property var _networks: [] property var _networks: []
property bool _wifiEnabled: true
property Process _scanner: Process { property Process _scanner: Process {
id: scanner id: scanner
running: true running: true
command: ["sh", "-c", "echo '---CONNS---';" + "nmcli -t -f NAME,UUID,TYPE,ACTIVE connection show 2>/dev/null;" + "echo '---WIFI---';" + "nmcli -t -f SSID,SIGNAL device wifi list --rescan no 2>/dev/null"] command: ["sh", "-c", "echo '---RADIO---';" + "nmcli radio wifi 2>/dev/null;" + "echo '---CONNS---';" + "nmcli -t -f NAME,UUID,TYPE,ACTIVE connection show 2>/dev/null;" + "echo '---WIFI---';" + "nmcli -t -f SSID,SIGNAL device wifi list --rescan no 2>/dev/null"]
stdout: StdioCollector { stdout: StdioCollector {
onStreamFinished: { onStreamFinished: {
const radioSection = text.split("---CONNS---")[0].split("---RADIO---")[1] || "";
menuWindow._wifiEnabled = radioSection.trim() === "enabled";
const sections = text.split("---WIFI---"); const sections = text.split("---WIFI---");
const connLines = (sections[0] || "").split("---CONNS---")[1] || ""; const connLines = (sections[0] || "").split("---CONNS---")[1] || "";
const wifiLines = sections[1] || ""; const wifiLines = sections[1] || "";
// Visible SSIDs with signal
const visible = {}; const visible = {};
for (const l of wifiLines.trim().split("\n")) { for (const l of wifiLines.trim().split("\n")) {
if (!l) if (!l)
@ -32,7 +35,6 @@ M.HoverPanel {
visible[ssid] = parseInt(parts[1]) || 0; visible[ssid] = parseInt(parts[1]) || 0;
} }
// Saved connections filter: show wired always, wifi only if visible
const nets = []; const nets = [];
for (const l of connLines.trim().split("\n")) { for (const l of connLines.trim().split("\n")) {
if (!l) if (!l)
@ -48,15 +50,14 @@ M.HoverPanel {
continue; continue;
nets.push({ nets.push({
name: name, "name": name,
uuid: uuid, "uuid": uuid,
isWifi: isWifi, "isWifi": isWifi,
active: active, "active": active,
signal: isWifi ? (visible[name] || 0) : -1 "signal": isWifi ? (visible[name] || 0) : -1
}); });
} }
// Active first, then by signal (wifi) or name
nets.sort((a, b) => { nets.sort((a, b) => {
if (a.active !== b.active) if (a.active !== b.active)
return a.active ? -1 : 1; return a.active ? -1 : 1;
@ -70,6 +71,14 @@ M.HoverPanel {
} }
} }
property Process _radioProc: Process {
id: radioProc
property string _state: ""
command: ["nmcli", "radio", "wifi", _state]
onRunningChanged: if (!running)
scanner.running = true
}
property Process _connectProc: Process { property Process _connectProc: Process {
id: connectProc id: connectProc
property string uuid: "" property string uuid: ""
@ -86,6 +95,70 @@ M.HoverPanel {
scanner.running = true scanner.running = true
} }
// Wi-Fi radio toggle header
Item {
width: menuWindow.contentWidth
height: 36
Rectangle {
anchors.fill: parent
anchors.leftMargin: 4
anchors.rightMargin: 4
color: headerArea.containsMouse ? M.Theme.base02 : "transparent"
radius: M.Theme.radius
}
Text {
id: wifiHeaderIcon
anchors.left: parent.left
anchors.leftMargin: 12
anchors.verticalCenter: parent.verticalCenter
text: "\uF1EB"
color: menuWindow._wifiEnabled ? menuWindow.accentColor : M.Theme.base04
font.pixelSize: M.Theme.fontSize + 1
font.family: M.Theme.iconFontFamily
}
Text {
anchors.left: wifiHeaderIcon.right
anchors.leftMargin: 8
anchors.verticalCenter: parent.verticalCenter
text: "Wi-Fi"
color: menuWindow._wifiEnabled ? M.Theme.base05 : M.Theme.base04
font.pixelSize: M.Theme.fontSize
font.family: M.Theme.fontFamily
font.bold: true
}
Text {
anchors.right: parent.right
anchors.rightMargin: 12
anchors.verticalCenter: parent.verticalCenter
text: "\uF011"
color: menuWindow._wifiEnabled ? menuWindow.accentColor : M.Theme.base04
font.pixelSize: M.Theme.fontSize
font.family: M.Theme.iconFontFamily
}
MouseArea {
id: headerArea
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: {
radioProc._state = menuWindow._wifiEnabled ? "off" : "on";
radioProc.running = true;
}
}
}
Rectangle {
width: menuWindow.contentWidth - 16
height: 1
anchors.horizontalCenter: parent.horizontalCenter
color: M.Theme.base03
}
Repeater { Repeater {
model: menuWindow._networks model: menuWindow._networks
@ -94,7 +167,7 @@ M.HoverPanel {
required property var modelData required property var modelData
required property int index required property int index
width: menuWindow.panelWidth width: menuWindow.contentWidth
height: 32 height: 32
Rectangle { Rectangle {
@ -161,14 +234,13 @@ M.HoverPanel {
} }
} }
// Empty state
Text { Text {
visible: menuWindow._networks.length === 0 visible: menuWindow._networks.length === 0
width: menuWindow.panelWidth width: menuWindow.contentWidth
height: 32 height: 32
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter verticalAlignment: Text.AlignVCenter
text: "No networks available" text: menuWindow._wifiEnabled ? "No networks available" : "Wi-Fi is off"
color: M.Theme.base04 color: M.Theme.base04
font.pixelSize: M.Theme.fontSize font.pixelSize: M.Theme.fontSize
font.family: M.Theme.fontFamily font.family: M.Theme.fontFamily