Skip to content

Commit

Permalink
appIconIndicators: Add add unhanded notifications to icon count
Browse files Browse the repository at this point in the history
Support monitoring notifications that are still around in the shell
notification tray and use it to increase the icon indicators count.

This is not visible in case we are in do-not-disturb mode.

Closes: #1861, #20
  • Loading branch information
3v1n0 committed Mar 7, 2023
1 parent be3069e commit 90fce4c
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ EXTRA_MODULES = \
fileManager1API.js \
launcherAPI.js \
locations.js \
notificationsMonitor.js \
windowPreview.js \
intellihide.js \
prefs.js \
Expand Down
23 changes: 20 additions & 3 deletions appIconIndicators.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,13 +691,14 @@ var UnityIndicator = class DashToDockUnityIndicator extends IndicatorBase {
this._source._iconContainer.add_child(this._notificationBadgeBin);
this.updateNotificationBadgeStyle();

const { remoteModel } = Docking.DockManager.getDefault();
const { remoteModel, notificationsMonitor } = Docking.DockManager.getDefault();
const remoteEntry = remoteModel.lookupById(this._source.app.id);
this._remoteEntry = remoteEntry;

this._signalsHandler.add([
remoteEntry,
['count-changed', 'count-visible-changed'],
(sender, { count, count_visible: countVisible }) =>
this.setNotificationCount(countVisible ? count : 0),
() => this._updateNotificationsCount(),
], [
remoteEntry,
['progress-changed', 'progress-visible-changed'],
Expand All @@ -707,6 +708,10 @@ var UnityIndicator = class DashToDockUnityIndicator extends IndicatorBase {
remoteEntry,
'urgent-changed',
(sender, { urgent }) => this.setUrgent(urgent),
], [
notificationsMonitor,
'changed',
() => this._updateNotificationsCount(),
], [
St.ThemeContext.get_for_stage(global.stage),
'changed',
Expand All @@ -720,8 +725,10 @@ var UnityIndicator = class DashToDockUnityIndicator extends IndicatorBase {

destroy() {
super.destroy();

this._notificationBadgeBin.destroy();
this._notificationBadgeBin = null;
this._remoteEntry = null;
}

updateNotificationBadgeStyle() {
Expand Down Expand Up @@ -768,6 +775,16 @@ var UnityIndicator = class DashToDockUnityIndicator extends IndicatorBase {
}
}

_updateNotificationsCount() {
const remoteCount = this._remoteEntry['count-visible']
? this._remoteEntry.count ?? 0 : 0;
const { notificationsMonitor } = Docking.DockManager.getDefault();
const notificationsCount = notificationsMonitor.getAppNotificationsCount(
this._source.app.id);

this.setNotificationCount(remoteCount + notificationsCount);
}

setNotificationCount(count) {
if (count > 0) {
const text = this._notificationBadgeCountToText(count);
Expand Down
7 changes: 7 additions & 0 deletions docking.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const {
intellihide: Intellihide,
launcherAPI: LauncherAPI,
locations: Locations,
notificationsMonitor: NotificationsMonitor,
theming: Theming,
utils: Utils,
} = Me.imports;
Expand Down Expand Up @@ -1663,6 +1664,7 @@ var DockManager = class DashToDockDockManager {
this._oldDash = Main.overview.isDummy ? null : Main.overview.dash;
this._discreteGpuAvailable = AppDisplay.discreteGpuAvailable;
this._appSpread = new AppSpread.AppSpread();
this._notificationsMonitor = new NotificationsMonitor.NotificationsMonitor();

if (this._discreteGpuAvailable === undefined) {
const updateDiscreteGpuAvailable = () => {
Expand Down Expand Up @@ -1753,6 +1755,10 @@ var DockManager = class DashToDockDockManager {
return this._appSpread;
}

get notificationsMonitor() {
return this._notificationsMonitor;
}

getDockByMonitor(monitorIndex) {
return this._allDocks.find(d => d.monitorIndex === monitorIndex);
}
Expand Down Expand Up @@ -2535,6 +2541,7 @@ var DockManager = class DashToDockDockManager {
this._fm1Client.destroy();
this._fm1Client = null;
}
this._notificationsMonitor.destroy();
this._appSpread.destroy();
this._trash?.destroy();
this._trash = null;
Expand Down
108 changes: 108 additions & 0 deletions notificationsMonitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-

/* exported NotificationsMonitor */

const { signals: Signals } = imports;

const {
Gio,
} = imports.gi;

const {
main: Main,
} = imports.ui;

const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();

const {
utils: Utils,
} = Me.imports;


const Labels = Object.freeze({
SOURCES: Symbol('sources'),
NOTIFICATIONS: Symbol('notifications'),
});


var NotificationsMonitor = class NotificationsManagerImpl {
constructor() {
this._settings = new Gio.Settings({
schema_id: 'org.gnome.desktop.notifications',
});

this._appNotifications = Object.create(null);
this._signalsHandler = new Utils.GlobalSignalsHandler(this);

this._isEnabled = this._settings.get_boolean('show-banners');
this._signalsHandler.add(this._settings, 'changed::show-banners', () => {
const isEnabled = this._settings.get_boolean('show-banners');
if (isEnabled !== this._isEnabled) {
this._isEnabled = isEnabled;
this.emit('state-changed');

this._updateState();
}
});

this._updateState();
}

destroy() {
this.emit('destroy');
this._signalsHandler.destroy();
this._signalsHandler = null;
this._appNotifications = null;
this._settings = null;
}

get enabled() {
return this._isEnabled;
}

getAppNotificationsCount(appId) {
return this._appNotifications[appId] ?? 0;
}

_updateState() {
if (this.enabled) {
this._signalsHandler.addWithLabel(Labels.SOURCES, Main.messageTray,
'source-added', () => this._checkNotifications());
this._signalsHandler.addWithLabel(Labels.SOURCES, Main.messageTray,
'source-removed', () => this._checkNotifications());
} else {
this._signalsHandler.removeWithLabel(Labels.SOURCES);
}

this._checkNotifications();
}

_checkNotifications() {
this._appNotifications = Object.create(null);
this._signalsHandler.removeWithLabel(Labels.NOTIFICATIONS);

if (this.enabled) {
Main.messageTray.getSources().forEach(source => {
this._signalsHandler.addWithLabel(Labels.NOTIFICATIONS, source,
'notification-added', () => this._checkNotifications());

source.notifications.forEach(notification => {
const app = notification.source?.app ?? notification.source?._app;

if (app?.id) {
this._signalsHandler.addWithLabel(Labels.NOTIFICATIONS,
notification, 'destroy', () => this._checkNotifications());

this._appNotifications[app.id] =
(this._appNotifications[app.id] ?? 0) + 1;
}
});
});
}

this.emit('changed');
}
};

Signals.addSignalMethods(NotificationsMonitor.prototype);

0 comments on commit 90fce4c

Please sign in to comment.