Files
skygqts/workspace-rename/Compositor.qml
T
2026-09-21 10:29:19 -04:00

105 lines
3.4 KiB
QML

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
signal refreshed
function refresh(): void {
if (root.kind === "")
return;
query.running = false;
query.command = root.kind === "niri" ? ["niri", "msg", "--json", "workspaces"] : ["hyprctl", "-j", "activeworkspace"];
query.running = true;
}
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;
}
Process {
id: query
running: false
stdout: StdioCollector {
onStreamFinished: {
let parsed = null;
try {
parsed = JSON.parse(this.text);
} catch (e) {
console.warn("workspace-rename: could not read workspace data:", e);
return;
}
if (root.kind === "niri") {
const focused = (parsed ?? []).find(ws => ws.is_focused);
if (!focused) {
console.warn("workspace-rename: no focused workspace reported");
return;
}
root.currentId = focused.id ?? -1;
root.currentName = focused.name ?? "";
} else {
root.currentId = parsed.id ?? -1;
// Unnamed Hyprland workspaces report their id as the name.
const name = String(parsed.name ?? "");
root.currentName = name === String(root.currentId) ? "" : name;
}
root.refreshed();
}
}
}
Process {
id: action
running: false
stderr: StdioCollector {
onStreamFinished: {
if (this.text.trim().length > 0)
console.warn("workspace-rename:", this.text.trim());
}
}
}
}