aboutsummaryrefslogtreecommitdiff
path: root/modules/system/quickshell/Notifications.qml
blob: 79223ebdbff273e7f94aeff567081f81a2b9ab0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import Quickshell
import Quickshell.Wayland
import Quickshell.Services.Notifications
import QtQuick

Scope {
    id: scope

    readonly property int defaultTimeout: 5000

    property int nextId: 0
    property var liveNotifs: ({})

    ListModel {
        id: popupModel
    }

    function removeNotificationData(id) {
        for (let i = 0; i < popupModel.count; i++) {
            if (popupModel.get(i).nId === id) {
                popupModel.remove(i);
                break;
            }
        }
        delete liveNotifs[id];
    }

    function dismissExplicitly(id) {
        const n = liveNotifs[id];
        if (n)
            n.dismiss();
        removeNotificationData(id);
    }

    function hidePopup(id) {
        for (let i = 0; i < popupModel.count; i++) {
            if (popupModel.get(i).nId === id) {
                popupModel.remove(i);
                break;
            }
        }
    }

    function activateById(id) {
        const n = liveNotifs[id];
        if (!n)
            return;
        let invoked = false;
        for (const action of n.actions) {
            if (action.identifier === "default") {
                action.invoke();
                invoked = true;
                break;
            }
        }
        if (!invoked && n.desktopEntry) {
            Quickshell.execDetached(["gtk-launch", n.desktopEntry]);
        }
        dismissExplicitly(id);
    }

    function invokeAction(id, identifier) {
        const n = liveNotifs[id];
        if (n) {
            for (const action of n.actions) {
                if (action.identifier === identifier) {
                    action.invoke();
                    break;
                }
            }
        }
        dismissExplicitly(id);
    }

    function sendReply(id, text) {
        const n = liveNotifs[id];
        if (n && n.hasInlineReply)
            n.sendInlineReply(text);
        dismissExplicitly(id);
    }

    NotificationServer {
        keepOnReload: false
        actionsSupported: true
        actionIconsSupported: true
        bodySupported: true
        bodyMarkupSupported: true
        bodyImagesSupported: true
        imageSupported: true
        inlineReplySupported: true
        persistenceSupported: true

        onNotification: notif => {
            notif.tracked = true;

            const id = nextId;
            nextId = nextId + 1;
            liveNotifs[id] = notif;

            const acts = [];
            let hasDefault = false;
            for (const a of notif.actions) {
                if (a.identifier === "default")
                    hasDefault = true;
                else
                    acts.push({
                        identifier: a.identifier,
                        text: a.text
                    });
            }

            notif.closed.connect(() => removeNotificationData(id));

            let formattedAppIcon = notif.appIcon || "";
            if (formattedAppIcon !== "") {
                if (formattedAppIcon.startsWith("file://")) {} else if (formattedAppIcon.startsWith("/")) {
                    formattedAppIcon = "file://" + formattedAppIcon;
                } else {
                    formattedAppIcon = `image://icon/${formattedAppIcon}`;
                }
            }

            let formattedImage = notif.image || "";
            if (formattedImage !== "" && formattedImage.startsWith("/")) {
                formattedImage = "file://" + formattedImage;
            }

            const data = {
                nId: id,
                nSummary: notif.summary,
                nBody: notif.body,
                nAppName: notif.appName || "Notification",
                nAppIcon: formattedAppIcon,
                nImage: formattedImage,
                nTimestamp: new Date(),
                nTimeout: notif.expireTimeout > 0 ? notif.expireTimeout : defaultTimeout,
                nActions: acts,
                nClickable: hasDefault || (notif.desktopEntry || "") !== "",
                nHasInlineReply: notif.hasInlineReply || false,
                nReplyPlaceholder: notif.inlineReplyPlaceholder || "Reply",
                nResident: notif.resident || false
            };

            GlobalState.addNotification(data);
            popupModel.insert(0, data);
        }
    }

    NotificationPopupList {
        popupModel: scope.popupModel
        onActionInvoked: (id, identifier) => scope.invokeAction(id, identifier)
        onReplySent: (id, text) => scope.sendReply(id, text)
        onDismissed: id => scope.dismissExplicitly(id)
        onActivated: id => scope.activateById(id)
        onHideRequested: id => scope.hidePopup(id)
    }
}