Trying to make a widget with icon of current focused window #1447
|
Hi, I’m trying to do a widget to print current window title and icon on Niri first (will try Mango later) I manage the title with a poll and scripts: And now I’m scratching my head on how to get the icon… |
Replies: 1 comment 1 reply
|
This is solvable, but it's worth knowing upfront - eww's Here is the full chain you need: 1. Get the niri msg --json focused-window | jq -r '.app_id'2. Map #!/usr/bin/env python3
import json, subprocess, glob, os
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
def get_app_id():
out = subprocess.run(["niri", "msg", "--json", "focused-window"],
capture_output=True, text=True).stdout
return (json.loads(out) or {}).get("app_id", "")
def icon_name_for(app_id):
desktop_files = glob.glob("/usr/share/applications/*.desktop") + \
glob.glob(os.path.expanduser("~/.local/share/applications/*.desktop"))
for path in desktop_files:
if os.path.basename(path).lower() == f"{app_id.lower()}.desktop":
return _icon_from(path)
for path in desktop_files:
try:
content = open(path, errors="ignore").read()
except OSError:
continue
if f"startupwmclass={app_id}".lower() in content.lower():
return _icon_from_content(content)
return app_id # last resort: try the app_id as the icon name directly
def _icon_from(path):
return _icon_from_content(open(path, errors="ignore").read())
def _icon_from_content(content):
for line in content.splitlines():
if line.startswith("Icon="):
return line.split("=", 1)[1].strip()
return None
app_id = get_app_id()
icon_name = icon_name_for(app_id) or app_id
info = Gtk.IconTheme.get_default().lookup_icon(icon_name, 48, 0)
print(info.get_filename() if info else "")3. Poll it from eww and feed it straight into (defpoll focused_icon :interval "1s" :initial "" "python3 ~/.config/eww/scripts/focused-icon.py")
(defwidget focused-window-icon []
(image :path {focused_icon} :image-width 24 :image-height 24))A couple of caveats worth flagging in advance:
|
This is solvable, but it's worth knowing upfront - eww's
imagewidget only takes a file path (:path), not a GTK icon-theme name. There's no:icon-nameproperty in eww (image widget only supportspath/image-width/image-height). So the actual icon resolution work has to happen in your script, not in eww itself.Here is the full chain you need:
1. Get the
app_id, not the title - that's the field icons key off of:2. Map
app_id-> an actual icon file path. This is the part eww can't do for you. The robust way: find the.desktopfile matching thatapp_id(either by filename or itsStartupWMClass=field), read itsIcon=line, then resolve that ic…