dotfiles/home/ags/files/notificationPopups.js

141 lines
3.5 KiB
JavaScript
Raw Normal View History

2024-07-05 18:07:30 +02:00
const notifications = await Service.import("notifications")
2024-10-02 14:56:27 +02:00
notifications.popupTimeout = 3000
notifications.forceTimeout = true
2024-07-05 18:07:30 +02:00
/** @param {import('resource:///com/github/Aylur/ags/service/notifications.js').Notification} n */
function NotificationIcon({ app_entry, app_icon, image }) {
2024-10-14 16:58:55 +02:00
if (image) {
return Widget.Box({
css: `background-image: url("${image}");`
+ "background-size: contain;"
+ "background-repeat: no-repeat;"
+ "background-position: center;",
})
}
2024-07-05 18:07:30 +02:00
2024-10-14 16:58:55 +02:00
let icon = "dialog-information-symbolic"
if (Utils.lookUpIcon(app_icon))
icon = app_icon
2024-07-05 18:07:30 +02:00
2024-10-14 16:58:55 +02:00
if (app_entry && Utils.lookUpIcon(app_entry))
icon = app_entry
2024-07-05 18:07:30 +02:00
2024-10-14 16:58:55 +02:00
return Widget.Box({
child: Widget.Icon(icon),
})
2024-07-05 18:07:30 +02:00
}
/** @param {import('resource:///com/github/Aylur/ags/service/notifications.js').Notification} n */
function Notification(n) {
2024-10-14 16:58:55 +02:00
const icon = Widget.Box({
vpack: "start",
class_name: "icon",
child: NotificationIcon(n),
})
2024-07-05 18:07:30 +02:00
2024-10-14 16:58:55 +02:00
const title = Widget.Label({
class_name: "title",
xalign: 0,
justification: "left",
hexpand: true,
max_width_chars: 24,
truncate: "end",
wrap: true,
label: n.summary,
use_markup: true,
})
2024-07-05 18:07:30 +02:00
2024-10-14 16:58:55 +02:00
const body = Widget.Label({
class_name: "body",
hexpand: true,
use_markup: true,
xalign: 0,
justification: "left",
label: n.body,
wrap: true,
})
2024-07-05 18:07:30 +02:00
2024-10-14 16:58:55 +02:00
const actions = Widget.Box({
class_name: "actions",
children: n.actions.map(({ id, label }) => Widget.Button({
class_name: "action-button",
on_clicked: () => {
n.invoke(id)
n.dismiss()
},
hexpand: true,
child: Widget.Label(label),
})),
})
2024-07-05 18:07:30 +02:00
2024-10-14 16:58:55 +02:00
return Widget.EventBox(
{
attribute: { id: n.id },
on_primary_click: n.dismiss,
},
Widget.Box(
{
class_name: `notification ${n.urgency}`,
vertical: true,
},
Widget.Box([
icon,
2024-07-05 18:07:30 +02:00
Widget.Box(
2024-10-14 16:58:55 +02:00
{ vertical: true },
title,
body,
2024-07-05 18:07:30 +02:00
),
2024-10-14 16:58:55 +02:00
]),
actions,
),
)
2024-07-05 18:07:30 +02:00
}
export function NotificationPopups(monitor = 0) {
2024-10-14 16:58:55 +02:00
const list = Widget.Box({
vertical: true,
children: notifications.popups.map(Notification),
})
2024-07-05 18:07:30 +02:00
2024-10-14 16:58:55 +02:00
function onNotified(_, /** @type {number} */ id) {
const n = notifications.getNotification(id)
if (n) {
list.children = [Notification(n), ...list.children]
// Schedule a timeout to dismiss the notification
setTimeout(() => {
2024-07-05 18:07:30 +02:00
const n = notifications.getNotification(id)
2024-10-14 16:58:55 +02:00
if (n) { // if the notification is still there
n.dismiss()
}
}, notifications.popupTimeout)
2024-07-05 18:07:30 +02:00
}
2024-10-14 16:58:55 +02:00
}
2024-07-05 18:07:30 +02:00
2024-10-14 16:58:55 +02:00
function onDismissed(_, /** @type {number} */ id) {
list.children.find(n => n.attribute.id === id)?.destroy()
}
2024-07-05 18:07:30 +02:00
2024-10-14 16:58:55 +02:00
list.hook(notifications, onNotified, "notified")
.hook(notifications, onDismissed, "dismissed")
2024-07-05 18:07:30 +02:00
2024-10-14 16:58:55 +02:00
return Widget.Window({
monitor,
name: `notifications${monitor}`,
class_name: "notification-popups",
anchor: ["top", "right"],
child: Widget.Box({
css: "min-width: 2px; min-height: 2px;",
class_name: "notifications",
vertical: true,
child: list,
2024-07-05 18:07:30 +02:00
2024-10-14 16:58:55 +02:00
/** this is a simple one liner that could be used instead of
2024-07-05 18:07:30 +02:00
hooking into the 'notified' and 'dismissed' signals.
but its not very optimized becuase it will recreate
the whole list everytime a notification is added or dismissed */
2024-10-14 16:58:55 +02:00
// children: notifications.bind('popups')
// .as(popups => popups.map(Notification))
}),
})
2024-07-05 18:07:30 +02:00
}