perf(quicksettings): poll VPNs only while the popover is open
Replace the always-on 2s nmcli poll with a shared poller that runs only while Quick Settings is visible, coalesce refreshes, and reuse the Bluetooth power binding.
This commit is contained in:
1 parent
a1cea0e3be
commit
38c6ce5812
1 file changed
+84
-18
+84
-18
@@ -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<VpnConn[]> {
|
||||
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<VpnConn[]>(fetchVpns())
|
||||
|
||||
interval(2000, () => setVpns(fetchVpns()))
|
||||
|
||||
const [vpns, setVpns] = createState<VpnConn[]>([])
|
||||
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<string, string> = {
|
||||
"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 (
|
||||
<box spacing={12} valign={Gtk.Align.CENTER} cssClasses={["qs-item"]}>
|
||||
<image
|
||||
iconName={createBinding(bluetooth, "is-powered").as((powered) =>
|
||||
powered ? "bluetooth-active-symbolic" : "bluetooth-disabled-symbolic",
|
||||
iconName={powered.as((isPowered) =>
|
||||
isPowered
|
||||
? "bluetooth-active-symbolic"
|
||||
: "bluetooth-disabled-symbolic",
|
||||
)}
|
||||
cssClasses={["qs-icon"]}
|
||||
/>
|
||||
<label
|
||||
label={createBinding(bluetooth, "is-powered").as((powered) =>
|
||||
powered ? "Bluetooth On" : "Bluetooth Off",
|
||||
label={powered.as((isPowered) =>
|
||||
isPowered ? "Bluetooth On" : "Bluetooth Off",
|
||||
)}
|
||||
halign={Gtk.Align.START}
|
||||
hexpand
|
||||
/>
|
||||
<switch
|
||||
valign={Gtk.Align.CENTER}
|
||||
active={createBinding(bluetooth, "is-powered")}
|
||||
active={powered}
|
||||
onNotifyActive={(sw) => {
|
||||
if (bluetooth.adapter) {
|
||||
bluetooth.adapter.set_powered(sw.get_active())
|
||||
@@ -156,13 +206,16 @@ function AudioSlider({ type }: { type: "speaker" | "microphone" }) {
|
||||
function VpnList() {
|
||||
const isEmpty = vpns((list) => list.length === 0)
|
||||
|
||||
function toggle(conn: VpnConn) {
|
||||
async function toggle(conn: VpnConn) {
|
||||
const cmd = conn.active
|
||||
? ["nmcli", "connection", "down", conn.name]
|
||||
: ["nmcli", "connection", "up", conn.name]
|
||||
execAsync(cmd)
|
||||
.then(() => setVpns(fetchVpns()))
|
||||
.catch((e) => console.error("VPN toggle failed:", e))
|
||||
try {
|
||||
await execAsync(cmd)
|
||||
await refreshVpns()
|
||||
} catch (e) {
|
||||
console.error("VPN toggle failed:", e)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -261,13 +314,26 @@ function PowerProfileItem() {
|
||||
}
|
||||
|
||||
export default function QuickSettings() {
|
||||
let popoverOpen = false
|
||||
|
||||
return (
|
||||
<menubutton
|
||||
cssClasses={["control-btn", "settings-btn"]}
|
||||
tooltipText="Quick Settings"
|
||||
>
|
||||
<image iconName="emblem-system-symbolic" />
|
||||
<popover>
|
||||
<popover
|
||||
$={(popover: Gtk.Popover) => {
|
||||
popover.connect("notify::visible", () => {
|
||||
const visible = popover.get_visible()
|
||||
if (visible === popoverOpen) return
|
||||
|
||||
popoverOpen = visible
|
||||
if (visible) acquireVpnPolling()
|
||||
else releaseVpnPolling()
|
||||
})
|
||||
}}
|
||||
>
|
||||
<box
|
||||
orientation={Gtk.Orientation.VERTICAL}
|
||||
spacing={16}
|
||||
|
||||
Reference in new issue
Block a user