summaryrefslogtreecommitdiff
path: root/src/modules/notifpopups.tsx
blob: cb5984dbb9e238904ea5857d27efaca7ed208dbf (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
import type { Monitor } from "@/services/monitors";
import { setupChildClickthrough } from "@/utils/widgets";
import Notification from "@/widgets/notification";
import { Astal, Gtk } from "astal/gtk3";
import { notifpopups as config } from "config";
import AstalNotifd from "gi://AstalNotifd";
import type SideBar from "./sidebar";
import { awaitSidebar } from "./sidebar";

export default ({ monitor }: { monitor: Monitor }) => (
    <window
        monitor={monitor.id}
        namespace="caelestia-notifpopups"
        anchor={Astal.WindowAnchor.TOP | Astal.WindowAnchor.RIGHT | Astal.WindowAnchor.BOTTOM}
    >
        <box
            vertical
            valign={Gtk.Align.START}
            className="notifpopups"
            setup={self => {
                const notifd = AstalNotifd.get_default();
                const map = new Map<number, Notification>();

                self.hook(notifd, "notified", (self, id) => {
                    if (notifd.dontDisturb) return;

                    const notification = notifd.get_notification(id);

                    const popup = (<Notification popup notification={notification} />) as Notification;
                    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
                            onClick={(_, event) => {
                                // Activate notif or go to notif center on primary click
                                if (event.button === Astal.MouseButton.PRIMARY) {
                                    if (notification.actions.length === 1)
                                        notification.invoke(notification.actions[0].id);
                                    else {
                                        sidebar?.shown.set("alerts");
                                        sidebar?.show();
                                        popup.destroyWithAnims();
                                    }
                                }
                                // Dismiss on middle click
                                else if (event.button === Astal.MouseButton.MIDDLE) notification.dismiss();
                            }}
                            // Close on hover lost
                            onHoverLost={() => popup.destroyWithAnims()}
                            setup={self => self.hook(popup, "destroy", () => self.destroy())}
                        >
                            {popup}
                        </eventbox>
                    );

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

                let sidebar: SideBar | null = null;
                awaitSidebar(monitor).then(s => (sidebar = s));

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