feat(eww -> notifications): Imlement notification timeout

This commit is contained in:
GaspardCulis 2024-06-23 12:51:44 +02:00
parent ea759f6791
commit a99d3b531e
No known key found for this signature in database
GPG key ID: BC18146756955609

View file

@ -1,14 +1,29 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys
import json import json
import dbus import dbus
from random import randint from random import randint
from threading import Timer
from gi.repository import GLib from gi.repository import GLib
from dbus.mainloop.glib import DBusGMainLoop from dbus.mainloop.glib import DBusGMainLoop
DEFAULT_NOTIF_TIMEOUT = 10 * 1000
notification_history = [] notification_history = []
def set_timeout(fn, ms, *args, **kwargs):
t = Timer(ms / 1000, fn, args=args, kwargs=kwargs)
t.start()
return t
def dismiss_notification(notification_id: int):
for notification in notification_history:
if notification["id"] == notification_id:
notification["dismissed"] = True
print(json.dumps(notification_history), flush=True)
def handle_notification(_, message): def handle_notification(_, message):
keys = ["app_name", "replaces_id", "app_icon", "summary", keys = ["app_name", "replaces_id", "app_icon", "summary",
"body", "actions", "hints", "expire_timeout"] "body", "actions", "hints", "expire_timeout"]
@ -19,19 +34,16 @@ def handle_notification(_, message):
notification["dismissed"] = False notification["dismissed"] = False
notification["id"] = randint(0, 8192 * 2) notification["id"] = randint(0, 8192 * 2)
notification_history.append(notification) notification_history.append(notification)
expire_timeout = int(notification["expire_timeout"])
set_timeout(dismiss_notification, expire_timeout if expire_timeout > 0 else DEFAULT_NOTIF_TIMEOUT, notification["id"])
print(json.dumps(notification_history), flush=True)
case "CloseNotification": case "CloseNotification":
notification_id = int(args[0]) dismiss_notification(int(args[0]))
for notification in notification_history:
if notification["id"] == notification_id:
notification["dismissed"] = True
case _: case _:
return return
print(json.dumps(notification_history), flush=True)
def handle_notification_dismiss(_, message):
print("jaaj")
print(message)
if __name__ == "__main__": if __name__ == "__main__":
DBusGMainLoop(set_as_default=True) DBusGMainLoop(set_as_default=True)