Files
nico 85fee48d10 Add notification panel with unread badge, drop window switcher button
Bell button (left of the workspaces, left of the clock in taskbar mode) opens
a full-height right-edge panel of mako notifications; clicking one marks it
read via scripts/notifs.py. Window switcher stays on Mod+Shift+A.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dujsk9jYfxUVLtb5B8eC94
2026-09-26 01:34:52 +02:00

71 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""Notification panel backend: mako's list + history minus what was marked read.
notifs.py print the unread notifications as JSON, newest first
notifs.py read KEY.. mark those keys as read
notifs.py readall mark everything currently listed as read
mako can't delete single history entries, so "read" lives in our own state file.
A key hashes id + app + text, so ids restarting from 1 after a mako restart
don't hide new notifications.
"""
import hashlib
import json
import os
import subprocess
import sys
STATE = os.path.join(os.environ.get("XDG_STATE_HOME", os.path.expanduser("~/.local/state")), "quickshell", "notifs-read")
KEEP = 500
def mako(cmd):
try:
out = subprocess.run(["makoctl", cmd, "-j"], capture_output=True, text=True, timeout=3).stdout
return json.loads(out or "[]")
except Exception:
return []
def all_notifs():
seen, items = set(), []
for n in mako("list") + mako("history"):
if n["id"] in seen:
continue
seen.add(n["id"])
raw = f'{n["id"]}\0{n.get("app_name")}\0{n.get("summary")}\0{n.get("body")}'
items.append({
"key": hashlib.sha1(raw.encode()).hexdigest()[:16],
"id": n["id"],
"app": n.get("app_name") or "",
"summary": n.get("summary") or "",
"body": n.get("body") or "",
"urgency": n.get("urgency") or "normal",
})
return sorted(items, key=lambda n: -n["id"])
def read_keys():
try:
return [l.strip() for l in open(STATE) if l.strip()]
except OSError:
return []
def mark(keys):
old = read_keys()
merged = old + [k for k in keys if k not in old]
os.makedirs(os.path.dirname(STATE), exist_ok=True)
with open(STATE, "w") as f:
f.write("\n".join(merged[-KEEP:]) + "\n")
if __name__ == "__main__":
if len(sys.argv) > 2 and sys.argv[1] == "read":
mark(sys.argv[2:])
elif len(sys.argv) > 1 and sys.argv[1] == "readall":
mark([n["key"] for n in all_notifs()])
else:
done = set(read_keys())
print(json.dumps([n for n in all_notifs() if n["key"] not in done]))