/ services / Notifs.qml
Notifs.qml
 1  pragma Singleton
 2  pragma ComponentBehavior: Bound
 3  
 4  import "root:/widgets"
 5  import "root:/config"
 6  import Quickshell
 7  import Quickshell.Services.Notifications
 8  import QtQuick
 9  
10  Singleton {
11      id: root
12  
13      readonly property list<Notif> list: []
14      readonly property list<Notif> popups: list.filter(n => n.popup)
15  
16      NotificationServer {
17          id: server
18  
19          keepOnReload: false
20          actionsSupported: true
21          bodyHyperlinksSupported: true
22          bodyImagesSupported: true
23          bodyMarkupSupported: true
24          imageSupported: true
25  
26          onNotification: notif => {
27              notif.tracked = true;
28  
29              root.list.push(notifComp.createObject(root, {
30                  popup: true,
31                  notification: notif
32              }));
33          }
34      }
35  
36      CustomShortcut {
37          name: "clearNotifs"
38          description: "Clear all notifications"
39          onPressed: {
40              for (const notif of root.list)
41                  notif.popup = false;
42          }
43      }
44  
45      component Notif: QtObject {
46          id: notif
47  
48          property bool popup
49          readonly property date time: new Date()
50          readonly property string timeStr: {
51              const diff = Time.date.getTime() - time.getTime();
52              const m = Math.floor(diff / 60000);
53              const h = Math.floor(m / 60);
54  
55              if (h < 1 && m < 1)
56                  return "now";
57              if (h < 1)
58                  return `${m}m`;
59              return `${h}h`;
60          }
61  
62          required property Notification notification
63          readonly property string summary: notification.summary
64          readonly property string body: notification.body
65          readonly property string appIcon: notification.appIcon
66          readonly property string appName: notification.appName
67          readonly property string image: notification.image
68          readonly property var urgency: notification.urgency // Idk why NotificationUrgency doesn't work
69          readonly property list<NotificationAction> actions: notification.actions
70  
71          readonly property Timer timer: Timer {
72              running: true
73              interval: notif.notification.expireTimeout > 0 ? notif.notification.expireTimeout : NotifsConfig.defaultExpireTimeout
74              onTriggered: {
75                  if (NotifsConfig.expire)
76                      notif.popup = false;
77              }
78          }
79  
80          readonly property Connections conn: Connections {
81              target: notif.notification.Retainable
82  
83              function onDropped(): void {
84                  root.list.splice(root.list.indexOf(notif), 1);
85              }
86  
87              function onAboutToDestroy(): void {
88                  notif.destroy();
89              }
90          }
91      }
92  
93      Component {
94          id: notifComp
95  
96          Notif {}
97      }
98  }