Files
tacit-shell/widget/QuickSettings.tsx
T

255 lines
7.1 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 { createBinding, createComputed, createState, For } from "ags"
import { interval } from "ags/time"
import { exec, execAsync } from "ags/process"
interface VpnConn {
name: string
active: boolean
}
const VPN_TYPES = new Set(["vpn", "wireguard"])
function fetchVpns(): VpnConn[] {
try {
const stdout = exec([
"nmcli",
"-t",
"-f",
"NAME,TYPE,DEVICE",
"connection",
"show",
])
return stdout
.trim()
.split("\n")
.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[]>(fetchVpns())
interval(2000, () => setVpns(fetchVpns()))
const vpnActive = vpns((list) => list.some((c) => c.active))
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()
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",
)}
cssClasses={["qs-icon"]}
/>
<label
label={createBinding(bluetooth, "is-powered").as((powered) =>
powered ? "Bluetooth On" : "Bluetooth Off",
)}
halign={Gtk.Align.START}
hexpand
/>
<switch
valign={Gtk.Align.CENTER}
active={createBinding(bluetooth, "is-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)
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))
}
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>
)
}
export default function QuickSettings() {
return (
<menubutton
cssClasses={["control-btn", "settings-btn"]}
tooltipText="Quick Settings"
>
<image iconName="emblem-system-symbolic" />
<popover>
<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}>
<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>
)
}