fix: track focused window in NiriIpc singleton to fix window title always hidden

This commit is contained in:
Damocles 2026-04-15 17:47:47 +02:00
parent 30b9e5c479
commit d7ca7e405f
2 changed files with 41 additions and 47 deletions

View file

@ -11,6 +11,28 @@ QtObject {
signal windowFocusChanged(var windowId) signal windowFocusChanged(var windowId)
signal windowOpenedOrChanged(var window) 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 { property var _eventStream: Process {
running: true running: true
command: ["niri", "msg", "--json", "event-stream"] command: ["niri", "msg", "--json", "event-stream"]
@ -23,10 +45,22 @@ QtObject {
root.workspacesChanged(ev.WorkspacesChanged.workspaces); root.workspacesChanged(ev.WorkspacesChanged.workspaces);
else if (ev.WorkspaceActivated !== undefined) else if (ev.WorkspaceActivated !== undefined)
root.workspaceActivated(ev.WorkspaceActivated.id, ev.WorkspaceActivated.focused); root.workspaceActivated(ev.WorkspaceActivated.id, ev.WorkspaceActivated.focused);
else if (ev.WindowFocusChanged !== undefined) else if (ev.WindowFocusChanged !== undefined) {
root.windowFocusChanged(ev.WindowFocusChanged.id); 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); 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) {} } catch (e) {}
} }
} }

View file

@ -1,63 +1,23 @@
import QtQuick import QtQuick
import QtQuick.Effects import QtQuick.Effects
import Quickshell import Quickshell
import Quickshell.Io
import Quickshell.Widgets import Quickshell.Widgets
import "." as M import "." as M
M.BarSection { M.BarSection {
id: root id: root
spacing: M.Theme.moduleSpacing 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: { readonly property string _iconSource: {
if (!root._appId) if (!M.NiriIpc.focusedAppId)
return ""; return "";
const entry = DesktopEntries.heuristicLookup(root._appId); const entry = DesktopEntries.heuristicLookup(M.NiriIpc.focusedAppId);
return entry ? Quickshell.iconPath(entry.icon) : ""; 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 { IconImage {
visible: root._iconSource !== "" visible: root._iconSource !== ""
source: root._iconSource source: root._iconSource
@ -71,7 +31,7 @@ M.BarSection {
} }
M.BarLabel { M.BarLabel {
label: root._title label: M.NiriIpc.focusedTitle
color: root.accentColor color: root.accentColor
elide: Text.ElideRight elide: Text.ElideRight
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter