summaryrefslogtreecommitdiff
path: root/src/modules/notifications.tsx
blob: 8b50a124630484ba56cdd239bf61a89dd52a6dcf (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
import { bind } from "astal";
import { Astal, Gtk } from "astal/gtk3";
import AstalNotifd from "gi://AstalNotifd";
import Notification from "../widgets/notification";
import PopupWindow from "../widgets/popupwindow";

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

            const addNotification = (notification: AstalNotifd.Notification) => {
                const notif = (<Notification notification={notification} />) as Notification;
                notif.connect("destroy", () => map.get(notification.id) === notif && map.delete(notification.id));
                map.get(notification.id)?.destroyWithAnims();
                map.set(notification.id, notif);

                self.pack_end(
                    <eventbox
                        // Dismiss on middle click
                        onClick={(_, event) => event.button === Astal.MouseButton.MIDDLE && notification.dismiss()}
                    >
                        {notif}
                    </eventbox>,
                    false,
                    false,
                    0
                );
            };

            notifd
                .get_notifications()
                .sort((a, b) => a.time - b.time)
                .forEach(addNotification);

            self.hook(notifd, "notified", (_, id) => addNotification(notifd.get_notification(id)));
            self.hook(notifd, "resolved", (_, id) => map.get(id)?.destroyWithAnims());
        }}
    />
);

export default () => (
    <PopupWindow name="notifications">
        <box vertical className="notifications">
            <box className="header">
                <label
                    label={bind(AstalNotifd.get_default(), "notifications").as(
                        n => `${n.length} notification${n.length === 1 ? "" : "s"}`
                    )}
                />
                <box hexpand />
                <button
                    cursor="pointer"
                    onClicked={() => (AstalNotifd.get_default().dontDisturb = !AstalNotifd.get_default().dontDisturb)}
                    label="Silence"
                    className={bind(AstalNotifd.get_default(), "dontDisturb").as(d => (d ? "enabled" : ""))}
                />
                <button
                    cursor="pointer"
                    onClicked={() => AstalNotifd.get_default().notifications.forEach(n => n.dismiss())}
                    label="Clear"
                />
            </box>
            <stack
                transitionType={Gtk.StackTransitionType.CROSSFADE}
                transitionDuration={150}
                shown={bind(AstalNotifd.get_default(), "notifications").as(n => (n.length > 0 ? "list" : "empty"))}
            >
                <box vertical valign={Gtk.Align.CENTER} name="empty">
                    <label className="icon" label="notifications_active" />
                    <label label="All caught up!" />
                </box>
                <scrollable expand hscroll={Gtk.PolicyType.NEVER} name="list">
                    <List />
                </scrollable>
            </stack>
        </box>
    </PopupWindow>
);