From 446a75d10d13a1970eb9a7d29299e549a8186062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Mon, 2 Sep 2024 18:55:18 +0200 Subject: [PATCH] appIconIndicators: Do not add notification badge code unless needed Only create the label and items if we are showing stuff, otherwise it's just a waste. Also update it on scale factor changes --- appIconIndicators.js | 77 +++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/appIconIndicators.js b/appIconIndicators.js index ecf4b28f9..8b348798f 100644 --- a/appIconIndicators.js +++ b/appIconIndicators.js @@ -740,23 +740,11 @@ export class UnityIndicator extends IndicatorBase { }, }; + static notificationBadgeSignals = Symbol('notification-badge-signals'); + constructor(source) { super(source); - this._notificationBadgeLabel = new St.Label(); - this._notificationBadgeBin = new St.Bin({ - child: this._notificationBadgeLabel, - x_align: Clutter.ActorAlign.END, - y_align: Clutter.ActorAlign.START, - x_expand: true, y_expand: true, - }); - this._notificationBadgeLabel.add_style_class_name('notification-badge'); - this._notificationBadgeLabel.clutter_text.ellipsize = Pango.EllipsizeMode.MIDDLE; - this._notificationBadgeBin.hide(); - - this._source._iconContainer.add_child(this._notificationBadgeBin); - this.updateNotificationBadgeStyle(); - const {remoteModel, notificationsMonitor} = Docking.DockManager.getDefault(); const remoteEntry = remoteModel.lookupById(this._source.app.id); this._remoteEntry = remoteEntry; @@ -782,14 +770,6 @@ export class UnityIndicator extends IndicatorBase { notificationsMonitor, 'changed', () => this._updateNotificationsCount(), - ], [ - St.ThemeContext.get_for_stage(global.stage), - 'changed', - () => this.updateNotificationBadgeStyle(), - ], [ - this._source._iconContainer, - 'notify::size', - () => this.updateNotificationBadgeStyle(), ], [ this._source, 'style-changed', @@ -804,7 +784,7 @@ export class UnityIndicator extends IndicatorBase { } destroy() { - this._notificationBadgeBin.destroy(); + this._notificationBadgeBin?.destroy(); this._notificationBadgeBin = null; this._hideProgressOverlay(); this.setUrgent(false); @@ -814,7 +794,7 @@ export class UnityIndicator extends IndicatorBase { super.destroy(); } - updateNotificationBadgeStyle() { + _updateNotificationBadgeStyle() { const themeContext = St.ThemeContext.get_for_stage(global.stage); const fontDesc = themeContext.get_font(); const defaultFontSize = fontDesc.get_size() / 1024; @@ -839,7 +819,7 @@ export class UnityIndicator extends IndicatorBase { fontSize = Math.round(sizeMultiplier * fontSize); const leftMargin = Math.round(sizeMultiplier * 3); - this._notificationBadgeLabel.set_style( + this._notificationBadgeBin.child.set_style( `font-size: ${fontSize}px;` + `margin-left: ${leftMargin}px` ); @@ -883,13 +863,52 @@ export class UnityIndicator extends IndicatorBase { this.setNotificationCount(remoteCount + notificationsCount); } + _updateNotificationsBadge(text) { + if (this._notificationBadgeBin) { + this._notificationBadgeBin.child.text = text; + return; + } + + this._notificationBadgeBin = new St.Bin({ + child: new St.Label({ + styleClass: 'notification-badge', + text, + }), + xAlign: Clutter.ActorAlign.END, + yAlign: Clutter.ActorAlign.START, + xExpand: true, + yExpand: true, + }); + this._notificationBadgeBin.child.clutterText.ellipsize = + Pango.EllipsizeMode.MIDDLE; + + this._source._iconContainer.add_child(this._notificationBadgeBin); + this._updateNotificationBadgeStyle(); + + const themeContext = St.ThemeContext.get_for_stage(global.stage); + this._signalsHandler.addWithLabel(UnityIndicator.notificationBadgeSignals, [ + themeContext, + 'changed', + () => this._updateNotificationBadgeStyle(), + ], [ + themeContext, + 'notify::scale-factor', + () => this._updateNotificationBadgeStyle(), + ], [ + this._source._iconContainer, + 'notify::size', + () => this._updateNotificationBadgeStyle(), + ]); + } + setNotificationCount(count) { if (count > 0) { const text = this._notificationBadgeCountToText(count); - this._notificationBadgeLabel.set_text(text); - this._notificationBadgeBin.show(); - } else { - this._notificationBadgeBin.hide(); + this._updateNotificationsBadge(text); + } else if (this._notificationBadgeBin) { + this._signalsHandler.removeWithLabel(UnityIndicator.notificationBadgeSignals); + this._notificationBadgeBin.destroy(); + this._notificationBadgeBin = null; } }