diff --git a/widget/QuickSettings.tsx b/widget/QuickSettings.tsx index faef3df..bd66133 100644 --- a/widget/QuickSettings.tsx +++ b/widget/QuickSettings.tsx @@ -4,8 +4,8 @@ import AstalBluetooth from "gi://AstalBluetooth?version=0.1" import AstalWp from "gi://AstalWp?version=0.1" import AstalPowerProfiles from "gi://AstalPowerProfiles?version=0.1" import { createBinding, createComputed, createState, For } from "ags" -import { interval } from "ags/time" -import { exec, execAsync } from "ags/process" +import { interval, Timer } from "ags/time" +import { execAsync } from "ags/process" interface VpnConn { name: string @@ -14,9 +14,9 @@ interface VpnConn { const VPN_TYPES = new Set(["vpn", "wireguard"]) -function fetchVpns(): VpnConn[] { +async function fetchVpns(): Promise { try { - const stdout = exec([ + const stdout = await execAsync([ "nmcli", "-t", "-f", @@ -27,6 +27,7 @@ function fetchVpns(): VpnConn[] { return stdout .trim() .split("\n") + .filter(Boolean) .map((line) => line.split(":")) .filter(([, type]) => VPN_TYPES.has(type)) .map(([name, , device]) => ({ name, active: !!device })) @@ -36,12 +37,58 @@ function fetchVpns(): VpnConn[] { } } -const [vpns, setVpns] = createState(fetchVpns()) - -interval(2000, () => setVpns(fetchVpns())) - +const [vpns, setVpns] = createState([]) const vpnActive = vpns((list) => list.some((c) => c.active)) +function sameVpns(a: VpnConn[], b: VpnConn[]) { + return ( + a.length === b.length && + a.every((conn, i) => conn.name === b[i].name && conn.active === b[i].active) + ) +} + +let refreshingVpns = false +let vpnRefreshQueued = false + +async function refreshVpns() { + if (refreshingVpns) { + vpnRefreshQueued = true + return + } + + refreshingVpns = true + try { + do { + vpnRefreshQueued = false + const next = await fetchVpns() + setVpns((prev) => (sameVpns(prev, next) ? prev : next)) + } while (vpnRefreshQueued) + } finally { + refreshingVpns = false + } +} + +// nmcli is only polled while a Quick Settings popover is open, and only one +// poller runs no matter how many bars have their popover open. +let vpnPollers = 0 +let vpnTimer: Timer | null = null + +function acquireVpnPolling() { + vpnPollers += 1 + if (vpnTimer) return + + // interval() runs its callback immediately, so this also refreshes on open. + vpnTimer = interval(3000, refreshVpns) +} + +function releaseVpnPolling() { + vpnPollers = Math.max(0, vpnPollers - 1) + if (vpnPollers > 0 || vpnTimer === null) return + + vpnTimer.cancel() + vpnTimer = null +} + const POWER_PROFILE_ICONS: Record = { "power-saver": "power-profile-power-saver-symbolic", balanced: "power-profile-balanced-symbolic", @@ -95,25 +142,28 @@ function NetworkItem() { function BluetoothItem() { const bluetooth = AstalBluetooth.get_default() + const powered = createBinding(bluetooth, "is-powered") return ( - powered ? "bluetooth-active-symbolic" : "bluetooth-disabled-symbolic", + iconName={powered.as((isPowered) => + isPowered + ? "bluetooth-active-symbolic" + : "bluetooth-disabled-symbolic", )} cssClasses={["qs-icon"]} />