hyprland: use qs hyprland service

Also use models
This commit is contained in:
2 * r + 2 * t 2025-04-28 00:17:39 +10:00
parent c2e78cc11c
commit 588e7fcffc
2 changed files with 50 additions and 120 deletions

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"editor.defaultFormatter": "theqtcompany.qt-qml"
}

View file

@ -1,5 +1,4 @@
pragma Singleton
pragma ComponentBehavior: Bound
import Quickshell
import Quickshell.Io
@ -9,151 +8,79 @@ import QtQuick
Singleton {
id: root
property list<var> clients: []
property list<var> monitors: []
property list<var> workspaces: []
readonly property ListModel clients: ListModel {}
readonly property var workspaces: Hyprland.workspaces
readonly property var monitors: Hyprland.monitors
property var activeClient: null
property var activeWorkspace: null
property var focusedMonitor: null
property HyprlandWorkspace activeWorkspace: null
property HyprlandMonitor focusedMonitor: Hyprland.monitors.values.find(m => m.lastIpcObject.focused) ?? null
Component.onCompleted: reload()
function reload() {
Hyprland.refreshWorkspaces();
Hyprland.refreshMonitors();
getClients.running = true;
getMonitors.running = true;
getWorkspaces.running = true;
getActiveClient.running = true;
getActiveWorkspace.running = true;
}
Component.onCompleted: reload()
function dispatch(request: string): void {
Hyprland.dispatch(request);
}
Connections {
target: Hyprland
function onRawEvent(event: string): void {
if (!event.endsWith("v2"))
function onRawEvent(event: HyprlandEvent): void {
if (!event.name.endsWith("v2"))
root.reload();
}
}
component HyprData: Process {
id: proc
required property string type
property string prop: type
property bool array: true
function transform(data) {
return data;
}
command: ["bash", "-c", `hyprctl -j ${type} | jq -c`]
Process {
id: getClients
command: ["bash", "-c", "hyprctl -j clients | jq -c"]
stdout: SplitParser {
onRead: data => {
root[proc.prop] = proc.array ? JSON.parse(data).map(proc.transform) : proc.transform(JSON.parse(data));
if (proc.type === "monitors")
root.focusedMonitor = root.monitors.find(m => m.focused);
const clients = JSON.parse(data);
root.clients.clear();
for (const client of clients) {
root.clients.append({
lastIpcObject: client,
address: client.address,
class: client.class,
title: client.title
});
}
}
}
function transformClient(client) {
if (!client.address)
return null;
return {
address: client.address,
x: client.at[0],
y: client.at[1],
width: client.size[0],
height: client.size[1],
workspace: transformWorkspace(client.workspace),
floating: client.floating,
monitor: client.monitor,
wmClass: client.class,
title: client.title,
initialClass: client.initialClass,
initialTitle: client.initialTitle,
pid: client.pid,
xwayland: client.xwayland,
pinned: client.pinned,
fullscreen: client.fullscreen,
focusHistoryId: client.focusHistoryID,
inhibitingIdle: client.inhibitingIdle
};
}
function transformMonitor(monitor) {
return {
id: monitor.id,
name: monitor.name,
description: monitor.description,
make: monitor.make,
model: monitor.model,
serial: monitor.serial,
x: monitor.x,
y: monitor.y,
width: monitor.width,
height: monitor.height,
refreshRate: monitor.refreshRate,
activeWorkspace: transformWorkspace(monitor.activeWorkspace),
specialWorkspace: transformWorkspace(monitor.specialWorkspace),
reserved: monitor.reserved,
scale: monitor.scale,
transform: monitor.transform,
focused: monitor.focused,
dpms: monitor.dpms,
vrr: monitor.vrr,
disabled: monitor.disabled
};
}
function transformWorkspace(workspace) {
return {
id: workspace.id,
name: workspace.name,
special: workspace.name.startsWith("special:")
};
}
}
HyprData {
id: getClients
type: "clients"
function transform(client) {
return transformClient(client);
}
}
HyprData {
id: getMonitors
type: "monitors"
function transform(monitor) {
return transformMonitor(monitor);
}
}
HyprData {
id: getWorkspaces
type: "workspaces"
function transform(workspace) {
return transformWorkspace(workspace);
}
}
HyprData {
Process {
id: getActiveClient
type: "activewindow"
prop: "activeClient"
array: false
function transform(client) {
return transformClient(client);
command: ["bash", "-c", "hyprctl -j activewindow | jq -c"]
stdout: SplitParser {
onRead: data => {
const client = JSON.parse(data);
root.activeClient = {
lastIpcObject: client,
address: client.address,
class: client.class,
title: client.title
};
}
}
}
HyprData {
Process {
id: getActiveWorkspace
type: "activeworkspace"
prop: "activeWorkspace"
array: false
function transform(workspace) {
return transformWorkspace(workspace);
command: ["bash", "-c", "hyprctl -j activeworkspace | jq -c"]
stdout: SplitParser {
onRead: data => {
const ws = JSON.parse(data);
root.activeWorkspace = root.workspaces.values.find(w => w.id === ws.id) ?? null;
}
}
}
}