Skip to content

Commit

Permalink
appIconIndicators: Do not add notification badge code unless needed
Browse files Browse the repository at this point in the history
Only create the label and items if we are showing stuff, otherwise it's
just a waste
  • Loading branch information
3v1n0 committed Sep 2, 2024
1 parent 5f25f06 commit 1095cd7
Showing 1 changed file with 43 additions and 29 deletions.
72 changes: 43 additions & 29 deletions appIconIndicators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand All @@ -804,7 +784,7 @@ export class UnityIndicator extends IndicatorBase {
}

destroy() {
this._notificationBadgeBin.destroy();
this._notificationBadgeBin?.destroy();
this._notificationBadgeBin = null;
this._hideProgressOverlay();
this.setUrgent(false);
Expand All @@ -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;
Expand All @@ -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`
);
Expand Down Expand Up @@ -883,13 +863,47 @@ 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();

this._signalsHandler.addWithLabel(UnityIndicator.notificationBadgeSignals, [
St.ThemeContext.get_for_stage(global.stage),
'changed',
() => 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;
}
}

Expand Down

0 comments on commit 1095cd7

Please sign in to comment.