Files
tacit-shell/widget/QuickSettings.tsx
T
pascalyazid 38c6ce5812 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.
2026-11-27 18:55:33 +01:00

403 lines
11 KiB
TypeScript

import { Gtk } from "ags/gtk4"
import AstalNetwork from "gi://AstalNetwork?version=0.1"
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, Timer } from "ags/time"
import { execAsync } from "ags/process"
interface VpnConn {
name: string
active: boolean
}
const VPN_TYPES = new Set(["vpn", "wireguard"])
async function fetchVpns(): Promise<VpnConn[]> {
try {
const stdout = await execAsync([
"nmcli",
"-t",
"-f",
"NAME,TYPE,DEVICE",
"connection",
"show",
])
return stdout
.trim()
.split("\n")
.filter(Boolean)
.map((line) => line.split(":"))
.filter(([, type]) => VPN_TYPES.has(type))
.map(([name, , device]) => ({ name, active: !!device }))
} catch (e) {
console.error("nmcli query failed:", e)
return []
}
}
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",
performance: "power-profile-performance-symbolic",
}
const POWER_PROFILE_LABELS: Record<string, string> = {
"power-saver": "Power Saver",
balanced: "Balanced",
performance: "Performance",
}
const powerprofiles = AstalPowerProfiles.get_default()
const activeProfile = createBinding(powerprofiles, "active-profile")
const powerProfiles = powerprofiles.get_profiles().map((p) => p.profile)
const hasPowerProfiles = powerProfiles.length > 0
function NetworkItem() {
const network = AstalNetwork.Network.get_default()
const primary = createBinding(network, "primary")
const label = createComputed(() => {
const p = primary()
if (p === AstalNetwork.Primary.WIFI && network.wifi) {
return network.wifi.ssid || "Unknown Wi-Fi"
} else if (p === AstalNetwork.Primary.WIRED) {
return "Wired Connection"
}
return vpnActive() ? "Connected to VPN" : "Disconnected"
})
return (
<box spacing={12} valign={Gtk.Align.CENTER} cssClasses={["qs-item"]}>
<image
iconName={primary.as((p) => {
if (p === AstalNetwork.Primary.WIFI && network.wifi) {
return network.wifi.icon_name
} else if (p === AstalNetwork.Primary.WIRED && network.wired) {
return network.wired.icon_name
}
return vpnActive()
? "network-vpn-symbolic"
: "network-offline-symbolic"
})}
cssClasses={["qs-icon"]}
/>
<label label={label} halign={Gtk.Align.START} hexpand />
</box>
)
}
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={powered.as((isPowered) =>
isPowered
? "bluetooth-active-symbolic"
: "bluetooth-disabled-symbolic",
)}
cssClasses={["qs-icon"]}
/>
<label
label={powered.as((isPowered) =>
isPowered ? "Bluetooth On" : "Bluetooth Off",
)}
halign={Gtk.Align.START}
hexpand
/>
<switch
valign={Gtk.Align.CENTER}
active={powered}
onNotifyActive={(sw) => {
if (bluetooth.adapter) {
bluetooth.adapter.set_powered(sw.get_active())
}
}}
/>
</box>
)
}
function AudioSlider({ type }: { type: "speaker" | "microphone" }) {
const wp = AstalWp.get_default()
const audio = wp?.audio
if (!audio) return <box />
const endpoint =
type === "speaker" ? audio.default_speaker : audio.default_microphone
if (!endpoint) return <box />
return (
<box spacing={12} valign={Gtk.Align.CENTER} cssClasses={["qs-item"]}>
<button
cssClasses={["qs-icon-btn"]}
onClicked={() => endpoint.set_mute(!endpoint.get_mute())}
>
<image iconName={createBinding(endpoint, "volume-icon")} />
</button>
<slider
hexpand
drawValue={false}
value={createBinding(endpoint, "volume")}
onChangeValue={({ value }) => endpoint.set_volume(value)}
/>
</box>
)
}
function VpnList() {
const isEmpty = vpns((list) => list.length === 0)
async function toggle(conn: VpnConn) {
const cmd = conn.active
? ["nmcli", "connection", "down", conn.name]
: ["nmcli", "connection", "up", conn.name]
try {
await execAsync(cmd)
await refreshVpns()
} catch (e) {
console.error("VPN toggle failed:", e)
}
}
return (
<box orientation={Gtk.Orientation.VERTICAL} spacing={8}>
<label label="VPN" cssClasses={["qs-title"]} halign={Gtk.Align.START} />
<box
spacing={12}
valign={Gtk.Align.CENTER}
cssClasses={["qs-item"]}
visible={isEmpty}
>
<label
label="No VPN connections configured"
halign={Gtk.Align.START}
hexpand
/>
</box>
<For each={vpns}>
{(conn: VpnConn) => (
<box spacing={12} valign={Gtk.Align.CENTER} cssClasses={["qs-item"]}>
<image
iconName={
conn.active
? "network-vpn-symbolic"
: "network-vpn-no-route-symbolic"
}
cssClasses={["qs-icon"]}
/>
<label label={conn.name} halign={Gtk.Align.START} hexpand />
<switch
valign={Gtk.Align.CENTER}
active={conn.active}
onNotifyActive={(sw) => {
if (sw.get_active() !== conn.active) {
toggle(conn)
}
}}
/>
</box>
)}
</For>
</box>
)
}
function PowerProfileItem() {
return (
<menubutton cssClasses={["qs-item"]} hexpand>
<box spacing={12} valign={Gtk.Align.CENTER}>
<image
iconName={activeProfile(
(p) => POWER_PROFILE_ICONS[p] ?? "power-profile-balanced-symbolic",
)}
cssClasses={["qs-icon"]}
/>
<label
label={activeProfile((p) => POWER_PROFILE_LABELS[p] ?? p)}
halign={Gtk.Align.START}
hexpand
/>
<image iconName="pan-down-symbolic" />
</box>
<popover>
<box orientation={Gtk.Orientation.VERTICAL} spacing={4}>
{powerProfiles.map((name) => (
<button
cssClasses={["qs-item"]}
onClicked={(btn) => {
powerprofiles.set_active_profile(name)
const popover = btn.get_ancestor(
Gtk.Popover.$gtype,
) as Gtk.Popover | null
popover?.popdown()
}}
>
<box spacing={12} valign={Gtk.Align.CENTER}>
<image
iconName={
POWER_PROFILE_ICONS[name] ??
"power-profile-balanced-symbolic"
}
cssClasses={["qs-icon"]}
/>
<label
label={POWER_PROFILE_LABELS[name] ?? name}
halign={Gtk.Align.START}
hexpand
/>
</box>
</button>
))}
</box>
</popover>
</menubutton>
)
}
export default function QuickSettings() {
let popoverOpen = false
return (
<menubutton
cssClasses={["control-btn", "settings-btn"]}
tooltipText="Quick Settings"
>
<image iconName="emblem-system-symbolic" />
<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}
cssClasses={["quick-settings-menu"]}
>
<box orientation={Gtk.Orientation.VERTICAL} spacing={8}>
<label
label="Network & Bluetooth"
cssClasses={["qs-title"]}
halign={Gtk.Align.START}
/>
<NetworkItem />
<BluetoothItem />
</box>
<VpnList />
<box
orientation={Gtk.Orientation.VERTICAL}
spacing={8}
visible={hasPowerProfiles}
>
<label
label="Power Profile"
cssClasses={["qs-title"]}
halign={Gtk.Align.START}
/>
<PowerProfileItem />
</box>
<box orientation={Gtk.Orientation.VERTICAL} spacing={8}>
<button
cssClasses={["qs-title"]}
halign={Gtk.Align.START}
onClicked={() => execAsync(["/usr/bin/easyeffects"])}
>
<label label="Audio" halign={Gtk.Align.START} />
</button>
<AudioSlider type="speaker" />
<AudioSlider type="microphone" />
</box>
<box orientation={Gtk.Orientation.VERTICAL} spacing={8}>
<label
label="Tools"
cssClasses={["qs-title"]}
halign={Gtk.Align.START}
/>
<button
cssClasses={["qs-item"]}
onClicked={() => execAsync(["hyprmod"])}
>
<box spacing={12} valign={Gtk.Align.CENTER}>
<image
iconName="applications-system-symbolic"
cssClasses={["qs-icon"]}
/>
<label
label="Hyprland Settings"
halign={Gtk.Align.START}
hexpand
/>
</box>
</button>
</box>
</box>
</popover>
</menubutton>
)
}