import { bind, execAsync, Binding, Variable } from "astal"; import { App, Astal, Gdk, Gtk } from "astal/gtk4"; import Bluetooth from "gi://AstalBluetooth"; import Hyprland from "gi://AstalHyprland"; import Network from "gi://AstalNetwork"; import { SHELL, IDLE_INHIBIT_SCRIPT, RANDOM_WALLPAPER_SCRIPT } from "@/settings.json"; import { hideWindow, openOnButton } from "@lib/utils"; type ButtonProps = { icon: string | Binding, command?: string, label: string | Binding, binding?: Binding, bindingMethod?: (binding: boolean) => void } const isIdleInhibitorEnabled = async () => { try { await execAsync([SHELL, "-c", `pgrep -f ${IDLE_INHIBIT_SCRIPT}`]); return true; } catch { return false; } }; function Preference(props: ButtonProps & JSX.IntrinsicElements["button"]) { const onClicked = () => { if (props.command) execAsync([ SHELL, "-c", props.command ]); else if (typeof props.binding !== 'undefined' && props.bindingMethod) props.bindingMethod(!props.binding.get()) } return } async function toggleIdleInhibitor(state: Variable, enable: boolean) { try { if (enable) { execAsync([SHELL, "-c", IDLE_INHIBIT_SCRIPT]); } else { const pids = await execAsync([SHELL, "-c", `pgrep -f ${IDLE_INHIBIT_SCRIPT}`]); if (pids) { const pidList = pids.trim().split("\n"); for (const pid of pidList) { await execAsync(["kill", "-9", pid]); } } } state.set(!state.get()); } catch { console.error("Failed to change state of idle inhibitor"); } } export default async function QuickSettings(_monitor_id: number) { const { BOTTOM, LEFT, RIGHT } = Astal.WindowAnchor const CENTER = Gtk.Align.CENTER; const hypr = Hyprland.get_default(); const bt = Bluetooth.get_default(); const net = Network.get_default(); const connectedBtDevice = bind(bt, "devices").as(devices => devices.find(device => device.connected)); const idleInhibitorEnabled = Variable(await isIdleInhibitorEnabled()); return monitor.id)} cssClasses={["quick_settings"]} keymode={Astal.Keymode.EXCLUSIVE} exclusivity={Astal.Exclusivity.IGNORE} anchor={BOTTOM | LEFT | RIGHT} onKeyPressed={hideWindow} application={App}> enabled && ssid ? ssid : "Wi-Fi"))} binding={bind(net.wifi, "enabled")} bindingMethod={enabled => net.wifi.set_enabled(enabled)} /> c ? "bluetooth-active-symbolic" : "bluetooth-disabled-symbolic" )} label={connectedBtDevice.as(device => bt.isPowered && device ? device.alias : "Bluetooth")} binding={bind(bt, "isPowered")} bindingMethod={() => bt.toggle()} onButtonPressed={(_self, event) => openOnButton(event, Gdk.BUTTON_SECONDARY) (() => App.toggle_window("qs_bluetooth")) } /> }