From e5e1d34e5c649236e353ad56899cd848aab83a11 Mon Sep 17 00:00:00 2001 From: Hannes Date: Sat, 22 Aug 2026 00:12:37 +0200 Subject: [PATCH] update to network updates --- modules/bar/states/Network/NetworkState.qml | 179 ++++++++++++-------- 1 file changed, 111 insertions(+), 68 deletions(-) diff --git a/modules/bar/states/Network/NetworkState.qml b/modules/bar/states/Network/NetworkState.qml index da9993f..820ad5e 100644 --- a/modules/bar/states/Network/NetworkState.qml +++ b/modules/bar/states/Network/NetworkState.qml @@ -34,6 +34,7 @@ QtObject { property real _prevTxBytes: 0 property var _prevUpdateTime: 0 property string _trafficActiveDevice: "" + property string _lastNetworkSignature: "" readonly property var networking: Networking @@ -53,11 +54,32 @@ QtObject { return "" } - property var _pendingDevices: [] - function updateAll() { + updateNetworks(true) + updateStats() + } + + function updateNetworks(force) { var connected = findAllConnectedDevices() - _pendingDevices = connected + + var sig = "" + for (var i = 0; i < connected.length; i++) { + var dev = connected[i] + sig += dev.name + ":" + dev.type + ":" + dev.connected + if (dev.type === DeviceType.Wifi) { + var nets = dev.networks.values + for (var j = 0; j < nets.length; j++) { + if (nets[j].connected) { + sig += ":" + nets[j].name + break + } + } + } + sig += ";" + } + + if (!force && sig === _lastNetworkSignature) return + _lastNetworkSignature = sig activeConnections.clear() var hasWifi = false @@ -109,7 +131,6 @@ QtObject { if (trafficDev) { _trafficActiveDevice = trafficDev.name - trafficScript.running = true } root.computeStatus(hasWired, hasWifi, bestWifiSignal) @@ -117,13 +138,27 @@ QtObject { if (connected.length > 0) { ipv4Script.running = true } + + networkDetailScript.running = true + } + + function updateStats() { + if (!_trafficActiveDevice) return + statsScript.running = true } property Timer networkTimer: Timer { - interval: 10000 + interval: 5000 repeat: true running: true - onTriggered: root.updateAll() + onTriggered: root.updateNetworks() + } + + property Timer statsTimer: Timer { + interval: 1000 + repeat: true + running: true + onTriggered: root.updateStats() } property Process ipv4Script: Process { @@ -161,16 +196,8 @@ QtObject { } } - property Process trafficScript: Process { + property Process networkDetailScript: Process { command: ["sh", "-c", - "echo '===TRAFFIC==='; " + - "DEV=\"" + root._trafficActiveDevice + "\"; " + - "if [ -n \"$DEV\" ]; then " + - " echo \"RX_BYTES=$(cat /sys/class/net/$DEV/statistics/rx_bytes 2>/dev/null || echo 0)\"; " + - " echo \"TX_BYTES=$(cat /sys/class/net/$DEV/statistics/tx_bytes 2>/dev/null || echo 0)\"; " + - "fi; " + - "echo '===PING==='; " + - "ping -c 1 -W 2 1.1.1.1 2>/dev/null | awk -F/ '/rtt/{print $5}' || echo ''; " + "echo '===VPN_ALL==='; " + "nmcli -t -f NAME,TYPE connection show 2>/dev/null | grep -i vpn || true; " + "echo '===VPN_ACTIVE==='; " + @@ -182,39 +209,11 @@ QtObject { ] stdout: StdioCollector { - onStreamFinished: root.parseOutput(text.trim()) + onStreamFinished: root.parseNetworkDetails(text.trim()) } } - function computeStatus(hasWired, hasWifi, bestSignal) { - if (!hasWired && !hasWifi) { - root.status = "󰤭/󱐤" - return - } - - var parts = [] - if (hasWired) parts.push("󰈀") - if (hasWifi) { - var sig = Math.round(bestSignal * 100) - if (sig > 80) parts.push("󰤨") - else if (sig > 60) parts.push("󰤥") - else if (sig > 40) parts.push("󰤢") - else if (sig > 20) parts.push("󰤟") - else if (sig > 0) parts.push("󰤯") - else parts.push("󰤭") - } - - root.status = parts.join(" ") - } - - function formatSpeed(bytesPerSec) { - if (bytesPerSec >= 1073741824) return (bytesPerSec / 1073741824).toFixed(1) + " GB/s" - if (bytesPerSec >= 1048576) return (bytesPerSec / 1048576).toFixed(1) + " MB/s" - if (bytesPerSec >= 1024) return (bytesPerSec / 1024).toFixed(1) + " KB/s" - return bytesPerSec.toFixed(0) + " B/s" - } - - function parseOutput(output) { + function parseNetworkDetails(output) { var sections = output.split("===TAILSCALE_IP===") var tailscaleIpSection = sections.length > 1 ? sections[1].trim() : "" @@ -230,8 +229,48 @@ QtObject { sections = beforeVpnActive.split("===VPN_ALL===") var vpnAllSection = sections.length > 1 ? sections[1].trim() : "" - var beforeVpnAll = sections[0] - sections = beforeVpnAll.split("===PING===") + root.vpnExists = vpnAllSection.length > 0 + if (vpnActiveSection) { + root.vpnConnected = true + var vpnLines = vpnActiveSection.split("\n") + if (vpnLines.length > 0) { + root.vpnName = vpnLines[0].split(":")[0] || "" + } + } else { + root.vpnConnected = false + root.vpnName = "" + root.vpnIp = "" + } + + root.tailscaleExists = tailscaleExistsSection.indexOf("EXISTS") >= 0 + if (tailscaleIpSection) { + root.tailscaleConnected = true + root.tailscaleIp = tailscaleIpSection + } else { + root.tailscaleConnected = false + root.tailscaleIp = "" + } + } + + property Process statsScript: Process { + command: ["sh", "-c", + "echo '===TRAFFIC==='; " + + "DEV=\"" + root._trafficActiveDevice + "\"; " + + "if [ -n \"$DEV\" ]; then " + + " echo \"RX_BYTES=$(cat /sys/class/net/$DEV/statistics/rx_bytes 2>/dev/null || echo 0)\"; " + + " echo \"TX_BYTES=$(cat /sys/class/net/$DEV/statistics/tx_bytes 2>/dev/null || echo 0)\"; " + + "fi; " + + "echo '===PING==='; " + + "ping -c 1 -W 2 1.1.1.1 2>/dev/null | awk -F/ '/rtt/{print $5}' || echo ''" + ] + + stdout: StdioCollector { + onStreamFinished: root.parseStats(text.trim()) + } + } + + function parseStats(output) { + var sections = output.split("===PING===") root.ping = sections.length > 1 ? sections[1].trim() : "" var pingVal = parseFloat(root.ping) @@ -242,9 +281,7 @@ QtObject { root.pingHistory = pingHist } - var beforePing = sections[0] - sections = beforePing.split("===TRAFFIC===") - var trafficSection = sections.length > 1 ? sections[1].trim() : "" + var trafficSection = sections[0].replace("===TRAFFIC===", "").trim() if (trafficSection) { var trafficLines = trafficSection.split("\n") @@ -284,28 +321,34 @@ QtObject { root._prevTxBytes = txBytes root._prevUpdateTime = Date.now() } + } - root.vpnExists = vpnAllSection.length > 0 - if (vpnActiveSection) { - root.vpnConnected = true - var vpnLines = vpnActiveSection.split("\n") - if (vpnLines.length > 0) { - root.vpnName = vpnLines[0].split(":")[0] || "" - } - } else { - root.vpnConnected = false - root.vpnName = "" - root.vpnIp = "" + function computeStatus(hasWired, hasWifi, bestSignal) { + if (!hasWired && !hasWifi) { + root.status = "󰤭/󱐤" + return } - root.tailscaleExists = tailscaleExistsSection.indexOf("EXISTS") >= 0 - if (tailscaleIpSection) { - root.tailscaleConnected = true - root.tailscaleIp = tailscaleIpSection - } else { - root.tailscaleConnected = false - root.tailscaleIp = "" + var parts = [] + if (hasWired) parts.push("󰈀") + if (hasWifi) { + var sig = Math.round(bestSignal * 100) + if (sig > 80) parts.push("󰤨") + else if (sig > 60) parts.push("󰤥") + else if (sig > 40) parts.push("󰤢") + else if (sig > 20) parts.push("󰤟") + else if (sig > 0) parts.push("󰤯") + else parts.push("󰤭") } + + root.status = parts.join(" ") + } + + function formatSpeed(bytesPerSec) { + if (bytesPerSec >= 1073741824) return (bytesPerSec / 1073741824).toFixed(1) + " GB/s" + if (bytesPerSec >= 1048576) return (bytesPerSec / 1048576).toFixed(1) + " MB/s" + if (bytesPerSec >= 1024) return (bytesPerSec / 1024).toFixed(1) + " KB/s" + return bytesPerSec.toFixed(0) + " B/s" } Component.onCompleted: {