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 // Live updates via shared NiriIpc singleton
Process { Connections {
id: eventStream target: M.NiriIpc
running: true function onWindowFocusChanged(windowId) {
command: ["niri", "msg", "--json", "event-stream"] if (windowId !== null)
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; initProc.running = true;
else { else {
root._title = ""; root._title = "";
root._appId = ""; 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 || "";
} }
} function onWindowOpenedOrChanged(window) {
} catch (e) {} if (window.is_focused) {
root._title = window.title || "";
root._appId = window.app_id || "";
} }
} }
} }

View file

@ -33,24 +33,15 @@ Row {
} }
} }
// Live updates // Live updates via shared NiriIpc singleton
Process { Connections {
id: eventStream target: M.NiriIpc
running: true function onWorkspacesChanged(workspaces) {
command: ["niri", "msg", "--json", "event-stream"] root._allWorkspaces = workspaces.sort((a, b) => a.idx - b.idx);
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) {}
} }
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 PowerProfile 1.0 PowerProfile.qml
IdleInhibitor 1.0 IdleInhibitor.qml IdleInhibitor 1.0 IdleInhibitor.qml
Notifications 1.0 Notifications.qml Notifications 1.0 Notifications.qml
singleton NiriIpc 1.0 NiriIpc.qml
singleton ProcessList 1.0 ProcessList.qml singleton ProcessList 1.0 ProcessList.qml
singleton NotifService 1.0 NotifService.qml singleton NotifService 1.0 NotifService.qml
NotifItem 1.0 NotifItem.qml NotifItem 1.0 NotifItem.qml