diff --git a/modules/NiriIpc.qml b/modules/NiriIpc.qml index 504888d..b959914 100644 --- a/modules/NiriIpc.qml +++ b/modules/NiriIpc.qml @@ -11,6 +11,28 @@ QtObject { signal windowFocusChanged(var windowId) signal windowOpenedOrChanged(var window) + property string focusedTitle: "" + property string focusedAppId: "" + + property var _focusedProc: Process { + running: true + command: ["niri", "msg", "--json", "focused-window"] + stdout: StdioCollector { + onStreamFinished: { + try { + const w = JSON.parse(text); + if (w) { + root.focusedTitle = w.title || ""; + root.focusedAppId = w.app_id || ""; + } else { + root.focusedTitle = ""; + root.focusedAppId = ""; + } + } catch (e) {} + } + } + } + property var _eventStream: Process { running: true command: ["niri", "msg", "--json", "event-stream"] @@ -23,10 +45,22 @@ QtObject { root.workspacesChanged(ev.WorkspacesChanged.workspaces); else if (ev.WorkspaceActivated !== undefined) root.workspaceActivated(ev.WorkspaceActivated.id, ev.WorkspaceActivated.focused); - else if (ev.WindowFocusChanged !== undefined) + else if (ev.WindowFocusChanged !== undefined) { root.windowFocusChanged(ev.WindowFocusChanged.id); - else if (ev.WindowOpenedOrChanged !== undefined) + if (ev.WindowFocusChanged.id !== null) + root._focusedProc.running = true; + else { + root.focusedTitle = ""; + root.focusedAppId = ""; + } + } else if (ev.WindowOpenedOrChanged !== undefined) { root.windowOpenedOrChanged(ev.WindowOpenedOrChanged.window); + const w = ev.WindowOpenedOrChanged.window; + if (w.is_focused) { + root.focusedTitle = w.title || ""; + root.focusedAppId = w.app_id || ""; + } + } } catch (e) {} } } diff --git a/modules/WindowTitle.qml b/modules/WindowTitle.qml index 6d48e6e..8fdd0ad 100644 --- a/modules/WindowTitle.qml +++ b/modules/WindowTitle.qml @@ -1,63 +1,23 @@ import QtQuick import QtQuick.Effects import Quickshell -import Quickshell.Io import Quickshell.Widgets import "." as M M.BarSection { id: root spacing: M.Theme.moduleSpacing - visible: M.Modules.windowTitle.enable && root._title !== "" + visible: M.Modules.windowTitle.enable && M.NiriIpc.focusedTitle !== "" - tooltip: root._appId ? root._appId + "\n" + root._title : root._title + tooltip: M.NiriIpc.focusedAppId ? M.NiriIpc.focusedAppId + "\n" + M.NiriIpc.focusedTitle : M.NiriIpc.focusedTitle - property string _title: "" - property string _appId: "" readonly property string _iconSource: { - if (!root._appId) + if (!M.NiriIpc.focusedAppId) return ""; - const entry = DesktopEntries.heuristicLookup(root._appId); + const entry = DesktopEntries.heuristicLookup(M.NiriIpc.focusedAppId); return entry ? Quickshell.iconPath(entry.icon) : ""; } - // Initial state — niri event-stream doesn't replay current focus - Process { - id: initProc - running: true - command: ["niri", "msg", "--json", "focused-window"] - stdout: StdioCollector { - onStreamFinished: { - try { - const w = JSON.parse(text); - if (w) { - 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 || ""; - } - } - } - IconImage { visible: root._iconSource !== "" source: root._iconSource @@ -71,7 +31,7 @@ M.BarSection { } M.BarLabel { - label: root._title + label: M.NiriIpc.focusedTitle color: root.accentColor elide: Text.ElideRight anchors.verticalCenter: parent.verticalCenter