@@ -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-
242165class 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
11011100function main ( metadata , orientation , panel_height , instance_id ) {
0 commit comments