Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions shell/plugins/panels/network/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
108 changes: 92 additions & 16 deletions shell/plugins/panels/network/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Details follow another link

With both links connected and a full-tunnel VPN active, finishRoute() selects the physical link, but detailsProc still runs omarchy-network-status --verbose without choosing an interface. That helper follows ip route get 1.1.1.1, so the hero can name Wi-Fi while the address and transfer counts belong to the VPN. The helper also reports the tunnel as Ethernet, which hides Wi-Fi sharing through canShareWifi.

Keep the winning interface from the route lookup and use it for the displayed details.

Knowledge Base Used: Captive portal and network connectivity

Fix in Claude Code

}

// Pulls everything we want about the active route's interface in one shot.
Process {
id: detailsProc
Expand All @@ -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 {
Expand Down Expand Up @@ -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"))
Expand Down
160 changes: 160 additions & 0 deletions test/shell.d/fixtures/network-panel/dual-interface.qml
Original file line number Diff line number Diff line change
@@ -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++]()
}
}
Loading