From e631eab4dd8c862cb36fbb628d903d05e3195fcc Mon Sep 17 00:00:00 2001 From: Nikolaos Karaolidis Date: Sun, 16 Feb 2025 21:45:23 +0000 Subject: [PATCH] Fix astal client monitoring Signed-off-by: Nikolaos Karaolidis --- .../config/widget/components/Workspaces.tsx | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/hosts/common/configs/user/gui/astal/config/widget/components/Workspaces.tsx b/hosts/common/configs/user/gui/astal/config/widget/components/Workspaces.tsx index 5678d78..43b69b9 100644 --- a/hosts/common/configs/user/gui/astal/config/widget/components/Workspaces.tsx +++ b/hosts/common/configs/user/gui/astal/config/widget/components/Workspaces.tsx @@ -6,16 +6,13 @@ const hyprland = Hyprland.get_default(); const BLOCK_SIZE = 10; const Workspace = ({ id }: { id: number }) => { - let maybeWorkspace: Variable; - let occupied: Variable; + let clients: Variable; try { const workspace = hyprland.get_workspace(id); - maybeWorkspace = Variable(workspace); - occupied = Variable(workspace.clients.length > 0); + clients = Variable(workspace.clients.map((client) => client.address)); } catch (_) { - maybeWorkspace = Variable(undefined); - occupied = Variable(false); + clients = Variable([]); } const active = Variable.derive( @@ -25,20 +22,38 @@ const Workspace = ({ id }: { id: number }) => { hyprland.connect("workspace-added", (_, workspace) => { if (workspace.id != id) return; - maybeWorkspace.set(workspace); - occupied.set(workspace.clients.length > 0); - workspace.connect("clients", (clients) => occupied.set(clients.length > 0)); + clients.set(workspace.clients.map((client) => client.address)); }); hyprland.connect("workspace-removed", (_, workspaceId) => { if (workspaceId != id) return; - maybeWorkspace.set(undefined); - occupied.set(false); + clients.set([]); }); - const className = Variable.derive([active, occupied], (active, occupied) => { + hyprland.connect("client-added", (_hyprland, client) => { + if (client.workspace.id != id) return; + clients.set([...clients.get(), client.address]); + }); + + // Explicit separate event handling instead of Variable.derive(workspaces, clients) + // because client-moved events appear to be broken if done that way. + hyprland.connect("client-moved", (_hyprland, client, workspace) => { + if (workspace.id == id) { + clients.set([...clients.get(), client.address]); + } else { + clients.set( + clients.get().filter((oldClient) => oldClient != client.address), + ); + } + }); + + hyprland.connect("client-removed", (_hyprland, address) => { + clients.set(clients.get().filter((oldClient) => oldClient != address)); + }); + + const className = Variable.derive([active, clients], (active, clients) => { if (active) return "button active"; - if (occupied) return "button occupied"; + if (clients.length > 0) return "button occupied"; return "button"; });