Skip to content

Commit 850faf7

Browse files
committed
refactor
1 parent 74f74ea commit 850faf7

File tree

2 files changed

+89
-80
lines changed

2 files changed

+89
-80
lines changed

files/usr/share/cinnamon/applets/[email protected]/appGroup.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class AppGroup {
9999
this.labelVisiblePref = this.state.settings.titleDisplay !== TitleDisplay.None && this.state.isHorizontal;
100100
this.drawLabel = this.labelVisiblePref;
101101
this.progress = 0;
102+
this.notifications = [];
102103

103104
this.actor = new Cinnamon.GenericContainer({
104105
name: 'appButton',
@@ -191,7 +192,7 @@ class AppGroup {
191192
this.calcWindowNumber();
192193
this.on_orientation_changed(true);
193194
this.handleFavorite();
194-
this.state.notifications.addAppGroup(this);
195+
this.state.trigger('copyNotifications', this);
195196
}
196197

197198
initThumbnailMenu() {
@@ -953,7 +954,7 @@ class AppGroup {
953954
this.setIcon(metaWindow)
954955

955956
this.calcWindowNumber();
956-
this.state.notifications.updateNotificationsBadge(this);
957+
this.updateNotificationsBadge();
957958
this.onFocusChange();
958959
}
959960
set({
@@ -1124,6 +1125,15 @@ class AppGroup {
11241125
}
11251126
}
11261127

1128+
updateNotificationsBadge() {
1129+
if (this.notifications.length > 0) {
1130+
this.notificationsBadgeLabel.text = this.notifications.length.toString();
1131+
this.notificationsBadge.show();
1132+
} else {
1133+
this.notificationsBadge.hide();
1134+
}
1135+
}
1136+
11271137
handleTitleDisplayChange() {
11281138
this.groupState.metaWindows.forEach(
11291139
win => this.onWindowTitleChanged(win, true)

files/usr/share/cinnamon/applets/[email protected]/applet.js

Lines changed: 77 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -162,83 +162,6 @@ class PinnedFavs {
162162
}
163163
}
164164

165-
class Notifications {
166-
// As an app can have multiple appgroups (multiple windows, different workspaces, multiple instances), all
167-
// appGroups with the same appId should always display the same number of notifications.
168-
constructor() {
169-
this._appGroupList = []; //stores all appGroups from all workspaces
170-
Main.messageTray.connect('notify-applet-update', (mtray, notification) => this._notificationReceived(mtray, notification));
171-
}
172-
173-
_notificationReceived(mtray, notification) {
174-
let appId = notification.source.app?.get_id();
175-
176-
if (!appId) {
177-
return;
178-
}
179-
180-
// Add notification to all appgroups with appId
181-
let notificationAdded = false;
182-
this._appGroupList.forEach(appGroup => {
183-
if (!appGroup.groupState || appGroup.groupState.willUnmount) return;
184-
if (appId === appGroup.groupState.appId) {
185-
appGroup.notifications.push(notification);
186-
notificationAdded = true;
187-
this.updateNotificationsBadge(appGroup);
188-
}
189-
});
190-
if (notificationAdded) {
191-
notification.appId = appId;
192-
notification.connect('destroy', () => this._removeNotification(notification));
193-
}
194-
}
195-
196-
_removeNotification(notification) {
197-
this._appGroupList.forEach(appGroup => {
198-
if (!appGroup.groupState || appGroup.groupState.willUnmount) return;
199-
if (notification.appId === appGroup.groupState.appId) {
200-
const index = appGroup.notifications.indexOf(notification);
201-
if (index > -1) {
202-
appGroup.notifications.splice(index, 1)
203-
this.updateNotificationsBadge(appGroup);
204-
}
205-
}
206-
});
207-
}
208-
209-
updateNotificationsBadge(appGroup) {
210-
if (appGroup.notifications.length > 0) {
211-
appGroup.notificationsBadgeLabel.text = appGroup.notifications.length.toString();
212-
appGroup.notificationsBadge.show();
213-
} else {
214-
appGroup.notificationsBadge.hide();
215-
}
216-
}
217-
218-
// Called from AppGroup constructor so that we always have a list of all appgroups from all workspaces.
219-
addAppGroup(newAppGroup) {
220-
newAppGroup.notifications = [];
221-
222-
//Copy notifications from any existing appGroup with the same appId.
223-
this._appGroupList.some(appGroup => {
224-
if (!appGroup.groupState || appGroup.groupState.willUnmount) return false;
225-
if (appGroup.groupState.appId === newAppGroup.groupState.appId) {
226-
newAppGroup.notifications = appGroup.notifications.slice(); //shallow copy
227-
return true;
228-
}
229-
})
230-
231-
this._appGroupList.push(newAppGroup);
232-
233-
//remove old deleted appgroups
234-
this._appGroupList = this._appGroupList.filter(appGroup =>
235-
appGroup !== null &&
236-
appGroup !== undefined &&
237-
appGroup.groupState &&
238-
!appGroup.groupState.willUnmount);
239-
}
240-
}
241-
242165
class GroupedWindowListApplet extends Applet.Applet {
243166
constructor(metadata, orientation, panel_height, instance_id) {
244167
super(orientation, panel_height, instance_id);
@@ -269,7 +192,6 @@ class GroupedWindowListApplet extends Applet.Applet {
269192
appletReady: false,
270193
willUnmount: false,
271194
settings: {},
272-
notifications: new Notifications(),
273195
homeDir: GLib.get_home_dir(),
274196
lastOverlayPreview: null,
275197
lastCycled: -1,
@@ -334,6 +256,7 @@ class GroupedWindowListApplet extends Applet.Applet {
334256
openAbout: () => this.openAbout(),
335257
configureApplet: () => this.configureApplet(),
336258
removeApplet: (event) => this.confirmRemoveApplet(event),
259+
copyNotifications: (appGroup) => this.copyNotifications(appGroup)
337260
});
338261

339262
this.settings = new AppletSettings(this.state.settings, metadata.uuid, instance_id);
@@ -370,6 +293,7 @@ class GroupedWindowListApplet extends Applet.Applet {
370293
this.signals.connect(global.display, 'window-created', (...args) => this.onWindowCreated(...args));
371294
this.signals.connect(global.settings, 'changed::panel-edit-mode', (...args) => this.on_panel_edit_mode_changed(...args));
372295
this.signals.connect(Main.themeManager, 'theme-set', (...args) => this.refreshCurrentWorkspace(...args));
296+
this.signals.connect(Main.messageTray, 'notify-applet-update', this._onNotificationReceived.bind(this));
373297
}
374298

375299
bindSettings() {
@@ -504,6 +428,9 @@ class GroupedWindowListApplet extends Applet.Applet {
504428
this.settings.finalize();
505429
unref(this, RESERVE_KEYS);
506430
MessageTray.extensionsHandlingNotifications--;
431+
if (MessageTray.extensionsHandlingNotifications === 0) {
432+
this._destroyAllNotifications();
433+
}
507434
}
508435

509436
on_panel_icon_size_changed(iconSize) {
@@ -1096,6 +1023,78 @@ class GroupedWindowListApplet extends Applet.Applet {
10961023
this.state.set({thumbnailCloseButtonOffset: global.ui_scale > 1 ? -10 : 0});
10971024
this.refreshAllWorkspaces();
10981025
}
1026+
1027+
copyNotifications(newAppGroup) {
1028+
// Copy notifications from any existing appGroup with the same appId.
1029+
this.workspaces.some(workspace => {
1030+
if (!workspace) return false;
1031+
return workspace.appGroups.some(appGroup => {
1032+
if (!appGroup || !appGroup.groupState || appGroup.groupState.willUnmount) return false;
1033+
if (appGroup.groupState.appId === newAppGroup.groupState.appId) {
1034+
newAppGroup.notifications = appGroup.notifications.slice(); // Shallow copy.
1035+
return true;
1036+
}
1037+
return false;
1038+
});
1039+
});
1040+
}
1041+
1042+
_onNotificationReceived(mtray, notification) {
1043+
let appId = notification.source.app?.get_id();
1044+
1045+
if (!appId) {
1046+
return;
1047+
}
1048+
1049+
// Add notification to all appgroups with appId.
1050+
let notificationAdded = false;
1051+
1052+
this.workspaces.forEach(workspace => {
1053+
if (!workspace) return;
1054+
workspace.appGroups.forEach(appGroup => {
1055+
if (!appGroup || !appGroup.groupState || appGroup.groupState.willUnmount) return;
1056+
if (appId === appGroup.groupState.appId) {
1057+
appGroup.notifications.push(notification);
1058+
notificationAdded = true;
1059+
appGroup.updateNotificationsBadge();
1060+
}
1061+
});
1062+
});
1063+
1064+
if (notificationAdded) {
1065+
notification.appId = appId;
1066+
notification.connect('destroy', () => this._onNotificationDestroyed(notification));
1067+
}
1068+
}
1069+
1070+
_onNotificationDestroyed(notification) {
1071+
this.workspaces.forEach(workspace => {
1072+
if (!workspace) return;
1073+
workspace.appGroups.forEach(appGroup => {
1074+
if (!appGroup || !appGroup.groupState || appGroup.groupState.willUnmount) return;
1075+
if (notification.appId === appGroup.groupState.appId) {
1076+
const index = appGroup.notifications.indexOf(notification);
1077+
if (index > -1) {
1078+
appGroup.notifications.splice(index, 1)
1079+
appGroup.updateNotificationsBadge();
1080+
}
1081+
}
1082+
});
1083+
});
1084+
}
1085+
1086+
_destroyAllNotifications() {
1087+
this.workspaces.forEach(workspace => {
1088+
if (!workspace) return;
1089+
workspace.appGroups.forEach(appGroup => {
1090+
if (!appGroup || !appGroup.groupState || appGroup.groupState.willUnmount) return;
1091+
// Iterate backwards due to in place element deletion.
1092+
for (let i = appGroup.notifications.length - 1; i >= 0; i--) {
1093+
appGroup.notifications[i].destroy();
1094+
}
1095+
});
1096+
});
1097+
}
10991098
}
11001099

11011100
function main(metadata, orientation, panel_height, instance_id) {

0 commit comments

Comments
 (0)