nixos-configs/modules/ags/config/ts/notifications/center.ts

168 lines
4.2 KiB
TypeScript
Raw Normal View History

const Notifications = await Service.import('notifications');
const { Label, Box, Icon, Scrollable, Revealer } = Widget;
const { timeout } = Utils;
import { Notification, HasNotifs } from './base.ts';
import CursorBox from '../misc/cursorbox.ts';
2023-09-11 19:57:21 -04:00
2024-01-13 11:15:08 -05:00
// Types
import { Notification as NotifObj } from 'resource:///com/github/Aylur/ags/service/notifications.js';
2024-01-29 20:56:56 -05:00
import { BoxGeneric, NotifGesture } from 'global-types';
2023-12-20 17:14:07 -05:00
const addNotif = (box: BoxGeneric, notif: NotifObj) => {
if (notif) {
const NewNotif = Notification({
notif,
slideIn: 'Right',
command: () => notif.close(),
});
if (NewNotif) {
box.pack_end(NewNotif, false, false, 0);
box.show_all();
}
}
};
const NotificationList = () => Box({
vertical: true,
vexpand: true,
vpack: 'start',
2023-12-20 17:14:07 -05:00
visible: HasNotifs.bind(),
setup: (self) => {
2024-02-18 13:10:25 -05:00
const initNotifId = 1337;
const delay = 2000;
2024-02-18 13:10:25 -05:00
// Fix the no notif placement with a fake notif
Notifications.Notify('', initNotifId, '', '', '', [''], {}, 0);
setTimeout(() => {
Notifications.getNotification(initNotifId)?.close();
}, delay);
self
.hook(Notifications, (box, id) => {
// Handle cached notifs
if (box.children.length === 0) {
Notifications.notifications.forEach((n) => {
addNotif(box, n);
});
}
else if (id) {
2023-12-20 17:14:07 -05:00
const notifObj = Notifications.getNotification(id);
if (notifObj) {
addNotif(box, notifObj);
}
}
}, 'notified')
.hook(Notifications, (box, id) => {
2024-01-29 20:56:56 -05:00
const notif = (box.children as NotifGesture[])
2024-01-13 11:15:08 -05:00
.find((ch) => ch.attribute.id === id);
if (notif?.sensitive) {
2023-12-20 17:14:07 -05:00
notif.attribute.slideAway('Right');
}
}, 'closed');
},
});
const ClearButton = () => CursorBox({
class_name: 'clear',
on_primary_click_release: () => {
Notifications.clear();
timeout(1000, () => App.closeWindow('notification-center'));
},
setup: (self) => {
self.hook(HasNotifs, () => {
self.disabled = !HasNotifs.value;
});
},
child: Box({
children: [
Label('Clear '),
Icon({
setup: (self) => {
self.hook(Notifications, () => {
self.icon = Notifications.notifications.length > 0 ?
'user-trash-full-symbolic' :
'user-trash-symbolic';
});
},
}),
],
}),
});
const Header = () => Box({
2023-12-20 17:14:07 -05:00
class_name: 'header',
children: [
Label({
label: 'Notifications',
hexpand: true,
xalign: 0,
}),
ClearButton(),
],
});
const Placeholder = () => Revealer({
transition: 'crossfade',
2023-12-20 17:14:07 -05:00
reveal_child: HasNotifs.bind()
.transform((v) => !v),
child: Box({
2023-12-20 17:14:07 -05:00
class_name: 'placeholder',
vertical: true,
vpack: 'center',
hpack: 'center',
vexpand: true,
hexpand: true,
2023-12-20 17:14:07 -05:00
children: [
Icon('notification-disabled-symbolic'),
Label('Your inbox is empty'),
],
}),
});
export default () => Box({
2023-12-20 17:14:07 -05:00
class_name: 'notification-center',
vertical: true,
children: [
Header(),
2023-12-20 17:14:07 -05:00
Box({
2023-12-20 17:14:07 -05:00
class_name: 'notification-wallpaper-box',
children: [
Scrollable({
2023-12-20 17:14:07 -05:00
class_name: 'notification-list-box',
hscroll: 'never',
vscroll: 'automatic',
2023-12-20 17:14:07 -05:00
child: Box({
2023-12-20 17:14:07 -05:00
class_name: 'notification-list',
vertical: true,
2023-12-20 17:14:07 -05:00
children: [
NotificationList(),
2023-12-20 17:14:07 -05:00
Placeholder(),
],
}),
}),
],
}),
],
});