nixos-configs/devices/wim/config/ags/ts/notifications/popup.ts

78 lines
2.3 KiB
TypeScript
Raw Normal View History

import Notifications from 'resource:///com/github/Aylur/ags/service/notifications.js';
import { Box } from 'resource:///com/github/Aylur/ags/widget.js';
import { interval } from 'resource:///com/github/Aylur/ags/utils.js';
import GLib from 'gi://GLib';
import { Notification } from './base.js';
const DELAY = 2000;
2024-01-13 11:15:08 -05:00
// Types
import AgsBox from 'types/widgets/box.js';
export default () => Box({
vertical: true,
// Needed so it occupies space at the start
css: 'padding: 1px;',
setup: (self) => {
2024-01-13 11:15:08 -05:00
const addPopup = (id: number) => {
if (!id) {
return;
}
const notif = Notifications.getNotification(id);
2023-12-20 17:14:07 -05:00
if (notif) {
const NewNotif = Notification({
notif,
2023-12-22 16:58:12 -05:00
command: () => {
if (notif.popup) {
notif.dismiss();
}
},
2023-12-20 17:14:07 -05:00
});
2023-12-20 17:14:07 -05:00
if (NewNotif) {
// Use this instead of add to put it at the top
self.pack_end(NewNotif, false, false, 0);
self.show_all();
}
}
};
2024-01-13 11:15:08 -05:00
const handleDismiss = (id: number, force = false) => {
const notif = (self.children as Array<AgsBox>)
.find((ch) => ch.attribute.id === id);
if (!notif) {
return;
}
// If notif isn't hovered or was closed, slide away
2023-12-20 17:14:07 -05:00
if (!notif.attribute.hovered || force) {
notif.attribute.slideAway('Left');
}
// If notif is hovered, delay close
2023-12-20 17:14:07 -05:00
else if (notif.attribute.hovered) {
const intervalId = interval(DELAY, () => {
if (!notif.attribute.hovered && intervalId) {
notif.attribute.slideAway('Left');
GLib.source_remove(intervalId);
}
});
}
};
self
.hook(Notifications, (_, id) => addPopup(id), 'notified')
.hook(Notifications, (_, id) => handleDismiss(id), 'dismissed')
.hook(Notifications, (_, id) => handleDismiss(id, true), 'closed');
},
});