Updated QuickSettings, added list of nmcli vpn connections with functionality for toggling the vpn connection
This commit is contained in:
1 parent
ea17c08587
commit
81de20ca68
4 files changed
+172
-33
No files matched your search
+110
-16
@@ -2,37 +2,75 @@ 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 } from "ags"
|
||||
import { execAsync } from "ags/process"
|
||||
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={createBinding(network, "primary").as((p) => {
|
||||
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 "network-offline-symbolic"
|
||||
return vpnActive()
|
||||
? "network-vpn-symbolic"
|
||||
: "network-offline-symbolic"
|
||||
})}
|
||||
cssClasses={["qs-icon"]}
|
||||
/>
|
||||
<label
|
||||
label={createBinding(network, "primary").as((p) => {
|
||||
if (p === AstalNetwork.Primary.WIFI && network.wifi) {
|
||||
return network.wifi.ssid || "Unknown Wi-Fi"
|
||||
} else if (p === AstalNetwork.Primary.WIRED) {
|
||||
return "Wired Connection"
|
||||
}
|
||||
return "Disconnected"
|
||||
})}
|
||||
halign={Gtk.Align.START}
|
||||
hexpand
|
||||
/>
|
||||
<label label={label} halign={Gtk.Align.START} hexpand />
|
||||
</box>
|
||||
)
|
||||
}
|
||||
@@ -97,6 +135,61 @@ function AudioSlider({ type }: { type: "speaker" | "microphone" }) {
|
||||
)
|
||||
}
|
||||
|
||||
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
|
||||
@@ -119,6 +212,7 @@ export default function QuickSettings() {
|
||||
<NetworkItem />
|
||||
<BluetoothItem />
|
||||
</box>
|
||||
<VpnList />
|
||||
<box orientation={Gtk.Orientation.VERTICAL} spacing={8}>
|
||||
<button
|
||||
cssClasses={["qs-title"]}
|
||||
|
||||
Reference in new issue
Block a user