import { For, createComputed } from "ags" import { Gdk } from "ags/gtk4" import { createSubprocess, execAsync } from "ags/process" import GLib from "gi://GLib?version=2.0" interface WorkspaceInfo { address: string name: string monitor: string active: boolean } const HIS = GLib.getenv("HYPRLAND_INSTANCE_SIGNATURE") const SOCK = `${GLib.getenv("XDG_RUNTIME_DIR")}/hypr/${HIS}/.socket2.sock` // Hyprland 0.57 omits the numeric "id" on specialWorkspace, which makes // AstalHyprland's monitor parsing log Json-CRITICAL errors on every workspace // switch. Read the IPC JSON directly instead. Only numbered workspaces are // shown in the bar. const WORKSPACES_SCRIPT = ` SOCK="${SOCK}" print_state() { hyprctl -j monitors | python3 -c ' import json, subprocess, sys try: monitors = json.load(sys.stdin) workspaces = json.loads(subprocess.check_output(["hyprctl", "-j", "workspaces"])) except Exception: raise SystemExit(0) def ident(ws): return ws.get("address") or ws.get("name") or str(ws.get("id") or "") seen = set() out = [] def add(ws, monitor, active): address = ident(ws) # only numbered workspaces; special and named ones are not part of the bar if not address.isdigit(): return key = (monitor, address) if key in seen: return seen.add(key) out.append({ "address": address, "name": ws.get("name") or address, "monitor": monitor, "active": active, }) for monitor in monitors: name = monitor.get("name") or "" active = monitor.get("activeWorkspace") or {} for ws in workspaces: if ws.get("monitor") == name: add(ws, name, ident(ws) == ident(active)) # an active workspace always exists, even if it is empty and not listed add(active, name, True) print(json.dumps(out)) ' } print_state nc -U "$SOCK" | while IFS= read -r line; do case "$line" in workspace*|focusedmon*|createworkspace*|destroyworkspace*|moveworkspace*|renameworkspace*|changeworkspaceid*|activespecial*|monitoradded*|monitorremoved*) print_state ;; esac done ` const workspaces = createSubprocess( [], ["sh", "-c", WORKSPACES_SCRIPT], (out, prev) => { try { const parsed = JSON.parse(out) return Array.isArray(parsed) ? parsed : prev } catch { return prev } }, ) export default function Workspaces({ gdkmonitor, }: { gdkmonitor: Gdk.Monitor }) { const monitorName = gdkmonitor.get_connector() const monitorWorkspaces = workspaces.as((all) => all .filter((ws) => ws.monitor === monitorName) .sort((a, b) => Number(a.address) - Number(b.address)), ) return ( ws.address}> {(ws) => { // Look the workspace up by identity so the button survives state // updates and only its props change (keeps the active transition). const current = createComputed(() => workspaces().find( (w) => w.monitor === monitorName && w.address === ws.address, ), ) return ( ) }} ) }