summaryrefslogtreecommitdiff
path: root/src/modules/notifications.tsx
blob: 66188a1d74343fb0186cbc72c252e8151e4714b2 (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
import { Gtk } from "astal/gtk3";
import AstalNotifd from "gi://AstalNotifd";
import { PopupWindow, setupChildClickthrough } from "../utils/widgets";

const List = () => (
    <box
        vertical
        valign={Gtk.Align.START}
        className="list"
        setup={self => {
            const notifd = AstalNotifd.get_default();
            const map = new Map<number, NotifPopup>();
            self.hook(notifd, "notified", (self, id) => {
                const notification = notifd.get_notification(id);

                const popup = (<NotifPopup notification={notification} />) as NotifPopup;
                popup.connect("destroy", () => map.get(notification.id) === popup && map.delete(notification.id));
                map.get(notification.id)?.destroyWithAnims();
                map.set(notification.id, popup);

                self.add(
                    <eventbox
                        // Dismiss on middle click
                        onClick={(_, event) => event.button === Astal.MouseButton.MIDDLE && notification.dismiss()}
                        // Close on hover lost
                        onHoverLost={() => popup.destroyWithAnims()}
                    >
                        {popup}
                    </eventbox>
                );

                // Limit number of popups
                if (config.maxPopups > 0 && self.children.length > config.maxPopups)
                    map.values().next().value?.destroyWithAnims();
            });
            self.hook(notifd, "resolved", (_, id) => map.get(id)?.destroyWithAnims());

            // Change input region to child region so can click through empty space
            setupChildClickthrough(self);
        }}
    />
);

export default class Notifications extends PopupWindow {
    constructor() {
        super({
            name: "notifications",
            child: (
                <box>
                    <List />
                </box>
            ),
        });

        setupChildClickthrough(self);
    }
}