import QtQuick import Quickshell import Quickshell.Io // Talks to whichever compositor is running. Everything goes through the // command line tools so this file works the same on niri and Hyprland. Item { id: root // "niri", "hyprland" or "" when neither was detected. readonly property string kind: { if (Quickshell.env("NIRI_SOCKET")) return "niri"; if (Quickshell.env("HYPRLAND_INSTANCE_SIGNATURE")) return "hyprland"; return ""; } readonly property string logoSource: { if (root.kind === "niri") return Qt.resolvedUrl("assets/niri.png"); if (root.kind === "hyprland") return Qt.resolvedUrl("assets/hyprland.png"); return ""; } // Name of the focused workspace, empty when it has none. property string currentName: "" // Id of the focused workspace, -1 when unknown. Only Hyprland needs it. property int currentId: -1 // Monitors top to bottom, each with its workspaces left to right. // A nested list of parsed JSON, so this stays an untyped array. property var monitors: [] // Output name -> { x, y, known, activeId, monitorFocused }. // Held between the two query steps. Same reason as monitors. property var outputLayout: ({}) // Raw workspace JSON saved between the workspaces and windows query stages. property var workspaceData: [] // Normalized windows with { title, app, output, idx, id }. property var windowList: [] // "" while idle, otherwise the query step currently running. property string queryStage: "" property bool refreshQueued: false // False while a query is being restarted, so a leftover finish is ignored. property bool acceptStream: false // Niri IPC responses come wrapped in {"Ok": {"Variant": value}}. // This helper unwraps them so the rest of the code sees bare arrays/objects. function unwrapJson(text: string): var { const parsed = JSON.parse(text); if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { if ("Ok" in parsed) { const inner = parsed.Ok; if (inner && typeof inner === "object" && !Array.isArray(inner)) { const keys = Object.keys(inner); if (keys.length === 1) return inner[keys[0]]; } return inner; } if ("Err" in parsed) { console.warn("workspace-manager: compositor IPC error:", parsed.Err); return null; } } return parsed; } // Argv arrays waiting to run. A list of lists, so this stays untyped. property var focusQueue: [] property var focusPending: [] property bool focusRestart: false signal refreshed function refresh(): void { if (root.kind === "") return; if (root.queryStage !== "") { root.refreshQueued = true; return; } root.startStage(root.kind === "niri" ? "outputs" : "monitors"); } function startStage(stage: string): void { root.acceptStream = false; query.running = false; root.queryStage = stage; if (stage === "outputs") query.command = ["niri", "msg", "--json", "outputs"]; else if (stage === "monitors") query.command = ["hyprctl", "-j", "monitors"]; else if (stage === "workspaces") { if (root.kind === "niri") query.command = ["niri", "msg", "--json", "workspaces"]; else query.command = ["hyprctl", "-j", "workspaces"]; } else if (stage === "windows") { if (root.kind === "niri") query.command = ["niri", "msg", "--json", "windows"]; else query.command = ["hyprctl", "-j", "clients"]; } root.acceptStream = true; query.running = true; } function finishQuery(): void { root.queryStage = ""; if (!root.refreshQueued) return; root.refreshQueued = false; root.refresh(); } function asString(value): string { if (value === null || value === undefined) return ""; return String(value); } function asNumber(value, fallback: int): int { const number = Number(value); return Number.isFinite(number) ? number : fallback; } function parseOutputs(parsed): void { const layout = {}; if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { // niri format: HashMap const names = Object.keys(parsed); for (let i = 0; i < names.length; i++) { const out = parsed[names[i]]; const name = root.asString(out.name); if (name.length === 0) continue; const logical = out.logical ?? {}; const x = root.asNumber(logical.x, 0); const y = root.asNumber(logical.y, 0); const known = Number.isFinite(Number(logical.x)) && Number.isFinite(Number(logical.y)); layout[name] = { x: x, y: y, known: known, activeId: -1, monitorFocused: false }; } } else { // Hyprland format: Array (unused, Hyprland uses parseMonitors) const list = Array.isArray(parsed) ? parsed : []; for (let i = 0; i < list.length; i++) { const out = list[i]; const name = root.asString(out.name); if (name.length === 0) continue; const logical = out.logical ?? {}; const x = root.asNumber(logical.x, 0); const y = root.asNumber(logical.y, 0); const known = Number.isFinite(Number(logical.x)) && Number.isFinite(Number(logical.y)); layout[name] = { x: x, y: y, known: known, activeId: -1, monitorFocused: false }; } } root.outputLayout = layout; } function parseMonitors(parsed): void { const layout = {}; const list = Array.isArray(parsed) ? parsed : []; for (let i = 0; i < list.length; i++) { const mon = list[i]; const name = root.asString(mon.name); if (name.length === 0) continue; const known = Number.isFinite(Number(mon.x)) && Number.isFinite(Number(mon.y)); const active = mon.activeWorkspace ?? {}; layout[name] = { x: root.asNumber(mon.x, 0), y: root.asNumber(mon.y, 0), known: known, activeId: root.asNumber(active.id, -1), monitorFocused: mon.focused === true }; } root.outputLayout = layout; } // Hyprland reports the numeric id as the name when the workspace has none. function workspaceName(rawName, id: int): string { const name = root.asString(rawName); if (root.kind === "hyprland" && name === String(id)) return ""; return name; } function parseWindows(parsed): void { const list = Array.isArray(parsed) ? parsed : []; const windows = []; for (let i = 0; i < list.length; i++) { const w = list[i]; let title, app, id; if (root.kind === "niri") { title = root.asString(w.title); app = root.asString(w.app_id); id = root.asNumber(w.workspace_id, -1); } else { if (w.mapped === false) continue; title = root.asString(w.title); app = root.asString(w.class); id = root.asNumber(w.workspace?.id, -1); } if (id < 0) continue; windows.push({ title: title, app: app, id: id }); } root.windowList = windows; } // Both niri and Hyprland match windows by workspace id. function matchWindow(w, output: string, idx: int, id: int): bool { return w.id === id; } function buildMonitors(parsed): void { const list = Array.isArray(parsed) ? parsed : []; const groups = {}; let focusedId = -1; let focusedName = ""; for (let i = 0; i < list.length; i++) { const ws = list[i]; const id = root.asNumber(ws.id, -1); if (root.kind === "hyprland" && id < 0) continue; const output = root.asString(root.kind === "niri" ? ws.output : ws.monitor); const idx = root.kind === "niri" ? root.asNumber(ws.idx, 0) : id; const name = root.workspaceName(ws.name, id); const meta = root.outputLayout[output] ?? {}; const active = root.kind === "niri" ? ws.is_active === true : id === meta.activeId; const focused = root.kind === "niri" ? ws.is_focused === true : active && meta.monitorFocused === true; if (!groups[output]) groups[output] = []; groups[output].push({ id: id, idx: idx, name: name, active: active, focused: focused }); if (focused) { focusedId = id; focusedName = name; } } const names = Object.keys(groups); names.sort((a, b) => root.compareOutputs(a, b)); const built = []; for (let i = 0; i < names.length; i++) { const name = names[i]; const workspaces = groups[name]; workspaces.sort((a, b) => a.idx - b.idx); for (let j = 0; j < workspaces.length; j++) { const ws = workspaces[j]; ws.windows = root.windowList.filter(w => root.matchWindow(w, name, ws.idx, ws.id)); } // Hide empty workspaces unless they are the active one on this monitor. const visible = workspaces.filter(ws => ws.active || ws.windows.length > 0); const pos = root.outputLayout[name] ?? {}; built.push({ name: name, x: root.asNumber(pos.x, 0), y: root.asNumber(pos.y, 0), workspaces: visible }); } if (focusedId < 0) { console.warn("workspace-manager: no focused workspace reported"); root.currentId = -1; root.currentName = ""; } else { root.currentId = focusedId; root.currentName = focusedName; } root.monitors = built; root.refreshed(); } function compareOutputs(a: string, b: string): int { const left = root.outputLayout[a] ?? {}; const right = root.outputLayout[b] ?? {}; const leftKnown = left.known === true; const rightKnown = right.known === true; if (leftKnown !== rightKnown) return leftKnown ? -1 : 1; const yDelta = root.asNumber(left.y, 0) - root.asNumber(right.y, 0); if (yDelta !== 0) return yDelta; const xDelta = root.asNumber(left.x, 0) - root.asNumber(right.x, 0); if (xDelta !== 0) return xDelta; if (a < b) return -1; if (a > b) return 1; return 0; } function apply(name: string): void { const trimmed = name.trim(); if (root.kind === "niri") { action.command = trimmed.length > 0 ? ["niri", "msg", "action", "set-workspace-name", trimmed] : ["niri", "msg", "action", "unset-workspace-name"]; } else if (root.kind === "hyprland") { if (root.currentId < 0) return; const id = String(root.currentId); // Hyprland has no "clear name", so fall back to the numeric id. action.command = ["hyprctl", "dispatch", "renameworkspace", id, trimmed.length > 0 ? trimmed : id]; } else { return; } action.running = false; action.running = true; root.currentName = trimmed; } // Rename a workspace by index (niri) or id (Hyprland). // Used from the carousel to rename the selected workspace. function renameWorkspace(wsId: int, wsIdx: int, name: string): void { const trimmed = name.trim(); if (root.kind === "niri") { // niri's --workspace flag accepts an index (number) or name (string). if (trimmed.length > 0) action.command = ["niri", "msg", "action", "set-workspace-name", "--workspace", String(wsIdx), trimmed]; else action.command = ["niri", "msg", "action", "unset-workspace-name", String(wsIdx)]; } else if (root.kind === "hyprland") { if (wsId < 0) return; const id = String(wsId); action.command = ["hyprctl", "dispatch", "renameworkspace", id, trimmed.length > 0 ? trimmed : id]; } else { return; } action.running = false; action.running = true; // Update the name locally so the carousel reflects it immediately. if (root.currentId === wsId) root.currentName = trimmed; const mons = root.monitors; for (let i = 0; i < mons.length; i++) { const wsList = mons[i].workspaces ?? []; for (let j = 0; j < wsList.length; j++) { if (wsList[j].id === wsId) { wsList[j].name = trimmed; // reassign to trigger QML re-evaluation root.monitors = root.monitors.slice(); return; } } } } // Niri indexes count on the focused monitor only, so focus that monitor // first, then the index. Hyprland switches by workspace id in one command. function focusWorkspace(output: string, idx: int, id: int): void { if (root.kind === "niri") { const commands = []; if (output.length > 0) commands.push(["niri", "msg", "action", "focus-monitor", output]); commands.push(["niri", "msg", "action", "focus-workspace", String(idx)]); root.focusQueue = commands; } else if (root.kind === "hyprland") { if (id < 0) return; root.focusQueue = [["hyprctl", "dispatch", "workspace", String(id)]]; } else { return; } root.pumpFocus(); } function pumpFocus(): void { if (root.focusQueue.length === 0) return; const next = root.focusQueue[0]; root.focusQueue = root.focusQueue.slice(1); if (focusProc.running) { root.focusPending = next; root.focusRestart = true; focusProc.running = false; return; } focusProc.command = next; focusProc.running = true; } function afterFocusExit(): void { if (root.focusRestart) { root.focusRestart = false; focusProc.command = root.focusPending; focusProc.running = true; return; } root.pumpFocus(); } Process { id: query running: false stdout: StdioCollector { onStreamFinished: { if (!root.acceptStream) return; root.acceptStream = false; let parsed = null; try { parsed = root.unwrapJson(this.text); } catch (e) { console.warn("workspace-manager: could not read workspace data:", e); if (root.queryStage === "outputs" || root.queryStage === "monitors") { root.outputLayout = ({ }); root.startStage("workspaces"); return; } if (root.queryStage === "windows") { console.warn("workspace-manager: could not read window data, continuing without windows"); root.windowList = []; root.buildMonitors(root.workspaceData); root.finishQuery(); return; } root.finishQuery(); return; } if (!parsed) { // unwrapJson returned null (niri IPC error) root.finishQuery(); return; } if (root.queryStage === "outputs") { root.parseOutputs(parsed); root.startStage("workspaces"); return; } if (root.queryStage === "monitors") { root.parseMonitors(parsed); root.startStage("workspaces"); return; } if (root.queryStage === "workspaces") { root.workspaceData = parsed; root.startStage("windows"); return; } root.parseWindows(parsed); root.buildMonitors(root.workspaceData); root.finishQuery(); } } } Process { id: action running: false stderr: StdioCollector { onStreamFinished: { if (this.text.trim().length > 0) console.warn("workspace-manager:", this.text.trim()); } } } Process { id: focusProc running: false onExited: root.afterFocusExit() stderr: StdioCollector { onStreamFinished: { if (this.text.trim().length > 0) console.warn("workspace-manager:", this.text.trim()); } } } }