From 987c2542db2a372eab6a5ad8c249fc41d1750eed Mon Sep 17 00:00:00 2001 From: Emir Beganovic Date: Sun, 29 Nov 2026 01:10:22 +0000 Subject: [PATCH] Keep Ethernet hero during Wi-Fi connect and follow active route --- shell/plugins/panels/network/Model.js | 48 ++++++ shell/plugins/panels/network/Panel.qml | 108 ++++++++++-- .../fixtures/network-panel/dual-interface.qml | 160 ++++++++++++++++++ test/shell.d/network-panel-runtime-test.sh | 47 +++++ test/shell.d/network-test.sh | 32 ++++ 5 files changed, 379 insertions(+), 16 deletions(-) create mode 100644 test/shell.d/fixtures/network-panel/dual-interface.qml diff --git a/shell/plugins/panels/network/Model.js b/shell/plugins/panels/network/Model.js index b439f4bd5970..4c2b4b43fcff 100644 --- a/shell/plugins/panels/network/Model.js +++ b/shell/plugins/panels/network/Model.js @@ -66,6 +66,51 @@ function connectivityState(kind, connectivity, states, checksEnabled, limitedSet return "unknown" } +// Parse `ip -4 route show default` (main table) and pick the lowest-metric +// default route on a connected physical interface, ignoring VPN tunnels so +// NetworkManager's main-table metrics stay authoritative. Ties prefer Ethernet. +// Virtual wired uppers (bridge, bond, VLAN, PPPoE) are not enumerated by +// Quickshell.Networking and are ignored here. +function parseDefaultRouteKind(raw, wiredNames, wifiNames) { + var lines = String(raw || "").split(/\r?\n/) + var bestKind = "" + var bestMetric = Infinity + for (var i = 0; i < lines.length; i++) { + var line = lines[i].trim() + if (!line || !/^default(?:\s|$)/.test(line)) continue + var devMatch = /\bdev\s+(\S+)/.exec(line) + if (!devMatch) continue + var dev = devMatch[1] + var kind = "" + if (wiredNames && wiredNames.indexOf(dev) >= 0) kind = "ethernet" + else if (wifiNames && wifiNames.indexOf(dev) >= 0) kind = "wifi" + else continue + var metricMatch = /\bmetric\s+(\d+)/.exec(line) + var metric = metricMatch ? parseInt(metricMatch[1], 10) : 0 + if (metric < bestMetric || (metric === bestMetric && kind === "ethernet" && bestKind !== "ethernet")) { + bestMetric = metric + bestKind = kind + } + } + return bestKind +} + +// Match `ip -4 monitor route` lines affecting main-table default routes only. +function isDefaultRouteEvent(line) { + var trimmed = String(line || "").trim() + if (!/^(?:Deleted\s+)?default(?:\s|$)/.test(trimmed)) return false + var tableMatch = /\btable\s+(\S+)/.exec(trimmed) + if (tableMatch && tableMatch[1] !== "main" && tableMatch[1] !== "254") return false + return true +} + +function connectionKind(wiredConnected, wifiConnected, routeKind) { + if (wiredConnected && wifiConnected) return routeKind === "wifi" ? "wifi" : "ethernet" + if (wiredConnected) return "ethernet" + if (wifiConnected) return "wifi" + return "disconnected" +} + function connectionIcon(kind, signalStrength, connectivity) { var restricted = connectivity === "portal" || connectivity === "limited" if (kind === "wifi") return restricted ? "󰤩" : wifiIconFor(signalStrength) @@ -448,6 +493,9 @@ if (typeof module !== "undefined") { module.exports = { parseNetworkStatus: parseNetworkStatus, wifiIconFor: wifiIconFor, + parseDefaultRouteKind: parseDefaultRouteKind, + isDefaultRouteEvent: isDefaultRouteEvent, + connectionKind: connectionKind, connectionIcon: connectionIcon, connectedSignalStrength: connectedSignalStrength, parseActiveApSignal: parseActiveApSignal, diff --git a/shell/plugins/panels/network/Panel.qml b/shell/plugins/panels/network/Panel.qml index c1b48fe76605..c43e992dbf3b 100644 --- a/shell/plugins/panels/network/Panel.qml +++ b/shell/plugins/panels/network/Panel.qml @@ -146,7 +146,7 @@ Panel { property int headerIndex: 0 readonly property bool canDisconnect: !!connectedWifiNetwork readonly property bool headerHasDisconnect: false - readonly property bool canShareWifi: info.type === "wifi" && canShareNetwork(connectedWifiNetwork) + readonly property bool canShareWifi: kind === "wifi" && info.type === "wifi" && canShareNetwork(connectedWifiNetwork) // The hero switch is the Wi-Fi radio, so it only exists when there is a // radio to switch. On a wired box it would otherwise sit there reading // "off" beside a perfectly live Ethernet connection. @@ -481,21 +481,49 @@ Panel { connectDirectly(net.ssid) } + function connectedDeviceNames(deviceType) { + var names = [] + var devices = networkDevices || [] + for (var i = 0; i < devices.length; i++) { + if (devices[i] && devices[i].type === deviceType && devices[i].connected && devices[i].name) + names.push(devices[i].name) + } + return names + } + // Bar pill state, derived from the native NetworkManager service so the - // icon reflects connection changes without polling. Wired is preferred - // when both are up, matching the default-route device. + // icon reflects connection changes without polling. When both wired and + // Wi-Fi are up, follow the lowest-metric main-table physical default route + // so local-only Ethernet yields to Wi-Fi. readonly property var wiredDevice: findDevice(DeviceType.Wired) - readonly property string kind: { - if (wiredDevice && wiredDevice.connected) return "ethernet" - if (connectedWifiNetwork) return "wifi" - // NetworkManager can leave the active profile out of the device's - // AvailableConnections, and Quickshell only builds known networks from - // that list. An OWE transition-mode network does this between scans: the - // in-use access point carries the hidden "_owetm_" SSID, so the profile - // for the open SSID looks out of range even while it is connected. Trust - // the device's own state then instead of flickering to disconnected. - if (wifiDevice && wifiDevice.connected) return "wifi" - return "disconnected" + readonly property var connectedWiredNames: connectedDeviceNames(DeviceType.Wired) + readonly property var connectedWifiNames: connectedDeviceNames(DeviceType.Wifi) + readonly property bool wiredConnected: !!(wiredDevice && wiredDevice.connected) + // NetworkManager can leave the active profile out of the device's + // AvailableConnections, and Quickshell only builds known networks from + // that list. An OWE transition-mode network does this between scans: the + // in-use access point carries the hidden "_owetm_" SSID, so the profile + // for the open SSID looks out of range even while it is connected. Trust + // the device's own state then instead of flickering to disconnected. + readonly property bool wifiConnected: !!(connectedWifiNetwork || (wifiDevice && wifiDevice.connected)) + readonly property bool dualConnected: wiredConnected && wifiConnected + property string routeKind: "" + property int routeGeneration: 0 + property int routeRequest: -1 + readonly property string kind: Model.connectionKind(wiredConnected, wifiConnected, routeKind) + readonly property string dualDeviceKey: dualConnected + ? connectedWiredNames.join(",") + "|" + connectedWifiNames.join(",") + : "" + onWiredConnectedChanged: if (!wiredConnected) routeKind = wifiConnected ? "wifi" : "" + onWifiConnectedChanged: if (!wiredConnected) routeKind = wifiConnected ? "wifi" : "" + onDualDeviceKeyChanged: if (dualDeviceKey !== "") refreshRoute() + onDualConnectedChanged: { + if (!dualConnected) { + routeGeneration++ + routeSettle.stop() + routeMonitorRestart.stop() + routeKind = wifiConnected ? "wifi" : "" + } } readonly property int signalStrength: kind === "wifi" ? Model.connectedSignalStrength(connectedWifiNetwork ? connectedWifiNetwork.signalStrength : 0, activeApSignal) @@ -509,7 +537,10 @@ Panel { // an nmcli read still in flight from before cannot write a stale strength. property int activeApSignalGeneration: 0 property int activeApSignalRequest: -1 - onKindChanged: if (kind !== "wifi") resetActiveApSignal() + onKindChanged: { + if (kind !== "wifi") resetActiveApSignal() + if (opened && !detailsProc.running) detailsProc.running = true + } // Switching Wi-Fi networks always passes through a non-"wifi" kind (both the // device and the old network drop out of the connected state while the new @@ -631,6 +662,7 @@ Panel { checkConnectivity() if (scanWifi === undefined) scanWifi = false if (!detailsProc.running) detailsProc.running = true + if (dualConnected) refreshRoute() if (!dnsProc.running) { dnsProc.command = ["bash", "-c", root.dnsCommand("") + "; omarchy-region"] dnsProc.running = true @@ -884,7 +916,7 @@ Panel { if (actionKind !== "" || wifiAction.running || !network) return var ssid = network.name || "" actionSsid = ssid - switchingNetwork = kind === "connect" && root.kind !== "disconnected" + switchingNetwork = kind === "connect" && root.kind === "wifi" switchHeldQr = switchingNetwork && canShareWifi switchHeldSpeed = switchingNetwork && canRunSpeedTest actionKind = kind @@ -1093,6 +1125,22 @@ Panel { Component.onCompleted: refresh() + function refreshRoute() { + if (!dualConnected) return + if (routeProc.running) { + routeSettle.start() + return + } + routeGeneration++ + routeRequest = routeGeneration + routeProc.running = true + } + + function finishRoute(raw) { + if (routeRequest !== routeGeneration || !dualConnected) return + routeKind = Model.parseDefaultRouteKind(raw, connectedWiredNames, connectedWifiNames) + } + // Pulls everything we want about the active route's interface in one shot. Process { id: detailsProc @@ -1103,6 +1151,33 @@ Panel { } } + // Zero-ping main-table default-route lookup active only when both Ethernet + // and Wi-Fi are up. + Process { + id: routeProc + command: ["ip", "-4", "route", "show", "default"] + stdout: StdioCollector { + waitForEnd: true + onStreamFinished: root.finishRoute(text) + } + } + + Process { + id: routeMonitor + command: ["ip", "-4", "monitor", "route"] + running: root.dualConnected && !routeMonitorRestart.running + onStarted: if (root.dualConnected) root.refreshRoute() + onExited: if (root.dualConnected) routeMonitorRestart.restart() + stdout: SplitParser { + onRead: function(line) { + if (Model.isDefaultRouteEvent(line) && !routeSettle.running) routeSettle.start() + } + } + } + + Timer { id: routeMonitorRestart; interval: 1000 } + Timer { id: routeSettle; interval: 250; onTriggered: if (root.dualConnected) root.refreshRoute() } + // Reads the in-use access point's strength without triggering a scan, for // the connected network that has none of its own (see signalStrength). Process { @@ -1527,6 +1602,7 @@ Panel { // The HTTP restriction does not undo association. Show the live // SSID even before route/details polling has returned anything. if (root.kind === "wifi" && root.connectedWifiNetwork) return root.connectedWifiNetwork.name || "Wi-Fi" + if (root.kind === "ethernet" && !root.bandBusy) return Commons.I18n.t("network.ethernet") if (root.info.type === "wifi") return root.info.ssid || "Wi-Fi" if (root.info.type === "ethernet") return Commons.I18n.t("network.ethernet") return root.info.iface || (root.kind === "disconnected" ? Commons.I18n.t("network.disconnected") : Commons.I18n.t("network.noConnection")) diff --git a/test/shell.d/fixtures/network-panel/dual-interface.qml b/test/shell.d/fixtures/network-panel/dual-interface.qml new file mode 100644 index 000000000000..23b123965b00 --- /dev/null +++ b/test/shell.d/fixtures/network-panel/dual-interface.qml @@ -0,0 +1,160 @@ +import QtQuick +import Quickshell +import Quickshell.Io +import Quickshell.Networking +import qs.Commons +import qs.Commons as Commons +import "mocks" +import "network" as Network +import "network/Model.js" as Model + +// Verifies dual-interface behavior in Panel.qml: +// 1. Connecting Wi-Fi while Ethernet is primary keeps switchingNetwork false +// and preserves the Ethernet hero. +// 2. Docking local-only Ethernet while Wi-Fi-only stays on Wi-Fi without +// flashing Ethernet, and unrelated route events are ignored. +// 3. Main-table route changes flip kind and icon even when a VPN tunnel exists. +// 4. Switching Wi-Fi while Wi-Fi owns the route sets switchingNetwork true. +ShellRoot { + id: test + property bool failed: false + function check(ok, message) { + if (!ok) { + failed = true + console.log("RESULT fail " + message) + } + } + + component Net: QtObject { + property string name + property bool connected: false + property bool known: true + property bool stateChanging: false + property real signalStrength: 0.8 + property int security: WifiSecurityType.Open + signal connectionFailed(int reason) + function connect() {} + function connectWithPsk(psk) {} + function disconnect() {} + function forget() {} + } + + property QtObject wired: QtObject { + property int type: DeviceType.Wired + property string name: "test-eth" + property bool connected: true + property var networks: ({ values: [] }) + } + property Net office: Net { name: "OfficeWiFi"; signalStrength: 0.8 } + property Net cafe: Net { name: "Cafe"; signalStrength: 0.6 } + + property var afterRouteWrite: null + Process { + id: routeSetter + onExited: { + var cb = test.afterRouteWrite + test.afterRouteWrite = null + if (cb) cb() + } + } + + function setRoute(mode, notifyMode, cb) { + afterRouteWrite = cb || null + routeSetter.command = notifyMode ? ["test-set-route", mode, notifyMode] : ["test-set-route", mode] + routeSetter.running = true + } + + Item { + Network.Panel { + id: panel + bar: QtObject { + property color foreground: Commons.Color.foreground + property color barForeground: Commons.Color.foreground + property color urgent: Commons.Color.urgent + property string fontFamily: Style.font.family + property string position: "top" + property int barSize: 24 + property bool vertical: false + property bool foregroundAnimationEnabled: false + property var activePopout: null + function requestPopout(owner) { activePopout = owner } + function releasePopout(owner) { activePopout = null } + function registerClickTarget(target) {} + function unregisterClickTarget(target) {} + function hideTooltip(target) {} + function showTooltip(target, text) {} + } + } + } + + property var steps: [ + function() { + NetworkMock.wifi.connected = false + NetworkMock.wifi.networks = { values: [office, cafe] } + NetworkMock.devices = { values: [wired, NetworkMock.wifi] } + panel.refresh() + }, + function() { + check(panel.kind === "ethernet", "Ethernet-only reports kind=ethernet (got " + panel.kind + ")") + panel.connectDirectly("OfficeWiFi") + check(!panel.switchingNetwork, "connecting Wi-Fi while Ethernet is primary keeps switchingNetwork false") + panel.testEnsureContent() + check(panel.testTitle.text.indexOf("Ethernet") === 0, "Ethernet hero stays Ethernet during Wi-Fi connect (got " + panel.testTitle.text + ")") + check(panel.testMeta.text !== "CONNECTING...", "Ethernet hero meta does not flip to CONNECTING... (got " + panel.testMeta.text + ")") + office.connected = true + NetworkMock.wifi.connected = true + check(panel.kind === "ethernet", "Wi-Fi connecting over Ethernet does not flash kind (got " + panel.kind + ")") + }, + function() { + check(panel.dualConnected && panel.kind === "ethernet", "working Ethernet stays kind=ethernet with Wi-Fi connected (got " + panel.kind + ")") + wired.connected = false + setRoute("wifi", false) + }, + function() { + check(panel.kind === "wifi", "Wi-Fi-only reports kind=wifi") + wired.connected = true + check(panel.kind === "wifi", "docking local-only Ethernet while on Wi-Fi does not flash Ethernet (got " + panel.kind + ")") + }, + function() { + check(panel.dualConnected && panel.kind === "wifi", "route check confirms Wi-Fi stays active (got " + panel.kind + ")") + setRoute("ethernet", "noise") + }, + function() { + check(panel.kind === "wifi", "unrelated subnet and table-52 events do not flip kind (got " + panel.kind + ")") + setRoute("vpn-eth", "notify") + }, + function() { + // Wait for routeSettle + routeProc. + }, + function() { + check(panel.kind === "ethernet", "working Ethernet wins over Wi-Fi even with VPN tunnel (got " + panel.kind + ")") + check(!panel.canShareWifi, "QR share stays hidden when kind is ethernet") + check(panel.testTitle.text.indexOf("Ethernet") === 0, "Ethernet title shows immediately on failover (got " + panel.testTitle.text + ")") + setRoute("wifi", "notify") + }, + function() { + // Wait for routeSettle + routeProc. + }, + function() { + check(panel.kind === "wifi" && panel.icon === Model.wifiIconFor(80), "failover back to Wi-Fi updates kind and bar icon (got " + panel.kind + ")") + panel.connectDirectly("Cafe") + check(panel.switchingNetwork, "switching Wi-Fi while Wi-Fi owns the route sets switchingNetwork true") + office.connected = false + NetworkMock.wifi.connected = false + panel.clearNetworkAction() + }, + function() { + check(!panel.dualConnected && panel.kind === "ethernet", "Ethernet-only restores kind=ethernet (got " + panel.kind + ")") + if (!test.failed) console.log("RESULT pass") + Qt.quit() + } + ] + property int step: 0 + + Timer { + interval: 400 + running: test.step < test.steps.length + repeat: true + onTriggered: test.steps[test.step++]() + } +} diff --git a/test/shell.d/network-panel-runtime-test.sh b/test/shell.d/network-panel-runtime-test.sh index 22a86d5ea873..f51c77a895cc 100644 --- a/test/shell.d/network-panel-runtime-test.sh +++ b/test/shell.d/network-panel-runtime-test.sh @@ -75,6 +75,8 @@ run_panel() { output=$(HOME="$stage/home" OMARCHY_PATH="$ROOT" PATH="$stage/bin:$PATH" \ NETWORK_TEST_BROWSER_LOG="$stage/browser.log" NETWORK_TEST_NMCLI_LOG="$stage/nmcli.log" \ NETWORK_TEST_WIFI_LOG="$stage/wifi.log" \ + NETWORK_TEST_ROUTE_FILE="${NETWORK_TEST_ROUTE_FILE:-}" \ + NETWORK_TEST_ROUTE_EVENTS="${NETWORK_TEST_ROUTE_EVENTS:-}" \ timeout 30 quickshell -p "$stage" --no-color 2>&1) || fail "$1 fixture exits cleanly" "$output" [[ $output == *"RESULT pass"* ]] || fail "$1 runtime assertions pass" "$output" if rg -q 'RESULT fail|ReferenceError|TypeError|Error:|Unable to assign|Binding loop' <<<"$output"; then @@ -119,3 +121,48 @@ grep -qx "connect --interface test-wifi -- Cafe" "$stage/wifi.log" || grep -q "Home" "$stage/wifi.log" && fail "network panel does not spawn omarchy-network-wifi for an already-connected SSID" "$(cat "$stage/wifi.log")" pass "network rows follow connects, drops, saves and forgets made outside the panel, with the cursor on its network" + +stage_panel dual-interface +printf 'ethernet\n' >"$stage/route.state" +: >"$stage/route.events" +cat >"$stage/bin/omarchy-network-status" <<'SH' +#!/bin/bash +mode=$(cat "$NETWORK_TEST_ROUTE_FILE" 2>/dev/null || echo ethernet) +if [[ $mode == "wifi" ]]; then + printf 'type\twifi\niface\ttest-wifi\nssid\tOfficeWiFi\nip\t192.0.2.20\ngateway\t192.0.2.1\n' +else + printf 'type\tethernet\niface\ttest-eth\nip\t192.0.2.10\ngateway\t192.0.2.1\nspeed\t1000\n' +fi +SH +chmod +x "$stage/bin/omarchy-network-status" +cat >"$stage/bin/ip" <<'SH' +#!/bin/bash +if [[ $1 == "-4" && $2 == "monitor" && $3 == "route" ]]; then + exec tail -n +1 -s 0.05 -F "$NETWORK_TEST_ROUTE_EVENTS" 2>/dev/null +fi +if [[ $1 == "-4" && $2 == "route" && $3 == "show" && $4 == "default" ]]; then + mode=$(cat "$NETWORK_TEST_ROUTE_FILE" 2>/dev/null || echo ethernet) + if [[ $mode == "wifi" ]]; then + printf 'default via 192.0.2.1 dev test-wifi metric 600\ndefault via 192.0.2.1 dev test-eth metric 20100\n' + elif [[ $mode == "vpn-eth" ]]; then + printf 'default dev tailscale0 scope link metric 50\ndefault via 192.0.2.1 dev test-eth metric 100\ndefault via 192.0.2.1 dev test-wifi metric 600\n' + else + printf 'default via 192.0.2.1 dev test-eth metric 100\ndefault via 192.0.2.1 dev test-wifi metric 600\n' + fi + exit 0 +fi +exec /usr/bin/ip "$@" +SH +chmod +x "$stage/bin/ip" +cat >"$stage/bin/test-set-route" <<'SH' +#!/bin/bash +printf '%s\n' "$1" >"$NETWORK_TEST_ROUTE_FILE" +if [[ ${2:-} == "notify" ]]; then + printf 'default via 192.0.2.1 dev %s\n' "$1" >>"$NETWORK_TEST_ROUTE_EVENTS" +elif [[ ${2:-} == "noise" ]]; then + printf '10.0.0.0/24 dev docker0 proto kernel\ndefault dev tailscale0 table 52\n' >>"$NETWORK_TEST_ROUTE_EVENTS" +fi +SH +chmod +x "$stage/bin/test-set-route" +NETWORK_TEST_ROUTE_FILE="$stage/route.state" NETWORK_TEST_ROUTE_EVENTS="$stage/route.events" run_panel "network dual interface" +pass "network keeps Ethernet hero during Wi-Fi connect and follows active route when both links are up" diff --git a/test/shell.d/network-test.sh b/test/shell.d/network-test.sh index 1cb97d090d3f..616e97b1ad39 100644 --- a/test/shell.d/network-test.sh +++ b/test/shell.d/network-test.sh @@ -96,6 +96,38 @@ assertDeepEqual( ) assertEqual(network.connectionIcon('wifi', 80), network.wifiIconFor(80), 'network maps wifi icon from signal') +assertEqual( + network.parseDefaultRouteKind( + 'default via 192.0.2.1 dev enp0s4 metric 100\ndefault via 192.0.2.1 dev wlan0 metric 600\n', + ['enp0s4'], ['wlan0'] + ), + 'ethernet', + 'network picks lowest-metric ethernet default route over wifi' +) +assertEqual( + network.parseDefaultRouteKind( + 'default via 192.0.2.1 dev wlan0 metric 600\ndefault via 192.0.2.1 dev enp0s4 metric 20100\n', + ['enp0s4'], ['wlan0'] + ), + 'wifi', + 'network picks wifi when ethernet carries NetworkManager connectivity penalty' +) +assertEqual( + network.parseDefaultRouteKind( + 'default dev tailscale0 scope link metric 50\ndefault via 192.0.2.1 dev enp1s0 metric 100\ndefault via 192.0.2.1 dev wlan0 metric 600\n', + ['enp0s4', 'enp1s0'], ['wlan0'] + ), + 'ethernet', + 'network ignores VPN tunnel routes and matches second wired adapter' +) +assertEqual(network.parseDefaultRouteKind('', ['enp0s4'], ['wlan0']), '', 'network tolerates empty ip route output') +assertEqual(network.isDefaultRouteEvent('default via 192.0.2.1 dev enp0s4 metric 100'), true, 'route monitor matches main default add') +assertEqual(network.isDefaultRouteEvent('Deleted default via 192.0.2.1 dev enp0s4 metric 20100'), true, 'route monitor matches main default delete') +assertEqual(network.isDefaultRouteEvent('default dev tailscale0 table 52'), false, 'route monitor ignores non-main tables') +assertEqual(network.isDefaultRouteEvent('10.0.0.0/24 dev docker0 proto kernel'), false, 'route monitor ignores non-default subnets') +assertEqual(network.connectionKind(true, true, ''), 'ethernet', 'dual-connected defaults to ethernet before a route is read') +assertEqual(network.connectionKind(true, true, 'wifi'), 'wifi', 'dual-connected follows wifi when wifi owns the route') + // NetworkManager's two dBm-to-strength formulas (scan results, and the // connected access point's station refresh) must not change the bars for the // same link, and a strong -50 dBm link shows full bars either way.