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

83 lines
2.4 KiB
JavaScript
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;
export default () => Box({
vertical: true,
setup: (self) => {
2023-12-20 17:14:07 -05:00
/** @param {number} id */
const addPopup = (id) => {
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();
}
}
};
2023-12-20 17:14:07 -05:00
/**
* @param {number} id
* @param {boolean} force
*/
const handleDismiss = (id, force = false) => {
2023-12-20 17:14:07 -05:00
// @ts-expect-error
const notif = self.children.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
// @ts-expect-error
if (!notif.attribute.hovered || force) {
// @ts-expect-error
notif.attribute.slideAway('Left');
}
// If notif is hovered, delay close
2023-12-20 17:14:07 -05:00
// @ts-expect-error
else if (notif.attribute.hovered) {
const intervalId = interval(DELAY, () => {
// @ts-expect-error
if (!notif.attribute.hovered && intervalId) {
// @ts-expect-error
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');
},
});