117 lines
4.1 KiB
QML
117 lines
4.1 KiB
QML
import QtQuick
|
|
import Quickshell
|
|
import Quickshell.Io
|
|
import "." as M
|
|
|
|
M.BarSection {
|
|
id: root
|
|
spacing: M.Theme.moduleSpacing
|
|
tooltip: {
|
|
const parts = [];
|
|
if (root.state === "wifi") {
|
|
parts.push("WiFi: " + root.essid);
|
|
if (root.signal)
|
|
parts.push("Signal: " + root.signal + "%");
|
|
} else if (root.state === "eth") {
|
|
parts.push("Ethernet");
|
|
} else if (root.state === "linked") {
|
|
parts.push("Linked");
|
|
} else {
|
|
return "Disconnected";
|
|
}
|
|
if (root.ipAddr)
|
|
parts.push("IP: " + root.ipAddr);
|
|
if (root.ifname)
|
|
parts.push("Interface: " + root.ifname);
|
|
return parts.join("\n");
|
|
}
|
|
|
|
property string ifname: ""
|
|
property string essid: ""
|
|
property string state: "disconnected"
|
|
property string ipAddr: ""
|
|
property string signal: ""
|
|
|
|
Process {
|
|
id: proc
|
|
running: true
|
|
command: ["sh", "-c", "line=$(nmcli -t -f NAME,TYPE,DEVICE connection show --active 2>/dev/null | head -1); if [ -z \"$line\" ]; then dev=$(nmcli -t -f DEVICE,STATE device 2>/dev/null | grep ':connected' | grep -v ':unmanaged\\|:unavailable\\|:disconnected\\|:connecting' | head -1 | cut -d: -f1); [ -n \"$dev\" ] && line=\"linked:linked:$dev\"; fi; [ -z \"$line\" ] && exit 0; echo \"$line\"; dev=$(echo \"$line\" | cut -d: -f3); ip=$(nmcli -t -f IP4.ADDRESS device show \"$dev\" 2>/dev/null | head -1 | cut -d: -f2); echo \"ip:${ip:-}\"; sig=$(nmcli -t -f GENERAL.SIGNAL device show \"$dev\" 2>/dev/null | head -1 | cut -d: -f2); echo \"sig:${sig:-}\""]
|
|
stdout: StdioCollector {
|
|
onStreamFinished: {
|
|
const lines = text.trim().split("\n");
|
|
if (!lines[0]) {
|
|
root.state = "disconnected";
|
|
root.essid = "";
|
|
root.ifname = "";
|
|
root.ipAddr = "";
|
|
root.signal = "";
|
|
return;
|
|
}
|
|
const parts = lines[0].split(":");
|
|
root.essid = parts[0] || "";
|
|
root.ifname = parts[2] || "";
|
|
if ((parts[1] || "").includes("wireless"))
|
|
root.state = "wifi";
|
|
else if (parts[0] === "linked")
|
|
root.state = "linked";
|
|
else
|
|
root.state = "eth";
|
|
// Parse extra info lines
|
|
root.ipAddr = "";
|
|
root.signal = "";
|
|
for (let i = 1; i < lines.length; i++) {
|
|
if (lines[i].startsWith("ip:"))
|
|
root.ipAddr = lines[i].slice(3);
|
|
else if (lines[i].startsWith("sig:"))
|
|
root.signal = lines[i].slice(4);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Timer {
|
|
interval: M.Modules.network.interval || 5000
|
|
running: true
|
|
repeat: true
|
|
onTriggered: proc.running = true
|
|
}
|
|
|
|
M.BarIcon {
|
|
icon: {
|
|
if (root.state === "wifi")
|
|
return "\uF1EB";
|
|
if (root.state === "eth")
|
|
return "\uDB80\uDE00";
|
|
if (root.state === "linked")
|
|
return "\uDB85\uDE16";
|
|
return "\uDB82\uDCFD";
|
|
}
|
|
color: root.state === "disconnected" ? M.Theme.base08 : M.Theme.base0D
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
}
|
|
M.BarLabel {
|
|
visible: root.state === "wifi"
|
|
label: root.essid
|
|
color: root.state === "disconnected" ? M.Theme.base08 : M.Theme.base0D
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
}
|
|
|
|
required property var bar
|
|
|
|
TapHandler {
|
|
cursorShape: Qt.PointingHandCursor
|
|
onTapped: {
|
|
menuLoader.active = !menuLoader.active;
|
|
M.FlyoutState.visible = false;
|
|
}
|
|
}
|
|
|
|
Loader {
|
|
id: menuLoader
|
|
active: false
|
|
sourceComponent: M.NetworkMenu {
|
|
screen: root.bar.screen
|
|
anchorX: root.mapToGlobal(root.width / 2, 0).x - (QsWindow.window?.screen?.x ?? 0)
|
|
onDismissed: menuLoader.active = false
|
|
}
|
|
}
|
|
}
|