refactor: consolidate niri event-stream into NiriIpc singleton

This commit is contained in:
Damocles 2026-04-15 00:28:40 +02:00
parent cc44bd1c0e
commit 71a843e0f3
4 changed files with 59 additions and 44 deletions

34
modules/NiriIpc.qml Normal file
View file

@ -0,0 +1,34 @@
pragma Singleton
import QtQuick
import Quickshell.Io
QtObject {
id: root
signal workspacesChanged(var workspaces)
signal workspaceActivated(int id, bool focused)
signal windowFocusChanged(var windowId)
signal windowOpenedOrChanged(var window)
property var _eventStream: Process {
running: true
command: ["niri", "msg", "--json", "event-stream"]
stdout: SplitParser {
splitMarker: "\n"
onRead: line => {
try {
const ev = JSON.parse(line);
if (ev.WorkspacesChanged !== undefined)
root.workspacesChanged(ev.WorkspacesChanged.workspaces);
else if (ev.WorkspaceActivated !== undefined)
root.workspaceActivated(ev.WorkspaceActivated.id, ev.WorkspaceActivated.focused);
else if (ev.WindowFocusChanged !== undefined)
root.windowFocusChanged(ev.WindowFocusChanged.id);
else if (ev.WindowOpenedOrChanged !== undefined)
root.windowOpenedOrChanged(ev.WindowOpenedOrChanged.window);
} catch (e) {}
}
}
}
}

View file

@ -39,32 +39,21 @@ M.BarSection {
}
}
// Live updates via event stream
Process {
id: eventStream
running: true
command: ["niri", "msg", "--json", "event-stream"]
stdout: SplitParser {
splitMarker: "\n"
onRead: line => {
try {
const ev = JSON.parse(line);
if (ev.WindowFocusChanged !== undefined) {
// WindowFocusChanged only gives id, re-query for full info
if (ev.WindowFocusChanged.id !== null)
initProc.running = true;
else {
root._title = "";
root._appId = "";
}
} else if (ev.WindowOpenedOrChanged !== undefined) {
const w = ev.WindowOpenedOrChanged.window;
if (w.is_focused) {
root._title = w.title || "";
root._appId = w.app_id || "";
}
}
} catch (e) {}
// Live updates via shared NiriIpc singleton
Connections {
target: M.NiriIpc
function onWindowFocusChanged(windowId) {
if (windowId !== null)
initProc.running = true;
else {
root._title = "";
root._appId = "";
}
}
function onWindowOpenedOrChanged(window) {
if (window.is_focused) {
root._title = window.title || "";
root._appId = window.app_id || "";
}
}
}

View file

@ -33,24 +33,15 @@ Row {
}
}
// Live updates
Process {
id: eventStream
running: true
command: ["niri", "msg", "--json", "event-stream"]
stdout: SplitParser {
splitMarker: "\n"
onRead: line => {
try {
const ev = JSON.parse(line);
if (ev.WorkspacesChanged !== undefined) {
root._allWorkspaces = ev.WorkspacesChanged.workspaces.sort((a, b) => a.idx - b.idx);
} else if (ev.WorkspaceActivated !== undefined) {
if (ev.WorkspaceActivated.focused)
root._activeId = ev.WorkspaceActivated.id;
}
} catch (e) {}
}
// Live updates via shared NiriIpc singleton
Connections {
target: M.NiriIpc
function onWorkspacesChanged(workspaces) {
root._allWorkspaces = workspaces.sort((a, b) => a.idx - b.idx);
}
function onWorkspaceActivated(id, focused) {
if (focused)
root._activeId = id;
}
}

View file

@ -32,6 +32,7 @@ Weather 1.0 Weather.qml
PowerProfile 1.0 PowerProfile.qml
IdleInhibitor 1.0 IdleInhibitor.qml
Notifications 1.0 Notifications.qml
singleton NiriIpc 1.0 NiriIpc.qml
singleton ProcessList 1.0 ProcessList.qml
singleton NotifService 1.0 NotifService.qml
NotifItem 1.0 NotifItem.qml