From 71a843e0f338cdb95bfdcce0d878b41c2e31abca Mon Sep 17 00:00:00 2001 From: Damocles Date: Wed, 15 Apr 2026 00:28:40 +0200 Subject: [PATCH] refactor: consolidate niri event-stream into NiriIpc singleton --- modules/NiriIpc.qml | 34 ++++++++++++++++++++++++++++++++++ modules/WindowTitle.qml | 41 +++++++++++++++-------------------------- modules/Workspaces.qml | 27 +++++++++------------------ modules/qmldir | 1 + 4 files changed, 59 insertions(+), 44 deletions(-) create mode 100644 modules/NiriIpc.qml diff --git a/modules/NiriIpc.qml b/modules/NiriIpc.qml new file mode 100644 index 0000000..504888d --- /dev/null +++ b/modules/NiriIpc.qml @@ -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) {} + } + } + } +} diff --git a/modules/WindowTitle.qml b/modules/WindowTitle.qml index a7bb134..1749745 100644 --- a/modules/WindowTitle.qml +++ b/modules/WindowTitle.qml @@ -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 || ""; } } } diff --git a/modules/Workspaces.qml b/modules/Workspaces.qml index 5efaf51..bac38d2 100644 --- a/modules/Workspaces.qml +++ b/modules/Workspaces.qml @@ -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; } } diff --git a/modules/qmldir b/modules/qmldir index aa9d8dd..c460e16 100644 --- a/modules/qmldir +++ b/modules/qmldir @@ -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