repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nuguland/Network.qml

Raw
import QtQuick
import Quickshell

Item {
    id: root

    readonly property string hostStack: "iwd + systemd-networkd + systemd-resolved"
    property string error: ""
    property var lastAction: ({
    })
    readonly property string nativeApi: "host stack: iwd + systemd-networkd + systemd-resolved"
    readonly property var boundedActions: ["scan", "select", "forget", "toggle-wifi", "check-connectivity"]
    readonly property bool nativeActionRoute: false

    function networkId(network) {
        return String(network.id || network.name || network.ssid || "network");
    }

    function cloneNetwork(network) {
        return {
            "id": networkId(network),
            "ssid": String(network.ssid || network.name || network.id || "network"),
            "state": String(network.state || (network.connected ? "connected" : "available")),
            "signal": Number(network.signal ?? Math.round(Number(network.signalStrength || 0) * 100)),
            "security": String(network.security || "unknown"),
            "known": !!network.known
        };
    }

    function hostNetworks() {
        return [];
    }

    function sortedNetworks(rows) {
        return rows.slice(0).sort((a, b) => {
            const aConnected = a.state === "connected" || a.state === "Connected" || a.connected;
            const bConnected = b.state === "connected" || b.state === "Connected" || b.connected;
            if (aConnected !== bConnected)
                return aConnected ? -1 : 1;

            const aKnown = !!a.known;
            const bKnown = !!b.known;
            if (aKnown !== bKnown)
                return aKnown ? -1 : 1;

            return Number(b.signal || 0) - Number(a.signal || 0);
        });
    }

    function networks() {
        return sortedNetworks(hostNetworks());
    }

    function connectivityPanelContract() {
        return {
            "source": "Nuguland bar popouts Network.qml plus Nuguland connections Network.qml Bluetooth.qml",
            "ownedBy": "PopoutHost.qml inline ConnectivityPanel",
            "combinedConnectivityPanel": true,
            "wifiToggleScanList": true,
            "bluetoothToggleScanList": true,
            "sortedNetworkRows": true,
            "sortedDeviceRows": true,
            "batteryBadges": true,
            "serviceActionsOnly": true,
            "rowKinds": ["wifi-network", "bluetooth-device"],
            "nativeServices": ["Network.qml host stack", "Bluetooth.qml"],
            "hostStack": hostStack,
            "noShellHelpers": true
        };
    }

    function activeNetworkObject() {
        const list = networks();
        for (let i = 0; i < list.length; i++) if (list[i].state === "connected" || list[i].state === "Connected" || list[i].connected) {
            return list[i];
        }
        for (let i = 0; i < list.length; i++) if (list[i].state === "connecting" || list[i].state === "Connecting") {
            return list[i];
        }
        return {
            "id": "host-network",
            "ssid": "host-network",
            "state": "host-managed",
            "signal": 0,
            "security": "systemd-networkd",
            "known": false
        };
    }

    function knownNetworksById() {
        const indexed = {
        };
        const list = networks();
        for (let i = 0; i < list.length; i++) indexed[networkId(list[i])] = cloneNetwork(list[i])
        return indexed;
    }

    function wifiOn() {
        return true;
    }

    function scanning() {
        return false;
    }

    function role() {
        const active = activeNetworkObject();
        if (error.length > 0)
            return "error";

        if (!wifiOn())
            return "ui.minor";

        if (active.state === "connecting" || active.state === "Connecting")
            return "warning";

        return active.state === "connected" || active.state === "Connected" || active.state === "host-managed" ? "ui.accent" : "ui.minor";
    }

    function stateObject() {
        const active = cloneNetwork(activeNetworkObject());
        return {
            "backend": "Network.qml",
            "usesNativeQuickshellNetworking": false,
            "usesHostStack": true,
            "hostStack": hostStack,
            "nativeApi": nativeApi,
            "nativeBackend": "iwd-systemd-networkd-resolved",
            "nativeWifiEnabled": wifiOn(),
            "nativeWifiHardwareEnabled": wifiOn(),
            "nativeCanCheckConnectivity": false,
            "nativeConnectivity": "host-managed",
            "nativeActionRoute": nativeActionRoute,
            "helperProcesses": [],
            "serviceOwnedActions": true,
            "boundedActions": boundedActions,
            "connectivityPanelContract": connectivityPanelContract(),
            "scanning": scanning(),
            "error": error,
            "wifi": {
                "enabled": wifiOn(),
                "hardwareEnabled": wifiOn()
            },
            "active": active,
            "knownNetworks": networks().map((network) => {
                return cloneNetwork(network);
            }),
            "knownNetworksById": knownNetworksById(),
            "compact": {
                "label": "Network",
                "role": role(),
                "signal": wifiOn() ? active.signal : 0
            },
            "shader": {
                "kind": "network-mini-graph",
                "clipped": true,
                "source": "shaders/network-mini-graph.frag.qsb",
                "priorArt": "Shadertoy smoothstep distance-to-line graph primitives plus matrix-rain inspired packet rain",
                "rateSource": "Resources.qml",
                "colors": ["ui.accent", "ui.focus", "ui.minor", "ui.backdrop", "warning", "error"]
            },
            "lastAction": lastAction
        };
    }

    function stateJson() {
        return JSON.stringify(stateObject());
    }

    function scan() {
        error = "scan-owned-by-iwd";
        lastAction = {
            "kind": "scan",
            "nativeActionRoute": nativeActionRoute,
            "hostStack": hostStack
        };
        return stateJson();
    }

    function select(id) {
        const networkIdValue = String(id);
        const list = networks();
        let selected = null;
        for (let i = 0; i < list.length; i++) if (networkId(list[i]) === networkIdValue) {
            selected = list[i];
        }
        if (!selected) {
            error = "unknown-network";
            lastAction = {
                "kind": "select",
                "id": networkIdValue,
                "nativeActionRoute": nativeActionRoute,
                "hostStack": hostStack
            };
            return stateJson();
        }
        error = "select-owned-by-iwd";
        lastAction = {
            "kind": "select",
            "id": networkIdValue,
            "nativeActionRoute": nativeActionRoute,
            "hostStack": hostStack
        };
        return stateJson();
    }

    function toggleWifi(enabled) {
        error = "wifi-toggle-owned-by-iwd";
        lastAction = {
            "kind": "toggle-wifi",
            "enabled": wifiOn(),
            "nativeActionRoute": nativeActionRoute,
            "hostStack": hostStack
        };
        return stateJson();
    }

    function forget(id) {
        const networkIdValue = String(id);
        lastAction = {
            "kind": "forget",
            "id": networkIdValue,
            "nativeActionRoute": nativeActionRoute,
            "hostStack": hostStack
        };
        return stateJson();
    }

    function checkConnectivity() {
        lastAction = {
            "kind": "check-connectivity",
            "nativeActionRoute": nativeActionRoute,
            "hostStack": hostStack
        };
        return stateJson();
    }

}