Skip to content

Commit

Permalink
refactor(notification): update GtkNotificationAppSource overrides
Browse files Browse the repository at this point in the history
Update the `GtkNotificationAppSource` overrides for `addNotification()`
and internal hooks for the service.
  • Loading branch information
andyholmes committed Apr 4, 2024
1 parent b345b20 commit c3ffbfd
Showing 1 changed file with 39 additions and 66 deletions.
105 changes: 39 additions & 66 deletions src/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,60 +206,30 @@ class Source extends GtkNotificationDaemonAppSource {
/*
* Override to control notification spawning
*/
addNotification(notificationId, notificationParams, showBanner) {
addNotification(notification) {
this._notificationPending = true;

// Parse the id to determine if it's from a device
let localId = notificationId;
let idMatch, deviceId, remoteId;

if ((idMatch = DEVICE_REGEX.exec(notificationId))) {
[, deviceId, remoteId] = idMatch;
localId = `${deviceId}|${remoteId}`;
// valent-modifications-begin
const [match, deviceId, remoteId] = DEVICE_REGEX.exec(notification.id);
if (match) {
notification.set({deviceId, remoteId});
notification.connect('destroy', (_notification, reason) => {
this._valentCloseNotification(notification, reason);
});
}
// valent-modifications-end

let notification = this._notifications[localId];

/* Check if existing notifications represent an exact repeat and return
* early if so. Otherwise, update the notification title and body. */
if (notification) {
const title = notificationParams.title.unpack();
const body = notificationParams?.body.unpack() || null;

if (notification.title === title &&
notification.bannerBodyText === body) {
this._notificationPending = false;
return;
}
this._notifications[notification.id]?.destroy(
MessageTray.NotificationDestroyedReason.REPLACED);

notification.title = title;
notification.bannerBodyText = body;

/* Notify the device when remote notifications are dismissed */
} else if (idMatch) {
notification = this._createNotification(notificationParams);
notification.deviceId = deviceId;
notification.remoteId = remoteId;

notification.connect('destroy', (remoteNotification, reason) => {
this._valentCloseNotification(remoteNotification, reason);
delete this._notifications[localId];
});
this._notifications[localId] = notification;

/* All other notifications are treated as local desktop notifications */
} else {
notification = this._createNotification(notificationParams);
notification.connect('destroy', () => {
delete this._notifications[localId];
});
this._notifications[localId] = notification;
}
notification.connect('destroy', () => {
delete this._notifications[notification.id];
});
this._notifications[notification.id] = notification;

if (showBanner)
this.showNotification(notification);
else
this.pushNotification(notification);
// valent-modifications-begin
MessageTray.Source.prototype.addNotification.call(this, notification);
// valent-modifications-end

this._notificationPending = false;
}
Expand Down Expand Up @@ -290,38 +260,40 @@ export function enable() {
});

for (const notification of Object.values(source._notifications)) {
const _id = notification.connect('destroy', (remoteNotification, reason) => {
source?._valentCloseNotification(remoteNotification, reason);
remoteNotification.disconnect(_id);
notification.connect('destroy', (_notification, reason) => {
source?._valentCloseNotification(notification, reason);
});
}
}

_sourceAddedId = Main.messageTray.connect('source-added', _onSourceAdded);

// Patch other applications' notification sources
const addNotification = function (notificationId, notificationParams, showBanner) {
/* eslint-disable func-style */
const addNotification = function (notification) {
this._notificationPending = true;

if (this._notifications[notificationId])
this._notifications[notificationId].destroy(MessageTray.NotificationDestroyedReason.REPLACED);
// valent-modifications-begin
notification.connect('destroy', (_notification, reason) => {
this?._valentRemoveNotification(notification, reason);
});
// valent-modifications-end

this._notifications[notification.id]?.destroy(
MessageTray.NotificationDestroyedReason.REPLACED);

const notification = this._createNotification(notificationParams);
notification.connect('destroy', (localNotification, reason) => {
this?._valentRemoveNotification(localNotification, reason);
delete this._notifications[notificationId];
notification.connect('destroy', () => {
delete this._notifications[notification.id];
});
this._notifications[notificationId] = notification;
this._notifications[notification.id] = notification;

if (showBanner)
this.showNotification(notification);
else
this.pushNotification(notification);
// valent-modifications-begin
MessageTray.Source.prototype.addNotification.call(this, notification);
// valent-modifications-end

this._notificationPending = false;
};

const _valentRemoveNotification = function (id, notification, reason) {
const _valentRemoveNotification = function (notification, reason) {
if (reason !== MessageTray.NotificationDestroyedReason.DISMISSED)
return;

Expand All @@ -330,13 +302,14 @@ export function enable() {
'/org/gtk/Notifications',
'org.gtk.Notifications',
'RemoveNotification',
new GLib.Variant('(ss)', [this._appId, id]),
new GLib.Variant('(ss)', [this._appId, notification.id]),
null,
Gio.DBusCallFlags.NO_AUTO_START,
-1,
null,
null);
};
/* eslint-enable func-style */

Object.assign(GtkNotificationDaemonAppSource.prototype, {
addNotification,
Expand Down

0 comments on commit c3ffbfd

Please sign in to comment.