From 0fc899f637c4fa0d7b513e2ee993476a14eeb1d8 Mon Sep 17 00:00:00 2001 From: GaspardCulis Date: Sat, 22 Jun 2024 21:16:35 +0200 Subject: [PATCH] feat(eww): Added "notification widget" --- bar/eww/eww.scss | 1 + bar/eww/eww.yuck | 1 + bar/eww/notification/style.scss | 26 ++++++++++++ bar/eww/notification/widget.yuck | 70 +++++++++++++++++++++++++++++++ bar/eww/scripts/get-notifications | 44 +++++++++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 bar/eww/notification/style.scss create mode 100644 bar/eww/notification/widget.yuck create mode 100755 bar/eww/scripts/get-notifications diff --git a/bar/eww/eww.scss b/bar/eww/eww.scss index 9e0fa17..ca9195c 100644 --- a/bar/eww/eww.scss +++ b/bar/eww/eww.scss @@ -3,6 +3,7 @@ @import "./music/style.scss"; @import "./timer/style.scss"; @import "./freqtrade/style.scss"; +@import "./notification/style.scss"; * { all: unset; diff --git a/bar/eww/eww.yuck b/bar/eww/eww.yuck index 7dc7b52..794f979 100644 --- a/bar/eww/eww.yuck +++ b/bar/eww/eww.yuck @@ -2,5 +2,6 @@ (include "./music/widget.yuck") (include "./timer/widget.yuck") (include "./freqtrade/widget.yuck") +(include "./notification/widget.yuck") diff --git a/bar/eww/notification/style.scss b/bar/eww/notification/style.scss new file mode 100644 index 0000000..5b2daeb --- /dev/null +++ b/bar/eww/notification/style.scss @@ -0,0 +1,26 @@ +.notifications-container { + padding: 8px; + + .notification { + padding: 6px; + background-color: $background; + border-radius: 20px; + + .notification-icon { + min-width: 128px; + min-height: 128px; + + background-size: cover; + background-position: center center; + border-radius: 16px; + } + + .notification-text-container { + padding-top: 6px; + + .notification-summary { + font-weight: bold; + } + } + } +} diff --git a/bar/eww/notification/widget.yuck b/bar/eww/notification/widget.yuck new file mode 100644 index 0000000..e4feb5b --- /dev/null +++ b/bar/eww/notification/widget.yuck @@ -0,0 +1,70 @@ +(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") + +(defwidget notification [notif] + (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) + (label + :text "${notif.app_name}" + :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}" + (image + :path "${close_icon}" + :image-width 24 + :image-height 24) + ) + ) + (box :orientation "h" :space-evenly false :spacing 8 + (box + :class "${notif.app_icon != "" ? "notification-icon" : ""}" + :style "background-image: url('${notif.app_icon}');" + ) + (box + :class "notification-text-container" + :orientation "v" + :hexpand true + :space-evenly false + (label + :class "notification-summary" + :text "${notif.summary}" + :halign "start") + (label + :text "${notif.body}" + :wrap true + :halign "fill" + :vexpand true) + ) + ) + ) +) + +(defwindow notification_overlay + :namespace "eww.notification_overlay" + :monitor 0 + :geometry (geometry + :width "400px" + :anchor "bottom right") + :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}") + ) + ) +) diff --git a/bar/eww/scripts/get-notifications b/bar/eww/scripts/get-notifications new file mode 100755 index 0000000..e9ea987 --- /dev/null +++ b/bar/eww/scripts/get-notifications @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +import sys +import json +import dbus +from random import randint +from gi.repository import GLib +from dbus.mainloop.glib import DBusGMainLoop + +notification_history = [] + +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) + case "CloseNotification": + notification_id = int(args[0]) + for notification in notification_history: + if notification["id"] == notification_id: + notification["dismissed"] = True + case _: + return + + print(json.dumps(notification_history), flush=True) + +def handle_notification_dismiss(_, message): + print("jaaj") + print(message) + +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()