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 AstalWp from "gi://AstalWp?version=0.1"
|
||||||
import AstalPowerProfiles from "gi://AstalPowerProfiles?version=0.1"
|
import AstalPowerProfiles from "gi://AstalPowerProfiles?version=0.1"
|
||||||
import { createBinding, createComputed, createState, For } from "ags"
|
import { createBinding, createComputed, createState, For } from "ags"
|
||||||
import { interval } from "ags/time"
|
import { interval, Timer } from "ags/time"
|
||||||
import { exec, execAsync } from "ags/process"
|
import { execAsync } from "ags/process"
|
||||||
|
|
||||||
interface VpnConn {
|
interface VpnConn {
|
||||||
name: string
|
name: string
|
||||||
@@ -14,9 +14,9 @@ interface VpnConn {
|
|||||||
|
|
||||||
const VPN_TYPES = new Set(["vpn", "wireguard"])
|
const VPN_TYPES = new Set(["vpn", "wireguard"])
|
||||||
|
|
||||||
function fetchVpns(): VpnConn[] {
|
async function fetchVpns(): Promise<VpnConn[]> {
|
||||||
try {
|
try {
|
||||||
const stdout = exec([
|
const stdout = await execAsync([
|
||||||
"nmcli",
|
"nmcli",
|
||||||
"-t",
|
"-t",
|
||||||
"-f",
|
"-f",
|
||||||
@@ -27,6 +27,7 @@ function fetchVpns(): VpnConn[] {
|
|||||||
return stdout
|
return stdout
|
||||||
.trim()
|
.trim()
|
||||||
.split("\n")
|
.split("\n")
|
||||||
|
.filter(Boolean)
|
||||||
.map((line) => line.split(":"))
|
.map((line) => line.split(":"))
|
||||||
.filter(([, type]) => VPN_TYPES.has(type))
|
.filter(([, type]) => VPN_TYPES.has(type))
|
||||||
.map(([name, , device]) => ({ name, active: !!device }))
|
.map(([name, , device]) => ({ name, active: !!device }))
|
||||||
@@ -36,12 +37,58 @@ function fetchVpns(): VpnConn[] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const [vpns, setVpns] = createState<VpnConn[]>(fetchVpns())
|
const [vpns, setVpns] = createState<VpnConn[]>([])
|
||||||
|
|
||||||
interval(2000, () => setVpns(fetchVpns()))
|
|
||||||
|
|
||||||
const vpnActive = vpns((list) => list.some((c) => c.active))
|
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> = {
|
const POWER_PROFILE_ICONS: Record<string, string> = {
|
||||||
"power-saver": "power-profile-power-saver-symbolic",
|
"power-saver": "power-profile-power-saver-symbolic",
|
||||||
balanced: "power-profile-balanced-symbolic",
|
balanced: "power-profile-balanced-symbolic",
|
||||||
@@ -95,25 +142,28 @@ function NetworkItem() {
|
|||||||
|
|
||||||
function BluetoothItem() {
|
function BluetoothItem() {
|
||||||
const bluetooth = AstalBluetooth.get_default()
|
const bluetooth = AstalBluetooth.get_default()
|
||||||
|
const powered = createBinding(bluetooth, "is-powered")
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<box spacing={12} valign={Gtk.Align.CENTER} cssClasses={["qs-item"]}>
|
<box spacing={12} valign={Gtk.Align.CENTER} cssClasses={["qs-item"]}>
|
||||||
<image
|
<image
|
||||||
iconName={createBinding(bluetooth, "is-powered").as((powered) =>
|
iconName={powered.as((isPowered) =>
|
||||||
powered ? "bluetooth-active-symbolic" : "bluetooth-disabled-symbolic",
|
isPowered
|
||||||
|
? "bluetooth-active-symbolic"
|
||||||
|
: "bluetooth-disabled-symbolic",
|
||||||
)}
|
)}
|
||||||
cssClasses={["qs-icon"]}
|
cssClasses={["qs-icon"]}
|
||||||
/>
|
/>
|
||||||
<label
|
<label
|
||||||
label={createBinding(bluetooth, "is-powered").as((powered) =>
|
label={powered.as((isPowered) =>
|
||||||
powered ? "Bluetooth On" : "Bluetooth Off",
|
isPowered ? "Bluetooth On" : "Bluetooth Off",
|
||||||
)}
|
)}
|
||||||
halign={Gtk.Align.START}
|
halign={Gtk.Align.START}
|
||||||
hexpand
|
hexpand
|
||||||
/>
|
/>
|
||||||
<switch
|
<switch
|
||||||
valign={Gtk.Align.CENTER}
|
valign={Gtk.Align.CENTER}
|
||||||
active={createBinding(bluetooth, "is-powered")}
|
active={powered}
|
||||||
onNotifyActive={(sw) => {
|
onNotifyActive={(sw) => {
|
||||||
if (bluetooth.adapter) {
|
if (bluetooth.adapter) {
|
||||||
bluetooth.adapter.set_powered(sw.get_active())
|
bluetooth.adapter.set_powered(sw.get_active())
|
||||||
@@ -156,13 +206,16 @@ function AudioSlider({ type }: { type: "speaker" | "microphone" }) {
|
|||||||
function VpnList() {
|
function VpnList() {
|
||||||
const isEmpty = vpns((list) => list.length === 0)
|
const isEmpty = vpns((list) => list.length === 0)
|
||||||
|
|
||||||
function toggle(conn: VpnConn) {
|
async function toggle(conn: VpnConn) {
|
||||||
const cmd = conn.active
|
const cmd = conn.active
|
||||||
? ["nmcli", "connection", "down", conn.name]
|
? ["nmcli", "connection", "down", conn.name]
|
||||||
: ["nmcli", "connection", "up", conn.name]
|
: ["nmcli", "connection", "up", conn.name]
|
||||||
execAsync(cmd)
|
try {
|
||||||
.then(() => setVpns(fetchVpns()))
|
await execAsync(cmd)
|
||||||
.catch((e) => console.error("VPN toggle failed:", e))
|
await refreshVpns()
|
||||||
|
} catch (e) {
|
||||||
|
console.error("VPN toggle failed:", e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -261,13 +314,26 @@ function PowerProfileItem() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function QuickSettings() {
|
export default function QuickSettings() {
|
||||||
|
let popoverOpen = false
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<menubutton
|
<menubutton
|
||||||
cssClasses={["control-btn", "settings-btn"]}
|
cssClasses={["control-btn", "settings-btn"]}
|
||||||
tooltipText="Quick Settings"
|
tooltipText="Quick Settings"
|
||||||
>
|
>
|
||||||
<image iconName="emblem-system-symbolic" />
|
<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
|
<box
|
||||||
orientation={Gtk.Orientation.VERTICAL}
|
orientation={Gtk.Orientation.VERTICAL}
|
||||||
spacing={16}
|
spacing={16}
|
||||||
|
|||||||
Reference in new issue
Block a user