eww -> notification: Refactor widget to work with end-rs

This commit is contained in:
GaspardCulis 2024-08-09 13:57:05 +02:00
parent 13f8a8707c
commit 63c1da3f34
No known key found for this signature in database
GPG key ID: BC18146756955609
3 changed files with 66 additions and 95 deletions

View file

@ -1,11 +1,18 @@
.notifications-container {
padding: 8px;
.notification {
.notification {
padding: 6px;
margin-right: 12px;
margin-bottom: 12px;
background-color: $background;
border-radius: 20px;
.notification-app-icon {
min-width: 24px;
min-height: 24px;
background-size: cover;
background-position: center center;
}
.notification-icon {
min-width: 128px;
min-height: 128px;
@ -22,5 +29,17 @@
font-weight: bold;
}
}
.notification-buttons {
margin-top: 6px;
.notification-button {
color: $text;
font-size: 0.8em;
padding: 8px;
border-radius: 10px;
margin-right: 12px;
border: 1px solid $text;
}
}
}

View file

@ -1,23 +1,27 @@
(defvar close_icon "/usr/share/icons/Qogir-ubuntu-dark/16/actions/window-close.svg")
(deflisten notifications :initial '[{"id": 69, "dismissed": false, "app_name": "notify-send", "replaces_id": 0, "app_icon": "/home/gaspard/Images/memes/vroom.png", "summary": "Work period over", "body": "You can take a little nap now.", "actions": [], "hints": {"urgency": 1, "sender-pid": 12406}, "expire_timeout": -1}]' "~/.config/eww/scripts/get-notifications")
(defvar end-binary "~/.cargo/bin/end-rs")
(defvar end-notifications '')
(defvar end-histories '')
(defvar end-replies '')
(defvar end-reply-text '')
(defwidget notification [notif]
(defwidget notification[notification]
(box
:class "notification"
:space-evenly false
:orientation "v"
:spacing 6
(box :orientation "h" :spacing 8 :space-evenly false
(image
:path "/usr/share/icons/Qogir-ubuntu/scalable/apps/spotify.svg"
:image-width 24
:image-height 24)
(box
:class "${notification.app_icon ?: "" != "" ? "notification-app-icon" : ""}"
:style "background-image: url('${notification.app_icon}');"
)
(label
:text "${notif.app_name}"
:text "${notification.application}"
:halign "start"
:hexpand true)
(button :onclick "dbus-send --session --dest=org.freedesktop.Notifications --type=method_call /org/freedesktop/Notifications org.freedesktop.Notifications.CloseNotification uint32:${notif.id}"
(button :onclick "${end-binary} close ${notification.id}"
(image
:path "${close_icon}"
:image-width 24
@ -26,8 +30,8 @@
)
(box :orientation "h" :space-evenly false :spacing 8
(box
:class "${notif.app_icon != "" ? "notification-icon" : ""}"
:style "background-image: url('${notif.app_icon}');"
:class "${notification.icon != "" ? "notification-icon" : ""}"
:style "background-image: url('${notification.icon}');"
)
(box
:class "notification-text-container"
@ -36,13 +40,26 @@
:space-evenly false
(label
:class "notification-summary"
:text "${notif.summary}"
:text "${notification.summary}"
:halign "start")
(label
:text "${notif.body}"
:text "${notification.body}"
:wrap true
:halign "fill"
:vexpand true)
(box
:class "notification-buttons"
:orientation "horizontal"
:space-evenly false
:halign "end"
:vexpand false
(for action in {notification.actions}
(button
:class "notification-button"
:onclick "${end-binary} action ${notification.id} ${action.id}"
:vexpand false
{action.text}))
)
)
)
)
@ -57,14 +74,5 @@
:stacking "overlay"
:focusable false
:exclusive true
(box
:class "notifications-container"
:orientation "v"
:halign "end"
:valign "end"
:spacing 6
(for notif in {jq(notifications, '[.[] | select(.dismissed == false)]')}
(notification :notif "${notif}")
)
)
(literal :content end-notifications)
)

View file

@ -1,56 +0,0 @@
#!/usr/bin/env python3
import json
import dbus
from random import randint
from threading import Timer
from gi.repository import GLib
from dbus.mainloop.glib import DBusGMainLoop
DEFAULT_NOTIF_TIMEOUT = 10 * 1000
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):
keys = ["app_name", "replaces_id", "app_icon", "summary",
"body", "actions", "hints", "expire_timeout"]
args = message.get_args_list()
match message.get_member() :
case "Notify":
notification = dict([(keys[i], args[i]) for i in range(8)])
notification["dismissed"] = False
notification["id"] = randint(0, 8192 * 2)
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":
dismiss_notification(int(args[0]))
case _:
return
if __name__ == "__main__":
DBusGMainLoop(set_as_default=True)
notification_bus = dbus.SessionBus()
notification_bus.add_match_string("type='method_call',interface='org.freedesktop.Notifications',eavesdrop=true")
notification_bus.add_message_filter(handle_notification)
loop = GLib.MainLoop()
loop.run()