From f7e43077c5051f1cf9b2ae5a0f091532ccebe1f7 Mon Sep 17 00:00:00 2001 From: pascalyazid Date: Fri, 27 Nov 2026 13:16:10 +0100 Subject: [PATCH] Updated workspaces to only show active workspaces per monitor, formatting --- style.scss | 4 +- theme.ts | 4 +- widget/Workspaces.tsx | 153 +++++++++++++++++++++++++++++++----------- 3 files changed, 116 insertions(+), 45 deletions(-) diff --git a/style.scss b/style.scss index bab5fd8..740c5ef 100644 --- a/style.scss +++ b/style.scss @@ -1,7 +1,7 @@ @use "/tmp/ags-colors.scss" as wal; $fg-color: wal.$foreground; -$bg-color: rgba(wal.$background, 1.0); +$bg-color: rgba(wal.$background, 1); $btn-height: 30px; $btn-radius: 8px; @@ -14,7 +14,7 @@ window.Bar { font-weight: bold; > centerbox { - background: rgba(wal.$background, 1.0); + background: rgba(wal.$background, 1); border-radius: 10px; margin: 4px 8px; padding: 0 8px; diff --git a/theme.ts b/theme.ts index 3afcfb6..07218e0 100644 --- a/theme.ts +++ b/theme.ts @@ -121,9 +121,7 @@ export async function setTheme(wallpaperPath: string) { // 2. Generate colors with pywal16 try { // -q: quiet, -i: image, -n: skip setting wallpaper (hyprpaper does it) - await execAsync( - `wal --contrast 21.0 -q -n -i "${wallpaperPath}"`, - ) + await execAsync(`wal --contrast 21.0 -q -n -i "${wallpaperPath}"`) // Add this line so hyprlock always knows the current wallpaper: await execAsync([ diff --git a/widget/Workspaces.tsx b/widget/Workspaces.tsx index 4cbc6fa..f108c95 100644 --- a/widget/Workspaces.tsx +++ b/widget/Workspaces.tsx @@ -1,9 +1,92 @@ -import { execAsync, createSubprocess } from "ags/process" +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` +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, @@ -12,48 +95,38 @@ export default function Workspaces({ }) { const monitorName = gdkmonitor.get_connector() - // Listen to the Hyprland socket for instant workspace updates - const activeWs = createSubprocess( - 1, - [ - "sh", - "-c", - ` -MON="${monitorName}" -print_ws() { - hyprctl monitors -j | python3 -c "import json, sys; mon = sys.argv[1]; data = json.load(sys.stdin); print(next((m.get('activeWorkspace', {}).get('id', 0) for m in data if m.get('name') == mon), 0))" "$MON" -} - -print_ws -nc -U ${sock} | while IFS= read -r line; do - case "$line" in - focusedmon*|workspace*|workspacev2*|moveworkspace*|moveworkspacev2*|createworkspace*|destroyworkspace*) - print_ws - ;; - esac -done -`, - ], - (out, prev) => { - const num = parseInt(out.trim()) - if (!isNaN(num) && num > 0) return num - - return prev - }, + const monitorWorkspaces = workspaces.as((all) => + all + .filter((ws) => ws.monitor === monitorName) + .sort((a, b) => Number(a.address) - Number(b.address)), ) return ( - {Array.from({ length: 9 }, (_, i) => i + 1).map((i) => ( - - ))} + 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 ( + + ) + }} + ) }